<? 
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");
$jcno=$_REQUEST['jcno'];
$invno=$_REQUEST['invno'];
$re=mysqli_query($db,"select * from jobcard where jcno='$jcno' and brcode='$brcode'");
if(mysqli_num_rows($re)<=0)
 {
    print "Jobcard does not exist";
    return;
 }
$jcrow=mysqli_fetch_array($re);
date_default_timezone_set('Asia/Kolkata');
$crtm=date('H:i:s');

?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Jobcard 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: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()'>
<?
    $qry=mysqli_query($db,"select * from jobcard where jcno='$jcno' and brcode='$brcode'");
    $sql="select * from jobcard where jcno='$jcno'  and brcode='$brcode'";
    //echo $sql;
    $row=mysqli_fetch_array($qry);       

    $balres1=mysqli_query($db,"select * from ledger where brcode = '$brcode' and led ='$row[cname]'");

    $balrow1=mysqli_fetch_row($balres1);
    $cusqry=mysqli_query($db,"select * from customer where acname='$row[cname]'");
    $cusrow=mysqli_fetch_array($cusqry);
    
    //$balres2=mysqli_query($db,"select sum(amt) from down_payment where jcno='$jcno' and brcode = '$brcode' and jvno>0 ");
    //$balrow2=mysqli_fetch_row($balres2);
    //$adv=$balrow2[0];
    
    $balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$row[cname]'");
    $balrow=mysqli_fetch_row($balres);
    if($balrow1[3]=='Dr')
    {
        $bal=($balrow[0]+$balrow1[2])-$balrow[1]; //print '<br>bal'.$row[2];
    }
    else
    {
        $bal=$balrow[1]+$balrow1[2]-$balrow[0];
    }

    $date = $row['estdate'];
    $invdate1 = $row['inv_date'];
    $invdate = date("d-m-Y", strtotime($invdate1));
    $newDate = date("d-m-Y", strtotime($date));


   

    $q_count2=mysqli_query($db,"select count(*) from jcardserv where jcno='$jcno' and brcode='$brcode'");
    $sq="select count(*) from jcardserv where jcno='$jcno' and brcode='$brcode'";
    //echo $sq;
    $cnt2=mysqli_fetch_array($q_count2);
   

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

    //$limit=23;
    $limit=15;
    $pg_no=floor($count/$limit);
 
 



    $j=1;
    $i=1;
	$jtot=0;
	$pdisctot=0;
	$ltot=0;
	$subtot=0;
	$ldis=0;
 
	$res2=mysqli_query($db,"select * from jcardparts where jcno='$jcno' and brcode='$brcode'");
	while($res3=mysqli_fetch_array($res2))
	{
        $slno_pt[$j]=$j;
        $code_pt[$j]=$res3['partcode'];
        $item_pt[$j]=$res3['partname'];
        $qty_pt[$j] =$res3['qty'];
        $rate_pt[$j]=$res3['rate'];
		//$ltot_pt+=$res3['tot'];
		//$ldis+=$res3['dis'];
		$tr = "select * from partmaster where partcode = '$res3[partcode]'";
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_array($trd);
        $hscd_pt[$j]="(".$trdrow['hsncode'].")";

		    $cgstprc_pt[$j]=$res3['cgstp'];
		    $sgstprc_pt[$j]=$res3['sgstp'];
		    $cgstamt_pt[$j]=$res3['cgstamt'];
		    $sgstamt_pt[$j]=$res3['sgstamt'];

		    $igstprc_pt[$j]=$res3['igstp'];
		    $igstamt_pt[$j]=$res3['igstamt'];
		   $ptot_pt[$j]=$res3['gtot'];
		
		$cgstot_pt+=$res3['cgstamt'];
		$sgstot_pt+=$res3['sgstamt'];
		$igstot_pt+=$res3['igstamt'];
		
		$prttot_pt+=$res3['rate']*$res3['qty'];
		$ltot_pt+=$res3['gtot'];
		$j++;
             
	}
	
$ntot_pt=$ltot_pt;

	$res21=mysqli_query($db,"select * from jcardserv where jcno='$jcno' and brcode='$brcode'");
	while($res31=mysqli_fetch_array($res21))
	{
        $slno[$i]=$i;
        $code[$i]=$res31['service'];
        $item[$i]=$res31['descr'];
        $qty[$i] ='1.00';
        $rate[$i]=$res31['servamt'];
		$ltot+=$res31['tot'];
		$subtot+=$res31['subamt'];
		$ldis+=$res31['dis'];

		    $cgstprc[$i]=$res31['cgstp'];
		    $sgstprc[$i]=$res31['sgstp'];
		    $cgstamt[$i]=$res31['cgstamt'];
		    $sgstamt[$i]=$res31['sgstamt'];

		    $igstprc[$i]=$res31['igstp'];
		    $igstamt[$i]=$res31['igstamt'];
		    //$hsncode[$i]=9963;
		    //$hscd[$i]="(9963)";
		
		$prttot+=$res31['servamt'];
	
		$cgstot+=$res31['cgstamt'];
		$sgstot+=$res31['sgstamt'];
		$igstot+=$res31['igstamt'];
		$cestot+=$res31['hredutx'];
		$ptot[$i]=$res31['tot'];
		$i++;
             
	}
	
