<body background="">

<?php include("../../include/dbconnect.php"); 


	


if($packing>0)
{
	if($mySalesexp =='')
	{
		?>
        <script language="javascript">
		alert("You have not opted Expense Type. Please select otherwise data won't be saved");
        </script>
        <?
	}
	$exp_accthead = $mySalesexp;
}


$sdate2 = $today2;
$tddt2 = us($today2);

$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($tddt2 < $startdt or $tddt2 > $enddt)
{
?>
	<script language="JavaScript">
	alert("Selected date does not belong to current accounting year. Please recheck the date");
	</script>	
<?
exit;
}

$gtotal = 0;
$amttype = 'J';

$result=mysqli_query($conn,"select salesreg.bcode, salesreg.inv_no, salesreg.inv_date,invoice_type  from salesreg where compcode ='$compcode' and salesreg.trans_id ='$trans_id' and account_year = '$account_year'  and prodcode = 'TOTAL' ") or die(mysqli_error());
$row=mysqli_fetch_row($result);
$bcode = $row[0];
$inv_no = $row[1];
$inv_date = $row[2];
$invtype = $row[3];


if($cnf=='Yes')
{


	$qtyresult = mysqli_query($conn,"select * from spurch ") 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 salesreg 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());




	$qtyres = mysqli_query($conn,"select sum(amount) from spurch ") or die(mysqli_error());
	$row1 = mysqli_fetch_row($qtyres);
	$qtyresult = mysqli_query($conn,"select * from spurch ") or die(mysqli_error());
	while($row = mysqli_fetch_row($qtyresult))
	{	
		$compcode = $row[0];
		$bcode = $row[1];
		$prodcode = $row[2];
		$qty = $row[3];
		$rate = $row[4];
		$batch_no = $row[5];
		//$amount = $row[6];
		$ut = $row[7];
		$discount_percent = $row[8];
		$taxtype = $row[9];
		$tax_percent = $row[10];
		$specification = $row[11];
		$stainer_rate = $row[12];
		$record = $row[13];
		$rate = $rate +$stainer_rate;

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

		$tottax = $tottax +$taxamt;
		$valuetot = $valuetot + $value;
		$disctotal = $disctotal + $disc_amt;

		
		$myresult = mysqli_query($conn,"select packing,schedule from product where prodcode = '$prodcode' ") or die(mysqli_error());
		$myrow = mysqli_fetch_row($myresult);
		$uom = $myrow[0];
		$schedule = $myrow[1];

		
		$result = "insert into stktrans (acctcode, invno, date, outqty, trans_id,   po_no, po_date,  compcode,  bcode, transact_type, account_year,batch_no,mrp,purreg_record) values ( '$prodcode', '$inv_no', '$tddt2',  '$qty', '$trans_id', '$pono', '$podt', '$compcode', '$bcode', 'S',  '$account_year','$batch_no','$rate','$purchase_record')"; 
		mysqli_query($conn,$result) or die (mysqli_error());		

		$query = "insert into salesreg values ('$inv_no', '$tddt2', '$compcode', '$prodcode', '$specification', '$bcode', '$ipno', '$batch_no', '',  '$schedule', '$qty', '$rate', '$value', '$discount_percent', '$disc_amt','', '', '', '',  '', '', '', '$tax_percent', '$taxamt', '$taxtype', '', '', '', '', '', '', '', '$uom', '', '', '$myType', '$account_year', '$trans_id',  NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
		
 	}
	
	$bamt =  $gtotal+$packing ;
	$billamt = round($bamt);
	$diff = $billamt- $bamt;
	
	$narration = 'Towards Sales Inv No. :' . $inv_no;
	$remarks = 'Against Bill No  '.$inv_no;
	$saleamt = $billamt;



		$query = "insert into salesreg values ('$inv_no', '$tddt2', '$compcode', 'TOTAL', '', '$bcode', '$ipno', '$tddt', '$rbill_no',  '$tddt1', '', '', '', '',  '','$packing', '$destination', '$vehicle_no', '$form20',  '$pur_no', '$pur_date', '$driver', '', '', '', '', '$saleamt', '$diff', '', '$carrier', '',  '', '', '', '$payterms', '$invtype', '$account_year', '$trans_id', NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());

	if($saletype == 'CFORM' or $saletype =='HFORM')
	{		
		if($saletype == 'CFORM')
		{
			$stype = 'CF';
		}
		else
		{
			$stype = 'HF';
		}
		
		$query="insert into cform values ('$bcode','$inv_no','$tddt2','$cnarr','$saleamt','$taxamt','F','$stype','$branch','','','','','$pur_no','$pur_date','$compcode','$account_year',NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
	}



	$query="insert into bill_patient (custcode,name,address1,address2,city,state,country,pin,phone,inv_no,inv_date,trans_id,account_year,taxtype) select custcode,name,address1,address2,city,state,country,pin,phone,'$inv_no','$tddt2','$trans_id','$account_year','$taxtype' from billcustomer ";
	mysqli_query($conn,$query) or die (mysqli_error());

	// Ledger posting starts from here.

	$stresult = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'DISCOUNT' ") or die(mysqli_error());
	$srow = mysqli_fetch_row($stresult);	
	$daccthead = $srow[0];
	
	
	
	
	// Discount Posting DEBIT
	if($disctotal >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 ( '$daccthead','$tddt2','$narration','$disctotal','','DR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	}
	
	$taxresult = mysqli_query($conn,"select taxtype,round(sum(amount*tax_rate/100),2),round(sum(qty*(rate+stainer_rate)),2) from spurch group by taxtype") or die(mysqli_error());
	while($trow = mysqli_fetch_row($taxresult))
	{
	
		$stresult = mysqli_query($conn,"select tax_acctcode,sp_acctcode from salestax where taxtype = '$trow[0]' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($stresult);	
		$saccthead = $srow[0];	
		$sales_accthead = $srow[1];
		
		// Local Sales Ledger Posting  CREDIT
	
		$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 ( '$sales_accthead','$tddt2','$narration','','$trow[2]','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Sales')") or die(mysqli_error());

		// Tax  Posting CREDIT
	
		$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 ( '$saccthead','$tddt2','$narration','','$trow[1]','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	}
	
	// SALES EXPENSE POSTING
	
	if($packing>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 ( '$exp_accthead','$tddt2','$narr','','$packing','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	}	
	
	// ROUND OFF POSTING
			
	$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','$tddt2','$narr','$diff','','DR','$inv_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','$tddt2','$narr','','$diff','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	
	}
	
	// SUN DEBT OR CASH POSTING...
	
	if($invtype == 'CASH')
	{
		
		$stresult = mysqli_query($conn,"select acctcode from acctmast where reserved_word = 'CASH' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($stresult);
		$accthead = $srow[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,taxtype) values ( '$accthead','$tddt2','$narration','$saleamt','','DR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Sales')") or die(mysqli_error());
	}
	else
	{
		$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','$tddt2','$narration','$saleamt','','DR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Sales')") or die(mysqli_error());
	}

		mysqli_query($conn,"truncate spurch ") or die(mysqli_error());
}
	   
?>



<br>
<meta http-equiv="refresh" content="0 url=tax_invoice.php?inv_no=<?=$inv_no?>&inv_date=<?=us($today2)?>&taxtype=<?=$myTax?>">
