<? 
session_start();
include("test.php");
include("opendb.php");
include("includes/function.php");
//include("calstock.php");

if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title></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:grey;
            }
        }

</style>
<body onload='window.print()'>
<?
$qry=mysqli_query($db,"select * from estentry where jcno='$jcno' and brcode='$brcode'");

$row=mysqli_fetch_array($qry);       
$date = $row['estdate'];
$newDate = date("d-m-Y", strtotime($date));



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




if($printinv){

  $head='INVOICE';
  $id=$row['estno']; 
  
}


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

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

$count=$cnt1[0]+$cnt2[0];

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

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




       $i=1;
	$ptot=0;
	$pdisctot=0;
	$ltot=0;
	$subtot=0;
	$ldis=0;
 
	$res=mysqli_query($db,"select * from est_jcardparts where jcno='$jcno' and brcode='$brcode'");
	 while($res1=mysqli_fetch_array($res))
	 { 
              $code[$i]='Spare';
		 $slno[$i]=$i;
              $item[$i]=$res1['partname'];
              $qty[$i] =$res1['qty'];
              $amt[$i]=$res1['gtot'];
		$totalamt=$res1['qty']*$res1['rate'];
		$ptot+=$totalamt;
		//$ptot+=$res1['gtot'];
		$pdisctot+=$res1['disc'];
		$i++;
             
	}


	$res2=mysqli_query($db,"select * from est_jcardserv where jcno='$jcno' and brcode='$brcode'");
	while($res3=mysqli_fetch_array($res2))
	{
              $slno[$i]=$i;
		//$code[$i]=$res3['jcno'];
		$code[$i]='Labour';
              $item[$i]=$res3['service'];
              $qty[$i] ='1.00';
              $amt[$i]=$res3['servamt'];
		$ltot+=$res3['servamt'];
		$subtot+=$res3['subamt'];
		$ldis+=$res3['dis'];
		$i++;
             
	}



$gtot=$ptot+$ltot;
$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='left' width='60%' id='add_td'> 
<?
	$qry1=mysqli_query($db,"select * from company_master where brcode='$brcode'");
	$row1=mysqli_fetch_array($qry1);
	 print "<font size='2'><b>".$row1[2]."</b></font><p >".$row1[4].", ".$row1[5].",".$row1[6];
	if(($row1[7]>0)&&($row1[7]!=''))
   		 print "</p><p>Tel: ".$row1[7];
 	if(($row1[8]>0)&&($row1[8]!=''))
  		 print " Fax: ".$row1[8];
	print "</p><p>Email: ".$row1[10];
	if($row1[11]!='')
		print ",".$row1[11]."<br>Web: ".$row1[12];
	else
		print "</p><p>Web: ".$row1[12];
?>
	</td>

	<td align='right'  width='40%'> <img src="images/logo.gif" width="60" height="60" /> </td></tr></table>
      
	<table border='1' width='100%' style='border-collapse:collapse;'><tr>
	<th  align='center'  width='100%' style='border:0px solid white;height:20px;font-size:18px;' id='head'><? ECHO $head; ?>&nbsp;(<? ECHO $id; ?>)</th></tr></table>
 	<p style='text-align:right;padding:0px;margin:0px;font-size:10px;'>Finance Code:<? echo $row1[13]; ?></p>
	<table width=100% class='tbl' >

	<tr><td align='left' style="width:80px;"><b>Payer</b></td><td ><b>:</b></td><td align='left' colspan='2' style="min-width:50%;" ><? echo $row['owner']; ?></td><td align='left'><b>Tel</b></td><td><b> :</b></td><td align='left'><? echo $row['telephone']; ?></td></tr>
	<tr><td align='left'><b>Address</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row['address']; ?></td><td align='left'><b>Mobile </b></td><td><b> :</b></td><td align='left'><? echo $row['mobile']; ?></td></tr>
	<tr><td align='left'><b>User Name</b></td><td ><b>:</b></td><td align='left' colspan='2'>--</td><td align='left'><b>Email</b></td><td><b> :</b></td><td align='left'><? echo $row['email']; ?></td></tr>
	</table>
	<table width=100% class='tbl' height='40'>
	<tr><td  ><b>Insurance Claim No:</b></td><td >--</td><td ><b>Date:</b></td><td ><? echo $newDate; ?></td><td rowspan='3' style="border-left:1px solid black;border-collapse:collapse;border-style:dotted;width:0px"></td><td ><b>Repair Invoice: </b></td><td><? echo $row['estno']; ?></td><td ><b>Date:</b></td><td><? echo $invdate; ?></td></tr>
	<tr><td colspan='4' style="border-bottom:0px solid black;border-collapse:collapse;"></td><td ><b>Repair Order: </b></td><td><? echo (int)$row[0]; ?></td><td ><b>Date:</b></td><td><? echo $newDate; ?></td></tr>
	<tr><td ><b>Counter Reading:</b></td><td ><? echo (int)$row['kms'];  ?></td><td ></td><td ></td><td ><b>Assignment No: </b></td><td>--</td><td ></td><td></td></tr>
	</table>
	<table width=100% class='tbl'>
	<tr><td ><b>Reg No:</b></td><td ><? echo $row['regno']; ?></td><td ><b>Model Code:</b></td><td ><? echo $row['model']; ?></td><td ><b>VIN No: </b></td><td><? echo $row['chasisno']; ?></td></tr>
	<tr><td ><b>Model Name:</b></td><td ><? echo $row['vehiclename']; ?></td><td ><b>Sale Date:</b></td><td >--</td><td ><b>Cust. Name: </b></td><td><? echo $row['owner']; ?></td></tr>
	</table>
	<table width=100% class='tbl' >
	<tr><td align='left'><b>LPO No</b></td><td ><b>:</b></td><td align='left'>--</td><td width=50%></td><td align='left'><b>Date </b></td><td><b> :</b></td><td align='left'><? echo $newDate; ?></td></tr>
	<tr><td align='left'><b>Estimate No</b></td><td ><b>:</b></td><td align='left'>--</td><td width=40%></td><td align='left'><b>Date </b></td><td><b> :</b></td><td align='left'><? echo $newDate; ?></td></tr>
	</table>
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>
	<tr><th>JobNo<th>Code<th >Repairs Carried Out  <th> Qty <th> Amount</tr>

