<body background="">
<?php include("../../include/dbconnect.php"); 


if($cnf=="Yes")
{

if(empty($bcode)) 
{
	echo("Customer not selected, Please select Customer and resubmit<br>");
	exit;
}


$result = mysql_query("select compcode,account_year from company where compcode like '%$compcode' ") or die(mysql_error());
$row = mysql_fetch_row($result);

$account_year = $row[1];





$result= mysql_query("select name,address1,address2,city,state,pin,cst,gstin,taxtype,eccno,vendercode from buy where bcode ='$bcode' ") or die(mysql_error());
$row = mysql_fetch_row($result);
$custname = $row[0];
$address1=$row[1];
$address2=$row[2];
$city = $row[3];
$state=$row[4];
$pin =$row[5];
$cst=$row[6];
$tin=$row[7];
$taxtype=$row[8];
$eccno=$row[9];
$vendercode=$row[10];


$result = mysql_query("select description,tariff,tax,surcharge,ad_surcharge from salestax where taxtype ='$taxcode' ") or die(mysql_error());;
$row = mysql_fetch_row($result);
$st_class=$row[0];
$st_type = $row[1];
$tax = $row[2];
$surcharge= $row[3];
$ad_surcharge=$row[4];	

$indt = substr($invdt,6,4).'-'.substr($invdt,3,2).'-'.substr($invdt,0,2);
$month = substr($invdt,3,2);
$year = substr($invdt,6,4);
	
for($ind=0;$ind<5;$ind++)
{

	if($qty1[$ind]>0)
	{
		
		$result = mysql_query("select issued_qty,balance_qty,schedule_qty from orders where record = '$record1[$ind]'") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$issue_qty = $row[0];
		$balance_qty = $row[1];
		$schedule_qty = $row[2];
	
		$issue_qty = $issue_qty + $qty1[$ind];
		$balance_qty = $balance_qty - $qty1[$ind];

		
		$result = mysql_query("select prodcode,drawing_no,order_no,order_date, issued_qty,balance_qty,rate from orders where record = '$record1[$ind]' ") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$prodcode=$row[0];
		$drawing_no = $row[1];
		$order_no=$row[2];
		$order_date=$row[3];
		$issued_qty=$row[4];
		$balance_qty=$row[5];
		$rate= $row[6];
	
		
	
	
		$result= mysql_query("select prodname,tariff from product where prodcode ='$prodcode' and bcode ='$bcode' ") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$prodname = $row[0];
		$tariff=$row[1];
	
	

		
		$result = mysql_query("select schedule_no,schedule_date from orders where record = '$record1[$ind]' ") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$schedule_no=$row[0];
		$schedule_date = $row[1];
		
		if(!empty($schedule_no))
		{
			$scn = 'Sch No:'. $schedule_no. 'Dt:&nbsp;'.substr($schedule_date,8,2).'-'.substr($schedule_date,5,2).'-'.substr($schedule_date,0,4);
		}
		else 
		{
			$scn=' ';
		}
		
		$ord_scd=$order_no.$scn;
		$value = $qty1[$ind] * $rate;
	
		$totalvalue = $totalvalue  + $value;
		
		$query = "insert into labourinv values('$invno','$indt','$compcode','$prodcode','$drawing_no','$bcode','$qty1[$ind]','$rate','','$value','','',	'','','','','','','','','','',	'F','$order_no','$order_date','$schedule_no','$schedule_date','$month','$year','$typeofsale','$cexreg','$prepare_time','$removal_time','$account_year','$cust_record','','','','','$record1[$ind]',NULL)";
		mysql_query($query) or die(mysql_error());
		$result = mysql_query("select issued_qty, balance_qty,inv_nos,schedule_qty from orders where record= '$record1[$ind]' ") or die(mysql_error());
		$row = mysql_fetch_row($result);
		$issue=$row[0];
		$balance=$row[1];
		$issue=$issue+$qty1[$ind];
		$balance=$balance-$qty1[$ind];
		$order_qty=$row[3];
		
		if($balance2 < 0)
		{
			echo("Stock qty is less than issueing qty.  Please correct stock register later<br>");
		}
	
	
		if(empty($row[2]))
		{
			$invnos=$invno.'&nbsp;/&nbsp;'.$indt.'('.$qty1[$ind].')';
		
		}
		elseif(!empty($row[2]))
		{
			$invnos=' '.$row[2].','.$invno.'/'.$invdt.'('.$qty1[$ind].')';
		
		}
		//$invdt=substr($inv_date,6,4).'-'.substr($inv_date,3,2).'-'.substr($inv_date,0,2);				
		
		$query="update orders set balance_qty='$balance',issued_qty='$issue',inv_nos='$invnos',inv_date='$indt' where record = '$record1[$ind]' ";
		mysql_query($query) or die(mysql_error());				

	}
}

	$vat_app_value = $totalvalue;
	$taxamt = $vat_app_value * $tax /100;
	$tax_rate = $tax;
	$tax_rate1 = $tax;
		if($typeofsale == 'STATE')
		{
			$sgst_amt = round($taxamt/2,2);
			$cgst_amt = $sgst_amt;
			$igst_amt =0;
			$tax_rate = round($tax_rate/2,2);
		}
		if($typeofsale == 'INTERSTATE')
		{
			$igst_amt = round($taxamt,2);
			$sgst_amt=0;
			$cgst_amt=0;
		}

	$sum_amt = round($vat_app_value + $taxamt+$transport_charges,2);
	$ceiled_amt = round($sum_amt,0);
	$difference  = $ceiled_amt - $sum_amt;
	


	$result= mysql_query("select max(record) from custledger order by record " ) or die(mysql_error());
	$row = mysql_fetch_row($result);
	$cust_record = $row[0]+1;

	$query = "insert into labourinv values('$invno','$indt','$compcode','','TOTAL','$bcode','','','','$totalvalue','$packing_percentage','$pack_amt',	'$taxcode','$sgst_amt','$cgst_amt','$igst_amt','$tax','$taxamt','$fright','$sum_amt','$difference','$ceiled_amt',	'F','$order_no','$order_date','$schedule_no','$schedule_date','$month','$year','$remarks1','$reference1','$prepare_time','$removal_time','$account_year','$cust_record','','','','','',NULL)";
	mysql_query($query) or die(mysql_error());
	

	$query="insert into custledger values ('$bcode','$invno','$indt','$prodcode','Towards Inv. $invno','','','$ceiled_amt','','DB','','','','','','','','No','$compcode','','','',NULL)";
	mysql_query($query) or die (mysql_error());	

	

	?>
	<table>
	<tr><td align="right" > <font color="#990066" size="+1">Thank you Sales Posted in Labour Invoice register .  </font> </td></tr>
	
	</table>
	<table>

	</table>
	<?
}	


?>
<br>
<br><br><font size=1><center><a href= "linput.php">Make another Labour Invoice</a></center></font>
<br><br><font size=1><center><a href= "index.html">Return to index page</a></center></font>
