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



<?php

if(isset($_GET["uid"]))
{
$uid=$_GET["uid"];
$rs=$objIms->getRows("view_purchase_supplier","md5(id)='$uid'");
$pur_id=$rs["id"];
$order_no=$rs["order_no"];
$purchase_date=$objIms->IntToDate($rs["purchase_date"]);
$supplier_name=$rs["supplier_name"];

			

}

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_purchase_return_mstr(purchasemstr_id,stampdate,user_id,ret_date) values('$pur_id','$stampdate','$user_id','$ret_date')";
				
				$res_insmas=mysql_query($sql_insmas);
				$lastid=mysql_insert_id();
				
				$ret_no='PURRET/'.date('M').'/'.date('Y').'/'.$lastid;
				$sql_upmas="Update tbl_purchase_return_mstr set return_no='$ret_no' where id='$lastid'";
				$res_upmas=mysql_query($sql_upmas);
				$totalamount=$_POST["totalamount"];
					
					for($str=1;$str<$count;$str++)
					{
					$purdet_id=$_POST["purdet".$str];
					$item_id=$_POST["item_id".$str];
					$unit_id=$_POST["unit_id".$str];
					$ret_qty=$_POST["ret_qty".$str];
					$discount=$_POST["discount".$str];
					$pur_price=$_POST["pur_price".$str];					
					$cgst=$_POST["cgst".$str];
					$sgst=$_POST["sgst".$str];
					$igst=$_POST["igst".$str];
					$sell_price=$_POST["sell_price".$str];
						if($ret_qty>0 or $ret_qty<>'')
						{
						
						$sql4="insert into tbl_purchase_return_details(purretmas_id,item_id,ret_qty,unit_id,discount,pur_price,sell_price,cgst,sgst,igst)values('$lastid','$item_id','$ret_qty','$unit_id','$discount','$pur_price','$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 unit_price='$pur_price' 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 unit_price='$pur_price' 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,Cr_Amt,sales_type,cash_type,clear,discount, grand_tot) values('$invoice_no','$lastid','$stampdate','$totalamount','$totalamount','Purchase Return','Cash',1,'$totdiscount','$totalamount')";
					
				
				$res_ins=mysql_query($sql_ins);
			    		
			
			
							
				
				header("location:purchasereturn_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;}


-->
</style>
</head>
<script language="javascript" type="text/javascript">
function chkqty(str)
{
var orderqty=$('#rest_qty'+str).val();
var stock=$('#stock'+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;
	}
	else
	{
		if(eval(stock)<eval(ret_qty))
		{
		alert("Return Qty Should Not Be greater than Stock");
		$('#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 pur_price=$("#pur_price"+ctr).val();


var price=eval(value)*eval(pur_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;">
View Purchase Details
</div>
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<table width="100%" cellpadding="4">
  <tr>
    <td height="38" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Order   No</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td nowrap="nowrap" bgcolor="#FFFFFF" ><?php echo $order_no;?></td>
    <td width="8%" height="38" nowrap="nowrap"><strong>Supplier Name<span style="color:#F00; font-weight:bold;">*</span> </strong></td>
    <td width="2%"><strong>:</strong></td>
    <td width="31%"><?php echo $supplier_name;?></td>
    </tr>
  <tr>
    <td width="7%" height="34" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>Order Date </strong></td>
    <td width="1%" nowrap="nowrap" bgcolor="#FFFFFF" ><strong>:</strong></td>
    <td width="28%" nowrap="nowrap" bgcolor="#FFFFFF" >
       <?php echo $purchase_date;?></td>
    <td nowrap="nowrap"><strong>Return Date</strong></td>
    <td><strong>:</strong></td>
    <td><?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>
    </tr>
</table>
<br />


     <div class="panel">
            <div class="panel-heading"><strong>Item List of Purchase </strong></div>
            <p>&nbsp;</p>
            <table width="100%" class="table table-striped">
		
            <?php 
				 $sql_viewtmp="select *  from view_purchase_det where purmas_id=$pur_id";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					?>
                     
                     <tr height="32" align="left">
            <th nowrap="nowrap" width="3%">Si No</th>
            <th nowrap="nowrap" width="23%">Item Name</th>
           
            <th nowrap="nowrap" width="12%">Unit Name </th>
            <th nowrap="nowrap" width="7%">Order Qty</th>
            
            <th nowrap="nowrap" width="9%">Sell Price(Per) </th>
            <th nowrap="nowrap" width="11%">Pur Price(Per)</th>
            <th width="5%" nowrap="nowrap">Total</th>
            <th width="9%" nowrap="nowrap">Discount(in %)</th>
            <th width="20%" nowrap="nowrap">CGST(%)</th>
            <th width="20%" nowrap="nowrap">CGST </th>
            <th width="20%" nowrap="nowrap">SGST(%)</th>
            <th width="20%" nowrap="nowrap">SGST</th>
            <th width="20%" nowrap="nowrap">IGST(%)</th>
            <th width="20%" nowrap="nowrap">IGST</th>
            <th width="20%" nowrap="nowrap">Total Price</th>
            <th width="9%" nowrap="nowrap">Rest qty </th>
            <th width="9%" nowrap="nowrap">Stock</th>
            <th width="10%" nowrap="nowrap">Return Qty </th>
              <th width="10%" nowrap="nowrap">Total Price </th>
              </tr>
            <?php
			$totdis=0;
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
						
				$sumretsql="select sum(ret_qty) as ret_qty from view_purret_mstrdet where purchasemstr_id=".$rows["purmas_id"]." and item_id=".$rows["item_id"];	
				$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 unit_price=".$rows["pur_price"]." and sell_price=".$rows["sell_price"];	
				$sumstockexe=mysql_query($sumstocksql);
				$sumstockrw=mysql_fetch_array($sumstockexe);
				$sumstock=$sumstockrw["stoct_qty"];
				if($sumstock<>''){$sumstock=$sumstock;}else{$sumstock=0;}		
							
							
			?>
           
            <tr height="32">
            <td nowrap="nowrap" width="3%"><?php echo $i;?></td>
           
		    <td nowrap="nowrap" width="23%"><input type="hidden" name="purdet<?=$i?>" id="purdet<?=$i?>"  value="<?php echo $rows["id"];?>"  />
		      <input type="hidden" name="item_id<?=$i?>" id="item_id<?=$i?>"  value="<?php echo $rows["item_id"];?>"  /><?php echo $rows["item_name"];?></td>
		    <td nowrap="nowrap" width="12%"><input type="hidden" name="unit_id<?=$i?>" id="unit_id<?=$i?>"  value="<?php echo $rows["unit_id"];?>"  /><?php echo $rows["unit_name"];?><input type="hidden" name="discount<?=$i?>" id="discount<?=$i?>"  value="<?php echo $rows["discount"];?>"  /></td>
            <td nowrap="nowrap" width="7%"><?php echo $rows["pur_qty"];?></td>
            
            <td nowrap="nowrap" width="9%"><input type="hidden" name="sell_price<?=$i?>" id="sell_price<?=$i?>"  value="<?php echo $rows["sell_price"];?>"  /><?php echo $rows["sell_price"];?></td>
            <td nowrap="nowrap" width="11%"><input type="hidden" name="pur_price<?=$i?>" id="pur_price<?=$i?>"  value="<?php echo $rows["pur_price"];?>"  /><?php echo $rows["pur_price"];?></td>
            <td nowrap="nowrap"><?php  $totsum=($rows["pur_qty"]*$rows["pur_price"]); echo number_format($totsum,2);?></td>
            <td nowrap="nowrap"><?php  $discount=$totsum*($rows["discount"]/100); $totprice=$totsum-$discount; echo number_format($rows["discount"],2); $totdis=$totdis+$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["pur_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 $sums=$sums+($totprice);$i++;} ?>
			<tr height="32">
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap">&nbsp;</td>
              <td nowrap="nowrap"><input type="hidden" name="totdis" id="totdis" style="width:75px;"  value="<?=$totdis?>" /></td>
              <td nowrap="nowrap">Total</td>
              <td nowrap="nowrap"><input type="text" name="totamt" id="totamt" style="width:75px;" readonly="" value="0" /></td>
            </tr>    
            <tr height="32">
              <td nowrap="nowrap"><input type="hidden" name="count" id="count"  value="<?=$i?>" style="width:75px;" /></td>
              <td colspan="18" nowrap="nowrap"><div align="right"></div></td>
              </tr> 
            <?php }?>
           </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>
       

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