<? 
session_start();
include("test.php");
include("opendb.php");

//include("calstock.php");

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


$qry=mysqli_query($db,"select * from estentry where jcno='$jcno' and brcode='$brcode'");

$row=mysqli_fetch_array($qry); 


if($printinv){

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

elseif($printest){

  $head='ESTIMATE';
  $id=$row['estno'];


}
else{

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


}

?>
<!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:20px;
    }
.tbl
{
border: 1px solid black;
border-bottom:0px solid white;
font-size:10px;

}
.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()'>
<?
      
$date = $row['estdate'];
$newDate = date("d-m-Y", strtotime($date));


$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]=$res1['jcno'];
		 $code[$i]='Spare';
		 $slno[$i]=$i;
              $item[$i]=$res1['partname'];
              $qty[$i] =$res1['qty'];
              $amt[$i]=$res1['gtot'];
		//$ptot+=$res1['gtot'];
		$totalamt=$res1['qty']*$res1['rate'];
		//$ptot+=$res1['gtot'];
		$ptot+=$totalamt;
		$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>
 	<? if($minstryinv)
		{
?>
<table width=100% class='tbl' >

	<tr><td align='left'><b>Payer</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row[owner]; ?></td><td align='left'><b>Code</b></td><td><b> :</b></td><td align='left'><? echo $row['email']; ?></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>Tel</b></td><td><b> :</b></td><td align='left'><? echo $row['telephone']; ?></td></tr>
	
	<tr><td align='left'><b>User Name</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $username; ?></td><td align='left'><b>Tel</b></td><td><b> :</b></td><td align='left'><? echo $row['mobile']; ?></td></tr>
	
</table>
<table width=100% class='tbl' >

	<tr><td align='left'><b>INSURANCE CLAIM NO. & DATE</b></td><td ><b>:</b></td><td align='left' colspan='2'><? ?></td><td align='left'><b>Code</b></td><td><b> :</b></td><td align='left'><?  ?></td></tr>
	<tr ><td align='left'><b>REPAIR INVOICE</b></td><td ><b>:</b></td><td align='left' colspan='2'><? ?></td><td align='left'><b>DATE</b></td><td><b> :</b></td><td align='left'><? ?></td></tr>
	
	<tr><td align='left'><b>REPAIR ORDER</b></td><td ><b>:</b></td><td align='left' colspan='2'><? ?></td><td align='left'><b>DATE</b></td><td><b> :</b></td><td align='left'><? ?></td></tr>
<tr><td align='left'><b>COUNTER READING</b></td><td ><b>:</b></td><td align='left' colspan='2'><? ?></td><td align='left'><b></b></td><td><b> </b></td><td align='left'></td></tr>
	
</table>	
<table width=100% class='tbl' >

	<tr><td align='left'><b>REG. NO.</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row[regno]; ?></td><td align='left'><b>MODEL CODE</b></td><td><b> :</b></td><td align='left'><? echo $row[model]; ?></td><td align='left'><b>VIN NO</b></td><td><b> :</b></td><td align='left'><? echo $row['email']; ?></td></tr>
	<tr ><td align='left'><b>MODEL NAME</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row[vehiclename]; ?></td><td align='left'><b>SALE DATE</b></td><td><b> :</b></td><td align='left'><? echo $row['deli_dt']; ?></td><td align='left'><b>CUSTOMER NAME</b></td><td><b> :</b></td><td align='left'><? echo $row[customer]; ?></td></tr>
	
	
</table>
	<table width=100% class='tbl' >

	<tr><td align='left'><b>LPO NO.</b></td><td ><b>:</b></td><td align='left' colspan='2'><? ?></td><td align='left'><b>DATE</b></td><td><b> :</b></td><td align='left'><? ?></td></tr>
	<tr ><td align='left'><b>ESTIMATE NO</b></td><td ><b>:</b></td><td align='left' colspan='2'><? ?></td><td align='left'><b>DATE</b></td><td><b> :</b></td><td align='left'><? ?></td></tr>
	
	
</table>
	
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>
	<tr><th>SL NO.<th>REPAIRS CARRIED OUT<th> Qty <th> Amount</tr>

<?




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

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

?>

	</table>


<?
		}
else
{
?>
	<table width=100% class='tbl' >

	<tr><td align='left' style="width:100px;"><b>Division</b></td><td ><b>:</b></td><td align='left' colspan='2' style="min-width:50%;" ><? echo $row['vehiclename']; ?></td><td align='left'><b>Estimate Type</b></td><td><b> :</b></td><td align='left'><? echo $row['instype']; ?></td></tr>
		<tr><td align='left'><b>Customer Name</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row['customer']; ?></td><td align='left'><b>Lic Plate No</b></td><td><b> :</b></td><td align='left'><? echo $row['regno']; ?></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>Model</b></td><td><b> :</b></td><td align='left'><? echo $row['model']; ?></td></tr>
	<tr ><td align='left'><b>Cust. Contact No.</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row[mobile]; ?></td><td align='left'><b>Chassis</b></td><td><b> :</b></td><td align='left'><? echo $row['chasisno']; ?></td></tr>
	
	<tr><td align='left'><b>Claim No.</b></td><td ><b>:</b></td><td align='left' colspan='2'><? echo $row[jobtype]; ?></td><td align='left'><b>Counter Reading</b></td><td><b> :</b></td><td align='left'><? echo $row['kms']; ?></td></tr>
	


</table>
	
	
	<table width=100% class='tbl' >
	<tr><td align='left'><b>LPO / Cus. Doc. Ref</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>
		</table>
	<table width=100% class='tble'  border='1' FRAME=BOX BORDER=1 CELLPADDING=0 CELLSPACING=0 RULES=COLS>
	<tr><th>Item<th >Item Cat<th >Job Description <th> Qty <th> Rate<th> Amount</tr>

<?




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

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

?>

	</table>
<?
}
	$qry1=mysqli_query($db,"select * from est_est_jcardparts where jcno='$jcno' and brcode='$brcode'");
	$row1=mysqli_fetch_array($qry1);
	$qry2=mysqli_query($db,"select * from est_est_jcardserv where jcno='$jcno' and brcode='$brcode'");
	$row2=mysqli_fetch_array($qry2);
	$qry3=mysqli_query($db,"select * from est_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.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'  colspan='2' valign=top >Terms and conditions:<br><? echo $row[remarks]; ?></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>

	

	

	
	</table>


	</td>

	</tr></table>
	
	 <table  width=100% class='tble' id='tbl'  style='height:50px;'>
	<tr><td align='left' colspan='3'  height=20>Note:1. This estimate is based on visual inspection only. Supplementary estimate will be submitted for additional repairs, if required.

	</td></tr>

	<tr><td colspan=2>
	2. Parts Quotations are valid for 7 days unless otherwise stated.


	</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

?>
