<? 
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}

include("opendb.php");
include ("test.php");
//include("calstock.php");
$invno=$_REQUEST['invno'];
$mnfr=$_REQUEST['mnfr'];
$re=mysqli_query($db,"select * from spareinvoice_lab where invno='$invno' and brcode='$brcode'");

if(mysqli_num_rows($re)<=0)
 {
    print "Invoice No does not exist";
    return;
 }
$jcrow=mysqli_fetch_array($re);
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
/*if($foc=='foc')
$re=mysqli_query($db,"select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'")or die('err:'.mysqli_error($db));
else*/
//print "select * from spareinvoice_lab where invno='$invno'  and brcode='$brcode'";
$re=mysqli_query($db,"select * from spareinvoice_lab where invno='$invno'  and brcode='$brcode'")or die(mysqli_error($db));
$row=mysqli_fetch_array($re)or die("Fetch Error:".mysqli_error($db));

$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);

$cusres=mysqli_query($db,"select * from customer where acname='$row[customer]' ");
$cusrow=mysqli_fetch_array($cusres);
$qrqry=mysqli_query($db,"select * from tbl_einvoice where invno='$row[invno]' and brcode='$brcode'");  
 $qrrow=mysqli_fetch_array($qrqry);
$signedqr=$qrrow['SignedQRCode'];
$irn=$qrrow['irnno'];
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Spare 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:15px;
margin-left:5px;
margin-right:5px;
padding-left:5px;
padding-right:2px;
}

.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;
            }
                  #ad{ display:none;}
                  #leftbar{ display:none;}
        }

</style>

<body onload='window.print()'>
<?
  /* if($foc=='foc')
 $sql="select * from spareinvoicefoc where invno='$invno' and invtype='$invtype' and brcode='$brcode'";
else*/
 $sql="select * from spareinvoice_lab where invno='$invno' and brcode='$brcode'";
$re1=mysqli_query($db,$sql)or die('err:'.mysqli_error($db));

 


    $q_count1=mysqli_query($db,"select count(*) from spareinvoice_lab where invno='$invno' and brcode='$brcode'");
    $cnt1=mysqli_fetch_array($q_count1);

   

    $count=$cnt1[0];
    
    if(strlen($count)<=0)
    {
        $count=0;
    }

    $limit=23;
 
    $pg_no=floor($count/$limit);
 
 
    $i=1;
	$jtot=0;
	$pdisctot=0;
	$ltot=0;
	$subtot=0;
	$ldis=0;
 
	$res=mysqli_query($db,"select * from spareinvoice_lab where invno='$invno' and brcode='$brcode'");

	 while($res1=mysqli_fetch_array($res))
	 { 
	    
        $slno[$i]=$i;
        $code[$i]=$res1['partcode'];
        $item[$i]=$res1['partname'];
        $qty[$i] =$res1['qty'];
        $rate[$i]=$res1['price'];
		$partstotal+=$res1['price'];
	//	$partstotal+=$res1['qty']*$res1['price'];
		$discamt[$i]=($res1['qty']*$res1['price'])*$res1['disc']/100;
		$total_disc+=$discamt[$i];
		$pdisctot+=$res1['disc'];
		$dis[$i]=$res1['disc'];
		$qty_tot+=$res1['qty'];
		$rate_tot+=$res1['price'];
		$fcess_tot+=$res1['fcessamt'];
		$fcessa[$i]=$res1['fcessamt'];
		$qry="select hsncode from partmaster where partcode='$res1[partcode]'";
		//echo $qry;
		$rsqr=mysqli_query($db,$qry);
		$rsrw=mysqli_fetch_array($rsqr);
		//echo strlen($rsrw[0]);
		//echo "<br>";
		if(strlen($rsrw[0])=='')
		{
		 $hsncd[$i]=0;   
		}
		else
		{
		$hsncd[$i]=$rsrw[0];
		}
		if($row['vatorcst']=='INTRASTATE')
		{
		    $cgstprc[$i]=$res1['cgstp'];
		    $sgstprc[$i]=$res1['sgstp'];
		    $cgstamt[$i]=$res1['cgstamt'];
		    $sgstamt[$i]=$res1['sgstamt'];
		    $sgst_tot+=$res1['sgstamt'];
		    $cgst_tot+=$res1['cgstamt'];
		    $igst_tot=0;
		}
		if($row['vatorcst']=='INTERSTATE')
		{
		    $igstprc[$i]=$res1['igstp'];
		    $igstamt[$i]=$res1['igstamt'];
		     $sgst_tot=0;
		    $cgst_tot=0;
		    $igst_tot+=$res1['igstamt'];
		}
		$ptot[$i]=$res1['tot'];
		$jtot+=$res1['tot'];
		$i++;
             
	}
//echo $jtot;
 $ptype=$res1['paytype'];
	

$gtot=$jtot+$ltot;


print $invdate1;



