<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com  2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 

if(isset( $_SESSION["sess_artistid"]) and $_SESSION["sess_artistid"]!= ""){
	$artistid = $_SESSION["sess_artistid"];
}else{
	header("Location:login.php");
	exit;
}

$currencycode=getSellerCurrencySybol($artistid);
$currencySymbol=getCurrencySybol($currencycode);

$refund_id=$_REQUEST['refund_id'];
$qry="select * from ".$tableprefix."settings ";
$rs_q=mysql_query($qry) or die(mysql_error());
$rr_settings=mysql_fetch_array($rs_q);

if ($_POST["btnSubmit"]) {
	
	 $message = "" ;
	 
	$txtFeedBack=$_POST['feedback'];
	$status=$_POST['rma_action'];
	$rmode=$_POST['rmode'];
	$refno=$_POST['refno'];
	$refund_id=$_POST['refundid'];

	if (!isNotNull($txtFeedBack)) {
        $message .= VAL_MSG_FEEDBACK."! <br>";
    }
	
    if ($message != "") { // error
        $message = "<b>".VAL_MSG_PLS_CORRECT ."<b>".$message;
    } else { // no error so insert user details
		$sql = "UPDATE ".$tableprefix."refunds SET 
			Customer_Feedback = '".addslashes($txtFeedBack)."',
			Status='".addslashes($status)."',
			refund_mode='".addslashes($rmode)."',
			reference_number='".addslashes($refno)."'
			WHERE refund_id =  '".addslashes($refund_id)."' ";
		$rs=mysql_query($sql);
		
		
		if($rs)
		{ // check status updated or not 
		
		$sql_r="select o.billing_first_name,o.billing_last_name,o.billing_email,o.order_id,a.artist_name,od.product_name,r.status,r.*
	 			from ".$tableprefix."orders o ,".$tableprefix."artists a , ".$tableprefix."order_details od ,".$tableprefix."refunds r 
				where o.order_id=r.order_id AND od.order_id=r.order_id and od.product_id=r.product_id and a.artist_id=r.seller_id  and r.seller_id=".$artistid." and r.refund_id=".$refund_id." ";
	$results_r=mysql_query($sql_r) or die(mysql_error());
	$numresults=mysql_num_rows($results_r);
		if($numresults>0)
		{
			$rm=mysql_fetch_array($results_r);
			$buyer=$rm['billing_first_name'] ." " . $rm['billing_last_name'];
			$sellername=$rm['artist_name'];
			$productname=stripslashes($rm['product_name']);
			$quanity=$rm['quantity'];
			$status=$rm['status'];
			$comments=stripslashes($rm['Customer_Feedback']);
			$to_buyer= $rm['billing_email'];
			$date=date("m-d-Y");
			$to_seller=$rm['selleremail'];
			$mode=$rm['refund_mode'];
			$rno=$rm['reference_number'];
			$orderid=$rm['order_id'];
			$refundid=$rm['refund_id'];
			
		}	// end number of rows check 
			if($rr_settings['rma_status_change']=='Y')
				{ // check whether Status message send for Customer
		$subject=TEXT_RET_PAY_CONF.SITE_NAME;
/*-------Email Template Reading Fuction Call--------*/				
		$mailcontent = readEmailTemplate('refund_request_statuschange');
		
		$mailcontent = str_replace('[user_name]',$buyer,$mailcontent);			
		$mailcontent = str_replace('[p_name]',$productname,$mailcontent);		
		$mailcontent = str_replace('[status]',$status,$mailcontent);	
		$mailcontent = str_replace('[quantity]',$quanity,$mailcontent);	
		$mailcontent = str_replace('[order_id]',$orderid,$mailcontent);	
		$mailcontent = str_replace('[refund_id]',$refundid,$mailcontent);	
		$mailcontent = str_replace('[date]',$date,$mailcontent);	
		$mailcontent = str_replace('[comments]',$comments,$mailcontent);	
		if($status=="Refund")
		{
			$mailcontent = str_replace('[rmode]',$mode,$mailcontent);
			$mailcontent= str_replace('[rno]',$rno,$mailcontent);
		}
		else
		{
		$mailcontent = str_replace('[rmode]','nil',$mailcontent);
		$mailcontent= str_replace('[rno]','nil',$mailcontent);
		
		}
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$mailcontent = nl2br($mailcontent);
		
		$mail_send_details = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Online reservation</title>
  <link href="./themes/'. $active_template[4].' rel="stylesheet" type="text/css">
</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">
         
         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';

	$mailsent = mail($to_buyer, $subject, $mail_send_details, $headers);
} // end  email status check 

	
// Send Mail for Administraor 
	$subject=TEXT_RET_PAY_ST.SITE_NAME;

		$to_admin=SITE_EMAIL;
/*-------Email Template Reading Fuction Call--------*/				
		$mailcontent = readEmailTemplate('refund_request_adminstatus');
		$mailcontent = str_replace('[user_name]',$buyer,$mailcontent);			
		$mailcontent = str_replace('[p_name]',$productname,$mailcontent);		
		$mailcontent = str_replace('[seller_name]',$sellername,$mailcontent);	
		$mailcontent = str_replace('[status]',$status,$mailcontent);	
		$mailcontent = str_replace('[quantity]',$quanity,$mailcontent);	
		$mailcontent = str_replace('[order_id]',$orderid,$mailcontent);	
		$mailcontent = str_replace('[refund_id]',$refundid,$mailcontent);	
		$mailcontent = str_replace('[date]',$date,$mailcontent);	
		$mailcontent = str_replace('[comments]',$comments,$mailcontent);	
		if($status=="Refund")
		{
		$mailcontent = str_replace('[rmode]',$mode,$mailcontent);
		$mailcontent= str_replace('[rno]',$rno,$mailcontent);
		}
		else
		{
		$mailcontent = str_replace('[rmode]','nil',$mailcontent);
		$mailcontent= str_replace('[rno]','nil',$mailcontent);
		
		}
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
		$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
		$mailcontent = nl2br($mailcontent);
		$mail_send_details = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Online reservation</title>
  <link href="./themes/'. $active_template[4].' rel="stylesheet" type="text/css">
</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">
         
         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';

$mailsent = mail($to_admin, $subject, $mail_send_details, $headers);



// end sending mail 
		
	} // end if 
		
		$message= TEXT_REF_ST_SUCC;
		
		header("Location:list_refunds.php");
    } 
} 
 $sql="select  o.billing_first_name,o.billing_last_name,o.billing_email,a.artist_name,od.product_name,r.status,r.*,sum(od.product_price * r.quantity) as refundamt,sum((od.product_price-(od.product_price*product_discount/100)) * od.product_quantity) as refundtotal
	 from ".$tableprefix."orders o ,".$tableprefix."artists a , ".$tableprefix."refunds r, ".$tableprefix."order_details od
	 where o.order_id=r.order_id AND od.order_id=r.order_id and od.product_id=r.product_id 
	 and a.artist_id=r.seller_id and r.seller_id=".$artistid." and r.refund_id=".$refund_id." ";
