<? include("../../include/dbconnect.php");
?>
 
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>South In Polymers Trading Invoice</title>

</head>
<body bgcolor="#FFFFFF" class="Normall" lang=EN-US>
<?

$fname = "invoice.txt";
$delfile = "D:/temp/invoice.txt";
if(file_exists($delfile))
 {
  unlink($delfile);
 }

if(!($myfile=fopen("d:/temp/invoice.txt","w")))
{
  print("file couldnot be opened");
}
for($ind=0;$ind <count($invnos);$ind++)
{

		$invresult = mysqli_query($conn,"select  invno, invdate, compcode, bcode, vat_percent, vat_amount, vat_cess, vatcess_amt,  freight, total, round_off, gtotal,  taxtype, cenvat_type, prepare_date, prepare_time, removal_date, remove_time, vehicle_no, account_year, trans_id, prodcode, bed, value, discount_amt, assess_value, bed_amt, edcess_amt, hscess_amt ,address from trading where invno = '$invnos[$ind]' and account_year = '$account_year'  and compcode = '$compcode' and prodcode='TOTAL' order by invno, invdate") or die(mysqli_error());
		$invrow =mysqli_fetch_row($invresult);

		$invno = $invrow[0];
		$invdate = substr($invrow[1],8,2).'-'.substr($invrow[1],5,2).'-'.substr($invrow[1],0,4);
		$compcode = $invrow[2];
		$bcode = $invrow[3];
		$vat_percent = substr($invrow[4],0,1);
		$vat_amount = $invrow[5];
		$vat_cess = substr($invrow[6],0,1);
		$vatcess_amt = $invrow[7];  
		$freight  = $invrow[8];
		$gtotal  = $invrow[9];
		$round_off  = $invrow[10]; 
		$gtotal   = $invrow[11];
		$taxtype  = $invrow[12];
		$cenvat_type  = $invrow[13]; 
		$prepare_date=substr($invrow[14],8,2).'/'.substr($invrow[14],5,2).'/'.substr($invrow[14],0,4);
		$prepare_time  = $invrow[15];
		$removal_date  = substr($invrow[16],8,2).'-'.substr($invrow[16],5,2).'-'.substr($invrow[16],0,4);
		$remove_time  = $invrow[17];
		$vehicle_no  = $invrow[18];
		$account_year  = $invrow[19];
		$trans_id = $invrow[20];
		$bed = $invrow[22];
		$totvalue = $invrow[23];
		$totdisamt = $invrow[24];
		$totassval = $invrow[25];
		$totbed_amt=$invrow[26];
		$totedcess=$invrow[27];
		$tothscess = $invrow[28];
		$addrow = explode(',',$invrow[29]);

		
		$result = mysqli_query($conn,"select name, tin, cst, ssi_no, cenvat_regno  from company where compcode like '%$compcode' ") or die(mysqli_error());
		$row = mysqli_fetch_row($result);
		$compname = $row[0];
		$comptin = $row[1];
		$compcst = $row[2];
		$compssino = $row[3];
		$compcenvat = $row[4];
		
		
		$result= mysqli_query($conn,"select name, address1, address2, city, state, pin, email, cst, tin, std, telno, fax from buy where bcode ='$bcode' ") or die(mysqli_error());
		$row = mysqli_fetch_row($result);
		$bname = $row[0];
		$baddress1=$row[1];
		$baddress2=$row[2];
		$bcity = $row[3].'-'.$row[5];
		$bemail = $row[6];
		$bcst=$row[7];
		$btin=$row[8];
		$bstd=$row[9];		
		$btelno=$row[10];		
		$bfax=$row[11];		
		$totline=0;

 		if($bcode =='111200020011')
		{
			$bname = substr(trim($addrow[0]),0,40);
			$baddress1=substr(trim($addrow[1]),0,40);
			$baddress2=substr(trim($addrow[2]),0,40);
			$bcity = substr(trim($addrow[3]).'-'.trim($addrow[5]),0,50);			
			
		}

		$result = mysqli_query($conn,"select commodity, notification, tariff from excise ") or die(mysqli_error());
		$row = mysqli_fetch_row($result);
		$commodity = $row[0];
		$notification = $row[1];
		$tariff = $row[2];
		
		while($linecnt > 64)
		{
			fputs($myfile,"\n\n");
			$linecnt++;
		}

		fputs($myfile,chr(27).chr(77));
		strcontrol($myfile,' ',15);
		strcontrol($myfile,$compcenvat,40);
		fputs($myfile,"\n");


		strcontrol($myfile,' ',15);
		strcontrol($myfile,$compcenvat,70);
		strcontrol($myfile,$comptin,20);
		fputs($myfile,"\n\n\n");


		strcontrol($myfile,' ',85);
		strcontrol($myfile,$compssino,20);
		fputs($myfile,"\n\n\n\n\n");


		strcontrol($myfile,' ',1);
		strcontrol($myfile,$commodity,20);
		strcontrol($myfile,' ',4);
		strcontrol($myfile,$notification,20);
		strcontrol($myfile,' ',1);
		strcontrol($myfile,$prepare_date.'/'.$prepare_time,25);
		strcontrol($myfile,' ',1);
		strcontrol($myfile,$removal_date.'/'.$remove_time,25);
		if($totbed_amt>0)
		{
		strcontrol($myfile,' ',1);
		strcontrol($myfile,'Payable',25);
		
	    }
		fputs($myfile,"\n\n");
		fputs($myfile,chr(27).chr(69));
		//$inv1 = $invno.'/'.$invdate;
		$inv1=$invno;
		strcontrol($myfile,' ',8);
		strcontrol($myfile,'T-'.$inv1,12);
		fputs($myfile,chr(27).chr(70));
		fputs($myfile,chr(27).chr(72));
		fputs($myfile,"\n\n");
		
		strcontrol($myfile,' ',5);
		strcontrol($myfile,$invdate,15);
		fputs($myfile,"\n\n");

		$telno = $bstd . '-'.$btelno;
		$fax = $bstd .'-'.$bfax;

		strcontrol($myfile,' ',5);
		strcontrol($myfile,$bname,52);
		strcontrol($myfile,' ',5);
		strcontrol($myfile,$telno,20);
		strcontrol($myfile,' ',6);
		strcontrol($myfile,$btin,15);
		fputs($myfile,"\n");


		strcontrol($myfile,' ',5);
		strcontrol($myfile,$baddress1,52);
		fputs($myfile,"\n");

		strcontrol($myfile,' ',5);
		strcontrol($myfile,$baddress2,52);
		strcontrol($myfile,' ',5);
		strcontrol($myfile,$fax,20);
		strcontrol($myfile,' ',5);
		strcontrol($myfile,$bcst,15);
		fputs($myfile,"\n");

		strcontrol($myfile,' ',5);
		strcontrol($myfile,$bcity,52);
		strcontrol($myfile,' ',5);
		strcontrol($myfile,$bemail,20);
		strcontrol($myfile,' ',7);
		strcontrol($myfile,$vehicle_no,15);
		fputs($myfile,"\n");

		fputs($myfile,"\n\n\n\n\n\n\n\n");				


		$linecnt=27;
						
		$x=0;

		$result = mysqli_query($conn,"select trading.prodcode, product.prodname, trading.quantity, trading.rate, trading.discount, trading.discount_amt, trading.value, trading.assess_value, trading.bed, trading.bed_amt, trading.edcess, trading.edcess_amt, trading.hs_cess, trading.hscess_amt, trading.capacity,trading.remarks  from trading, product where trading.prodcode = product.prodcode and trading.trans_id= '$trans_id'   and account_year = '$account_year' ") or die(mysqli_error());
		while($row = mysqli_fetch_row($result))
		{
				$prodcode = $row[0];
				$remarks = $row[15];
				if($remarks !='')
				{
					$prodname = $row[1].'('.$remarks.')'.
					$prodname = substr($prodname,0,40);
				}
				else
				{
						$prodname = substr($row[1],0,40);
				}

				$quantity1 = intval($row[2]);
				$rate = $row[3];
				$discount = $row[4];
				$discount_amt = $row[5];
				$value = $row[6];
				$assess_value = $row[7];
				$bed1 = $row[8];
				$bed_amt1 = $row[9];
				$edcess = $row[10];
				$edcess_amt = $row[11];
				$hs_cess = $row[12];
				$hscess_amt = $row[13];
				$capacity = intval($row[14]);

				
				$x++;
				
				fputs($myfile,chr(27).chr(15));
				strcontrol($myfile,' ',1);
				numcontrol($myfile,$x,2); 
				strcontrol($myfile,' ',5);
				strcontrol($myfile,' ',5);
				strcontrol($myfile,' ',5);
				strcontrol($myfile,$prodname,40);
				strcontrol($myfile,' ',6);
				strcontrol($myfile,$vat_percent,2);
				strcontrol($myfile,' ',6);
				numcontrol($myfile,$rate,8);

		       
			    $cap = $capacity . ' X '. $quantity1;
				$cap1 = $capacity * $quantity1;
				strcontrol($myfile,' ',4);
				strcontrol($myfile,$cap,10);
				strcontrol($myfile,' ',3);
				numcontrol($myfile,$cap1,11);
		

				numcontrol($myfile,number_format($value,2,'.',''),14);
				numcontrol($myfile,number_format($discount_amt,2,'.',''),15);
				numcontrol($myfile,number_format($assess_value,2,'.',''),18);
				numcontrol($myfile,number_format($bed_amt1,2,'.',''),16);
				numcontrol($myfile,number_format($edcess_amt,2,'.',''),12);
				numcontrol($myfile,number_format($hscess_amt,2,'.',''),11);
				fputs($myfile,chr(18).chr(27));

				$gcap1 = $gcap1 + $cap1;
				$gvalue = $gvalue + $value;
				$gdiscount_amt = $gdiscount_amt + $discount_amt;
				$gassess_value = $gassess_value + $assess_value;
				$gbed_amt1 = $gbed_amt1 + $bed_amt1;
				$gedcess_amt = $gedcess_amt + $edcess_amt;
				$ghscess_amt = $ghscess_amt + $hscess_amt;
				fputs($myfile,"\n\n");
				$linecnt=$linecnt+2;
				
				
		}

		        $taxableamt = $gassess_value + $gbed_amt1 + $gedcess_amt + $ghscess_amt;



	if($x <= 2)
	{	
		while($linecnt< 53)
		{
			fputs($myfile,"\n");
			$linecnt++;

		}
	}
	if($x == 3)
	{	
		while($linecnt< 54)
		{
			fputs($myfile,"\n");
			$linecnt++;

		}
	}	
	if($x>3)
	{
		while($linecnt< 56)
		{
			fputs($myfile,"\n");
			$linecnt++;

		}
	}
	
				fputs($myfile,chr(27).chr(15));
				strcontrol($myfile,' ',82);

				strcontrol($myfile,' ',10);
				strcontrol($myfile,$gcap1,10);

				numcontrol($myfile,number_format($gvalue,2,'.',''),10);
				numcontrol($myfile,number_format($gdiscount_amt,2,'.',''),15);
				numcontrol($myfile,number_format($gassess_value,2,'.',''),18);
				numcontrol($myfile,number_format($gbed_amt1,2,'.',''),16);
				numcontrol($myfile,number_format($gedcess_amt,2,'.',''),12);
				numcontrol($myfile,number_format($ghscess_amt,2,'.',''),11);
				fputs($myfile,chr(18).chr(27));
     			fputs($myfile,"\n\n\n");


        		fputs($myfile,chr(27).chr(77));
				strcontrol($myfile,' ',92);
				numcontrol($myfile,number_format($taxableamt,2,'.',''),12);
				fputs($myfile,"\n");

				strcontrol($myfile,' ',5);
				fputs($myfile,'Rupees ');
				fputs($myfile,connum($gtotal));
				fputs($myfile,"\n");

	
				strcontrol($myfile,' ',5);
    			fputs($myfile," only ");
				strcontrol($myfile,' ',70);
				strcontrol($myfile,$vat_percent,2);
    			fputs($myfile,"% ");
				strcontrol($myfile,' ',10);
				numcontrol($myfile,number_format($vat_amount,2,'.',''),12);
				fputs($myfile,"\n\n");
	

				strcontrol($myfile,' ',80);
				strcontrol($myfile,$vat_cess,2);
    			fputs($myfile,"% ");
				strcontrol($myfile,' ',10);
				numcontrol($myfile,number_format($vatcess_amt,2,'.',''),12);
				fputs($myfile,"\n\n");

				strcontrol($myfile,' ',92);
				numcontrol($myfile,number_format($gtotal,2,'.',''),12);
				fputs($myfile,"\n");
		
		fputs($myfile,"\n");
		$linecnt++;

				
				fputs($myfile,chr(12));
					$linecnt=65;
}


?>
<meta http-equiv="refresh" content="0 url=inv_select.php">
<?

function line($myfile,$maxlen)
{
for($sk=1;$sk <$maxlen;$sk++)
{ fputs($myfile,"_"); };
fputs($myfile,"\n");

}


function strcontrol($myfile,$stname,$maxlen)
{
$plen = strlen($stname);
$tlen = $maxlen-$plen;
fputs($myfile,$stname);
for($sk=1;$sk<$tlen;$sk++)
{ fputs($myfile," "); };
}


function numcontrol($myfile,$stname,$maxlen)
{
$plen = strlen($stname);
$tlen = $maxlen-$plen;
for($sk=1;$sk <$tlen;$sk++)
{ fputs($myfile," "); };
fputs($myfile,$stname);

}


?>
