<?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>
 <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>GST Purchase Register from &nbsp;<? echo($stdt);?> to  &nbsp;<? echo($edt);?>&nbsp;(<?=$reporttype?>)</td></tr></table>

				
<table width="995" border="0">
  <tr> 
<td  colspan="100%"><hr width="100%"></td>
  </tr>
  <tr> 

    <td width="67" align="right"><font size="-1">Inv. No</font></td>
    <td width="79" height="20" align="left"><font size="-1">Inv.Date</font></td>
    <td width="79" height="20" align="left"><font size="-1">Entry.Date</font></td>    
	<td width="90" align="right"><font size="-1">GSTIN</font></td>			
    <td width="307" align="left"><font size="-1">Customer  Name </font></td>
    <td width="86" align="right"><font size="-1">Accessable Value</font></td>
    <td width="307" align="left"><div align="right"><font size="-1"> SGST </font></div></td>	    
    <td width="87" align="right"><font size="-1">CGST</font></td>
     <td width="87" align="right"><font size="-1">IGST</font></td>
     <td width="87" align="right"><font size="-1">TCS</font></td>	 
    <td width="98" align="right"><font size="-1">Total Amount</font></td>

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

	$gvalue=0;
	$sgst_tot =0;
	$cgst_tot =0;
	$igst_tot =0;
	$gtot=0;
	$net = 0;
	$gtcs = 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;



