<?php

		include "common.php";

		include "mysql.php";

      extract($_REQUEST);

        //echo $val;

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

        $itemid=$exp[0];

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

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

       if($itemid=="All")
        {

                //$icondn=" where BILL_DATE_TIME between '$fdate' and '$tdate' and ITEM_CODE in(select ITEM_CODE from item_info)";

				//$icondn=" where BILL_DATE_TIME between '$fdate' and '$tdate'";

					
				$icondn=" where (BILL_DATE_TIME between '$fdate' and '$tdate') and ITEM_CODE in(select ITEM_CODE from item_details)";
        
        
		
		}
        else
        {
               
                $icondn=" where ITEM_CODE='$itemid' and BILL_DATE_TIME between '$fdate' and '$tdate'";

        }   


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

        //$getdetails_rows=$db->rows($getdetails);

      	//echo "$icondn@@".$getdetails_rows."@@";
		
		$getdetails_rows=$db->fetch($getdetails);

		
		
		if($getdetails_rows[0]==0)
        {
   
                echo 0;

        }
        else
        {
       
            $getdetails_code=$db->select(RBI,"distinct(ITEM_CODE)",$icondn);

			$table="<table style='width:100%;'><tr><td align='center'><b><span>ARULMIGU MEENAKSHI  SUNDARESHWARAR THIRUKOVIL,MADURAI</span> </b></td></tr><tr><td align='center'><b><span>ITEMWISE CONSOLIDATED DATEWISE 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))
            {
   

                     $code=$getdetails_fetch[0];

                    
					$getitem_name=$db->select(ITI,"*"," where ITEM_CODE='$code'");

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

                    $item_code=$getitem_name_fetch["item_name_oth"];

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

                   
					//echo $code."@@@".$item_code."@@@".$getitem_name_fetch["unit_rate"]."^^^";

                    $table=$table."<tr><th align='left'>$item_code - <span>Rs.</span>$UNIT_RATE</th></tr><tr><td colspan='2'><table width='800px;' cellpadding='4' cellspacing='4' border=1 style='border:1px solid black;border-collapse:collapse;'><tr><th width='20%' >இயந்திர எண்</th><th width='20%'>ஆரம்ப எண்</th><th width='20%'>முடிவு எண்</th><th width='20%'>எண்ணிக்கை</th><th width='20%'>மொத்தம்</th></tr>";

                  $micondn=" where item_code='$code' and BILL_DATE_TIME between '$fdate' and '$tdate' order by START_BILL_NUMBER";
            
                    $getminfo=$db->select(RBI,"distinct(machine_id)",$micondn);

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

                       $machine_id=$getminfo_fetch[0];

                            $getcondn=" where  machine_id='$machine_id' and item_code='$code' and BILL_DATE_TIME between '$fdate' and '$tdate'";

							//echo $machine_id."<br>";
							
							$getdoft_name=$db->select(MD,"*"," where machine_id='$machine_id'");

							$getdoft_name_fetch=$db->fetch($getdoft_name);

							$SOFTWARE_ID=$getdoft_name_fetch["softwareid"];
							
                           
                            $getcount_info=$db->select(RBI,"min(`START_BILL_NUMBER`),max(`START_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>$SOFTWARE_ID</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;
					
							$i++;
					}

                   

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

                  
   
            }

				$over_total=number_format($over_total,2);

                $table=$table."<tr><td colspan='2' align='right'><b>Total :  $over_total</b></td></tr><tr><td colspan='2' align='center'><p align='center'><!--<img onclick='itemwise_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->WriteHTML($table);

$mpdf->Output(); 

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


?>