<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();
?>



<?php
if(isset($_GET["uid"]))
{$uid=$_GET["uid"];
	$whr1="md5(id)='$uid'";
$get=$objIms->getRows('view_sales_master',$whr1);
$salesmas_id=$get["id"];
$sale_no=$get["sale_no"];
$sales_type=$get["sales_type"];
$client_name=$get["client_name"];
$client_address=$get["client_address"];
$client_tin=$get["tin_no"];
$saledt=$objIms->IntToDate($get["stampdate"]);

	}
	
	if(isset($_POST["Save"]))
	{
	
	$stampdate=$objIms->DateToInt(date('d-m-Y'));
	$count=$_POST["count"];	
	$ret_date=$objIms->DateToInt($_POST["ret_date"]);
		$user_id=$_SESSION["id"];

				$sql_insmas="insert into tbl_sales_return_mstr(salesmas_id,stampdate,user_id,return_date) values('$salesmas_id','$stampdate','$user_id','$ret_date')";
				
				$res_insmas=mysql_query($sql_insmas);
				$lastid=mysql_insert_id();
				
				$ret_no='SELLRET/'.date('M').'/'.date('Y').'/'.$lastid;
				$sql_upmas="Update tbl_sales_return_mstr set salereturn_no='$ret_no' where id='$lastid'";
				$res_upmas=mysql_query($sql_upmas);
				$totalamount=$_POST["totalamount"];
					
					for($str=1;$str<$count;$str++)
					{
					$item_id=$_POST["item_id".$str];
					$unit_id=$_POST["unit_id".$str];
					$ret_qty=$_POST["ret_qty".$str];
					$discount=$_POST["discount".$str];					
					$sell_price=$_POST["sell_price".$str];
					$cgst=$_POST["cgst".$str];
					$sgst=$_POST["sgst".$str];
					$igst=$_POST["igst".$str];
						if($ret_qty>0 or $ret_qty<>'')
						{
						
						$sql4="insert into tbl_sales_return_details(salesretmas_id,item_id,ret_qty,unit_id,discount,sell_price,cgst,sgst,igst)values('$lastid','$item_id','$ret_qty','$unit_id','$discount','$sell_price','$cgst','$sgst','$igst')";
						//echo $sql4;
						$rs1=mysql_query($sql4);
						$sel="select * from tbl_stock where item_id='$item_id' and unit_id='$unit_id' and sell_price='$sell_price'";
						$query=mysql_query($sel);
							if(mysql_num_rows($query)>0)
							{
							$rsq=mysql_fetch_array($query);
							$tot_qty=$rsq["tot_qty"]+$ret_qty;
							$update="update tbl_stock set tot_qty='$tot_qty' where item_id='$item_id' and unit_id='$unit_id' and sell_price='$sell_price'";
							mysql_query($update);
							}
						}
					}
					
					$totdiscount=$_POST["totdis"];
					$invoice_no=$objIms->trasaction_no();
					 $sql_ins="insert into tbl_account(invoice_no,mas_id,trans_date,actual_amt,Dr_Amt,sales_type,cash_type,clear,discount, grand_tot) values('$invoice_no','$lastid','$stampdate','$totalamount','$totalamount','Sales Return','Cash',1,'$totdiscount','$totalamount')";
					
				
				$res_ins=mysql_query($sql_ins);
			    		
			
				
				header("location:salereturn_view.php?uid=".md5($lastid));	
			
	
}
?>

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

		
	
		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        


<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}
.text_style1 {font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size:12px;}


-->
</style>
</head>
<script language="javascript" type="text/javascript">
function chkqty(str)
{
var orderqty=$('#rest_qty'+str).val();
var ret_qty=$('#ret_qty'+str).val();
//alert(ret_qty);
var show=0;
	if(eval(orderqty)<eval(ret_qty))
	{
	alert("Return Qty Should Not Be greater than Rest Qty");
	$('#ret_qty'+str).val('0');
	show=1;
	}
	if(show==0)
	{
	$(':input[type="submit"]').prop('disabled',false);
	}
}


function amtsum(value,ctr)
{
var discount=$("#discount"+ctr).val();
var sell_price=$("#sell_price"+ctr).val();
var price=eval(value)*eval(sell_price);
var totalp=eval(price)-(eval(discount)*eval(price))/100;
var cgst=eval(totalp)*eval($("#cgst"+ctr).val())/100;
var sgst=eval(totalp)*eval($("#sgst"+ctr).val())/100;
var igst=eval(totalp)*eval($("#igst"+ctr).val())/100;


var total=totalp+cgst+sgst+igst;
$("#totprice"+ctr).val(total.toFixed(2));
var count=$("#count").val();
var sum=0;
for(var sh=1;sh<count;sh++)
{
sum=eval(sum)+eval($("#totprice"+sh).val());}
$("#totamt").val(sum);

$("#totalamount").val(Math.round(sum));
}
</script>
<body>

