<?php include("../../include/dbconnect.php"); 
$delfile = "d:/temp/invoice.txt";

if(file_exists($delfile))
 {
  unlink($delfile);
 }

$fname = 'invoice.txt';
if(!($myfile=fopen("d:/temp/invoice.txt","w")))
{
  print("file couldnot be opened");
}

$pgno=1;
$kount=1;

$result=mysqli_query($conn," select name from company where  compcode='$compcode' order by name") or die(mysqli_error());
$row=mysqli_fetch_row($result);
$company=$row[0];

$string1='Sales Register from '. $stdt. ' to  ' .$edt;


title($myfile,$company,$string1);
$kount=$kount+6;	

?>

<table width=999  border=0>
  <thead>
    <tr>
      <td colspan="100%" align ="center"><h4><font face="Arial, Helvetica, sans-serif"> 
          <?=$company?>
          </font></h4></td>
    </tr>
    <tr> 
      <td colspan="100%"align ="center"><font face="Arial, Helvetica, sans-serif"><h5>Sales 
        Register from &nbsp;<? echo($stdt);?> to &nbsp;<? echo($edt);?></font></td>
    </tr>
    <tr> 
      <td width="53" align="left"><font size="-1" face="Arial, Helvetica, sans-serif">Date</font></td>
      <td width="44" align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Inv. 
        No</font></td>
      <td width="44" align="right"><font size="-1" face="Arial, Helvetica, sans-serif">TIN. 
        No</font></td>        
      <td width="200" align="left"><font size="-1" face="Arial, Helvetica, sans-serif">Customer 
        Name </font></td>
      <td width="77" align="left"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Liters 
          </font></div></td>
      <td width="74" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Assess.Value</font></div></td>
      <td width="56" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Duty 
          Amt</font></div></td>

      <td width="64" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Net 
          Amt </font></div></td>
      <td width="60" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Vat</font></div></td>

      <td width="54" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">CST</font></div></td>
      <td width="93" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Total 
          Amount</font></div></td>
    </tr>
    <tr> 
      <td><hr></td>
      <td><hr></td>
      <td><hr></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
    </tr>
  </thead>
  <tbody>
    <?

	$gvalue=0;
	$gtaxamt=0;
	$ge1taxamt=0;
	$ground_off=0;
	$gtot=0;
	$net =0;
	
$todate = $stdt;
$temp = $stdt;
$startdt = us($stdt);

$temp=$edt;
$enddt = us($edt);

$temp=' ';
	$assess_total=0;
	$liter_total=0;
	$bed_total =0;
	$edcess_total = 0;
	$hscess_total =0;
	$vat_total = 0;
	$vatcess_total =0;
	$cst_total = 0;
	$grtotal =0;
	$net_total=0;
