<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}
$invno=$_REQUEST['invno'];
include("test.php");
include("opendb.php");
//include("includes/function.php");
//include("calstock.php");
?>
<script src="/zfqrcode/qrcode.js"></script> 
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title></title>
<style>
body{
  -webkit-print-color-adjust:exact;
}
.printable{ 
            margin-top:30px;
    }
.tbl{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;
text-transform:uppercase;
}
.tbl td{
border: 0px solid black;
}
#tbl{
border: 1px solid black;
}
.tble{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;
border-collapse:collapse;
text-transform:uppercase;

}
.tble  td{
height:8px;
margin-left:10px;
margin-right:10px;
padding-left:10px;
padding-right:10px;
}
.tble th{
border:1px solid black;
}
.tbl1{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;
border-collapse:collapse;
text-align:right;
text-transform:uppercase;
}
.tbl1 td{
//border-bottom:1px solid black;
height:10px;
}
#add_td{
font-size:12px;
height:15px;
}
#add_td br{
margin:0px;
padding:0px;
}
.tot_td{
border-bottom:0px solid black;
}
.tot_td1{
border-top:0px solid black;
}
#tot_tbl td{
border:1px solid black;
border-collapse:collapse;
}
body
 {font-family:Arial;
  }
p{
 margin:0px;
 padding:0px;
 }
@media print{
            #head
            {
                background-color:white;
            }
        }
</style>
<body onload='window.print()'>
<?
//print "select * from gst_incomecust where refno='$invno' and brcode='$brcode'";
$result=mysqli_query($db,"select * from gst_incomecustrev where refno='$invno' and brcode='$brcode'");
$res1=mysqli_fetch_array($result);

$qrqry=mysqli_query($db,"select * from tbl_einvoice where invno='$invno' and brcode='$brcode'");
$qrrow=mysqli_fetch_array($qrqry);
$signedqr=$qrrow['SignedQRCode'];

$irnno=$qrrow['irnno'];
//$re=mysqli_query($db,"select * from branch where brcode='$brcode'");
//$brow=mysqli_fetch_array($re);

/*$brcol=substr($res1['cmpcode'],0,3).'_ret_veh';
$tr = "select $brcol from branch where brcode = '$brcode'";
$trd=mysqli_query($db,$tr);
$trdrow=mysqli_fetch_row($trd);
 */
$minno =  $trdrow[0].substr(("00000".$invno),-5);
        
        $amt=$res1['netamt'];
        $qty =1;
        $disc =$res1['disc'];
        $cgstp=$res1['cgstp'];
        $sgstp=$res1['sgstp'];
        $igstp=$res1['igstp'];
        $cgstamt=$res1['cgstamt'];
        $sgstamt=$res1['sgstamt'];
        $igstamt=$res1['igstamt'];
        $tcsper = $res1['tcsper'];
        $tcsamt = $res1['tcsamt'];

        if($res1['invtype']=='INTERSTATE')
        {
		    $tot=$res1['amt2'];
        }
        else
        {
           $tot=$res1['amt2'];
        }
        //print "select * from sup_master where web='$res1[vendor]'";
        $r=mysqli_query($db,"select * from sup_master where web='$res1[vendor]'")or die(mysqli_error($db));
        $cus_row=mysqli_fetch_array($r);
$res33=mysqli_query($db,"select * from branch where brcode = '$brcode'") or die(mysqli_error($db));
$brow=mysqli_fetch_row($res33);
?>
	<table border='0' class='printable' width='100%'>
	  
	   <tr><td colspan=6 align=center style="font-size: 22px;"<b>PSN AUTOMOTIVE MARKETING PVT LTD</td></tr>
 	   <tr><td colspan=6 align=center><b>GST NO : <?echo $brow[9]?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow[3]?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow[4]?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow[5]?></td></tr>
	   
	<tr><td align='left'  id='add_td'></td><td align='right' ></td></tr></table> 
    <br>
	<table border='1' style='border-collapse:collapse;' width='100%'>
	    <tr><th  align='center' style='border:0px solid white;height:20px;font-size:18px;' id='head'>TAX INVOICE</th></tr>
	    
	    </table>
<? $invdate=implode("-",array_reverse(explode("-",$res1['date']))); ?>

	<table border='1' class='tbl' width='100%'>
	<tr><td colspan=2 align='left'><b style="font-size: 14px;">Details Of Receiver(Billed to)</b></td><td align='left' colspan=2><b style="font-size: 14px;"><b>Invoice No :</b><b><? echo $invno; ?></b></td></tr>
	<tr><td align='left'><b>Name    :</b></td><td ><b><? print $cus_row['supname']; ?></b></td><td align='left'><b style="font-size: 14px;">Date    :</b><b style="font-size   : 14px;"><? echo $invdate; ?></td></tr>
   	<tr><td align='left'><b>Address :</b></td><td align='left'><? echo $cus_row['addr1']; ?></td><td align='left'></td><td align='left'></td></tr>
	<tr><td align='left'><b>Pin No   :</b></td><td align='left' ><? echo $cus_row['pin']; ?></td><td align='left'></td><td align='left'></td></tr>

	<tr><td align='left'><b>District   :</b></td><td align='left' ><? echo $cus_row['dist']; ?></td><td align='left'></td><td align='left'></td></tr>
	<tr><td align='left'><b>State   :</b></td><td align='left'><? echo $cus_row['stat']; ?></td><td align='left'></td><td align='left'></td></tr>
	<tr><td align='left'><b>PAN   :</b></td><td align='left'><? echo $cus_row['kgst']; ?></td><td align='left'></td><td align='left'></td></tr>
	<tr><td align='left'><b>GSTIN   :</b></td><td align='left'><? echo $cus_row['cst']; ?></td><td align='left'></td><td align='left'></td></tr>
	
	</table>
	<br>
	<table border=1><tr><td>IRN No:<?print $qrrow['irnno']; ?> </td></tr></table>
	<table class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS width='100%'>
	<tr><th>SR.No<th>Account<th>HSN/SAC CODE<th>Net Value<th>Taxable Amount<th>GST %<th>GST AMT<th>Cessp<th>Cessamt<th>Grand Total</tr>
