<?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=999  border=0>
<tr>
  <td width="993" align ="center"><h5>    Sales Register (TRADING) from &nbsp;<? echo($stdt);?> to  &nbsp;<? echo($edt);?></td>
</tr></table>

				
<table width="995" border="0">
  <tr> 
<td  colspan="100%"><hr width="100%"></td>
  </tr>
  <tr> 
    <td width="79" align="left"><font size="-1">Date</font></td>
    <td width="67" align="right"><font size="-1">Inv. No</font></td>
    <td width="307" align="left"><font size="-1">Buyer Name </font></td>
    <td width="86" align="right"><font size="-1">Amount</font></td>
    <td width="80" align="right"><font size="-1">Rate of Tax</font></td>
    <td width="87" align="right"><font size="-1">VAT AMT</font></td>
    <td width="112" align="right"><font size="-1">Total Amount</font></td>
<td width="143" align="right"><font size="-1">Tin</font></td>	
	
  </tr>
  <tr> 
<td  colspan="100%"><hr width="100%"></td>
  </tr>
  <?

	$gvalue=0;
	$gtaxamt=0;
	$ge1taxamt=0;
	$ground_off=0;
	$gtot=0;
	$net =0;
	
$todate = $stdt;
$temp = $stdt;
$startdt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);

$temp=$edt;
$enddt = substr($temp,6,4).substr($temp,3,2).substr($temp,0,2);

$temp=' ';
$temptot=0;
$invresult= mysqli_query($conn,"select distinct invno,bcode,invdate from trading  where invdate >='$startdt' and invdate <='$enddt'  and compcode='$compcode' order by invno,invdate,record ")or die(mysqli_error());
while($invrow = mysqli_fetch_row($invresult))
{

		$bcode = $invrow[1];
		$date = substr($invrow[2],8,2).'-'.substr($invrow[2],5,2).'-'.substr($invrow[2],0,4);
		$invno= $invrow[0];
		
		$presult = mysqli_query($conn,"select distinct taxtype from trading where invdate >='$startdt' and invdate <='$enddt'  and compcode='$compcode' and invno = '$invrow[0]' and prodcode = '' order by record ")or die(mysqli_error());
		$prow = mysqli_fetch_row($presult);
		$taxcode = $prow[0];
		
		$result = mysqli_query($conn,"select tax from salestax  where taxtype = '$taxcode' ");
		$row=mysqli_fetch_row($result);
		$tax_rate = $row[0];
		
		$presult = mysqli_query($conn,"select sum(value),sum(vat_amount),sum(gtotal) from trading where invdate >='$startdt' and invdate <='$enddt'  and compcode='$compcode' and invno = '$invrow[0]' order by record ")or die(mysqli_error());
		$prow = mysqli_fetch_row($presult);
		
		
		$value=$prow[0];
		$taxamt=$prow[1];
		$gtot = $prow[2];
		
		$gvalue=$gvalue+$value;
		$gtaxamt=$gtaxamt+$taxamt;
		
		$net = $net+$gtot;
		
		
			

		
		$ground_off=$ground_off+$round_off;
	
		$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="79" align="left"><font size="-1"> 
      <?  echo($date);?>
      </font></td>
    <td width="67" align="center"><font size="-1"> 
      <?  echo('T'.$invno);?>
      </font></td>
    <td width="307" align="left"><font size="-1"> 
      <?  echo($bname);?>
      </font></td>
    <td width="86" align="right"><font size="-1"> 
      <? if($value>0)  echo(number_format($value,2,'.',''));?>
      </font></td>
    <td width="80" align="right"><font size="-1"  ><? if($tax_rate >0) echo $tax_rate.'%'; ?></font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($taxamt>0)  echo($taxamt);?>
      </font></td>
    <td width="112" align="right"><font size="-1"  > 
      <? if( $gtot>0)  echo(number_format($gtot,2,'.','')); ?>
      </font></td>
    <td width="143" align="right"><font size="-1"  > 
      <?=$tin?>
      </font></td>	  
  </tr>
  <?  



}										
?>

 
  <tr> 
<td  colspan="100%"><hr width="100%"></td>
  </tr>
 

<tr>


<td colspan="4" align="right"><font size="-1" ><? if($gvalue>0)  echo(number_format($gvalue,2,'.',''));?></font></td>

<td  colspan="2" align="right"><font size="-1" ><? if($gtaxamt>0)  echo(number_format($gtaxamt,2,'.',''));?></font></td>
								
<td width="112" align="right"><font size="-1" ><? if($net>0)  echo(number_format($net,2,'.',''));?></font></td>				

</tr>
</table>			 
<?
					
?>


</table>
<a href = "http://192.168.1.2/stt/dload.php?dfile=<?echo($fname);?>">Download Text file</a>
    <br>
    <br><font size=1><center><a href= "../../index.php">Return to index page</center></font>
</body>
</form>

<?