<div class="span11" style=" width:83%;">



<div class="12" style=" margin-left:0px; ">


<a href="<?php echo $objIms->pageCaption($varFileName)?>_list.php?page=1&cmd=Clear" style="float:right">
<span class="btn" style="margin:20px 0px 20px 0px; font-weight:bold">Go Back</span></a>
<br />
<br />
<br />

<div class="12" style=" margin-left:0px;">
<div class="panel" style="border:<?php echo THEMECOLOR?> solid 1px; border-radius:0px;">
<div class="panel-heading" style="margin-bottom:15px; background:<?php echo THEMECOLOR?>; color:<?php echo THEMEFONRCOLOR?>; font-weight:bold; border-radius:0px;">
Sales Return
</div>
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="100%" class="table table-bordered" cellpadding="4">
  <tr>
    <td height="38" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Sale   No</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><?php echo $sale_no;?></td>
    <td width="8%" height="38" nowrap="nowrap"><strong>Client Name<span style="color:#F00; font-weight:bold;">*</span> </strong></td>
    <td width="2%"><strong>:</strong></td>
    <td width="31%"><?php echo $client_name;?></td>
    </tr>
  <tr>
    <td width="7%" height="34" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Sales Date </strong></td>
    <td width="1%" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" nowrap="nowrap" bgcolor="#FFFFFF" >
       <?php echo $saledt;?></td>
    <td nowrap="nowrap"><span class="text_style1">Client Address </span></td>
    <td><strong>:</strong></td>
    <td><?=$client_address?></td>
    </tr>
  <tr>
    <td height="34" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Return Date</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><?php if(($_POST["ret_date"])=="") { $dt=date("d-m-Y");} else { $dt=$_POST["ret_date"];}?>
        <input name="ret_date" type="text" id="ret_date"  style="width:185px; background-color:#FFF"  value="<?=$dt?>" class="tcal" readonly=""/></td>
    <td nowrap="nowrap">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<br />


     <div class="panel">
            <div class="panel-heading"><strong>Item List of Sale </strong></div>
            <p>&nbsp;</p>
            <table width="100%" class="table table-striped">
		
            <?php
  $sql_viewtmp="select *  from view_sales_detail where salesmas_id=$salesmas_id";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sum=0;
  ?>
  <tr>
    <td width="5%" height="28"><span class="text_style1">Si No</span></td>
    <td width="13%"><span class="text_style1">Item Name</span></td>
    <td width="10%"><span class="text_style1">Unit Name</span></td>
    <td width="8%"><span class="text_style1">Order Qty</span></td>
    <td width="11%"><span class="text_style1">Price(Pc)</span></td>
    <td width="10%"><strong>Total</strong></td>
    <td width="16%"><strong>Discount(in %)</strong></td>
    <th width="20%" align="left" nowrap="nowrap">CGST(%)</th>
    <th width="20%" align="left" nowrap="nowrap">CGST </th>
    <th width="20%" align="left" nowrap="nowrap">SGST(%)</th>
    <th width="20%" align="left" nowrap="nowrap">SGST</th>
    <th width="20%" align="left" nowrap="nowrap">IGST(%)</th>
    <th width="20%" align="left" nowrap="nowrap">IGST</th>
    <th width="20%" align="left" nowrap="nowrap">Total Price</th>
    <th width="6%" align="left" nowrap="nowrap">Rest qty </th>
    <th width="6%" align="left" nowrap="nowrap">Stock</th>
    <th width="6%" align="left" nowrap="nowrap">Return Qty </th>
    <th width="9%" align="left" nowrap="nowrap">Total </th>
  </tr>
  <?php
  $sum=0;
  $totdis=0;
  while($rows=mysql_fetch_assoc($res_viewtmp))
					{
					
					$sumretsql="select sum(ret_qty) as ret_qty from view_saleret_masdet where salesmas_id=".$rows["salesmas_id"]." and item_id=".$rows["item_id"]." and unit_id=".$rows["unit_id"]." and sell_price=".$rows["unit_price"];	
				$sumretexe=mysql_query($sumretsql);
				$sumretrw=mysql_fetch_array($sumretexe);
				$sumret=$sumretrw["ret_qty"];
				if($sumret<>''){$sumret=$sumret;}else{$sumret=0;}		
				
				$sumstocksql="select sum(tot_qty) as stoct_qty from tbl_stock where unit_id=".$rows["unit_id"]." and item_id=".$rows["item_id"]." and sell_price=".$rows["unit_price"];	
				$sumstockexe=mysql_query($sumstocksql);
				$sumstockrw=mysql_fetch_array($sumstockexe);
				$sumstock=$sumstockrw["stoct_qty"];
				if($sumstock<>''){$sumstock=$sumstock;}else{$sumstock=0;}
  ?>
  <tr>
    <td height="27"><span class="text_style"><?=$i?></span></td>
    <td><?=$rows["item_name"]?><input type="hidden" name="item_id<?=$i?>" id="item_id<?=$i?>"  value="<?php echo $rows["item_id"];?>"  /></td>
    <td><?=$rows["unit_name"]?><input type="hidden" name="unit_id<?=$i?>" id="unit_id<?=$i?>"  value="<?php echo $rows["unit_id"];?>"  /></td>
    <td><?=$rows["qty"]?></td>
    <td><span class="text_style"><input type="hidden" name="sell_price<?=$i?>" id="sell_price<?=$i?>"  value="<?php echo $rows["unit_price"];?>"  />
      <?=$rows["unit_price"]?>
    </span></td>
    <td><?php echo $totsum=($rows["unit_price"]*$rows["qty"]);?></td>
    <td><?php $discount=$totsum*($rows["discount"]/100); echo $rows["discount"]; $totdis=$totdis+$discount; $totprice=$totsum-$discount;?><input type="hidden" name="discount<?=$i?>" id="discount<?=$i?>"  value="<?php echo $rows["discount"];?>"  /></td>
    
    <td width="20%" nowrap="nowrap"><input type="hidden" name="cgst<?=$i?>" id="cgst<?=$i?>"  value="<?php echo $rows["cgst"];?>">
        <?php echo $rows["cgst"];?></td>
    <td width="20%" nowrap="nowrap"><?php  $cgst=$totprice*($rows["cgst"]/100); echo number_format($cgst,2);?></td>
    <td width="20%" nowrap="nowrap"><input type="hidden" name="sgst<?=$i?>" id="sgst<?=$i?>"  value="<?php echo $rows["sgst"];?>">
        <?php echo $rows["sgst"];?></td>
    <td width="20%" nowrap="nowrap"><?php  $sgst=$totprice*($rows["sgst"]/100); echo number_format($sgst,2);?></td>
    <td width="20%" nowrap="nowrap"><input type="hidden" name="igst<?=$i?>" id="igst<?=$i?>"  value="<?php echo $rows["igst"];?>">
        <?php echo $rows["igst"];?></td>
    <td width="20%" nowrap="nowrap"><?php  $igst=$totprice*($rows["igst"]/100); echo number_format($igst,2);?></td>
    <td nowrap="nowrap"><?php   $totamt=$totprice+$cgst+$sgst+$igst; echo number_format($totamt,2);?></td>
	<td nowrap="nowrap"><input type="text" name="rest_qty<?=$i?>" id="rest_qty<?=$i?>" readonly="" value="<?=$rows["qty"]-$sumret?>" style="width:75px;" /></td>
    <td nowrap="nowrap"><input type="text" name="stock<?=$i?>" id="stock<?=$i?>" style="width:75px;" readonly="" value="<?=$sumstock?>" /></td>
    <td nowrap="nowrap"><input type="text" name="ret_qty<?=$i?>" id="ret_qty<?=$i?>" style="width:75px;" onblur="chkqty(<?=$i?>);amtsum(this.value,<?=$i?>)" /></td>
    <td nowrap="nowrap"><input type="text" name="totprice<?=$i?>" id="totprice<?=$i?>" style="width:75px;" readonly="" value="0" /></td>
  </tr>
  <?php $sum=$sum+$totprice; $i++;}}?>
  <tr>
    <td height="27" colspan="17" align="right" class="text_style1">Total:</td>
	<td nowrap="nowrap"><input type="hidden" name="totdis" id="totdis" style="width:75px;"  value="<?=$totdis?>" /><input type="text" name="totamt" id="totamt" style="width:75px;" readonly="" value="0" /><input type="hidden" name="count" id="count"  value="<?=$i?>" style="width:75px;" /></td>
  </tr>
           </table>
		   
		   <table width="100%" class="table table-bordered">  
			  <tr>
              <td>&nbsp;</td>
              <td><div align="right"><strong>Total Amount : </strong></div></td>
              <td><input type="text" name="totalamount" id="totalamount" style="width:100px;" readonly value="0"></td>
            </tr>
			  <tr>
			    <td>&nbsp;</td>
			    <td><input type="submit" id="Save" name="Save" value="Save" class="btn btn-info"  disabled="disabled" style="margin:inherit; width:100px"/></td>
			    <td>&nbsp;</td>
		    </tr>
    </table>
    </div>
	
		

   
<p></p>
		<br />
        

</form></div>
</body>
</html>