$result=mysql_query($sql) or die(mysql_error());
$rr=mysql_fetch_array($result);
include "../includes/htmltop.php";
?>
		<body>
		<center>
		<div align="left" id="layout">
				<!--header area start -->
<?php

	include_once("../includes/topheader_seller.php")

?>
				<!--header area end -->
				<div align="left" class="main_body">
				<!--main body start -->
<?php
		include_once("../includes/artisttopmenu.php")
?>		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
							
					<div align="left" class="page_title"><?php echo TEXT_REF_SETTL;?> </div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">																			
						<form name="frmrefund" action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">
						<input type="hidden" name="userid" value="<?php echo htmlentities($artistid);?>">
						<?php if($rr_settings['enable_rma']=='Y') { ?>
							<table cellpadding="4" cellspacing="4" align="center" class="manage_table" width="100%">
								<tr>
								  <td width="35%" align="right"><?php echo TEXT_PRODUCT;?><?php echo TEXT_NAME;?></td>
								  <td width="4%">:</td>
								  <td width="61%"><?php echo (stripslashes($rr['product_name']));?></td>
								</tr>
								<tr>
								  <td align="right"><?php echo TEXT_RET_REASON;?> </td>
								  <td>:</td>
								  <td><?php echo htmlentities(stripslashes($rr['return_reason']));?></td>
							  </tr>
								<tr>
								  <td align="right"><?php echo TEXT_PRODUCT;?><?php echo TEXT_PRICE;?> </td>
								  <td>:</td>
								  <td>&nbsp; <?php echo $currencySymbol ?> <?php echo number_format($rr['refundtotal'],2,".","");?></td>
							  </tr>
								<tr>
								  <td align="right"><?php echo TEXT_RET_SETTL;?><?php echo TEXT_ACTION;?> <span class="required">*</span></td>
								  <td>:</td>
								  <td><select name="rma_action" class="textarea" onChange="displayDiv();" id="rma_action">
								  	  <option value=""><?php echo TEXT_SEL_ONE;?></option>
										<?php if($rr_settings['rma_repair']=='Y')
										{
										?>
										<option value="Repair"><?php echo TEXT_REPAIR;?></option>
										<?php
										}
										?>
										<?php if($rr_settings['rma_replace']=='Y')
										{
										?>
										<option value="Replace"><?php echo TEXT_REPLACE;?></option>
										<?php
										}
										?>
										<?php if($rr_settings['rma_refund']=='Y')
										{
										?>
										<option value="Refund"><?php echo TEXT_REFUND;?></option>
										<?php
										}
										?>
										</select>
							      <input type="hidden" name="refundid" value="<?php echo $refund_id;?>"> </td>
							  </tr>
								<tr>
								  <td align="right" valign="top"><?php echo TEXT_SELLER_FEEDBK;?> <span class="required">*</span></td>
								  <td>:</td>
								  <td><textarea name="feedback" class="textarea2" cols="40" rows="4" id="feedback"></textarea></td>
							  </tr>
								<tr>
								  <td colspan="3">
								  <div id="refundmode" style="display:none;"><table width="100%" cellpadding="5" cellspacing="5">
								  <tr>
								    <td width="34%"><?php echo TEXT_REF_MODE;?>  <span class="required">*</span></td>
								    <td width="2%">:</td>
								    <td width="64%"><select name="rmode" class="textbox">
									<option value=""><?php echo TEXT_SEL_ONE;?><?php echo TEXT_REF_MODE;?></option>
									<option value="Cash"><?php echo TEXT_CASH;?></option>
									<option value="Cheque"><?php echo TEXT_CHEQ;?></option>
									<option value="Cheque"><?php echo TEXT_DEM_DRAFT;?></option>
							        </select></td>
								  </tr>
								  <tr>
								    <td><?php echo TEXT_REF_NO;?></td>
								    <td>:</td>
								    <td><input name="refno" type="text" id="refno" class="textbox"></td>
								    </tr>
								  </table></div>
								  </td>
							  </tr>
								
							  <tr align="center">
							    <td colspan="3"><input type="button" name="btnBack" value="<?php echo BTN_BACK_REF_REQ;?>" class="button" onClick="window.location.href='list_refunds.php'">
							    <input type="submit" name="btnSubmit" value="<?php echo BTN_SAVE_CHANGE;?>" class="button" onClick="return validation();"></td>
							  </tr>
						  </table> 
							<?php } ?>
							<?php if($rr_settings['enable_rma']=='N')
									{
									?>		
									<table class="manage_table" cellpadding="5" cellspacing="5" width="100%">
									<tr><td align="center" class="text_information">&nbsp;<?php echo MSG_ADMIN;?>  </td></tr>
									<tr>
									  <td>&nbsp;</td>
									  </tr>
									<tr>
									  <td>&nbsp;</td>
									  </tr>
									</table>
									<?php } ?>
						</form>
				
				</div>
										
				<!--content area end-->
								
				</div>
				
								
					<div align="left" class="clear_float"><img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
				
				
