<? 
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;
}
include("test.php");
include("../opendb.php");
include ("../test.php");
//include("calstock.php");
$ref=$_REQUEST['ref'];
$re=mysqli_query($db,"select * from invoice_item where invno='$ref' 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);
$re=mysqli_query($db,"select * from invoice_item where invno='$ref' and brcode='$brcode'")or die(mysqli_error());
$row=mysqli_fetch_array($re)or die("Fetch Error:".mysqli_error());


$brres=mysqli_query($db,"select * from branch where brcode='$brcode'");
$brrow=mysqli_fetch_array($brres);
$cusres=mysqli_query($db,"select * from cust_master where acname='$row[soldto]' and branch='$brcode'");
$cusrow=mysqli_fetch_array($cusres);
?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>PSN 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:12px;
margin-left:10px;
margin-right:10px;
padding-left:5px;
padding-right:5px;
}

.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()'>
<?
$sql="select * from invoice_item where invno='$ref' and brcode='$brcode'";
$re1=mysqli_query($db,$sql)or die('err:'.mysqli_error());

$q_count1=mysqli_query($db,"select count(*) from invoice_item where invno='$ref' 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 invoice_item where invno='$ref' and brcode='$brcode'");
	 while($res1=mysqli_fetch_array($res))
	 { 
	   
        $slno[$i]=$i;
        $code[$i]=$res1['item_code'];
        $item[$i]='INTERFACE AND CABLES';
        $qty[$i] =$res1['qty'];
        $rate[$i]=$res1['uni_amt'];
	 $aprice[$i]=$res1['gross'];
	 $mrp2[$i]=0;
        $mrp[$i]=0;
	 $disc[$i]=0;
		$partstotal+=$res1['gross'];
		$pdisctot+=0;
		

		    $cgstprc[$i]=9;
		    $sgstprc[$i]=9;
		    $cgstamt[$i]=$res1['taxamt']/2;
		    $sgstamt[$i]=$res1['taxamt']/2;
		    $sgst_tot+=$res1['taxamt']/2;
		    $cgst_tot+=$res1['taxamt']/2;
            $igstprc[$i]=0;
		    $igstamt[$i]=0;
	
		    $igst_tot+=0;
		$ptot[$i]=$aprice[$i]+$cgstamt[$i]+$sgstamt[$i]+$igstamt[$i];
		$jtot+=$ptot[$i];
		$i++;
        	$reff = '';
	}

$ptype='';
	
$gtot=$jtot+$ltot;

print $invdate1;

$ntot1=$gtot-$pdisctot;
$ntot=$ntot1-$row['uni_amt']-$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 "<font size='5'><b>PSN AUTOMOBILES PVT LTD</b></font></p>";
	print "<p style='font-size:15px;'><b>GSTIN:$row1[gst]</b></p>";
	print "<font size='2'><b>".$row1[braddr]."</b></font><p >".$row1[brcity].",<b>".$row1[region]."<br>Phone : ".$row1[brphone]."<br>CIN : U50300KL1996PTC010842";
	//if(($row1[7]>0)&&($row1[7]!=''))
	
 	//if(($row1[8]>0)&&($row1[8]!=''))
  		 //print " Fax: ".$row1[8];
	print "<p>Email:$row1[email]";
	//if($row1[11]!='')
	//	print ",".$row1[11]."<br>Web: ".$row1[12];
	//else
		//print "</p><p>Web: ".$row1[12];
		$fromdt1=implode('-', array_reverse(explode('-', $row[invdate])));
		
	    $brcol=substr($row[mnfr_code],0,3).'_ret_spr';
	
		
		

		
		
?>
	</td>

	<td align='right'  width='30%'>  </td></tr>
	    
	    
	</table>
       <br>
	<table border='1' width='100%' style='border-collapse:collapse;'><tr>
	    <td  align='left' width='25%' >INVNO:
	    
	    
<?
if($brcode == 'HUBCHI')
{
print '[CHI]';
}
else if($brcode == 'HUBEKA')
{
print '[ILK]';
}
else if($brcode == 'HUBHOS')
{
print '[HOS]';
}
else if($brcode == 'PAT649')
{
print '[TLA]';
}
else 
{
	if($brcode =='PSN470')
		print '';
}
if ($brcode =='PSN470')
{
	print "";
}
if($brcode=='PSN470' || $brcode=='PSN606')
{
	if($brcode=='PSN606')
		echo "TCR/".$ref;
	else
	    echo $ref;
}
else
	    echo 'T0000'.$ref;
