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

$string1='Sales Register from '. $stdt. ' to  ' .$edt;



$kount=$kount+6;	

?>

<table width=1241  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  colspan="100%" align ="center"><h5>Sales Register from &nbsp;<? echo($stdt);?> to  &nbsp;<? echo($edt);?></td></tr>
<tr><td colspan="100%"><hr/></td></tr>

				<tr>
				<td width="38" align="left"><font size="-1">Date</font></td>

				<td width="33" align="right"><font size="-1">Inv. No</font></td>
				<td width="64" align="left"><font size="-1">Customer   Name </font></td>		
                				<td width="46" align="left"><font size="-1">GSTIN </font></td>		
				<td width="53" align="left"><font size="-1">Product </font></td>
				<td width="42" align="left"><font size="-1">HSN Code </font></td>                
				<td width="53" align="left"><font size="-1">Product Type </font></td>                
                				<td width="52" align="right"><font size="-1">Quantity</font></td>
				<td width="40" align="right"><font size="-1">Rate</font></td>				

			
				<td width="49" align="right"><font size="-1">Amount</font></td>
				<td width="54" align="right"><font size="-1">Discount</font></td>
				<td width="49" align="right"><font size="-1">Taxable Amount</font></td>                
				<td width="41" align="right"><font size="-1">Tax %</font></td>                
				<td width="39" align="right"><font size="-1">SGST </font></td>
				
				
    <td width="42" align="right"><font size="-1">CGST</font></td>	
    <td width="44" align="right"><font size="-1">CESS</font></td>    
    <td width="44" align="right"><font size="-1">IGST</font></td>	
	    
    <td width="42" align="right"><font size="-1">Tot Tax</font></td>	

								
				<td width="61" align="right"><font size="-1">Total Amount</font></td>		
					<td width="172" align="right"><div align="left"><font size="-1">State</font></div></td>	                		
					<td width="89" align="right"><div align="left"><font size="-1">Dealer Type</font></div></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><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td width="172"><hr></td><td width="89"><hr></td></tr>

 </thead>

<?

	$gvalue=0;
	$sgst_tot=0;
	$cgst_tot=0;
	$igst_tot=0;
	$cess_tot = 0;
	$ge1vat_amount=0;
	$ground_off=0;
	$gtot=0;
	$net =0;
	$disc_total = 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 inv_no,account_year from salesreg8b  where inv_date >='$startdt' and inv_date <='$enddt'  and compcode='$compcode' order by inv_no,inv_date,record ")or die(mysqli_error());