<?




       for($k=$lower_lmt;$k<=$upper_lmt;$k++)
	{

             print "<tr><td width='4%' >".$slno[$k]."</td><td >".$code[$k]."</td><td>".$item[$k]."</td><td align='right' >".$qty[$k] ."</td><td align='right' width='75'>".$amt[$k]."</td></tr>";
	}
    

?>

	</table>
<?
	$qry1=mysqli_query($db,"select * from est_jcardparts where jcno='$jcno' and brcode='$brcode'");
	$row1=mysqli_fetch_array($qry1);
	$qry2=mysqli_query($db,"select * from est_jcardserv where jcno='$jcno' and brcode='$brcode'");
	$row2=mysqli_fetch_array($qry2);
	$qry3=mysqli_query($db,"select * from est_jcardserv where subamt>'0' and jcno='$jcno' and brcode='$brcode'");

	$sql=mysqli_query($db,"select sum(est_jcardparts.qty*est_jcardparts.rate) from est_jcardparts INNER JOIN newpartmaster ON est_jcardparts.partcode=newpartmaster.partcode where (newpartmaster.subgroup='PARTS' or newpartmaster.subgroup='FILTER') and est_jcardparts.jcno='$jcno' and est_jcardparts.brcode='$brcode'");
	$rr=mysqli_fetch_array($sql);
	//$sql1=mysqli_query($db,"select sum(est_jcardparts.gtot) from est_jcardparts INNER JOIN newpartmaster ON est_jcardparts.partcode=newpartmaster.partcode where newpartmaster.subgroup='TYRE' and est_jcardparts.jcno='$jcno' and est_jcardparts.brcode='$brcode'");
	$sql1=mysqli_query($db,"select sum(est_jcardparts.qty*est_jcardparts.rate) from est_jcardparts INNER JOIN newpartmaster ON est_jcardparts.partcode=newpartmaster.partcode where newpartmaster.subgroup='TYRE' and est_jcardparts.jcno='$jcno' and est_jcardparts.brcode='$brcode'");
	$rr1=mysqli_fetch_array($sql1);
	$sql2=mysqli_query($db,"select sum(est_jcardparts.qty*est_jcardparts.rate) from est_jcardparts INNER JOIN newpartmaster ON est_jcardparts.partcode=newpartmaster.partcode where newpartmaster.subgroup='OIL' and est_jcardparts.jcno='$jcno' and est_jcardparts.brcode='$brcode'");
	$rr2=mysqli_fetch_array($sql2);

       
?>
	<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'>Labour</td><td width='92'><?if($p==$pg_no){ echo putcoma($ltot);} ?></td></tr>
	   
          	<tr><td  align='right' height='10'>Parts & Filter</td><td ><? if($p==$pg_no){ echo $rr[0]; }?></td></tr>
	   	<tr><td align='right'>Tyre</td><td ><? if($p==$pg_no){ echo $rr1[0];} ?></td></tr>
	   	<tr><td  align='right'>Gas & Oil</td><td ><? if($p==$pg_no){ echo $rr2[0];} ?></td></tr>
   	   	<tr><td align='right'>Consumables</td><td></td></tr>
	  	 <tr><td  align='right'>Sublet</td><td><? if($p==$pg_no){ echo putcoma($subtot); } ?></td></tr>
	   	<tr><td align='right'>Total</td><td ><? if($p==$pg_no){ echo putcoma($gtot); } ?></td></tr>
	   	<tr> <td  align='right'>Advance</td><td ><? if($p==$pg_no){ echo $row['adv_amt']; }?></td></tr>
	   	<tr><td align='right'>Labour disc</td><td><? if($p==$pg_no){ echo $ldis;} ?> </td></tr>
	   	<tr><td align='right'>Parts disc</td><td ><? if($p==$pg_no){ echo putcoma($pdisctot); } ?> </td></tr>
              <tr><td align='right'>Round off</td><td > </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($ntot); } ?></b></td></tr>
          </table>


	</td></tr>

	<tr><td align='left' colspan='2'  height=20><b>Advance:</b></td></tr>

	<tr><td align='left' colspan='2'  height=20><b>Sublet Name	:</b>
<?
	while($row3=mysqli_fetch_array($qry3))
	{
		print $row3['subnm'].",";
	}
?>
	</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;'>Company accepts no responsibility for money paid against this invoice unless cash stamp affixed and signed AND/OR evidenced by an offical receipt of the company .
	</td><td style='border:0px solid white; text-align:right;'> <img src='images/est21.png' /></td></tr>
	<tr><td style='border:0px solid white;text-align:left;text-transform:none;'> Received vehicle to my entire satisfaction with all tools and accessories complete.</br>

			SIGNATURE OF CUSTOMER.</td><td style='border:0px solid white; text-align:right;'>
	<img src='images/est22.png' /></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

?>
