<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$copy=$_REQUEST['copy'];
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");
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script src="/zfqrcode/qrcode.js"></script> 
<title>PSN VEHICLE INVOICE</title>
<style>
body{
  -webkit-print-color-adjust:exact;
}
.printable{ 
      page-break-before: always;
      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()'>
<?

$result=mysqli_query($db,"select * from gst_income where refno='$invno' and brcode='$brcode'");
$res1=mysqli_fetch_array($result);

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

$qrqry=mysqli_query($db,"select * from tbl_einvoice where invno='$invno' and brcode='$brcode'");
$qrrow=mysqli_fetch_array($qrqry);
$signedqr=$qrrow['SignedQRCode']; 
$irn=$qrrow['irnno']; 
 
$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'];
	$hsn = $res1['hsncode'];

        if($res1['invtype']=='INTERSTATE')
        {
		    $tot=$res1['amt2'];
        }
        else
        {
           $tot=$res1['amt2'];
        }
        $r=mysqli_query($db,"select * from cust_master where acname='$res1[vendor]'")or die(mysqli_error());
        $cus_row=mysqli_fetch_array($r);
?>

	<table border='0' class='printable' width='100%'>
	  <tr><td><?
    if($copy!='none') {echo $copy;echo ' Copy';}?>
        </td></tr>
	   <tr><td colspan=6 align=center style="font-size: 22px;"<b>PSN AUTOMOBILES PVT LTD</td></tr>
 	   <tr><td colspan=6 align=center><b>GST NO : <?echo $brow['gst']?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow['braddr']?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow['brcity']?></td></tr>
	   <tr><td colspan=6 align=center><b><?echo $brow['brphone']?></td></tr>
	   
	<tr><td align='left'  id='add_td'><? if($signedqr!='')
	{ ?>IRN : <?echo $irn;?><?
	}
	?></td><td align='right' ><? if($signedqr!='')
	{ ?>
	
	<div id="qrcode" ></div>
	<script>
    var qrcode = new QRCode("qrcode","<?echo $signedqr?>");
    </script>
	
	 <?
	}
	?></td></tr></table> 
    
	<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>
	<table  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;">Details Of Consignee(Shipped to)</b></td></tr>
	<tr><td align='left'><b>Name    :</b></td><td ><b><? print $cus_row['custname']; ?></b></td><td align='left'><b>Name</b></td><td align='left'><? print $cus_row['custname']; ?></td></tr>
   	<tr><td align='left'><b>Address :</b></td><td align='left'><? echo $cus_row['addr1']; ?></td><td align='left'><b>Address</b></td><td align='left'><? echo $cus_row['addr1']; ?></td></tr>
	<tr><td align='left'><b>Pin No   :</b></td><td align='left' ><? echo $cus_row['pin']; ?></td><td align='left'><b>Pin No</b></td><td align='left'><? echo $cus_row['pin']; ?></td></tr>

	<tr><td align='left'><b>District   :</b></td><td align='left' ><? echo $cus_row['dist']; ?></td><td align='left'><b>District</b></td><td align='left'><? echo $cus_row['dist']; ?></td></tr>
	<tr><td align='left'><b>State   :</b></td><td align='left'><? echo $cus_row['stat']; ?></td><td align='left'><b>State  :</b></td><td align='left'><? echo $cus_row['stat']; ?></td></tr>
	<tr><td align='left'><b>PAN   :</b></td><td align='left'><? echo $cus_row['kgst']; ?></td><td align='left'><b>PAN  :</b></td><td align='left'><? echo $cus_row['kgst']; ?></td></tr>
	<tr><td align='left'><b>GSTIN/Unique ID</b></td><td align='left'><? echo $cus_row['tin']; ?></td><td align='left'><b>GSTIN/Unique ID</b></td><td align='left'><? echo $cus_row['tin']; ?></td></tr>
	</table>
	<table class='tbl' height='40' width='100%'>
<? $invdate=implode("-",array_reverse(explode("-",$res1['date']))); ?>

	<tr style="font-size: 13px;"><td  ><b>Terms of Delivery:</b></td><td ></td><td ><b></b></td><td ></td><td rowspan='3' style="border-left:1px solid black;border-collapse:collapse;border-style:dotted;width:0px"></td><td align=right><b>Invoice No : </b></td><td align=left><b><? echo $invno; ?></td><td align=right><b>Date:</b></td><td align=left><b><? echo $invdate; ?></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<th>Net Value</tr>
<?
$i=1;
	
	$result1=mysqli_query($db,"select * from gst_income where refno='$invno' and brcode='$brcode'");
	while($row=mysqli_fetch_array($result1))
	{
	    $kfc_tot = $row[21];
	    $tcs_tot = $row[22];
	    $narration=nl2br($row['narr']);
        print "<tr><td width='5%'>".$i."</td><td width='60%'>".$row['expense_head']."<br>   ".$narration."</br></td><td align='right' width='20%px'>".$row['hsncode']."</td>
	<td align='right' width='15%'>".putcoma($row['aprice'])."</td></tr>";
	
	$cgst_tot+=$row['cgstamt'];
	$sgst_tot+=$row['sgstamt'];
	$igst_tot+=$row['igstamt'];
	$tot+=$row['expense_amount'];
	$amt_aprice+=$row['aprice'];
	$amt+=$row['before_tax'];
	$discamt=$row['discamt'];
	$i++;
	}
	 for($k=$i;$k<=8;$k++)
	{
	    print "<tr><td></td><td></td><td align='right' width='0'></td><td align='right' width='0'></td></tr>";
	}
    $kfc_tot1 = $amt+$cgst_tot+$sgst_tot+$igst_tot+$kfc_tot;
?>
    
	</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>Accounts</td></tr>
    	     	<tr><td  align='right'> Total Amount Before Discount</td><td width='92'><?if($p==$pg_no){ echo putcoma($amt_aprice);} ?></td></tr>
    	     	<tr><td  align='right' height='10'>Discount</td><td ><? if($p==$pg_no){ echo round($discamt,2); }?></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'>KFC Total</td><td ><? if($p==$pg_no){ echo $kfc_tot; }?></td></tr>
        <tr><td  align='right' height='10'>Total</td><td ><? if($p==$pg_no){echo putcoma($kfc_tot1); }?></td></tr>          
        <tr><td  align='right' height='10'>TCS Total</td><td ><? if($p==$pg_no){ echo $tcs_tot; }?></td></tr>          
	   <?
	    $tot = $tot + $tcs_tot;
	    $rnd=round($tot);
        $dif=round(($rnd-$tot),2);
        if($dif==.5)
        {
            $tot = $tot-1;
            $dif = -.5;
        }
	   ?>

	   	<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>

			Authorised Signatory.</td><td style='border:0px solid white;text-align:left;text-transform:none;'></br>

			Customer Sign/Seal</td></tr></table>


	</td>

	</tr></table>