$gtot=$jtot+$ltot;


print $invdate1;

if($invdate1>'2016-01-31')

{ 
$gtot=$ptot+$ltot+$subtot;


}

$ntot1=$gtot-$pdisctot;
$ntot=$ntot1-$row['adv_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);
	if($brcode=='MEN888')
        print "<font size='5'><b>PSN MENTOR AND CAREER PRIVATE LIMITED</b></font></p>";
    else
        print "<font size='5'><b>PSN HOLDINGS PVT LTD</b></font></p>";
	
		print "<p><b>GSTIN:$row1[gst]</b></p>";
	print "<font size='2'><b>".$row1['braddr']."</b></font><p >".$row1['brcity'].", ".$row1['brstate']."<br>Phone : $row1[brphone]";

	print "Email: $row1[email]";

		$fromdt1=implode('-', array_reverse(explode('-', $row['entrydate'])));
?>
	</td>

	<td align='right'  width='40%'>  </td></tr>
	</table>
       <br>
	<table border='1' width='100%' style='border-collapse:collapse;'><tr>
	    <td  align='left' width='20%'  >INVNO:
	    
	    
	    <?
/*if($brcode == 'MAN140')
{
?>
<b>ZFMDJC
<?
}
else if($brcode == 'ERA153')
{
?>
<b>ZFEMJC
<?
}
else if($brcode == 'CAL556')
{
?>
 <b>ZFCTJC
<?
}
else
{
	print '['.substr($brcode,0,3).']';
}*/
?>
<?echo $jcrow['invno']?></td>
	    
	<th  align='center'  width='60%' style='border:0px solid white;height:20px;font-size:18px;' id='head'>FORM GST INVOICE</th>
	<td  align='right' width='20%' >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['cname']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $cusrow['addr1'].",".$cusrow['dist1'].",".$cusrow['pin1']; ?></td>    
           </tr>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['state1']; ?></td>    
           </tr>
           <tr>
            <td>GSTIN/Unique ID</td><td>:</td> <td><? echo $cusrow['gst']; ?></td>    
           </tr>
           <tr>
            <? if($brcode == 'MEN888'){?>
            
            <td>Staff Name</td><td>:</td> <td><? echo $row['model']; ?></td>    
            <?}else{?>
            <td>Vehicle No</td><td>:</td> <td><? echo $row['model']." --- ".$row['msno']; ?></td>    
            <?}?>
           </tr>
           </table> 
    </td>
    
    <td>
       <table width=100%  border='0'>
           <tr>
            <td colspan=3>Details of Receiver (Shipped to)</td>   
           </tr>
           <tr>
            <td>Name</td><td>:</td> <td><? echo $row['cname']; ?></td>    
           </tr>
           <tr>
            <td>Address</td><td>:</td> <td><? echo $cusrow['addr1'].",".$cusrow['dist1'].",".$cusrow['pin1']; ?></td>    
           </tr>
           <tr>
            <td>State</td><td>:</td> <td><? echo $cusrow['state1']; ?></td>    
           </tr>
           <tr>
            <td>GSTIN/Unique ID</td><td>:</td> <td><? echo $cusrow['gst']; ?></td>    
           </tr>
           <tr>
            <td>Jobcard No</td><td>:</td> <td><? echo $row['jcno']; ?></td>    
           </tr>
           </table> 
        
    </td>
        
        
        
        
	</tr>
	</table>
       
       <table border='1' width='100%' style='border-collapse:collapse;margin-top:0px;font-size:10px;'>
	<td align='center'  width='100%' style='border:0px solid white;height:12px;'><b>Service</td></tr></table>
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>
	<tr><th>SlNo<th>Service<th >Service Amt<th>CGST %<th>CGSt Amt<th>SGST %<th>SGST Amt<th>IGST %<th>IGSt Amt<th>Total Amt</tr>
<?
        for($k=$lower_lmt;$k<=15;$k++)
	    {
        
        print "<tr><td width='4%' >".$slno[$k]."</td><td width='75'>".$item[$k].$hscd[$k]."</td><td align='right' width='25'>".$rate[$k]."</td><td align='right' width='25'>".$cgstprc[$k]."</td><td align='right' width='25'>".$cgstamt[$k]."</td><td align='right' width='25'>".$sgstprc[$k]."</td><td align='right' width='50'>".$sgstamt[$k]."</td><td align='right' width='25'>".$igstprc[$k]."</td><td align='right' width='50'>".$igstamt[$k]."</td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
	    }
?>
	</table>

	<table  width=100% class='tbl1' id='tbl' border='1' style='height:50px;'>
	<tr><td align='left'  width='25%' ><b>Prepared by:</b><? echo $pre_by; ?></td><td align='left' width='25%'><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=4 class='tot_td1' align=center>Accounts</td></tr>
 	   	<tr><td align='right'>Spares Total</td><td width='92'><?if($p==$pg_no){ echo putcoma($prttot_pt);} ?></td><td align='right'>Labour Total</td><td width='92'><?if($p==$pg_no){ echo putcoma($prttot);} ?></td></tr>
		<tr><td  align='right' height='10'>cgst Total</td><td ><? if($p==$pg_no){ echo $cgstot_pt; }?></td><td  align='right' height='10'>cgst Total</td><td ><? if($p==$pg_no){ echo $cgstot; }?></td></tr>
		<tr><td  align='right' height='10'>sgst Total</td><td ><? if($p==$pg_no){ echo $sgstot_pt; }?></td><td  align='right' height='10'>sgst Total</td><td ><? if($p==$pg_no){ echo $sgstot; }?></td></tr>
		<tr><td  align='right' height='10'>igst Total</td><td ><? if($p==$pg_no){ echo $igstot_pt; }?></td><td  align='right' height='10'>igst Total</td><td ><? if($p==$pg_no){ echo $igstot; }?></td></tr>
		
		<tr><td  align='right' height='10'>Cess Total</td><td ><? if($p==$pg_no){ echo $cestot_pt; }?></td><td  align='right' height='10'>Cess Total</td><td ><? if($p==$pg_no){ echo $cestot; }?></td></tr>
		
		
	
		
		
		
		
        <tr><td  align='right' height='10'>Total</td><td ><? if($p==$pg_no){ echo $ntot_pt; }?></td><td  align='right' height='10'>Total</td><td ><? if($p==$pg_no){ echo $ntot; }?></td></tr>
          

	   <?
	   $tot_all=$ntot+$ntot_pt;
	    $rnd=round($tot_all);
        $dif=round(($rnd-$tot_all),2);
        
	   ?>

	   	<tr><td align='right' colspan=3>Round Off</td><td><? if($p==$pg_no){ echo putcoma($dif);} ?> </td></tr>
	   	
              
              <tr><td align='right' colspan=3 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_all)); } ?></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>

			SIGNATURE OF CUSTOMER.</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