while($invrow = mysqli_fetch_row($invresult))
{
	$account_year = $invrow[1];

	$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,discount_amt  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());
	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];
		$cess_amt = $prow[13];
		$discount_amt = $prow[15];



		$tresult = mysqli_query($conn,"select description,tax from salestax where taxtype = '$taxtype' ") or die(mysqli_error());
		$trow = mysqli_fetch_row($tresult);
		$taxtype= $trow[1];
		$cess_tot = $cess_tot+$cess_amt;		
		$gvalue=$gvalue+$value;
		$disc_total=$disc_total+$discount_amt;
		$net = $net + $value+$vat_amount+$cess_amt;
	
			$bslct= mysqli_query($conn,"select name,gstin,taxtype from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$bname = $brow[0];
		$gstin = $brow[1];
		$buyertype = $brow[2];
		if($buyertype =='')
		{
			$buyertype = 'STATE';
		}
		if($buyertype == 'STATE')
		{
			$sgst_amt = $vat_amount/2;
			$cgst_amt = $sgst_amt;
			$sgst_tot = $sgst_tot +$sgst_amt;
		}
		else
		{
			$igst_amt = $vat_amount;
			$igst_tot =$igst_tot+$igst_amt;
		}
	/*
		$tresult = mysqli_query($conn,"select round_off,total,packing  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];
		$packing = $trow[2];		
		
		$ground_off=$ground_off+$round_off;
*/

		$bslct= mysqli_query($conn,"select name,gstin from buy where bcode = '$bcode' ") or die(mysqli_error());
		$brow=mysqli_fetch_row($bslct);
		$bname = $brow[0];
		$gstin = $brow[1];
		
		$state_code = substr($gstin,0,2);
		$sresult = mysqli_query($conn,"select state_name from states where state_code = '$state_code' ") or die(mysqli_error());
		$srow = mysqli_fetch_row($sresult);
		$state = $srow[0];
		if($gstin !='')
		{
			$reg_type = 'Registered';
		}
		else
		{
			$reg_type = 'Unregistered';
		}
	
		$prodslct= mysqli_query($conn,"select prodname,schedule,catcode,tax_rate from product where prodcode ='$prodcode' ") or die(mysqli_error());
		$prrow=mysqli_fetch_row($prodslct);
		$prodname = $prrow[0];
		$hsn_code = $prrow[1];
		$category = $prrow[2];
		$tax_rate = $prrow[3];
		 $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="116" align="left"><font size="-1"><? if($temp!= $inv_no) echo($bname);?> </font></td>		
			*/
			?>
   		<td width="38" align="left"><font size="-1"><?  echo($inv_date);?></font></td>
												
		<td width="33" align="center"><font size="-1"><?  echo($inv_no);?></font></td>
		<td width="64" align="left"><font size="-1"><?  echo($bname);?> </font></td>	
		<td width="46" align="left"><font size="-1"><?  echo($gstin);?> </font></td>	        
                													
		<td width="53" align="left"><font size="-1" color="<?=$color?>"><? echo($prodname);?> </font></td>
		<td width="42" align="left"><font size="-1"><?  echo($hsn_code);?> </font></td>	        
		<td width="53" align="left"><font size="-1" color="<?=$color?>"><? echo($category);?> </font></td>        
		<td width="52" align="right"><font size="-1"><? if($quantity>0)  echo($quantity);?></font></td>        
		<td width="40" align="right"><font size="-1"><? if($rate>0) echo($rate);?></font></td>				
		<td width="49" align="right"><font size="-1"><? if($value>0)  echo(number_format($value,2,'.',''));?></font></td>
		<td width="54" align="right"><font size="-1"><? if($discount_amt>0)  echo(number_format($discount_amt,2,'.',''));?></font></td>

		<td width="49" align="right"><font size="-1"><? if($value-$discount_amt>0)  echo(number_format($value-$discount_amt,2,'.',''));?></font></td>
		<td width="41" align="right"><font size="-1"  color="#CC0000"><?  echo($vat_percent);?></font></td>			

		<td width="39" align="right"><font size="-1"  color="#CC0000"><? if($sgst_amt>0)  echo(number_format($sgst_amt,2));?></font></td>
       
		<td width="42" align="right"><font size="-1"  color="#CC0000"><?  if($sgst_amt>0) echo(number_format($cgst_amt,2,'.',''));?></font></td>		
		<td width="44" align="right"><font size="-1"  color="#CC0000"><? if($cess_amt >0)  echo(number_format($cess_amt,2,'.',''));?></font></td>	      

		<td width="44" align="right"><font size="-1"  color="#CC0000"><? if($igst_amt >0)  echo(number_format($igst_amt,2,'.',''));?></font></td>	      
		<td width="42" align="right"><font size="-1"  color="#CC0000"><? if($sgst_amt+$cgst_amt+$igst_amt >0)  echo(number_format($sgst_amt+$cgst_amt+$igst_amt,2,'.',''));?></font></td>	          	
									
		<td width="61" align="right"><font size="-1"  color="#CC0000"><?   echo(number_format(round($value+$sgst_amt+$cgst_amt+$igst_amt+$cess_amt,0),2,'.','')); ?></font></td>		

		<td width="172" align="left"><font size="-1"  color="#CC0000">
	    <?=$state?>
		  </font></td>
        		<td width="89" align="left"><font size="-1"  color="#CC0000">
       		    <?=$reg_type?>
      		  </font></td>
		
		
		</tr>
		 

		<?  
		$gtot =0;
		$igst_amt=0;
		$cgst_amt=0;
		$sgst_amt=0;
		

}


}										
?>

				</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><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="38" align="left"><font size="-1">&nbsp;</font></td>
<td width="33" align="left"><font size="-1">&nbsp;</font></td>	
<td width="64" align="left"><font size="-1">&nbsp;</font></td>	
													
<td width="46" align="left"><font size="-1">&nbsp;</font></td>												
<td width="53" align="left"><font size="-1">&nbsp; </font></td>
<td width="42" align="left"><font size="-1">&nbsp; </font></td>
<td width="53" align="left"><font size="-1">&nbsp; </font></td>
<td width="52" align="left"><font size="-1">&nbsp; </font></td>
<td width="40" align="left"><font size="-1">&nbsp; </font></td>

<td width="49" align="right"><font size="-1" color="#CC0000"><? if($gvalue>0)  echo(number_format($gvalue,2,'.',''));?></font></td>
<td width="54" align="right"><font size="-1" color="#CC0000"><? if($disc_total>0)  echo(number_format($disc_total,2,'.',''));?></font></td>

<td width="49" align="right"><font size="-1" color="#CC0000"><? if($gvalue-$disc_total>0)  echo(number_format($gvalue-$disc_total,2,'.',''));?></font></td>

				
<td width="41" align="right"><font size="-1">&nbsp;</font></td>		<td width="39" align="right"><font size="-1"  color="#CC0000"><? if($sgst_tot>0)  echo(number_format($sgst_tot,2));?></font></td>
       
		<td width="42" align="right"><font size="-1"  color="#CC0000"><?   echo(number_format($sgst_tot,2,'.',''));?></font></td>		
		<td width="44" align="right"><font size="-1"  color="#CC0000"><? if($cess_tot >0)  echo(number_format($cess_tot,2,'.',''));?></font></td>	  

		<td width="44" align="right"><font size="-1"  color="#CC0000"><? if($igst_tot >0)  echo(number_format($igst_tot,2,'.',''));?></font></td>	  

		<td width="42" align="right"><font size="-1"  color="#CC0000"><? if($sgst_tot+$igst_tot+$igst_tot >0)  echo(number_format($sgst_tot+$igst_tot+$igst_tot,2,'.',''));?></font></td>	               	
								
<td width="61" align="right"><font size="-1" color="#CC0000"><? if($net>0)  echo(number_format($net,2,'.',''));?></font></td>				
			
		
		</tr>

</table>			 



</table>


</body>
</form>

<?



