<?php include("../../include/dbconnect.php"); 

$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];
?>

<table width=441  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 
        Details from &nbsp;<? echo($from_date);?> to &nbsp;<? echo($to_date);?></td>
    </tr>
    <tr> 
      <td width="137" align="left"><div align="left"><font size="-1" face="Arial, Helvetica, sans-serif">Invno</font></div></td>
      <td width="138" align="right"><div align="left"><font size="-1" face="Arial, Helvetica, sans-serif">Inv. 
          Date</font></div></td>
      <td width="152" align="right"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif">Total 
          Amount</font></div></td>
    </tr>
    <tr> 
      <td><hr align="left"></td>
      <td><hr align="left"></td>
      <td><hr align="right"></td>
    </tr>
  </thead>
  <tbody>
    <?
$startdt = us($from_date);
$enddt = us($to_date);

$grtotal = 0;
for($ind=0;$ind<count($bcode);$ind++)
{
	$total =0;
	$invstatus = 'T';
	$invresult= mysqli_query($conn,"select distinct invno,invdate,gtotal from salesreg  where invdate >='$startdt' and invdate <='$enddt'  and compcode='$compcode' and bcode = '$bcode[$ind]' and prodcode = 'TOTAL' order by invno,invdate,record ")or die(mysqli_error());
	while($invrow = mysqli_fetch_row($invresult))
	{
		$total = $total + $invrow[2];	
		$grtotal = $grtotal + $invrow[2];
		$bslct= mysqli_query($conn,"select name from buy where bcode = '$bcode[$ind]' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$bname = $brow[0];
		if($invstatus == 'T')
		{
			?>
  <tbody>
    <tr> 
      <td align="left" colspan="3"><div align="left"><font size="-1" face="Arial, Helvetica, sans-serif" color="#0000FF"> 
          <? echo($bname);?> </font></div></td>
    </tr>
    <?
			$invstatus = 'F';
		}
		?>
    <tr> 
      <td width="137" align="left"><div align="left"><font size="-1" face="Arial, Helvetica, sans-serif"> 
          <? echo($invrow[0]);?> </font></div></td>
      <td width="138" align="center"><div align="left"><font size="-1" face="Arial, Helvetica, sans-serif"><? echo(british($invrow[1]));?> 
          </font></div></td>
      <td width="152" align="left"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif"> 
          <? echo($invrow[2]);?> </font></div></td>
    </tr>
    <?
	}
	if($total >0)
	{
	?>
    <tr> 
      <td ><div align="left"></div></td>
      <td width="138" align="center"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif" color="#990000">Total</font></div></td>
      <td width="152" align="left"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif" color="#990000"> 
          <? echo(number_format($total,2));?> </font></div></td>
    </tr>
    <?
	}
	?>
  </tbody>
  <?  



}										
?>
  <tr> 
    <td><hr align="left"></td>
    <td><hr align="right"></td>
    <td><hr align="right"></td>
  </tr>
  <tr> 
    <td width="137" align="left"><div align="left"><font size="-1" face="Arial, Helvetica, sans-serif"> 
        &nbsp; </font></div></td>
    <td width="138" align="center"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif" color="#990000"> 
        Grand total </font></div></td>
    <td width="152" align="left"><div align="right"><font size="-1" face="Arial, Helvetica, sans-serif" color="#990000"> 
        <?=number_format($grtotal,2)?>
        </font></div></td>
  </tr>
</table>

  
			 