$invresult= mysqli_query($conn,"select distinct inv_no from salesreg8b  where inv_date >='$startdt' and inv_date <='$enddt'  and compcode='$compcode' order by inv_no,inv_date,record ")or die(mysqli_error());
while($invrow = mysqli_fetch_row($invresult))
{


	$presult = mysqli_query($conn,"select sum(capacity*quantity) from salesreg8b where inv_date >='$startdt' and inv_date <='$enddt'  and compcode='$compcode' and inv_no = '$invrow[0]' order by record ")or die(mysqli_error());
	$prow = mysqli_fetch_row($presult);
	$liters = $prow[0];
	
	$presult = mysqli_query($conn,"select assess_value,bed_amt,edcess_amt,hscess_amt,vat_amount,vatcess_amt,total,taxtype,inv_date,bcode from salesreg8b where inv_date >='$startdt' and inv_date <='$enddt'  and compcode='$compcode' and inv_no = '$invrow[0]' and prodcode = 'TOTAL' order by record ")or die(mysqli_error());
	$prow = mysqli_fetch_row($presult);
	$assess_value = $prow[0];
	$bed_amt = $prow[1];
	$edcessamt = $prow[2];
	$hscessamt = $prow[3];
	$tax_amt = $prow[4];
	$vatcess_amt = $prow[5];
	$total = $prow[6];
	$taxcode = $prow[7];
	$invdate = british($prow[8]);
	$bcode = $prow[9];	
	
	

	$net_amt = $assess_value + $bed_amt +$hscessamt+$edcessamt;

			
		$gvalue=$gvalue+$value;
		if($taxcode ==9)
		{
			$vatamt=$tax_amt;
			$vat_total = $vat_total + $vatamt;				
		}
		elseif($taxcode ==6)
		{
			$cstamt =$tax_amt;
			$cst_total = $cst_total + $cstamt;			
		}
	
	$assess_total = $assess_total + $assess_value;
	$liter_total = $liter_total + $liters;
	$bed_total =$bed_total + $bed_amt;
	$edcess_total = $edcess_total + $edcessamt;
	$hscess_total = $hscess_total + $hscessamt;
	$net_total = $net_total + $net_amt;
	$vatcess_total = $vatcess_total + $vatcess_amt ;
	$grtotal = $grtotal + $total;

	
	
		$bslct= mysqli_query($conn,"select name,tin from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$bname = $brow[0];
		$tin = $brow[1];

		?>
    <tr> 
      <td width="53" align="left"><font size="-1" face="Arial, Helvetica, sans-serif"> 
        <? echo($invdate);?> </font></td>
      <td width="44" align="center"><font size="-1" face="Arial, Helvetica, sans-serif"> 
        <? echo($invrow[0]);?> </font></td>
      <td width="44" align="center"><font size="-1" face="Arial, Helvetica, sans-serif"> 
        <? echo($tin);?> </font></td>        
      <td width="200" align="left"><font size="-1" face="Arial, Helvetica, sans-serif"> 
        <? echo($bname);?> </font></td>
      <td width="77" align="left"><div align="right"><font color="<?=$color?>" size="-1" face="Arial, Helvetica, sans-serif"><? echo($liters);?> 
          </font></div></td>
      <td width="74" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"> 
          <? echo($assess_value);?> </font></div></td>
      <td width="56" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"> 
          <? if($bed_amt >0) echo($bed_amt);?>
          </font></div></td>

      <td width="64" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <?=$net_amt;?>
          </font></div></td>
      <td width="60" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <? if($vatamt >0) echo($vatamt);?>
          </font></div></td>

      <td width="54" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <? if($cstamt >0) echo($cstamt);?>
          </font></div></td>
      <td width="93" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <? if($total >0) echo(round($total,0));?>
          </font></div></td>
    </tr>
    <?  

		$vatamt=0;
		$cstamt=0;
		if($temptot== $gtot)
		{
			$temptot=0;
		}
					

}										
?>
    <tr> 
      <td><hr></td>
      <td><hr></td>
      <td><hr></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
      <td><hr align="right"></td>
    </tr>
    <tr> 
      <td width="53" align="left"><font size="-1" face="Arial, Helvetica, sans-serif">&nbsp; 
         </font></td>
      <td width="44" align="center"><font size="-1" face="Arial, Helvetica, sans-serif">&nbsp; 
         </font></td>
      <td width="44" align="center"><font size="-1" face="Arial, Helvetica, sans-serif">&nbsp; 
         </font></td>         
      <td width="200" align="left"><font size="-1" face="Arial, Helvetica, sans-serif">&nbsp; 
         </font></td>
      <td width="77" align="left"><div align="right"><font color="<?=$color?>" size="-1" face="Arial, Helvetica, sans-serif"><? echo(number_format($liter_total,2));?> 
          </font></div></td>
      <td width="74" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"> 
          <? echo(number_format($assess_total,2));?> </font></div></td>
      <td width="56" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"> 
          <? if($bed_total >0) echo(number_format($bed_total,2));?>
          </font></div></td>

      <td width="64" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <?=number_format($net_total,2);?>
          </font></div></td>
      <td width="60" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <? if($vat_total >0) echo(number_format($vat_total,2));?>
          </font></div></td>

      <td width="54" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <? if($cst_total >0) echo(number_format($cst_total,2));?>
          </font></div></td>
      <td width="93" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"  > 
          <? if($grtotal >0) echo(number_format(round($grtotal,0),2));?>
          </font></div></td>
    </tr>
</table>

  
			 
<?
strcontrol($myfile,"",105);

numcontrol($myfile,number_format($gvalue,2),12);
numcontrol($myfile,$gtaxamt,10);
numcontrol($myfile,$ge1taxamt,7);
strcontrol($myfile,$ground_off,8);				
numcontrol($myfile,$gtot,12);
fputs($myfile,"\n");
$kount++;					
?>



    <br>
    <br><font size=1><center><a href= "../../index.html">Return to index page</center></font>
                                                                             
</body>
</form>

<?

function title($myfile,$company,$string1) 
{
	fputs($myfile,chr(18).chr(14));
	fputs($myfile,"$company \n\n");
	fputs($myfile,"$string1 \n");
	fputs($myfile,chr(27).'M');
	fputs($myfile,"_________________________________________________________________________________________________________________________________________________________\n");
	fputs($myfile,"Date     Inv No   Buyer                               Product                                Rate    Qty     Amount     VAT       E1 Sales     Total Amt.\n"); 
	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," "); };
	if($stname >0)
	{
		fputs($myfile,$stname);
	}
	else
	{
	fputs($myfile,"    ");
	}
}
?>