$ntot1=$gtot-$pdisctot;
$ntot=$ntot1-$row['price']-$ldis;


 $tot_fill=0;

 for($p=0;$p<=$pg_no;$p++)
 {
   if($p==$pg_no)
      {
       $tot_fill=1;
      }
               /* to fill total amt in last table if it is last print page */
   $lower_lmt=($p*$limit)+1;
    $upper_lmt=($p+1)*$limit;
    

?>

	<table border='0' width='100%' 
     
          <? if($p>0){
           ?>
            class='printable'
           <?
           }
           ?>
             >
	    
	    <tr>
	<td align='center' width='100%' id='add_td'> 
<?
	$qry1=mysqli_query($db,"select * from branch where brcode='$brcode'");
	$row1=mysqli_fetch_array($qry1);
		print "<p style='font-size: 20px;'><b>GSTIN:$row1[cstno]</b></p>";
	print "<font size='5'><b>PSN AUTOMOTIVE MARKETING PVT LTD</b></font></p>";
	print "<font size='2'><b>".$row1['braddr']."</b></font><p >".$row1['brcity'].",<b>".$row1['region']."<br>Phone : ".$row1['brphone']."<br>CIN : U50300KL1996PTC010841<br>PAN :".$row1['tin'];
	//if(($row1[7]>0)&&($row1[7]!=''))
	
 	//if(($row1[8]>0)&&($row1[8]!=''))
  		 //print " Fax: ".$row1[8];
	//print "<p>Email: ";
	//if($row1[11]!='')
	//	print ",".$row1[11]."<br>Web: ".$row1[12];
	//else
		//print "</p><p>Web: ".$row1[12];
		$fromdt1=implode('-', array_reverse(explode('-', $row['entrydate'])));
		
	   /* $brcol=substr($row['mnfr_code'],0,3).'_ret_spr';
        $tr = "select $brcol from branch where brcode = '$brcode'";
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_row($trd);	*/
		
		
		
		
		
?>
	</td>

	<td align='right'  width='40%'>  </td>
	
	<td align='right'  width='40%'>  </td>	<td align='right'  width='40%'> <? if($signedqr!='')
	{ ?><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>
	    
	    <tr><td><?if($irn!='')
	{ print "IRN: ".$irn;
	}
	?></td></tr>  
	</table>
       <br>
	<table border='1' class='tble'  width='100%' ><tr>
	    <?
	  /*  if($brcode=='TCR001')
	    $minv = '37448817';
	    if($brcode=='UNI001')
	    $minv = '37528817';*/
	    ?>
	    <td  align='left' style='font-size:14px;'>INVNO: <? echo $minv.$trdrow[0].$invno?></td>
	    
	<th  align='center'  width='70%' style='border:0px solid white;height:20px;font-size:18px;' id='head'>FORM GST INVOICE
	<br>
		 <?
	    if($row['paytype']=='Cash')
	    {
	        print "Cash Invoice";
	    }
	    else
	    {
	        print "Credit Invoice";
	    }
	    ?> </th>
	<td  align='right' width='40%' style='font-size:14px;'>INVDT:<? echo $fromdt1 ?></td>
	</tr></table>
 

    <table width=100% class='tble' border='0'>
    <tr><td>
       <table width=100%  border='0'>
           <tr>
            <td colspan=3>Details of Receiver (Billed to)</td>   
           </tr>
           <tr>
            <td>Name</td><td>:</td> <td><? echo $row['customer']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $cusrow['addr1']; ?></td>    
           </tr>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['state1']; ?></td>    
           </tr>
           <tr>
            
            <td>Dist</td><td>:</td> <td><? echo $cusrow['dist1']; ?></td>    
           </tr>
          
                <tr>
            <td>Pin</td><td>:</td> <td><? echo $cusrow['pin1']; ?></td>    
           </tr>
           <tr>
               
            <td>GSTIN/Unique ID</td><td>:</td> <td><? echo $cusrow['cst']; ?></td>    
           </tr>
            <tr><td>PAN</td><td>:</td> <td><? echo $cusrow['kgst']; ?></td></tr>
            <tr><td>Vehicle No:</td><td>:</td> <td><? echo $row['msno']; ?></td></tr>
            
           </table> 
    </td>
    
    <td>
       <table width=100%  border='0'>
           <tr>
            <td colspan=3>Details of Consignee (Shipped to)</td>   
           </tr>
           <tr>
            <td>Name</td><td>:</td> <td><? echo $row['customer']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $cusrow['addr1']; ?></td>    
           </tr>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['state1']; ?></td>    
           </tr>
           
           <tr>
            
            <td>Dist</td><td>:</td> <td><? echo $cusrow['dist1']; ?></td>    
           </tr>
          
                <tr>
            <td>Pin</td><td>:</td> <td><? echo $cusrow['pin1']; ?></td>    
           </tr>
           
           <tr>
            <td>GSTIN/Unique ID</td><td>:</td> <td><? echo $cusrow['cst']; ?></td>    
           </tr>
         <tr>
            <td>PAN</td><td>:</td> <td><? echo $cusrow['kgst']; ?></td></tr>
           
           </table> 
        
    </td>
        
        
        
        
	</tr>
	</table>
	
	
	
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>
	 <? if($row['vatorcst']=='INTRASTATE')
		{
		    ?>
		   <tr><th>SlNo<th>Code<th>Description<th>HSN/SAAC Code  <th> Value<th>Disc%<th>Dis Amt<th>CGST %<th>CGSt Amt<th>SGST %<th>SGST Amt<th>Cess<th>Total Amt</tr>
		   
		    <? 
		}
		    if($row['vatorcst']=='INTERSTATE')
		{
		    ?>
		  
	<tr><th>SlNo<th>Code<th>Description<th>HSN Code <th> Value<th>Disc%<th>Dis Amt<th>IGST %<th>IGST Amt<th>Total Amt</tr>
<?
        }
    if($row['vatorcst']=='INTRASTATE')
	{ 
	    
        for($k=$lower_lmt;$k<=$upper_lmt;$k++)
	    {
	       if(round($discamt[$k],2)==0)
	       {
	       $disamt='';
	       }
	       else
	       {
	       $disamt=round($discamt[$k],2);
	       }
	       
	       $reff="[".$row['ref']."]";
	      
	       if(strlen($item[$k])<=0)
	       {
	           $reff="";
	           
	       }
	       $qry="select hsncode from partmaster where partcode='$code[$k]'";
		//echo $qry;
		$rsqr=mysqli_query($db,$qry);
		$rsrw=mysqli_fetch_row($rsqr);
            print "<tr><td width='5%'>".$slno[$k]."</td><td width='5%'>".$code[$k]."</td><td width='30%'>".$item[$k]."<br>".$reff."</td><td width='5%'>".$rsrw[0]."</td><td align='right' width='5%'>".$rate[$k]."</td><td align='right' width='5%'>".$dis[$k]."</td><td align='right' width='5%'>".$disamt."</td><td align='right' width='5%'>".$cgstprc[$k]."</td><td align='right' width='5%'>".$cgstamt[$k]."</td><td align='right' width='5%'>".$sgstprc[$k]."</td><td align='right' width='5%'>".$sgstamt[$k]."</td><td align='right' width='5%'>".$fcessa[$k]."</td><td align='right' width='5%'>".$ptot[$k]."</td></tr>";
	    }
	    print "<tr><th><th><th><th></th><th>$qty_tot<th>$rate_tot<th><th><th><th><th><th></tr>";
	}
	
	if($row['vatorcst']=='INTERSTATE')
	{ 
	    
        for($k=$lower_lmt;$k<=$upper_lmt;$k++)
	    {
	        
	        $reff="[".$row['ref']."]";
	      
	       if(strlen($item[$k])<=0)
	       {
	           $reff="";
	           
	       }
	        
            print "<tr><td width='5%'>".$slno[$k]."</td><td width='5%'>".$code[$k]."</td><td  width='30%'>".$item[$k]."<br>".$reff."</td><td width='5%'>".$hsncd[$k]."</td><td align='right' width='5%'>".$rate[$k]."</td><td align='right' width='5%'>".$dis[$k]."</td><td align='right' width='5%'>".$disamt."</td><td align='right' width='5%'>".$igstprc[$k]."</td><td align='right' width='5%'>".$igstamt[$k]."</td><td align='right' width='5%'>".$ptot[$k]."</td></tr>";
	    }
	    print "<tr><th><th><th><th></th><th>$qty_tot<th>$rate_tot<th><th><th><th></tr>";
	}

?>

	</table>

	<table  width=100% class='tbl1' id='tbl' border='1' style='height:50px;'>
	<tr><td align='left'  width='30%' ><b>Prepared by:</b><? echo $pre_by; ?></td><td align='left' width='30%'><b>Verified by:</b>--</td><td rowspan='4'>

           <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'>Accounts</td></tr>
 	   	
	   
          	

	   	<tr><td align='right'>Parts Total</td><td ><? if($p==$pg_no){ echo putcoma($partstotal); } ?></td></tr>
	   	<tr><td align='right'>DISCOUNT</td><td ><? if($p==$pg_no){ echo putcoma($total_disc); } ?></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'>SGST Total</td><td ><? if($p==$pg_no){ echo $sgst_tot; } ?> </td></tr>
             <tr><td align='right'>IGST Total</td><td ><? if($p==$pg_no){ echo $igst_tot; } ?> </td></tr> 
             <tr><td align='right'>Cess Total</td><td ><? if($p==$pg_no){ echo $fcess_tot; } ?> </td></tr> 
             <?
             $dif1=round($jtot);
              $dif=round(($dif1-$jtot),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($jtot)); } ?></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;'><?echo word(round($jtot))?>
	</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>

			SIGNATURE OF CASHIER.</td><td style='border:0px solid white; text-align:right;'>
	</td></tr></table>


	</td>

	</tr></table>
      
       <table border='1' width='100%' style='border-collapse:collapse;margin-top:10px;font-size:10px;'>
	<td align='right'  width='100%' style='border:0px solid white;height:12px;'><? echo 'Page:'.($p+1).'/'.($pg_no+1);?></td></tr></table>

<?
 }//close for loop

?>
