<?php

include("mpdf.php");

$mpdf=new mPDF(); 

$mpdf->useAdobeCJK = true;		// Default setting in config.php
						// You can set this to false if you have defined other CJK fonts

$mpdf->SetAutoFont(AUTOFONT_ALL);	//	AUTOFONT_CJK | AUTOFONT_THAIVIET | AUTOFONT_RTL | AUTOFONT_INDIC	// AUTOFONT_ALL
						// () = default ALL, 0 turns OFF (default initially)

//$mpdf->setFooter('{PAGENO}');

//$mpdf->WriteHTML($table);
       
		
		include "../include/mysql.php";

        include "../include/common.php";

		extract($_REQUEST);

		//echo $val;

		$exp=explode("***",$val);

		$MACHINE_ID=$exp[0];

		$fdate=date("Y-m-d",strtotime($exp[1]));

		$tdate=date("Y-m-d",strtotime($exp[2]));

		$cname=$exp[3];

	if($MACHINE_ID=="All")
		{

				//$icondn=" where BILL_DATE_TIME between '$fdate' and '$tdate' and MACHINE_ID in(select MACHINE_ID from setting_details)  and item_code in(select item_code from item_details) order by bill_date_time";

				
				$icondn=" where DATE(BILL_DATE_TIME) between '$fdate' and '$tdate' and MACHINE_ID in(select MACHINE_ID from cmp_machine where cmp_id='$cname')  and item_code in(select item_code from item_details) order by bill_date_time";

		}
		else
		{
				
				$icondn=" where MACHINE_ID='$MACHINE_ID' and item_code in(select item_code from item_details) and DATE(BILL_DATE_TIME) between '$fdate' and '$tdate' order by bill_date_time";

		}	

		$getdetails=$db->select(RBI,"count(*)",$icondn);

		//$getdetails_rows=$db->rows($getdetails);
	
		$getdetails_rows=$db->fetch($getdetails);
	
		
		if($getdetails_rows[0]==0)
		{
				echo 0;

		}
		else
		{
		
			$cmp_select=$db->select(CMP,"*"," where id='$cname'");

			$cmp_fetch=$db->fetch($cmp_select);

			$cmp_name=$cmp_fetch["company_name"];


			$mpdf->WriteHTML("<table style='width:100%;'><tr><td align='center'><b><span>$cmp_name,MADURAI</span> </b></td></tr><tr><td align='center'><b><span>LASTITEM SERIAL REPORT</span></b></td></tr><tr><td align='center'><b><span>$exp[1] To  $exp[2]</span></b></td></tr></table><br />");
			
			$load_machine=$db->select(RBI,"distinct(machine_id)",$icondn);
					
					while($fetch_machine=$db->fetch($load_machine))
					{
					
									
					$MACHINE_ID=$fetch_machine[0];
					
					
					$getitem_name=$db->select(MD,"*"," where MACHINE_ID='$MACHINE_ID'");

                    $getitem_name_fetch=$db->fetch($getitem_name);

                    $SOFTWARE_ID=$getitem_name_fetch["softwareid"];
		
		
		  			$mpdf->WriteHTML("<table width='100%' cellpadding='4' cellspacing='4' ><tr><th align='left'>$SOFTWARE_ID</th></tr></table><br><table width='800px;'  border=0 class='price1' cellpadding='8' cellspacing='6' style='border-collapse:collapse;'><col style='width: 10%' class='col1'>			<col style='width: 20%'>	<col style='width: 10%'>	<col style='width: 15%'>	<col style='width: 15%'>	<col style='width: 10%'><col style='width: 20%'><thead><tr><th align='center' class='pdftbl'>பில் தேதி</th><th align='center' class='pdftbl'>டிக்கெட் பெயர்</th>	<th align='center' class='pdftbl'>விலை</th><th align='center' class='pdftbl'>ஆரம்ப எண்</th><th align='center' class='pdftbl'>முடிவு எண்</th><th align='center' class='pdftbl'>எண்ணிக்கை</th><th align='center' class='pdftbl'>மொத்தம்</th></tr>	</thead>");
					
					//$mpdf->WriteHTML($table);


		$bd_date=$db->select(RBI,"DISTINCT(item_code)",$icondn);

				while($bd_fetch=$db->fetch($bd_date))
					{
				
				$item_code=$bd_fetch[0];

				
				//echo $bill_date."<br>";
		
				

				$max_condn1=" where machine_id='$MACHINE_ID' and item_code='$item_code' and DATE(BILL_DATE_TIME) between '$fdate' and '$tdate'";
				
				$max_sno_select1=$db->select(RBI,"max(serial_count)",$max_condn1);	
				
				$max_fetch1=$db->fetch($max_sno_select1);
								
				$serial_count=$max_fetch1[0];

				
				

			
				$max_condn=" where machine_id='$MACHINE_ID' and serial_count='$serial_count' and item_code='$item_code' and DATE(BILL_DATE_TIME) between '$fdate' and '$tdate'";
				
				$max_sno_select=$db->select(RBI,"item_code,start_bill_number,end_bill_number,bill_date_time",$max_condn);	
				
				$max_fetch=$db->fetch($max_sno_select);						
				
				
				//echo $item_code."-".$serial_count."-".$max_condn."<br>";
				
				//echo $bill_date."-".$ls_fetch[0]."-".$max_fetch[0]."<br>";
								
				  $ITEM_CODE=$max_fetch[0];

					
				$getitem_info=$db->select(ITI,"item_name_oth,unit_rate"," where ITEM_CODE='$ITEM_CODE' and MACHINE_ID='$MACHINE_ID'");

				$getitem_info_fetch=$db->fetch($getitem_info);

				$item_name=$getitem_info_fetch[0];							
				
				$UNIT_RATE=$getitem_info_fetch[1];	
								
					
				$start_no=$max_fetch[1];
				
				 $end_no=$max_fetch[2];

				  $bill_date_time=date("d-m-Y",strtotime($max_fetch[3]));

                 $rcount=($end_no+1)-$start_no;

                  $total_amt=$rcount*$UNIT_RATE;

                  $total_amt=number_format($total_amt,2);

                 //$table=$table."<tr><td align='center'>$bill_date_time</td><td>$item_name</td><td align='right'>$UNIT_RATE</td><td align='right'>$start_no</td><td align='right'>$end_no</td><td align='right'>$rcount</td><td align='right'>$total_amt</td></tr>";
                       
				   if($item_name!="")
						{
				$mpdf->WriteHTML("<tr><td align='center'>$bill_date_time</td><td>$item_name</td><td align='right'>$UNIT_RATE</td><td align='right'>$start_no</td><td align='right'>$end_no</td><td align='right'>$rcount</td><td align='right'>$total_amt</td></tr>");
						}
					
					
					}

        $mpdf->WriteHTML("</table>");
					}
		
		}
           
           

//==============================================================
//==============================================================
//==============================================================


$mpdf->Output(); 

exit;
//==============================================================
//==============================================================
//==============================================================


?>