<?
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>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:5px;
padding-right:0px;
}
.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()'>
<?

$qrqry=mysqli_query($db,"select * from tbl_einvoice where invno='$invno' and brcode='$brcode'");


$result=mysqli_query($db,"select * from veh_invoice where invno='$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);
 
$minno =  $invno;
        
        $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'];
	$mcessp = $res1['cessp'];
	$mcessamt = $res1['cessamt'];
        if($res1['invtype']=='INTERSTATE')
        {
		    $tot=$res1['amt2'];
        }
        else
        {
           $tot=$res1['amt2'];
        }
        $r=mysqli_query($db,"select * from cust_master where code='$res1[cust]'")or die(mysqli_error());
        $cus_row=mysqli_fetch_array($r);
$qrrow=mysqli_fetch_array($qrqry);
$signedqr=$qrrow['SignedQRCode'];
$irn =$qrrow['irnno'];
$chkflg=0;
if(($row['tin']!='NOT REGISTERED')&&($row['entrydate']>='2021-01-01'))
{
    $chkflg=1;
}
if($signedqr>'')
{
    $chkflg=0;
}
if($chkflg=='1')
{
    print "QR code for E-invoice not generated. Please generate the same before printing invoice.";
}
else
{
?>


	<table border='0' class='printable' width='100%'>
	  
	   <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>
<?
 $invdate=implode("-",array_reverse(explode("-",$res1['invdate']))); 
if($invdate<"03-09-2018")
$addr="35/189, P Box No 2270 Palarivattom";
else
$addr=$brow['braddr'];

?>
	   <tr><td colspan=6 align=center><b><?echo $addr?></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 colspan=6 align=center><b>CIN NO : U50300KL 1996PTC010842</td></tr>
	<tr><td align='left'  id='add_td'></td><td align='right' ></td></tr>
	<tr><td><? if($signedqr!='')
	{ ?><td width='40%'>B2B Invoice<br>IRN: <?echo $irn?> </td> <?
	}
	?></td><td align='right'><? if($signedqr!='')
	{ ?>
	<div id="qrcode" ></div>
	<script>
    var qrcode = new QRCode("qrcode","<?echo $signedqr?>");
    </script>
	<!-- <td align='right'  width='40%'> <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;"></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'>FORM GST</th></tr>
	    <tr><th  align='center' style='border:0px solid white;height:18px;font-size:12px;' id='head'>VEHICLE INVOICE</th></tr>
	    </table>
	<table  class='tbl' width='100%'>
<?if($res1['ship_to']>'')
{
    $mcust_name = $cus_row['custname'];
    if($mcust_name=='')
    {
        $tr = "select supname,addr1,city,state,pin from branch where code  = '$res1[cust]' brcode = '$brcode'";
        print $tr;
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_row($trd);
        $mcust_name=$trdrow[0];
    }
    if($invno==3751012400444 ||$invno== 3751012400443 ||$invno== 3751012400449 ||$invno== 3751012400448 ||$invno== 3751012400446 ||$invno== 3751012400447 ||$invno== 3751012400445 ||$invno== 3751012400442 ||$invno== 3751012400440 ||$invno==3751012400441)
    {?>
    <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 $mcust_name; ?></b></td><td align='left'><b>Name</b></td><td align='left'><? print $res1['ship_to']; ?></td></tr>
   	<tr><td align='left'><b>Address :</b></td><td align='left'>EXPO LOGISTICS, 13/76, IAC ROAD, KALAMASSERY, PATHALAM, KADUNGALLOOR</td><td align='left'><b>Address</b></td><td align='left'><? echo $res1['ship_addr']; ?></td></tr>
	<tr><td align='left'><b>Pin No   :</b></td><td align='left' >683501</td><td align='left'><b>Pin No</b></td><td align='left'><? echo $res1['ship_pin']; ?></td></tr>
	<tr><td align='left'><b>District   :</b></td><td align='left' >ERNAKULAM</td><td align='left'><b>District</b></td><td align='left'><? echo $res1['ship_dist']; ?></td></tr>
	<tr><td align='left'><b>State   :</b></td><td align='left'>KERALA</td><td align='left'><b>State  :</b></td><td align='left'><? echo $res1['ship_state']; ?></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 $res1['ship_pan']; ?></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 $res1['ship_gst']; ?></td></tr>    
	<?
    }
    else
    {
    ?>
	<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 $mcust_name; ?></b></td><td align='left'><b>Name</b></td><td align='left'><? print $res1['ship_to']; ?></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 $res1['ship_addr']; ?></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 $res1['ship_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 $res1['ship_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 $res1['ship_state']; ?></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 $res1['ship_pan']; ?></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 $res1['ship_gst']; ?></td></tr>
	<?}?>
	</table>
<?}
else
{
     $mcust_name = $cus_row['custname'];
     $mcust_addr1 = $cus_row['addr1'];
     $mcust_pin = $cus_row['pin'];
     $mcust_dist = $cus_row['dist'];
     $mcust_stat = $cus_row['stat'];
     $mcust_tin = $cus_row['tin'];
     $mcust_pan = $cus_row['kgst'];
     
    if($mcust_name=='')
    {
        $tr = "select supname,addr1,city,stat,pin,dist,tin,web from sup_master where code = '$res1[cust]' and branch = '$brcode'";
        print $tr;
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_row($trd);
        $mcust_name=$trdrow[0];
        $mcust_addr1 = $trdrow[1];
        $mcust_pin = $trdrow[4];
        $mcust_dist = $trdrow[5];
        $mcust_stat = $trdrow[3];
        $mcust_tin = $trdrow[6];
        $mcust_pan =$trdrow[7];

    }
?>
	<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 $mcust_name; ?></b></td><td align='left'><b>Name</b></td><td align='left'><? print $mcust_name; ?></td></tr>
   	<tr><td align='left'><b>Address :</b></td><td align='left'><? echo $mcust_addr1; ?></td><td align='left'><b>Address</b></td><td align='left'><? echo $mcust_addr1; ?></td></tr>
	<tr><td align='left'><b>Pin No   :</b></td><td align='left' ><? echo $mcust_pin; ?></td><td align='left'><b>Pin No</b></td><td align='left'><? echo $mcust_pin; ?></td></tr>

	<tr><td align='left'><b>District   :</b></td><td align='left' ><? echo $mcust_dist; ?></td><td align='left'><b>District</b></td><td align='left'><? echo $mcust_dist; ?></td></tr>
	<tr><td align='left'><b>State   :</b></td><td align='left'><? echo $mcust_stat; ?></td><td align='left'><b>State  :</b></td><td align='left'><? echo $mcust_stat; ?></td></tr>
	<tr><td align='left'><b>PAN   :</b></td><td align='left'><? echo $mcust_pan; ?></td><td align='left'><b>PAN  :</b></td><td align='left'><? echo $mcust_pan; ?></td></tr>
	<tr><td align='left'><b>GSTIN/Unique ID</b></td><td align='left'><? echo $mcust_tin; ?></td><td align='left'><b>GSTIN/Unique ID</b></td><td align='left'><? echo $mcust_tin; ?></td></tr>
	</table>
<?}?>
	<table class='tbl' height='40' width='100%'>


	<tr style="font-size: 15px;"><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 ><b>Invoice No : </b></td><td><b><? echo $minno; ?></td><td ><b>Date:</b></td><td><b><? echo $invdate; ?></td></tr>
	<tr><td colspan='4' style="border-bottom:0px solid black;border-collapse:collapse;"></td><td ><b></b></td><td></td><td ><b></b></td><td></td></tr>
	</table>
	<table class='tbl' style=" font-size: 12px;" width='100%'>
	<tr><td ><b>Hypothicated to:<? echo $res1['fin']; ?> </b></td><td>EMLDA NO</td><td><? echo $res1['da']; ?></td><td ><b>Engine No:</b></td><td ><? echo $res1['engno']; ?></td></tr>
	<tr><td></td><td ><b>Chassis No:</b></td><td ><? echo  $res1['chasis']; ?></td><td ><b>Color</b></td><td ><? echo  $res1['color']; ?></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 colspan='3'>Description Of Goods<th>HSN<th>Unit<th>Gross Value<th>Cash Disc<th>Net Value<th>Taxable Amount<th>CGST %<th>CGST AMT<th>SGST %<th>SGST AMT<th>
	<?
    if($igstp>0)
    {?>
	IGST %<th>IGST AMT<th>
	<?}?>
Grand Total</tr>
<?
$i=1;
if($res1['taxtype']=='INTRASTATE')
	$amt12 = round($tot+$sgstamt+$cgstamt,2);
else
	$amt12 = round($tot+$igstamt,2);


    print "<tr><td width='5%'>".$i."</td><td width='20%' colspan='3'>".$res1['config']."</td><td align='right' width='5%px'>".$res1['hsncode']."</td><td align='right' width='5%'>1</td><td width='10%'>".putcoma($tot)."</td>
	 <td align='right' width='5%'>".$disc."</td><td align='right' width='5%'>".putcoma($tot)."</td><td align='right' width='5%'>".putcoma($tot)."</td><td align='right' width='5%'>".$cgstp."</td>
	<td align='right' width='5%'>".$cgstamt."</td><td align='right' width='5%'>".$sgstp."</td><td align='right' width='5%'>".$sgstamt."</td>";
	//if($mcessp>0)
	//print "<td align='right' width='5%'>".$mcessp."</td><td align='right' width='5%'>".$mcessamt."</td>";
	if($igstp>0)
		print "<td align='right' width='5%'>".$igstp."</td><td align='right' width='5%'>".$igstamt."</td>";
	print "<td align='right' width='10%'>".putcoma($amt12)."</td></tr>";
	for($k=$i;$k<=17;$k++)
	{
	    print "<tr><td align='right' width='0'></td><td  colspan='3'></td><td align='right' ></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td><td align='right' width='0'></td></tr>";
	}
 
?>
    
	</table>

	<table class='tbl1' id='tbl' border='1' style='height:50px;' width='100%'>
	 <?if($mcessamt>0){?>
	    <tr><td align='left'  ><b></b></td><td align='right' ><b>Cess @ 15% : &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp </b><? echo putcoma($mcessamt);
	    ?></td></tr>
	    
	<?}
		
	if($tcsper>0){?>
		<tr><td align='left'  ><b></b></td><td align='right' ><b>Tax Collected at Source (TCS @ <?echo $tcsper?>%): &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp </b><? echo putcoma($tcsamt); ?></td></tr>
		<?
	
	}
	else
	{
		$amt = $amt12;
	}
	if($res1['da']=='462295')
	    $amt = $amt + $mcessamt;
	?>
	<tr><td align='left'  ><b>Attended by Engineer:</b></td><td align='right' ><b>Grand Total  : &nbsp&nbsp&nbsp&nbsp <? echo putcoma(round($amt)); $amt=round($amt);?></b></td></tr>
	<tr><td colspan=2>
	<table class='tbl'  style='height:130px;'  width='100%'>
	<tr><td style='text-align:left;text-transform:none;'>Rupees  :  <?echo word($amt)?>  Only</td><td></td></tr>
	<tr><td style='text-align:left;text-transform:none;'  > We Confirm having received above mentioned goods in good condition.</br>CUSTOMER SIGNATURE.</td>
	<td style='text-align:right;'>For PSN AUTOMOBILES PVT LTD</td></tr>
	<tr></tr>
	<tr><td align=right></td><td align=right>AUTHORISED SIGNATORY</td></tr>
	</table>
	</td></tr></table>
  
<?}?>