$curtm=date(Y-m-d);
$curtm=implode('-', array_reverse(explode('-', $curtm)));
?>
	 <table border='1' width='100%' style='border-collapse:collapse;margin-top:0px;font-size:10px;'>
	<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>Vlaue</th><th>Taxable Amt With Tax</th></tr>
	 <?for($k=1;$k<=5;$k++)
	    {
	    $tr = "select * from partmaster where partcode = '$code_pt[$k]'";
        $trd=mysqli_query($db,$tr);
        $trdrow=mysqli_fetch_array($trd);
        print "<tr><td width='4%' >".$trdrow['hsncode']."</td><td align='right' width='75'>".$rate_pt[$k]."</td><td align='right' width='75'>".$igstprc_pt[$k]."</td><td align='right' width='75'>".$igstamt_pt[$k]."</td><td align='right' width='75'>".$cgstprc_pt[$k]."</td><td align='right' width='75'>".$cgstamt_pt[$k]."</td><td align='right' width='75'>".$sgstprc_pt[$k]."</td><td align='right' width='75'>".$sgstamt_pt[$k]."</td><td align='right' width='75'>".$ptot_pt[$k]."</td></tr>";
	    if($rate[$k]>0)
	    print "<tr><td width='4%' >".$hscd[$k]."</td><td align='right' width='75'>".$rate[$k]."</td><td align='right' width='75'>".$igstprc[$k]."</td><td align='right' width='75'>".$igstamt[$k]."</td><td align='right' width='75'>".$cgstprc[$k]."</td><td align='right' width='75'>".$cgstamt[$k]."</td><td align='right' width='75'>".$sgstprc[$k]."</td><td align='right' width='75'>".$sgstamt[$k]."</td><td align='right' width='75'>".$ptot[$k]."</td></tr>";
	    }?>
	</table>
	<table>
	    <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td align='right'>
For PSN MENTOR AND CAREER PRIVATE LIMITED
<br><br><br>
Authorised Signatory</td></tr>
</table>