<?php include("../../include/dbconnect.php"); 
$today = substr($today,6,4).'-'.substr($today,3,2).'-'.substr($today,0,2);
$today1 = substr($today1,6,4).'-'.substr($today1,3,2).'-'.substr($today1,0,2);
$amttype = 'J';
$aresult = mysqli_query($conn,"select distinct account_year from branches ");
$arow = mysqli_fetch_row($aresult);
$account_year = $arow[0];
$po_no = $mypono;

$mcresult = mysqli_query($conn,"select poinv_date from salesreturn where trans_id = '$trans_id' and account_year  = '$account_year' and compcode ='$compcode' ") or die(mysqli_error());
$mcrow = mysqli_fetch_row($mcresult);
$tddt1 = $mcrow[0];

	
if ( isset($submit) && $submit == "Update")
{

$qtyresult = mysqli_query($conn,"select * from tpurch ") or die(mysqli_error());
$che = mysqli_num_rows($qtyresult);
if($che <= 0)
{
	echo("Sorry with Empty Products Data can't be updated.  Rather Please selet delete option. Pl. close the window and edit again");
	exit;
}


		mysqli_query($conn,"delete from ledger where trans_id = '$trans_id' and account_year  = '$account_year'  ") or die(mysqli_error());
		mysqli_query($conn,"delete from salesreturn where trans_id = '$trans_id' and account_year  = '$account_year'  ") or die(mysqli_error());
		mysqli_query($conn,"delete from stktrans where trans_id = '$trans_id' and account_year  = '$account_year'  ") or die(mysqli_error());

	    $narration = 'Towards Sales ReturnInv No. :' . $mypono;
	$qtyresult = mysqli_query($conn,"select * from tpurch ") or die(mysqli_error());
	while($row = mysqli_fetch_row($qtyresult))
	{	
	
		$compcode = $row[0];
		$bcode = $row[1];
		$accthead = $row[2];
		$manufact = $row[3];
		$exp_date= $row[4];
		$qty = $row[5];
		$rate = $row[6];
		$batch_no = $row[8];
		$mrp = $row[9];
	
		$porec = $row[10];
		$mypo = $row[11];
		$discount_percent = $row[12];
		$taxtype = $row[13];
		$tax_percent = $row[14];
		$record = $row[15];
	

		$value= round($qty * $rate,2);
		$damt = round($value * $discount_percent /100,2);
		$taxable_value = round($value - $value * $discount_percent /100,2);
		$taxamt = round($taxable_value * $tax_percent / 100,2);
		$amount=  $taxable_value + $taxamt; 
		
		$tottax = $tottax +$taxamt;
		$gtotal = $gtotal + $amount;
		$tot = $tot + $taxable_value;	




		$query = "insert into salesreturn values ('$po_no', '$tddt1', '$compcode', '$accthead',  '$bcode', '$manufact', '$exp_date','$batch_no',  '$qty', '$rate','$mrp', '$taxable_value', '$discount_percent', '$damt','',  '$tax_percent', '$taxamt', '$taxtype', '', '', '', '', '', '$pono', '$podt', '$account_year', '$trans_id',  NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
	
		$result = "insert into stktrans (acctcode, invno, date, inqty, trans_id,   po_no, po_date,  compcode,  bcode, transact_type, account_year,batch_no,mrp,taxtype) values ( '$accthead', '$po_no', '$tddt1',  '$qty', '$trans_id', '$pono', '$podt', '$compcode', '$bcode', 'SR',  '$account_year','$batch_no','$mrp','$taxtype')"; 
		mysqli_query($conn,$result) or die (mysqli_error());	



			
	 }
	$billamt = $gtotal +(float)$pack - (float)$sch;
	   

		$query = "insert into salesreturn values ('$po_no', '$tddt1', '$compcode', 'TOTAL',  '$bcode',  '$sch',  '', '','','', '', '', '',  '','',  '', '', '', '$pack', '$billamt', '', '', '$remarks', '$pono', '$podt', '$account_year', '$trans_id', NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());




		$stresult = mysqli_query($conn,"select  acctcode from acctmast where reserved_word = 'SALESRETURN' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($stresult);	
		$myPurgl = $srow[0];


		
		$accthead = $myPurgl;
		$remarks = 'Against Bill No  '.$po_no;
  	    $result = mysqli_query($conn,"insert into ledger (acctcode,date,narration,dbamt,cramt,type,invno,amttype,compcode,branch_code,trans_id,account_year,cheqno,entered_by,voucher_no,date_of_entry,taxtype) values ( '$accthead','$tddt1','$remarks','$billamt','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Salesreturn')") or die(mysqli_error());
		




	
		if($pack >0)
		{

			$mresult = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'PACKING' ") or die(mysqli_error());
			$mrow = mysqli_fetch_row($mresult);
			$pacctcode = $mrow[0];


			$remarks = 'Against Bill No  '.$po_no;
			$result = mysqli_query($conn,"insert into ledger (acctcode,date,narration,dbamt,cramt,type,invno,amttype,compcode,branch_code,trans_id,account_year,cheqno,entered_by,voucher_no,date_of_entry) values ( '$pacctcode','$tddt1','$remarks','$pack','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}

		if($sch>0)
		{

			$mresult = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'SCHEME' ") or die(mysqli_error());
			$mrow = mysqli_fetch_row($mresult);
			$scacctcode = $mrow[0];


			$remarks = 'Against Bill No  '.$po_no;
			$result = mysqli_query($conn,"insert into ledger (acctcode,date,narration,dbamt,cramt,type,invno,amttype,compcode,branch_code,trans_id,account_year,cheqno,entered_by,voucher_no,date_of_entry) values ( '$scacctcode','$tddt1','$remarks','','$sch','CR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}


	
		$narration = 'Towards Sales ReturnInv No. :' . $po_no;
		$result = mysqli_query($conn,"insert into ledger (acctcode,date,narration,dbamt,cramt,type,invno,amttype,compcode,branch_code,trans_id,account_year,cheqno,entered_by,voucher_no,date_of_entry,taxtype) values ( '$bcode','$tddt1','$narration','','$billamt','CR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Salesreturn')") or die(mysqli_error());


}
elseif (isset($submit) && $submit == "Delete")
{
		$result="delete from salesreturn where trans_id = '$trans_id' and account_year='$account_year'   ";
		mysqli_query($conn,$result) or die (mysqli_error());

		$result = "delete from stktrans  where trans_id = '$trans_id'  and account_year='$account_year'  "; 
		mysqli_query($conn,$result) or die (mysqli_error());

		$result = "delete from ledger where trans_id = '$trans_id'  and account_year='$account_year'  "; 
		mysqli_query($conn,$result) or die (mysqli_error());

}

$query = "truncate tpurch";
mysqli_query($conn,$query) or die (mysqli_error());

?>
<script language="JavaScript">
close();
</script>




