<?php
$reportDate=$_POST['date'];
include_once 'iconnect.php';
date_default_timezone_set("Africa/Nairobi");
$currdate=date("Y-m-d");
$report_date=  date_create($reportDate);
$format="Y-m-d";
$reportDate=  date_format($report_date, $format);
//$prevedateSQL="select max(data_date) as prevDate from pumpdata where data_date<'$reportDate'";
//$prevedateSQL="select DATE_ADD('$reportDate',INTERVAL 1 day) as prevDate";
//$previousDate=get_Qty($prevedateSQL,"prevDate");
//echo "Report Date: ".$reportDate. " Staff Name ".$staff_Name." Previous Date ".$previousDate;
  postTranslog("Reports Module","Viewed Staff Sales Analysis  Report for Date ".$reportDate);
 global $db;
 $staffpumpSQL="Select TankID,Tank_Name from tankstbl order by TankID asc";
  $pumplist=$db->query($staffpumpSQL);
$index=0;
  $toolbar=" <div id='custom-toolbar'>
 <div class='btn-group pull-right'>
 <button type='button' id='print-raw' class='btn btn-default'onclick='window.print();''> <span class='glyphicon glyphicon-print'></span> Print</button>
 </div>
 </div>";
  $tableStr=$toolbar."<table class='table table-condensed table-bordered' id='pumpdata'>
    <thead>
      <tr><th>#:</th><th>TANK NAME:</th><th class='text-right'>SALES QUANTITY:</th><th class='text-right'>EXPECTED SALES AMOUNT:</th></tr></thead><tbody>";
    $cashreadingSQL="Select Sum(data_eleccash) as data_eleccash  from pumpdata where data_date='$reportDate'";
    $cashReading=get_Qty($cashreadingSQL,"data_eleccash");
    $index=$index+1;
    // $newline="<tr><td>".$index."</td><td>FUEL CASH SALE:</td><td class='text-right'>".number_format($cashReading,2)."</td></tr/>";
    // $tableStr=$tableStr.$newline;
     $prevSQL="Select case when Max(data_date) is null then DATE_ADD('$reportDate',INTERVAL -1 day) else Max(data_date) end Prevdate from pumpdata where data_date<'$reportDate'";
     $prevDate=get_Qty($prevSQL,"Prevdate");
   if($pumplist->num_rows>0){
    // $index=0;
       while($itemrow=$pumplist->fetch_array(MYSQLI_ASSOC)){
         $index=$index+1;
         $tankID=$itemrow['TankID'];
         $tankName=$itemrow['Tank_Name'];
         $currentSQL="Select case when Sum(data_elecltrs) is null then 0 else Sum(data_elecltrs)  end Reading from pumpdata where data_Date='$reportDate' and data_tankname='$tankName'";
         $currentQty=get_Qty($currentSQL,"Reading");

         $prevReadingSQL="Select case when Sum(data_elecltrs) is null then 0 else Sum(data_elecltrs)  end Reading from pumpdata where data_Date='$prevDate' and data_tankname='$tankName'";
         $prevQty=get_Qty($prevReadingSQL,"Reading");
         $deliveredSQL="Select case when Sum(fuel_Qty) is null then 0 else Sum(fuel_Qty) end Delivered from fueldelivery where fuel_Date='$reportDate'";
         $deliveredQty=get_Qty($deliveredSQL,"Delivered");
         $newreading=$prevQty-($currentQty+$deliveredQty);
         $metercashSQL="Select case when Sum(data_eleccash) is null then 0 else Sum(data_eleccash)  end MeterCash from pumpdata where data_Date='$prevDate' and data_tankname='$tankName'";
         $meterQty=get_Qty($metercashSQL,"MeterCash");

         $newline="<tr><td>".$index."</td><td>".$tankName."</td><td class='text-right'>".number_format($newreading,2)."</td><td class='text-right'>".number_format($meterQty,2)."</td></tr/>";

     if($newline==''){
     }else{
     $tableStr=$tableStr.$newline;
     }

       }

   }else{

 }
 $nonCashSalesSQL="Select  Sum(sales_mpesa+sales_visa+sales_KCard+sales_Invoice) as NonCashSales from staff_sales where  sales_Date='$reportDate'";
    $nonCashSales=get_Qty($nonCashSalesSQL,"NonCashSales");
    $index=$index+1;
     $newline="<tr><td>".$index."</td><td>NON CASH SALE:</td><td class='text-right'>".number_format($nonCashSales,2)."</td></tr/>";
     $tableStr=$tableStr.$newline;
  $tableStr=$tableStr."</tbody></table>";
   echo $tableStr;
?>
