<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 taxtype from buy where bcode = (select distinct bcode from spurch) ") or die(mysqli_error());
$row = mysqli_fetch_row($result);
$buyertype = $row[0];


$result=mysqli_query($conn,"select salesreg8b.bcode, salesreg8b.inv_no, salesreg8b.inv_date,invoice_type  from salesreg8b where compcode ='$compcode' and salesreg8b.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 salesreg8b 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());


	$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];
		$cess_percent = $row[11];
		$specification = $row[12];
		//$stainer_rate = $row[12];
		$record = $row[14];
		$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);
		$cess_amt = round($taxable_value * $cess_percent/100,2);
		$amount=  round($taxable_value + $taxamt+$cess_amt,2); 
		$cesstotal = $cesstotal + $cess_amt;		
		$gtotal = $gtotal + $amount;
		$tot = $tot + $taxable_value;	

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

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

		if($pn !='FREIGHT')
		{
		$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 salesreg8b values ('$inv_no', '$tddt2', '$compcode', '$prodcode', '$specification', '$bcode', '$ipno', '$batch_no', '',  '$schedule', '$qty', '$rate', '$value', '$discount_percent', '$disc_amt','', '', '', '',  '', '', '', '$tax_percent', '$taxamt', '$taxtype', '$cess_percent', '$cess_amt','','', '', '', '', '', '$uom', '', '', '$myType', '$account_year', '$trans_id',  NULL)";
		mysqli_query($conn,$query) or die (mysqli_error());
		
 	}
	
	$bamt =  round($gtotal+$packing,2);
	$billamt = round($bamt,0);
	$diff = round($billamt- $bamt,2);
	
	$narration = 'Towards Sales Inv No. :' . $inv_no;
	$remarks = 'Against Bill No  '.$inv_no;
	$saleamt = $billamt;



		$query = "insert into salesreg8b 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());


	$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(rate*qty*tax_rate/100),2),round(sum((rate*qty)),2) from spurch group by taxtype") or die(mysqli_error());
	while($trow = mysqli_fetch_row($taxresult))
	{
		$stresult = mysqli_query($conn,"select sgst_acctcode, cgst_acctcode, igst_acctcode, sp_acctcode,tax,cess_acctcode,cess from salestax where taxtype = '$trow[0]' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($stresult);	
		$sgst_acctcode = $srow[0];	
		$cgst_acctcode = $srow[1];
		$igst_acctcode = $srow[2];
		$mySalesgl = $srow[3];
		$tax_rate = $srow[4];
		$cess_acctcode = $srow[5];
		//$cess_percent = $srow[6];
		
		$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')
		{
			//$cess_amt = round($taxamt * $cess_percent/100,2);
			$sgst_amt = round($taxamt/2,2);
			$cgst_amt = $sgst_amt;
			$igst_amt =0;
			$tax_rate = $tax_rate/2;
		}
		if($buyertype == 'INTERSTATE')
		{
			$igst_amt = round($taxamt,2);
			$sgst_amt=0;
			$cgst_amt=0;
			$cess_amt=0;
		}
		
			

		// 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 ( '$mySalesgl','$tddt2','$narration','','$pvalue','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate(),'Sales')") or die(mysqli_error());

		// Tax  Posting CREDIT
	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','$tddt2','$narration','','$sgst_amt','CR','$inv_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','$tddt2','$narration','','$cgst_amt','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	}
	if($cesstotal>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 ( '$cess_acctcode','$tddt2','$narration','','$cesstotal','CR','$inv_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','$tddt2','$narration','','$igst_amt','CR','$inv_no','$amttype','$compcode','$branch','$trans_id','$account_year','$cheqno','$filename','$voucher_no',curdate())") or die(mysqli_error());
	}	
}//end of while loop
	
	// SALES EXPENSE POSTING
	$packing = round($packing,2);
	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
	$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','$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...
	$saleamt = round($saleamt,2);
	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?>">
