<?php

        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)";

			

				$getdetails=mysql_query("SELECT count(*) FROM `bill_details` as bd,machine_master as ms where DATE(bd.BILL_DATE_TIME) between '$fdate' and '$tdate' and bd.MACHINE_ID in(select cm.MACHINE_ID from cmp_machine as cm where cm.cmp_id='$cname') and bd.MACHINE_ID=ms.MACHINE_ID order by ms.mid");
		}
		else
		{
				
				$icondn=" where MACHINE_ID='$MACHINE_ID' and DATE(BILL_DATE_TIME) between '$fdate' and '$tdate'";

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

		}	


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

		}
		else
		{
		
     if($MACHINE_ID=="All")
		{
			$getdetails_code=mysql_query(" SELECT distinct(bd.machine_id) FROM `bill_details` as bd,machine_master as ms where DATE(bd.BILL_DATE_TIME) between '$fdate' and '$tdate' and bd.MACHINE_ID in(select cm.MACHINE_ID from cmp_machine as cm where cm.cmp_id='$cname') and bd.MACHINE_ID=ms.MACHINE_ID order by ms.mid");
		}
		else
			{
				$getdetails_code=$db->select(RBI,"distinct(machine_id)",$icondn);
			}


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

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

			$cmp_name=$cmp_fetch["company_name"];
			
			
			
			$table="<table style='width:100%;'><tr><td align='center'><b><span>$cmp_name,MADURAI</span> </b></td></tr><tr><td align='center'><b><span>COMPANY WISE REPORT</span></b></td></tr><tr><td align='center'><b><span>$exp[1] To  $exp[2]</span></b></td></tr></table><br />";            
			
			//$table=$table.="<table width='100%' cellpadding='4' cellspacing='4'>";

           $over_total="0";

            while($getdetails_fetch=$db->fetch($getdetails_code))
            {
   
                    $MACHINE_CODE=$getdetails_fetch[0];

     					$cmp_select=$db->select(HD,"company_name"," where MACHINE_ID='$MACHINE_CODE'");

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

                    $getitem_name=$db->select(MD,"*"," where machine_id='$MACHINE_CODE' and MACHINE_ID='$MACHINE_CODE'");

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

                    $SOFTWARE_ID=$getitem_name_fetch["softwareid"];

                    //$UNIT_RATE=number_format($getitem_name_fetch["UNIT_RATE"],2);

                    //$table=$table."<tr><th align='left'>$SOFTWARE_ID</th></tr><tr><td><table width='900px;' cellpadding='4' cellspacing='4' border=1 style='border:1px solid black;border-collapse:collapse;'><tr><th width='30%'>டிக்கெட் பெயர்</th><th width='10%'>விலை</th><th width='15%'>ஆரம்ப எண்</th><th width='15%'>முடிவு எண்</th><th width='10%'>எண்ணிக்கை</th><th width='20%'>மொத்தம்</th></tr>";

      			 $table=$table."<table width='100%' cellpadding='4' cellspacing='4' ><tr><th align='left'>$cmp_fetch[0] - $SOFTWARE_ID</th></tr><tr><td></td></tr></table><table id='$field_display_table' width='800px;'  border=0 class='price1' cellpadding='8' cellspacing='6' style='border-collapse:collapse;'>
											<col style='width: 30%' class='col1'>
											<col style='width: 10%'>
											<col style='width: 15%'>
											<col style='width: 15%'>
											<col style='width: 10%'>	
											<col style='width: 20%'>	
											<col style='width: 20%'>	
										  <thead>			
												<tr>
																									
													<th align='center' class='pdftbl' style='text-align:left;'>டிக்கெட் பெயர்</th>

													<th align='center' class='pdftbl' style='text-align:right;'>விலை</th>

													<th align='center' class='pdftbl' style='text-align:right;'>ஆரம்ப எண்</th>

													<th align='center' class='pdftbl' style='text-align:right;'>முடிவு எண்</th>
													
													<th align='center' class='pdftbl' style='text-align:right;'>எண்ணிக்கை</th>

													<th align='center' class='pdftbl' style='text-align:right;'>மொத்தம்</th>
																<th align='center' class='pdftbl' style='text-align:right;'></th>
													</thead>";

      


					
				$micondn=" where MACHINE_ID='$MACHINE_CODE' and DATE(BILL_DATE_TIME) between '$fdate' and '$tdate' and ITEM_CODE in(select ITEM_CODE from item_details) order by item_code,START_BILL_NUMBER";
            
               
					
					$getminfo=$db->select(RBI,"distinct(ITEM_CODE)",$micondn);


					

                    $grand_total=0;
                   
                    $gtotal="";
					
					while($getminfo_fetch=$db->fetch($getminfo))
                    {

                            $ITEM_CODE=$getminfo_fetch[0];

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

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

							$item_name=$getitem_info_fetch["item_name_oth"];							
							
							$UNIT_RATE=$getitem_info_fetch["unit_rate"];	
							
							
							$getcondn=" where MACHINE_ID='$MACHINE_CODE' and item_code='$ITEM_CODE' and DATE(BILL_DATE_TIME) between '$fdate' and '$tdate'";

                           

                           
                            $getcount_info=$db->select(RBI,"min(`START_BILL_NUMBER`),max(`END_BILL_NUMBER`)",$getcondn);

                            $getcount_info_fetch=$db->fetch($getcount_info);

                            $start_no=$getcount_info_fetch[0];

                            $end_no=$getcount_info_fetch[1];

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

                            $total_amt=$rcount*$UNIT_RATE;

                            $total_amt=number_format($total_amt,2);

                            $table=$table."<tr><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>";
                       
                            $grand_total=$grand_total+$total_amt;

							$total_amt1=$rcount*$UNIT_RATE;
							
							$gtotal=$gtotal+$total_amt1;

							 $over_total=$over_total+$total_amt1;
					
					
					}

                   

                    $grand_total=number_format($grand_total,2);
                       
                    $gtotal=number_format($gtotal,2);
					
					$table=$table."<tr><td colspan='6' align='right'>மொத்தம்</td><td align='right'>$gtotal</td></tr></table><br><!--</td></tr>-->";

                  
   
            }

			

				$over_total=number_format($over_total,2);

                $table=$table."<table width='100%' cellpadding='4' cellspacing='4' ><tr><td colspan='2' align='right'><b>Total :  $over_total</b></td></tr><tr><td colspan='2' align='center'><p align='center'><!--<img onclick='machinewise_pdf();' style='cursor:pointer;' src='images/print3.png'>--></p></td></tr></table>";

               // echo $table;

        }
           
           


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


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);

$mpdf->Output(); 

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


?>