<?
$i=1;
	$tcs_tot=0;
	$tcs_per=0;
	$result1=mysqli_query($db,"select * from gst_incomecustrev where refno='$invno' and brcode='$brcode'");
	while($row=mysqli_fetch_array($result1))
	{
	$result22=mysqli_query($db,"select hsncode from partmaster where partname='$row[expense_head]' ")or die(mysqli_error($db));

$row22=mysqli_fetch_row($result22);
$gstp=$row['cgstp']+$row['sgstp']+$row['igstp'];
$gsta=$row['cgstamt']+$row['sgstamt']+$row['igstamt'];

        print "<tr><td width='5%'>".$i."</td><td width='40%'>".$row['narr']."</td><td align='center' width='10%px'>".$row22[0]."</td>
	<td align='right' width='5%'>".putcoma($row['before_tax'])."</td><td align='right' width='5%'>".putcoma($row['before_tax'])."</td>
	<td align='right' width='5%'>".$gstp."</td><td align='right' width='5%'>".$gsta."</td><td align='right' width='5%'>".putcoma($row['fcessp'])."</td><td align='right' width='5%'>".putcoma($row['fcessamt'])."</td><td align='right' width='10%'>".putcoma($row['expense_amount'])."</td></tr>";
    $tcs_tot+=$row['tcsamt'];	
    $tcs_per=round($row['tcsp'],2);	
	$cgst_tot+=$row['cgstamt'];
	$sgst_tot+=$row['sgstamt'];
	$igst_tot+=$row['igstamt'];
	$tot+=$row['expense_amount'];
	$amt+=$row['before_tax'];
	$fcessa+=$row['fcessamt'];
	$i++;
	}
	 for($k=$i;$k<=25;$k++)
	{
	    print "<tr><td></td><td></td><td align='right' width='0'></td><td align='right' width='0'></td><td></td><td align='right' ></td>
        
        <td align='right' width='0'></td><td align='right' width='0'></td></tr>";
	}
 
?>
    
	</table>

	<table  width=100% class='tbl1' id='tbl' border='1' style='height:50px;'>
	<tr><td align='left'  width='60%' ><b>Rupees  :  <?echo word(round($tot))?>  Only</b></td><td></td><td rowspan='2'>

           <table width='100%'  style='border:0px solid black;border-collapse:collapse;margin:0px' border=0 id='tot_tbl'>
    	     	<tr><td colspan=2 class='tot_td1' align=right></td></tr>
 	   	<tr><td  align='right'> Total Amount Before GST</td><td width='92'><?if($p==$pg_no){ echo putcoma($amt);} ?></td></tr>
		<tr><td  align='right' height='10'>cgst Total</td><td ><? if($p==$pg_no){ echo $cgst_tot; }?></td></tr>
		<tr><td  align='right' height='10'>sgst Total</td><td ><? if($p==$pg_no){ echo $sgst_tot; }?></td></tr>
		<tr><td  align='right' height='10'>igst Total</td><td ><? if($p==$pg_no){ echo $igst_tot; }?></td></tr> 
			<tr><td  align='right' height='10'>Cess Total</td><td ><? if($p==$pg_no){ echo $fcessa; }?></td></tr>
			<tr><td  align='right' height='10'>TCS (<?echo $tcs_per;?>%)Total</td><td ><? if($p==$pg_no){ echo $tcs_tot; }?></td></tr> 

	   <?
	   
	    $rnd=round($tot);
        $dif=round(($rnd-$tot),2);
        
	   ?>

	   	<tr><td align='right'>Round Off</td><td><? if($p==$pg_no){ echo putcoma($dif);} ?> </td></tr>
	   	
              
              <tr><td align='right' class='tot_td' style='font-size:12px'><b>Net Amount</td><td class='tot_td' style='font-size:12px'><b><? if($p==$pg_no){ echo putcoma(round($tot)); } ?></b></td></tr>
         

 </table>


	</td></tr>

	

	

	<tr><td colspan=2>
	<table border=0 style='border:0px solid white; ' width=100%><tr>
	<td style='border:0px solid white;text-align:left;text-transform:none;'>
	</td><td style='border:0px solid white; text-align:right;'> </td></tr>
	<tr><td style='border:0px solid white;text-align:left;text-transform:none;'></br>
			For PSN AUTOMOTIVE MARKETING PVT. LTD.<BR><BR><BR></br></br></br>
			Authorised Signatory.</td><td style='border:0px solid white; text-align:right;'>
	</td><td><table width=50%><tr><td align='right'  width='40%'> <? if(strlen($signedqr)>0)
	{ ?>
	<!-- <img src="https://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=<?print $signedqr;?>&choe=UTF-8" alt="generation qr-code" style="border: solid 0px black;width:200px;height:200px;"> -->
	<div id="qrcode" ></div>
    <script>
    var qrcode = new QRCode("qrcode","<?echo $signedqr?>");
    </script>
	
	<?
	}
	?>
	</td></tr></table></td></tr></table>


	</td>

	</tr></table>


