<body background="">

<?php include("../../include/dbconnect.php"); 
$sdate = $today;
$tddt = us($today);

$sdate1 = $today1;
$tddt1 = us($today);

$result = mysqli_query($conn,"select account_year,start_date,end_date from branches  ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$account_year = $row[0];
$startdt = $row[1];
$enddt = $row[2];


if($tddt1 < $startdt or $tddt1 > $enddt)
{
?>
	<script language="JavaScript">
	alert("Selected date does not belong to current accounting year. Please recheck the date");
	</script>	
<?
exit;
}


$result = mysqli_query($conn,"select max(trans_id) from tranid where account_year = '$account_year' ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$trans_id = $row[0]+1;

	




$amttype ='J';
if($cnf=='Yes')
{

$qtyresult = mysqli_query($conn,"select * from tpurch ") or die(mysqli_error());
$che = mysqli_num_rows($qtyresult);
if($che <= 0)
{
	echo("Sorry You have not Selected any Products ..  data won't be saved.  Please close the Windows");
	exit;
}
	
	$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)$packing - (float)$scheme;
	   

	$query = "insert into salesreturn values ('$po_no', '$tddt1', '$compcode', 'TOTAL',  '$bcode',  '$scheme',  '', '','','', '', '', '',  '','',  '', '', '', '$packing', '$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];

		$remarks = 'Against Bill No  '.$po_no;
	
		
		$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($packing >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','$packing','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}
	
	

		if($scheme >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','','$scheme','CR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}
	
		$narration = 'Towards Sales Return Inv 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());




	


	mysqli_query($conn,"insert into tranid values('$trans_id','$account_year')") or die(mysqli_error());

	   
}
	   
?>



<br>
<?
$query = "truncate tpurch";
mysqli_query($conn,$query) or die (mysqli_error());
?>

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