<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
validate_admin();
$_SESSION['sess_msg']="";
if($_REQUEST['submitForm']=='yes'){
	$refund_order_id=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['refund_order_id']);
	$usertype=getFieldWhere('user_type','tbl_user','id',getFieldWhere('user_id','tbl_order','id',$refund_order_id)); 

	if($usertype==0){   
	$result=$obj->query("select id,user_id from tbl_order where id='$refund_order_id'");
$num=$obj->numRows($result);
if($num>0){ }else{ $_SESSION['sess_msg']="Order Id : <strong>$refund_order_id</strong> not found.!"; }
}else{ $_SESSION['sess_msg']="Order Id : <strong>$refund_order_id</strong> is from a guest user. It can be refund by Payment Gateway Panel.!"; }

}
if($_REQUEST['refundtouser']=='Refund'){
$orderid=mysqli_real_escape_string($GLOBALS['conn'],$_REQUEST['orderid']);    

$storeamount=$_REQUEST['storeamount']; 

$storeamountid=$obj->query("select distinct storeid from tbl_order_itmes where order_id='$orderid' order by storeid asc");
$storeidarr=array();

while($resstoreamount=$obj->fetchNextObject($storeamountid)){
$storeidarr[]=$resstoreamount->storeid;
}

for($x=0; $x < count($storeamount); $x++){
if($storeamount[$x]!='0' && $storeamount[$x]!=''){
$userid=getFieldwhere('user_id','tbl_order','id',$orderid);
$obj->query("insert into tbl_wallet set user_id='$userid', order_id='$orderid', storeid='".$storeidarr[$x]."',wallet_balance='$storeamount[$x]',type='Cr',added_date=now(),status='1'");
$_SESSION['sess_msg']="Amount refunded for order id $orderid "; 
}
}
//$sum=$obj->query("select sum((price+tax_value)*qty) as total from tbl_order_itmes where order_id='$refund_order_id' and storeid='$resstoreamount->storeid' order by storeid asc");
	//	  $rs=$obj->fetchNextObject($sum);
	//      $total=number_format($rs->total,2);
		
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<title><?php echo SITE_TITLE; ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="css/admin.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript">

function validate(obj){

if(obj.refund_order_id.value=='')	{

alert("Please Enter Order Id");	

obj.refund_order_id.focus();

return false;	

}

}

</script>

	
	<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
	</head>

	<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
    <td align="left" valign="middle" class="headingbg bodr text14"><em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Add Refund</td>
  </tr>
      <tr>
    <td height="100" align="left" valign="top" bgcolor="#f7faf9" class="bodr"><form name="frm" method="POST" enctype="multipart/form-data" action="" onSubmit="return validate(this)">
        <input type="hidden" name="submitForm" value="yes" />
        
        <table width="100%" cellpadding="0" cellspacing="0">
          
          <tr>
            <td width="35%" align="right" class="paddBot11 paddRt14"><strong>Order Id : </strong></td>
            <td width="30%" align="left" class="paddBot11"><input type="text" name="refund_order_id" class="form-control" style="width:90%; height:27px; border-radius:4px; border:1px solid #999" placeholder="Enter Order Id" /></td>
			  <td width="35%" align="left" class="paddBot11"><input type="submit" name="submit" value="Submit"  class="submit" border="0" />
              &nbsp;&nbsp;&nbsp;&nbsp;</td>
          </tr> 
		  <tr>&nbsp;</tr>
		  <tr>
            <td align="center" colspan="3" style="color:#C00;"><?php echo $_SESSION['sess_msg'];$_SESSION['sess_msg']=''; ?></td>
          </tr> </table>
      </form></td>
  </tr>
  <?php if($usertype=='1'){ exit; die(); }?>
        <tr>
    <td align="center"></td>
  </tr>
      <?php $storeArrid=$obj->query("select distinct storeid from tbl_order_itmes where order_id='$refund_order_id' order by storeid asc");

	if($obj->numRows($storeArrid)>0){?>
	<tr>
    <td align="center" style="font-size:14px; font-weight:bold">Refund for Order Id : <strong><?php echo $refund_order_id; ?> </strong></td>
  </tr>
      <tr>
    <td><table width="100%" border="0" cellspacing="4" cellpadding="4" bgcolor="#f7faf9" class="bodr">
       <form name="frmrefund" method="POST" enctype="multipart/form-data" action="">
	   <input type="hidden" name="orderid" value="<?php echo $refund_order_id; ?>" />
        <?php while($resultstoreid=$obj->fetchNextObject($storeArrid)){?>
        <tr>
          <td align="right"><?php $sum=$obj->query("select sum((price+tax_value)*qty) as total from tbl_order_itmes where order_id='$refund_order_id' and storeid='$resultstoreid->storeid' order by storeid asc");
		  $rs=$obj->fetchNextObject($sum);
	      $total=number_format($rs->total,2);
		  echo getFieldWhere('storename','stores','storeid',$resultstoreid->storeid); ?> (<?php echo $website_currency_symbol.$total;?>)</td>
          <td><input type="text" name="storeamount[]" value="0" placeholder="eg. <?php echo $total; ?>" style="width:90%; height:27px; border-radius:4px; border:1px solid #999" /></td>
          
        </tr>
        <?php } ?>
		<tr>&nbsp;<br /></tr>
		<tr>
         <td></td>
		  <td><input type="submit" name="refundtouser" value="Refund"  class="submit" border="0" /></td>
          
          <td></td>
        </tr></form>
      </table></td>
  </tr>
      <?php } ?>
    </table>
</td>
</tr>
</table>

</body>
</html>