<?php
	include "../includes/footer.php";
?>
<script language="javascript">

function validation()
{
	var aa =document.frmrefund.rma_action.value;
	
	if(document.frmrefund.rma_action.value=="")
	{
	
		alert("<?php echo ALERT_SEL_REF;?>");
		document.frmrefund.rma_action.focus();
		return false;
	
	}
	if(document.frmrefund.feedback.value=="")
	{
		alert("<?php echo ALERT_ENTER_FEEDBK;?>");
		document.frmrefund.feedback.focus();
		return false;
		
	}
	if(aa=="Refund")
	{
		if(document.frmrefund.rmode.value=="")
		{
		
			alert("<?php echo ALERT_SEL_REF_MODE;?>");
			document.frmrefund.rmode.focus();
			return false;
		}	
		
		if(document.frmrefund.rmode.value=="Cheque" || document.frmrefund.rmode.value=="Demand Draft")
		{
			if(document.frmrefund.refno.value=="")
			{
			alert("<?php ALERT_ENTER_REF_NO;?>");
			document.frmrefund.rmode.focus();
			return false;
			}
		}	
		
		
	}
	
	return true;
}

function displayDiv()
	{
		
		var aa =document.frmrefund.rma_action.value;
		if(aa=="Refund")
		{
		document.getElementById("refundmode").style.display="";
		}
		else
		{
			document.getElementById("refundmode").style.display="none";
		}
		
	
	}
</script>