<?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=911  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>        Sales 
        Report (Van Sales)from &nbsp;<? echo($stdt);?> to &nbsp;<? echo($edt);?></td>
    </tr>
    <tr> 
      <td width="38" align="left"><div align="left"><font  face="Arial, Helvetica, sans-serif">Sl 
          No</font></div></td>
      <td width="260" align="right"><div align="left"><font  face="Arial, Helvetica, sans-serif">Description</font></div></td>
      <td width="67" align="left"><div align="right"><font  face="Arial, Helvetica, sans-serif">Opening 
          Balance </font></div></td>
      <td width="76" align="left"><div align="right"><font  face="Arial, Helvetica, sans-serif">Manuf / Purchased</font></div></td>
      <td width="51" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">Total</font></div></td>
      <td width="45" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">Issued</font></div></td>
      <td width="75" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">Assess 
          Value</font></div></td>
      <td width="61" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">Duty</font></div></td>
      <td width="70" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">ED 
          Cess</font></div></td>
      <td width="58" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">HSc 
          Cess</font></div></td>
      <td width="64" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif">Closing 
          Balance</font></div></td>
    </tr>
  </thead>
  <?
$startdt = us($stdt);
$enddt = us($edt);
$slno=1;
	$opening_total =0;
	$purchased_total =0;
	$issued_total =0;
	$closing_total = 0;
	$assess_total = 0;
	$bed_total =0;
	$edcess_total = 0;
	$hscess_total = 0;
