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


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






?>

<table width=999  border=0>
 <thead>
<tr> 
<td colspan="100%"  align="center"><div align="center"></div><font size="+2" face="Arial, Helvetica, sans-serif"> <?=$company?></font></td>  </tr>
<tr><td width="993" align ="center"><h5>Sales Register from &nbsp;<? echo($stdt);?> to  &nbsp;<? echo($edt);?></td></tr></table>
<hr align="left" width = 999></hr>
				<table width="999" border="0">
				<tr>
				<td width="64" align="left"><font size="-1">Date</font></td>

				<td width="47" align="right"><font size="-1">Inv. No</font></td>
				<td width="253" align="left"><font size="-1">Customer   Name </font></td>				
				<td width="73" align="left"><font size="-1">Product </font></td>
				<td width="53" align="right"><font size="-1">Rate</font></td>				
				<td width="62" align="right"><font size="-1">Quantity</font></td>
			
				<td width="63" align="right"><font size="-1">Amount</font></td>
				<td width="59" align="right"><font size="-1">Tax </font></td>

				<td width="59" align="right"><font size="-1">Tax %</font></td>				
				
    <td width="62" align="right"><font size="-1">Sales Exp</font></td>	

    <td width="64" align="right"><font size="-1">Round off</font></td>	
								
				<td width="90" align="right"><font size="-1">Total Amount</font></td>				
				
				</tr>
			 <tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td></tr>

 </thead>

<?
$gvalue=0;
$gvat_amount=0;
$ge1vat_amount=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;
$nettot = 0;

	$invresult= mysqli_query($conn,"select distinct inv_no from salesreg8b  where inv_date >='$startdt' and inv_date <='$enddt'  and compcode='$compcode'  and taxtype ='$Tax' order by inv_no,inv_date,record ")or die(mysqli_error());


