<?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 Report from '. $stdt. ' to  ' .$edt;


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

?>

<table width=904  border=1 rules="all">
  <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>Stock 
        Report &nbsp;<? echo($stdt);?> to &nbsp;<? echo($edt);?></td>
    </tr>
    <tr> 
      <td width="46" align="left"><font face="Arial, Helvetica, sans-serif">Sl 
        No</font></td>
      <td width="269" align="right"><div align="left"><font face="Arial, Helvetica, sans-serif">Description</font></div></td>
      <td width="66" align="left"><div align="right"><font face="Arial, Helvetica, sans-serif">Opening 
      Balance </font></div></td>
      <td width="75" align="left"><div align="right"><font face="Arial, Helvetica, sans-serif">Manu. Purchased</font></div></td>
      <td width="52" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Total</font></div></td>
      <td width="51" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Weight</font></div></td>
      <td width="59" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Liters</font></div></td>
      <td width="51" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Sales</font></div></td>
      <td width="50" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Weight</font></div></td>
      <td width="60" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Liters</font></div></td>
      <td width="55" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif">Closing</font></div></td>
    </tr>

  </thead>
  <?
$startdt = us($stdt);
$enddt = us($edt);
$slno=1;

$opening_total = 0;
$purchased_total = 0;
$total_qty = 0;
$grtotal_wt =0;
$grtotal_liters=0;
$sales_total =0;
$sales_wt = 0;
$sales_liters =0;
$closing_total  = 0;
for($ind =0;$ind < count($prodcode);$ind++)
{

	$prodresult = mysqli_query($conn,"select prodname,capacity,weight from product where prodcode = '$prodcode[$ind]' ") or die(mysqli_error());
	$prodrow = mysqli_fetch_row($prodresult);
	$prodname = $prodrow[0];
	$capacity = $prodrow[1];
	$weight = $prodrow[2];


	$result = mysqli_query($conn,"select opening_balance  from acctmast where acctcode = '$prodcode[$ind]' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$op = $row[0];	

	$result = mysqli_query($conn,"select sum(inqty) - sum(outqty)  from stktrans where acctcode = '$prodcode[$ind]'  and date < '$startdt' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$opening = $op + $row[0];	
	
	$result = mysqli_query($conn,"select sum(inqty) from stktrans where acctcode = '$prodcode[$ind]'  and date >= '$startdt' and date <= '$enddt' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$purchased =  $row[0];
	
	$result = mysqli_query($conn,"select sum(outqty) from stktrans where acctcode = '$prodcode[$ind]'  and date >= '$startdt' and date <= '$enddt' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$sold =  $row[0];
	
	
	$total = $opening + $purchased;
//	$total_liters = $total * $capacity;
//   As per request, the opening balance should not be included in Litters.
	$total_liters = $purchased * $capacity;
	$total_wt = $total * $weight;
	$sold_wt = $sold * $weight;
	$sold_liters = $sold * $capacity;
	$closing = $total - $sold;
		
	$opening_total = $opening_total+$opening;
	$purchased_total = $purchased_total+$purchased;
	$total_qty = $total_qty+$total;
	$grtotal_wt =$grtotal_wt+$total_wt;
	$grtotal_liters=$grtotal_liters+$total_liters;
	$sales_total =$sales_total+$sold;
	$sales_wt = $sales_wt+$sold_wt;
	$sales_liters =$sales_liters+$sold_liters;
	$closing_total  = $closing_total+$closing;
	
		
	$mt= $opening + $purchased+ $sold;
	
if($mt!=0)
{
		?>
  <tbody>
    <tr> 
      <td width="46" align="left"><div align="left"><font face="Arial, Helvetica, sans-serif"> 
          <? echo($slno);?> </font></div></td>
      <td width="269" align="center"><div align="left"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($prodname);?> </font></div></td>
      <td width="66" align="left"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($opening);?> </font></div></td>
      <td width="75" align="left"><div align="right"><font color="<?=$color?>" face="Arial, Helvetica, sans-serif"><? echo($purchased);?> 
      </font></div></td>
      <td width="52" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($total);?> </font></div></td>
      <td width="51" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($total_wt);?> </font></div></td>
      <td width="59" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($total_liters);?> </font></div></td>
      <td width="51" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($sold);?> </font></div></td>
      <td width="50" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($sold_wt);?> </font></div></td>
      <td width="60" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
      <? echo($sold_liters);?> </font></div></td>
      <td width="55" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"  > 
      <? echo($closing);?> </font></div></td>
    </tr>
  </tbody>
  <?  
  $slno++;
  }
}


					
?>

  <tr> 
    <td width="46" align="left"><div align="left"><font face="Arial, Helvetica, sans-serif"> 
        &nbsp; </font></div></td>
    <td width="269" align="center"><div align="left"><font face="Arial, Helvetica, sans-serif"> 
    &nbsp; </font></div></td>
    <td width="66" align="left"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($opening_total);?> </font></div></td>
    <td width="75" align="left"><div align="right"><font color="<?=$color?>" face="Arial, Helvetica, sans-serif"><? echo($purchased_total);?> 
    </font></div></td>
    <td width="52" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($total_qty);?> </font></div></td>
    <td width="51" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($grtotal_wt);?> </font></div></td>
    <td width="59" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($grtotal_liters);?> </font></div></td>
    <td width="51" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($sales_total);?> </font></div></td>
    <td width="50" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($sales_wt);?> </font></div></td>
    <td width="60" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"> 
    <? echo($sales_liters);?> </font></div></td>
    <td width="55" align="right"><div align="right"><font face="Arial, Helvetica, sans-serif"  > 
        <? if($closing_total !=0) echo($closing_total);?>
    </font></div></td>
  </tr>
</table>
 
  
			 



</table>
    <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,"    ");
	}
}
?>