$linecnt = 1;
for($ind =0;$ind < count($prodcode);$ind++)
{

	$opening=0;
	$purchased=0;
	$total =0;
	$assess_value=0;
	$bedamt=0;
	$edcessamt=0;
	$hscessamt=0;
	$issued=0;
	$prodresult = mysqli_query($conn,"select prodname from product where prodcode = '$prodcode[$ind]' ") or die(mysqli_error());
	$prodrow = mysqli_fetch_row($prodresult);
	$prodname = $prodrow[0];	

	$result = mysqli_query($conn,"select opening_balance  from acctmast where acctcode = '$prodcode[$ind]' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$opening = $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 = $opening + $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];	
	$total = $opening + $purchased;
	
	$result = mysqli_query($conn,"select sum(quantity),sum(assess_value),sum(bed_amt),sum(edcess_amt),sum(hscess_amt) from trading where prodcode = '$prodcode[$ind]'  and invdate >= '$startdt' and invdate <= '$enddt' ") or die(mysqli_error());
	$row = mysqli_fetch_row($result);
	$issued =  $row[0];	
	$assess_value = $row[1];
	$bedamt = $row[2];
	$edcessamt = $row[3];
	$hscessamt = $row[4];
	$closing = $total - $issued;
	$opening_total = $opening_total + $opening;
	$purchased_total = $purchased_total + $purchased;
	$closing_total = $closing_total + $closing;
	$issued_total = $issued_total + $issued;
	
	$assess_total = $assess_total + $assess_value;
	$bed_total =$bed_total + $bedamt;
	$edcess_total = $edcess_total + $edcessamt;
	$hscess_total = $hscess_total + $hscessamt;
	$mt = $total + $issued;

if($mt!=0)
{
		?>
  <tbody>
    <tr> 
      <td width="38" align="left"><div align="left"><font  face="Arial, Helvetica, sans-serif"> 
          <? echo($slno);?> </font></div></td>
      <td width="260" align="center"><div align="left"><font  face="Arial, Helvetica, sans-serif"> 
          <? echo($prodname);?> </font></div></td>
      <td width="67" align="left"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
          <? echo(number_format($opening,0));?> </font></div></td>
      <td width="76" align="left"><div align="right"><font color="<?=$color?>"  face="Arial, Helvetica, sans-serif"><? echo(number_format($purchased,0));?> 
          </font></div></td>
      <td width="51" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
          <? echo(number_format($total,0));?> </font></div></td>
      <td width="45" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
          <? echo(number_format($issued,0));?> </font></div></td>
      <td width="75" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
          <? echo(number_format($assess_value,0));?> </font></div></td>
      <td width="61" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
          <?  echo(number_format($bedamt,0));?>
          </font></div></td>
      <td width="70" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
          <?  echo(number_format($edcessamt,0));?>
          </font></div></td>
      <td width="58" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"  > 
          <?  echo(number_format($hscessamt,0));?>
          </font></div></td>
      <td width="64" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"  > 
          <?  echo(number_format($closing,0));?>
          </font></div></td>
    </tr>
  </tbody>
  <?  
 
  strcontrol($myfile,$slno,3);
  strcontrol($myfile," ",2); 
  strcontrol($myfile,substr($prodname,0,29),30);
  numcontrol($myfile,number_format($opening,2),9);
  numcontrol($myfile,$purchased,9);
  numcontrol($myfile,number_format($total,2),9);
  numcontrol($myfile,$issued,9);
  numcontrol($myfile,$assess_value,12);
  numcontrol($myfile,$bedamt,9);
  numcontrol($myfile,$edcessamt,9);
  numcontrol($myfile,$hscessamt,9);
  numcontrol($myfile,$closing,9);
  $slno++;

  fputs($myfile,"\n");
  $linecnt++;
  
  }
  		if ($linecnt > 53)
		{
            title($myfile,$company,$string1);
			$linecnt=1;
			fputs($myfile,"\n");
			$linecnt++;

		}

}
  line($myfile,110);
  $linecnt++;
  fputs($myfile,"\n");
  strcontrol($myfile," ",31); 
  numcontrol($myfile,$opening_total,9);
  numcontrol($myfile,$purchased_total,9);
  $tot = $opening_total+$purchased_total;
  numcontrol($myfile,$tot,9);
  numcontrol($myfile,$issued_total,9);
  numcontrol($myfile,$assess_total,13);
  numcontrol($myfile,$bed_total,9);
  numcontrol($myfile,$edcess_total,9);
  numcontrol($myfile,$hscess_total,9);
  numcontrol($myfile,$closing_total,10);

  fputs($myfile,"\n");
  line($myfile,110);
  $linecnt++;

					
?>
   <tr> 
    <td width="38" align="left"><font  face="Arial, Helvetica, sans-serif">&nbsp; 
       </font></td>
    <td width="260" align="center"><font  face="Arial, Helvetica, sans-serif">&nbsp; 
       </font></td>
    <td width="67" align="left"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
        <?=number_format($opening_total,0)?>
        </font></div></td>
    <td width="76" align="left"><div align="right"><font color="<?=$color?>"  face="Arial, Helvetica, sans-serif">
        <?=number_format($purchased_total,0)?>
        </font></div></td>
    <td width="51" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
        <?=number_format($opening_total+$purchased_total,0)?>
        </font></div></td>
    <td width="45" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
        <?=number_format($issued_total,0)?>
        </font></div></td>
    <td width="75" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
        <?  echo(number_format($assess_total,0));?>
        </font></div></td>
    <td width="61" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"> 
        <?  echo(number_format($bed_total,0));?>
        </font></div></td>
    <td width="70" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"  > 
        <?  echo(number_format($edcess_total,0));?>
        </font></div></td>
    <td width="58" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"  > 
        <? echo(number_format($hscess_total,0));?>
        </font></div></td>
    <td width="64" align="right"><div align="right"><font  face="Arial, Helvetica, sans-serif"  > 
        <? echo(number_format($closing_total,0));?>
        </font></div></td>
  </tr>
</table>
 
  <tr> 
<td colspan="100%"><font face="Arial, Helvetica, sans-serif"><a href = "dload.php?dfile=<?echo($fname);?>">Download 
Text file</a></font></td>
</tr>

			 



</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,chr(27).'M');
	line($myfile,110);
    fputs($myfile,"\n");
	fputs($myfile,"Sl.No   Description              Opening   Manuf/  Total     Iss     Assess    Duty     ED     HSC    Closing \n"); 
	fputs($myfile,"                                           Purch                      Value            Cess   Cess      Bal \n"); 
   line($myfile,110);
   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,"    ");
//	}
}

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

}

?>