while($invrow = mysqli_fetch_row($invresult))
{

		$tresult = mysqli_query($conn,"select round_off,total,packing ,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());
		$trow = mysqli_fetch_row($tresult);
		$round_off=$trow[0];
		$gtot = $trow[1];
		$nettot = $nettot +$gtot;
		$packing = $trow[2];
		$bcode = $trow[3];		
		$ground_off=$ground_off+$round_off;

		$bslct= mysqli_query($conn,"select name from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$bname = $brow[0];
		$value_tot = 0;
		$vat_tot =0;
		$inv_tot =0;
	$presult = mysqli_query($conn,"select inv_date,bcode,inv_no,prodcode,specification,rate,quantity,value,vat_percent,vat_amount,round_off,total,taxtype,vatcess_amt,packing  from salesreg where inv_date >='$startdt' and inv_date <='$enddt'  and compcode='$compcode' and inv_no = '$invrow[0]'  and taxtype ='$Tax' order by record ")or die(mysqli_error());
	while($prow = mysqli_fetch_row($presult))
	{

		$total=0;
		$inv_date = substr($prow[0],8,2).'-'.substr($prow[0],5,2).'-'.substr($prow[0],0,4);
		$bcode= $prow[1];
		$inv_no= $prow[2];		
		$prodcode= $prow[3];		
		$specification= $prow[4];			
		$rate= $prow[5];				
		$quantity=$prow[6];
		$value=$prow[7];
		$vat_percent=$prow[8];
		$vat_amount=$prow[9];

		$taxtype = $prow[12];


		$net = $net + $gtot;

		$tresult = mysqli_query($conn,"select description,tax from salestax where taxtype = '$taxtype' ") or die(mysqli_error());
		$trow = mysqli_fetch_row($tresult);
		$taxtype= $trow[1];
					
		$value_tot=$value_tot+$value;
		$gvalue = $gvalue+$value;
		$vat_tot = $vat_tot +$vat_amount;
		$gvat_amount =$gvat_amount+$vat_amount;
	

	
		$prodslct= mysqli_query($conn,"select prodname from product where prodcode ='$prodcode' ") or die(mysqli_error());
		$prrow=mysqli_fetch_row($prodslct);
		$prodname = $prrow[0];
		 $color="#000000";
		if($prodcode== '')
		{	
			$prodname = 'TOTAL';
			 $color="#CC0000";
		}
	

		?>

		<tr>
		<td width="64" align="left"><font size="-1"><? if($temp!= $inv_no) echo($inv_date);?></font></td>
															
		<td width="47" align="center"><font size="-1"><? if($temp!= $inv_no) echo($inv_no);?></font></td>
		<td width="253" align="left"><font size="-1"><? if($temp!= $inv_no) echo($bname);?> </font></td>															
		<td width="73" align="left"><font size="-1" color="<?=$color?>"><? echo($prodname);?> </font></td>
		<td width="53" align="right"><font size="-1"><? if($rate>0) echo($rate);?></font></td>				
		<td width="62" align="right"><font size="-1"><? if($quantity>0)  echo($quantity);?></font></td>
	
		<td width="63" align="right"><font size="-1"><? if($value>0)  echo(number_format($value,2,'.',''));?></font></td>
		<td width="59" align="right"><font size="-1"  color="#CC0000"><? if($vat_amount>0)  echo($vat_amount);?></font></td>
       
		<td width="59" align="right"><font size="-1"  color="#CC0000"><? if($vat_amount>0)  echo($taxtype);?></font></td>			
		<td width="62" align="right"><font size="-1"  color="#CC0000"><? if($packing!=0)  echo(number_format($packing,2,'.',''));?></font></td>			
		<td width="64" align="right"><font size="-1"  color="#CC0000"></font></td>									
		<td width="90" align="right"><font size="-1"  color="#CC0000"><? if( $value+$vat_amount>0)  echo(number_format($value+$vat_amount,2,'.','')); ?></font></td>				
		
		</tr>
		 

		<?  
		$bname ='';

	}
	?>
		<tr bgcolor="#B1B5E5">
	
		<td colspan="6"><font size="-1"  color="#CC0000">Inv Total</font></td>
		<td width="63" align="right"><font size="-1"><? if($value_tot>0)  echo(number_format($value_tot,2,'.',''));?></font></td>
		<td width="59" align="right"><font size="-1"  color="#CC0000"><? if($vat_tot>0)  echo($vat_tot);?></font></td>
       
		<td width="59" align="right"><font size="-1"  color="#CC0000"></font></td>			
		<td width="62" align="right"><font size="-1"  color="#CC0000"><? if($packing!=0)  echo(number_format($packing,2,'.',''));?></font></td>			
		<td width="64" align="right"><font size="-1"  color="#CC0000"><? if($round_off!=0)  echo(number_format($round_off,2,'.',''));?></font></td>									
		<td width="90" align="right"><font size="-1"  color="#CC0000"><? if( $gtot>0)  echo(number_format($gtot,2,'.','')); ?></font></td>				
<?	
}										
	
?>

				</tr>
			 <tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td></tr>

 

<tr>
<td width="64" align="left"><font size="-1">&nbsp;</font></td>
													
<td width="47" align="left"><font size="-1">&nbsp;</font></td>												
<td width="253" align="left"><font size="-1">&nbsp; </font></td>
<td width="73" align="right"><font size="-1">&nbsp;</font></td>				
<td width="53" align="right"><font size="-1">&nbsp;</font></td>

<td width="62" align="right"><font size="-1" color="#CC0000"><? if($gvalue>0)  echo(number_format($gvalue,2,'.',''));?></font></td>
<td width="63" align="right"><font size="-1" color="#CC0000"><? if($gvat_amount>0)  echo(number_format($gvat_amount,2,'.',''));?></font></td>
<td width="59" align="right"><font size="-1" color="#CC0000">&nbsp;</font></td>		
<td width="59" align="right"><font size="-1" color="#CC0000">&nbsp;</font></td>		
<td width="62" align="right"><font size="-1" color="#CC0000">&nbsp;</font></td>			
<td width="64" align="right"><font size="-1" color="#CC0000"><? if($ground_off!=0)  echo($ground_off);?></font></td>								
<td width="90" align="right"><font size="-1" color="#CC0000"><? if($nettot>0)  echo(number_format($nettot,2,'.',''));?></font></td>				

</tr>
</table>			 



</table>


</body>
</form>

<?

