<?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;
echo("select poinv_date,poinv_no from purchasereg where trans_id = '$trans_id' and account_year  = '$account_year' and compcode ='$compcode' <br>");

$mcresult = mysqli_query($conn,"select poinv_date,poinv_no,exp_date from purchasereg where trans_id = '$trans_id' and account_year  = '$account_year' and compcode ='$compcode' ") or die(mysqli_error());
$mcrow = mysqli_fetch_row($mcresult);
$tddt1 = $mcrow[2];
$po_no = $mcrow[1];
$inv_date = $mcrow[0];
$valuetot = 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' and compcode ='$compcode' ") or die(mysqli_error());
		mysqli_query($conn,"delete from purchasereg where trans_id = '$trans_id' and account_year  = '$account_year' and compcode ='$compcode' ") or die(mysqli_error());
		mysqli_query($conn,"delete from stktrans where trans_id = '$trans_id' and account_year  = '$account_year' and compcode ='$compcode' ") or die(mysqli_error());

	    $narration = 'Towards Purchase Inv 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; 
		
		$valuetot = $valuetot +$taxable_value;		
		$tottax = $tottax +$taxamt;
		$gtotal = $gtotal + $amount;
		$tot = $tot + $taxable_value;	

		if ($mypo == 'PO')
		{
			$cq=mysqli_query($conn,"select po_no, po_date, inwardqty, record from outpo where record='$porec'") or die(mysqli_error());
			$brow = mysqli_fetch_row($cq);
			$pono = $brow[0];
			$podt = $brow[1];
			$inwqty = $brow[2];
			$inw_qty = $inwqty + $qty + $rtqty;
			$query="update  outpo set inwardqty='$inw_qty'  where record='$porec' and account_year='$account_year'";
						mysqli_query($conn,$query) or die (mysqli_error());
		
		}


		$query = "insert into purchasereg values ('$po_no', '$inv_date', '$compcode', '$accthead',  '$bcode', '$manufact', '$tddt1','$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', 'P',  '$account_year','$batch_no','$mrp','$taxtype')"; 
		mysqli_query($conn,$result) or die (mysqli_error());	



			
	 }
	$billamt = $gtotal +(float)$pack - (float)$sch+(float)$tcs;
	$diff = round($billamt,0) - $billamt;
$billamt = round($billamt,0);   

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


	$taxresult = mysqli_query($conn,"select tax_type,round(sum(((rate*qty)- (rate*qty*discount_percent/100))*tax_percent/100),2),round(sum((rate*qty)- (rate*qty*discount_percent/100)),2) from tpurch group by tax_type") or die(mysqli_error());
	while($trow = mysqli_fetch_row($taxresult))
	{
		echo("select sgst_acctcode, cgst_acctcode, igst_acctcode, sp_acctcode,tax from salestax where taxtype = '$trow[0]' <br>");
		$stresult1 = mysqli_query($conn,"select sgst_acctcode, cgst_acctcode, igst_acctcode, sp_acctcode,tax from salestax where taxtype = '$trow[0]' ") or die(mysqli_error());
		$srow1 = mysqli_fetch_row($stresult1);	
		$sgst_acctcode = $srow1[0];	
		$cgst_acctcode = $srow1[1];
		$igst_acctcode = $srow1[2];
		$myPurgl = $srow1[3];
		$tax_rate = $srow1[4];
		$bresult = mysqli_query($conn,"select taxtype from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow = mysqli_fetch_row($bresult);
		$buyertype =  $brow[0];
		$taxamt=round($trow[1],2);
		$pvalue = round($trow[2],2);		
		if($buyertype == 'STATE')
		{
			$sgst_amt = round($taxamt/2,2);
			$cgst_amt = $sgst_amt;
			$igst_amt =0;
		}
		if($buyertype == 'INTERSTATE')
		{
			$igst_amt = round($taxamt,2);
			$sgst_amt=0;
			$cgst_amt=0;
		}

		$remarks = 'Against Purchase Bill No  '.$po_no;
		if($sgst_amt>0)
		{
  	    	$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 ( '$sgst_acctcode','$tddt1','$remarks','$sgst_amt','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}	
		if($cgst_amt>0)
		{
  	    	$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 ( '$cgst_acctcode','$tddt1','$remarks','$cgst_amt','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}			

		if($igst_amt>0)
		{

  	    	$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 ( '$igst_acctcode','$tddt1','$remarks','$igst_amt','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
		}	
				
  	    $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 ( '$myPurgl','$tddt1','$remarks','$trow[2]','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Purchase')") 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 = 'DISCOUNT' ") 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());
		}

		if($tcs >0)
		{

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


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

	// ROUND OFF POSTING
	$diff = round($diff,2);
	$stresult = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'ROUNDOFF' ") or die(mysqli_error());
	$srow = mysqli_fetch_row($stresult);
	$accthead = $srow[0];
	if($diff <0)
	{
			$diff = $diff*-1;
			$narr = 'Towards Inv:'.$inv_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 ( '$accthead','$tddt1','$narr','','$diff','CR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	}
	elseif($diff>0)
	{
			$narr = 'Towards Inv:'.$inv_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 ( '$accthead','$tddt1','$narr','$diff','','DR','$po_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	
	}
	
	
		$narration = 'Towards Purchase 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(),'Purchase')") or die(mysqli_error());


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

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

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

}

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

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