?></td>
	    
	<th  align='center'  width='50%' style='border:0px solid white;height:20px;font-size:18px;' id='head'>FORM GST INV - 1
	<br>
		 <?
	    if($row[paytype]=='Cash')
	    {
	        print "Cash Invoice";
	    }
	    else
	    {
	        print "Credit Invoice";
	    }
	    ?> </th>
	<td  align='right' width='30%' >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['soldto']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $cusrow['addr1']; ?></td>    
           </tr>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['stat']; ?></td>    
           </tr>
           <tr>
            <td>GSTIN/Unique ID</td><td>:</td> <td><? echo $cusrow['tin']; ?></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['soldto']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $cusrow['addr1']; ?></td>    
           </tr>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['stat']; ?></td>    
           </tr>
           <tr>
            <td>GSTIN/Unique ID</td><td>:</td> <td><? echo $cusrow['tin']; ?></td>    
           </tr>
         
           </table> 
        
    </td>
        
        
        
        
	</tr>
	</table>
	
	
	
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>

		  
	<tr><th>SlNo<th>PartCode<th >Part Name  <th>HSN Code<th>Qty <th>Rate<th>CGST %<th>CGSt Amt<th>SGST %<th>SGST Amt<th>IGST %<th>IGST Amt<th>Total Amt</tr>
<?
 
        for($k=$lower_lmt;$k<=$upper_lmt;$k++)
	    {
        //$tr = "select * from partmaster where partcode = '$code[$k]'";
        //$trd=mysqli_query($db,$tr);
        //$trdrow=mysqli_fetch_array($trd);
		//$rate1 =  $mrp[$k]/(1+(($cgstprc[$k]+$sgstprc[$k])/100));
		//$rate1 =  putcoma(($rate1 - ($rate1 * $disc[$k]/100))*$qty[$k]);
		$rate1 = $aprice[$k]/$qty[$k];
		if($rate1==0)
			$rate1='';
	$hsncode = '';
	 if($slno[$k]>0)
		$hsncode = '90318000';
	 
        print "<tr><td width='4%' >".$slno[$k]."</td><td >".$code[$k]."</td><td>".$item[$k]."</td><td>$hsncode</td><td align='right' >".$qty[$k] ."</td><td align='right' width='75'>".$rate1."</td><td align='right' width='25'>".$cgstprc[$k]."</td><td align='right' width='75'>".$cgstamt[$k]."</td><td align='right' width='25'>".$sgstprc[$k]."</td><td align='right' width='75'>".$sgstamt[$k]."</td><td align='right' width='75'>".$igstprc[$k]."</td><td align='right' width='75'>".$igstamt[$k]."</td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
	   
		//echo $k."<br>";
		 }

?>

	</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' 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> 
             <?
             $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;'>
	</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:right;'>
	</td></tr></table>


	</td>

	</tr></table>

	        <table border='1' width='100%' style='border-collapse:collapse;margin-top:0px;font-size:12px;'>
	<td align='center'  width='100%' style='border:0px solid white;height:12px;'><b>HSN Code Summary</td></tr></table>
    <table class='tble' style="border-bottom: 1px solid black;" border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS width=100%><tr>
       
	<tr style="font-weight: bold;"><th>HSN</th><th>Taxable Amt</th><th>IGST Rate</th><th>Value</th><th>CGST Rate</th><th>Value</th><th>SGST Rate</th><th>Value</th><th>Taxable Amt With Tax</th></tr>
	 <?
	if($brcode=='PSN470'||$brcode=='PSN606')
	{
		$re11=mysqli_query($db,"select sum(aprice),cgstp,sgstp,igstp,sum(cgstamt),sum(sgstamt),sum(igstamt),hsncode from spareinvoice,partmaster where ref ='$ref' and brcode = '$brcode' and partmaster.partcode = spareinvoice.partcode group by hsncode");
	}
	else
	{
		$re11=mysqli_query($db,"select sum(aprice),cgstp,sgstp,igstp,sum(cgstamt),sum(sgstamt),sum(igstamt),hsncode from spareinvoice,partmaster where invno='$ref' and brcode = '$brcode' and partmaster.partcode = spareinvoice.partcode group by hsncode");
	}
	while($res11=mysqli_fetch_array($re11))
	{ 
		$ptot1 = $res11[0]+$res11[4]+$res11[5]+$res11[6];
		print "<tr><td width='4%' >".$res11[7]."</td><td align='right' width='75'>".$res11[0]."</td><td align='right' width='75'>".$res11[3]."</td><td align='right' width='75'>".$res11[6]."</td><td align='right' width='75'>".$res11[1]."</td><td align='right' width='75'>".$res11[4]."</td><td align='right' width='75'>".$res11[2]."</td><td align='right' width='75'>".$res11[5]."</td><td align='right' width='75'>".$ptot1."</td></tr>";
	}
	?>
	
	</table>
	
       <table border='1' width='100%' style='border-collapse:collapse;margin-top:10px;font-size:10px;'>
	<td  width='100%' style='border:0px solid white;height:12px;'>Rupees  :  <?echo word($jtot)?>  Only</td></tr></table>

<?
 }//close for loop

?>