if($reporttype == 'Listed by Invoice Date')
{
$invresult= mysqli_query($conn,"select distinct poinv_no,bcode,poinv_date,trans_id,account_year,exp_date from purchasereg  where poinv_date >='$startdt' and poinv_date <='$enddt'  and compcode='$compcode'   order by poinv_date,poinv_no,record ")or die(mysqli_error());
}
else
{
$invresult= mysqli_query($conn,"select distinct poinv_no,bcode,poinv_date,trans_id,account_year,exp_date from purchasereg  where exp_date >='$startdt' and exp_date <='$enddt'  and compcode='$compcode'   order by poinv_date,poinv_no,record ")or die(mysqli_error());
	
}
while($invrow = mysqli_fetch_row($invresult))
{

		$bcode = $invrow[1];

		$poinv_date = substr($invrow[2],8,2).'-'.substr($invrow[2],5,2).'-'.substr($invrow[2],0,4);
		$poinv_no= $invrow[0];
		$trans_id = $invrow[3];
		$exp_date = british($invrow[5]);
		
if($reporttype == 'Listed by Invoice Date')
{		
		$presult = mysqli_query($conn,"select sum(value),sum(vat_amount),sum(tcs) from purchasereg where poinv_date >='$startdt' and poinv_date <='$enddt'  and compcode='$compcode' and poinv_no = '$invrow[0]' and trans_id = '$trans_id'  order by record ")or die(mysqli_error());
}
else
{
			$presult = mysqli_query($conn,"select sum(value),sum(vat_amount),sum(tcs) from purchasereg where exp_date >='$startdt' and exp_date <='$enddt'  and compcode='$compcode' and poinv_no = '$invrow[0]' and trans_id = '$trans_id'  order by record ")or die(mysqli_error());

}
		$prow = mysqli_fetch_row($presult);
		
		
		$value=$prow[0];
		$taxamt=$prow[1];
		$tcs = $prow[2];
		
		$gtot = $value + $taxamt+$tcs;
		$gtcs = $gtcs+$tcs;

		$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 = $taxamt/2;
			$cgst_amt = $sgst_amt;
			$sgst_tot = $sgst_tot +$sgst_amt;
		}
		else
		{
			$igst_amt = $taxamt;
			$igst_tot =$igst_tot+$igst_amt;
		}
		if($taxtype == 'All')
		{
		
				$gvalue=$gvalue+$value;
				$net = $net +$gtot;
		?>
  <tr> 

    <td width="67" align="center"><font size="-1"> 
      <?  echo($poinv_no);?>
      </font></td>
    <td width="79" align="left"><font size="-1"> 
      <?  echo($poinv_date);?>
      </font></td>	  
    <td width="79" align="left"><font size="-1"> 
      <?  echo($exp_date);?>
      </font></td>	
            
    <td width="90" align="right"><font size="-1"  > 
      <?=$gstin?>
      </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="87" align="right"><font size="-1"  >
      <? if($sgst_amt>0)  echo($sgst_amt);?>
      </font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($cgst_amt>0)  echo($cgst_amt);?>
      </font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($igst_amt>0)  echo($igst_amt);?>
      </font></td>      
    <td width="87" align="right"><font size="-1"  >
      <? if($tcs>0)  echo($tcs);?>
      </font></td>   
	  
    <td width="98" align="right"><font size="-1"  > 
      <? if( $gtot>0)  echo(number_format($gtot,2,'.','')); ?>
      </font></td>      

  </tr>
  <?  
		}
		if($taxtype == 'Within State')
		{
			$igst_amt=0;
			$igst_tot=0;
			if($sgst_amt >0)
			{
				
				$gvalue=$gvalue+$value;
				$net = $net +$gtot;				
				?>
 <tr> 

    <td width="67" align="center"><font size="-1"> 
      <?  echo($poinv_no);?>
      </font></td>
    <td width="79" align="left"><font size="-1"> 
      <?  echo($poinv_date);?>
      </font></td>	  
    <td width="79" align="left"><font size="-1"> 
      <?  echo($exp_date);?>
      </font></td>	
            
    <td width="90" align="right"><font size="-1"  > 
      <?=$gstin?>
      </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="87" align="right"><font size="-1"  >
      <? if($sgst_amt>0)  echo($sgst_amt);?>
      </font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($cgst_amt>0)  echo($cgst_amt);?>
      </font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($igst_amt>0)  echo($igst_amt);?>
      </font></td>     
    <td width="87" align="right"><font size="-1"  >
      <? if($tcs>0)  echo($tcs);?>
      </font></td>    
	  

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

  </tr>		
  <?		
			}
			
		}
		if($taxtype == 'Inter State')
		{
			$sgst_amt=0;
			$sgst_tot=0;
			$cgst_amt=0;
			$cgst_tot=0;
			if($igst_amt >0)
			{
				$gvalue=$gvalue+$value;
				$net = $net +$gtot;				
				?>
 <tr> 

    <td width="67" align="center"><font size="-1"> 
      <?  echo($poinv_no);?>
      </font></td>
    <td width="79" align="left"><font size="-1"> 
      <?  echo($poinv_date);?>
      </font></td>	  
    <td width="79" align="left"><font size="-1"> 
      <?  echo($exp_date);?>
      </font></td>	
            
    <td width="90" align="right"><font size="-1"  > 
      <?=$gstin?>
      </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="87" align="right"><font size="-1"  >
      <? if($sgst_amt>0)  echo($sgst_amt);?>
      </font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($cgst_amt>0)  echo($cgst_amt);?>
      </font></td>
    <td width="87" align="right"><font size="-1"  >
      <? if($igst_amt>0)  echo($igst_amt);?>
      </font></td>      
    <td width="87" align="right"><font size="-1"  >
      <? if($tcs>0)  echo($tcs);?>
      </font></td>  
    <td width="98" align="right"><font size="-1"  > 
      <? if( $gtot>0)  echo(number_format($gtot,2,'.','')); ?>
      </font></td>      

  </tr>	
  <?				
			}
						
		}		
		
	$sgst_amt=0;
	$igst_amt=0;
	$gtot=0;
	$tcs=0;
	$cgst_amt=0;


}										
?>

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

<tr>

<td>&nbsp;</td>
<td colspan="4" align="right"><font size="-1" ></font></td>
<td   align="right"><font size="-1" >&nbsp;<? if($gvalue>0)  echo(number_format($gvalue,2,'.',''));?></font></td>	
<td  align="right"><font size="-1" ><? if($sgst_tot>0)  echo(number_format($sgst_tot,2,'.',''));?></font></td>
<td  align="right"><font size="-1" ><? if($sgst_tot>0)  echo(number_format($sgst_tot,2,'.',''));?></font></td>
<td  align="right"><font size="-1" ><? if($igst_tot>0)  echo(number_format($igst_tot,2,'.',''));?></font></td>			
<td  align="right"><font size="-1" ><? if($gtcs>0)  echo(number_format($gtcs,2,'.',''));?></font></td>	
<td width="90" align="right"><font size="-1" ><? if($net>0)  echo(number_format($net,2,'.',''));?></font></td>				

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


</table>


</body>
</form>

<?

