<?php
header('Content-Type: application/json');
  include("dbConfig.php");
  include("dbConfig_old.php");

    $employ_id = $_GET["employ_id"];
    $date_from = $_GET["date_from"];
    $date_to = $_GET["date_to"];
    $month = date("m", strtotime($date_from));
$year = date("Y", strtotime($date_from));

   $sql_e="SELECT * FROM hr_employ_tbl where employ_id='$employ_id' ";
   $result_e = $conn->query($sql_e);
   if ($result_e->num_rows > 0) {
   while($row_e = $result_e->fetch_assoc()) {   
         $employ_id=$row_e['employ_id'];
         $dept_id=$row_e['dept_id'];
         $job_id=$row_e['job_id'];

    $employ_name_ar = $row_e['employ_name_ar']; 
    $employ_name_en = $row_e['employ_name_en'];
    $employ_email = $row_e['employ_email'];
    $mobile_number = $row_e['mobile_number'];
    $id_num = $row_e['id_num'];
 
    $employ_target = $row_e['employ_target']; 
    $last_date_commencement = $row_e['last_date_commencement'];
  
    //-----------------------------------------------------
    $basic_salary = $row_e['basic_salary']; 
    $housing_allowance = $row_e['housing_allowance'];
    $transportation_allowance = $row_e['transportation_allowance'];
    $other_allowances = $row_e['other_allowances'];
    $other_allowances_tow = $row_e['other_allowances_tow'];

    $sum_sal_employ=$basic_salary+$housing_allowance+$transportation_allowance+$other_allowances+$other_allowances_tow;
    
    $allowances=$housing_allowance+$transportation_allowance+$other_allowances+$other_allowances_tow;  

    $holiday_dates = array();
     date_default_timezone_set("Asia/Riyadh");
     $dd1=strtotime($date_from);
     $dd2=strtotime($date_to);
   $datediff = $dd2 - $dd1;
   $add_ayam=round($datediff / (60 * 60 * 24)+1);
   //echo $add_ayam ;
   
$from_date = new DateTime($date_from);
$to_date = new DateTime($date_to);
$i = 0;
for ($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {
    $d = $date->format('l');
  if($d=="Friday"){
      $i++;
  }
}
 // صافي عدد الأيام
 
   $safy_ayam=$add_ayam-$i; 
   $target_matloop=$safy_ayam*$employ_target;
    

   $sql_p2="SELECT *,SUM(price),COUNT(did_id),SUM(driver_debit_amount),SUM(driver_credit_amount),SUM(bonus) FROM driver_saned_export_excel_tblll where driver_username ='$id_num' AND (dispatch_time_tow between '$date_from' AND '$date_to')AND (subscriber='The Chefz')  GROUP BY driver_username ";
   $result_p2 = $conn2->query($sql_p2);
   if ($result_p2->num_rows > 0) {
   while($row_p2 = $result_p2->fetch_assoc()) {
   $chfz_count_order=$row_p2["COUNT(did_id)"];
   $chefz_debit=$row_p2["SUM(driver_debit_amount)"]; 
   $chefz_credit=$row_p2["SUM(driver_credit_amount)"]; 
   $chefz_bonus=$row_p2["SUM(bonus)"];   
   $chfz_price=$row_p2["SUM(price)"];
   $chfz_pricee=$chfz_price/(1.15);
   $chfz_priceee3=round($chfz_pricee,2); 

   }}

   //-----------------------Jahez------------------------------------------------------------------
  
   $sql_p1="SELECT *,SUM(price),COUNT(did_id),SUM(driver_debit_amount),SUM(driver_credit_amount),SUM(bonus) FROM driver_saned_export_excel_tblll where driver_username ='$id_num' AND (dispatch_time_tow between '$date_from' AND '$date_to')AND (subscriber='Jahez APP'OR subscriber='PiK' or subscriber='blu')  GROUP BY driver_username ";

   $result_p1 = $conn2->query($sql_p1);
   if ($result_p1->num_rows > 0) {
   while($row_p1 = $result_p1->fetch_assoc()) {
   $jahez_count_order=$row_p1["COUNT(did_id)"];
   
   
       
   $jahez_price=round($row_p1["SUM(price)"],2);
   $jahez_debit=$row_p1["SUM(driver_debit_amount)"]; 
   $jahez_credit=$row_p1["SUM(driver_credit_amount)"]; 
   $jahez_bonus=$row_p1["SUM(bonus)"]; 
  
   }}
   
  //-----------------------------/Jahez---------------------------------------------------------
    
 $sum_all_debit=round($chefz_debit+$jahez_debit,2);
  $sum_all_credit=round($chefz_credit+$jahez_credit,2); 
  
  $sum_all_bouns=round($chefz_bonus+$jahez_bonus,2); 
 
  //-----------------------------hunger---------------------------------------------------------


$sql244="SELECT * FROM new_employ_tb where id_num='$id_num'  ";
       $result244 = $conn2->query($sql244);
       if ($result244->num_rows > 0) {
       while($row244 = $result244->fetch_assoc()) { 

$sum_raseed_all=0;
$summ_pp=0;
 $sqlhung="SELECT *,SUM(number_orders_delivered) FROM driver_hunger_orders_tbl where rider_id ='".$row244['rider_id']."' AND(report_date between '$date_from' AND '$date_to') AND report_date!='0000-00-00' GROUP BY rider_id";
  $resulthung = $conn2->query($sqlhung);
  if ($resulthung->num_rows > 0) {
    while($rowhung = $resulthung->fetch_assoc()) {

      $num_orders=$rowhung["SUM(number_orders_delivered)"];
      if($num_orders>=1 && $num_orders<=150){$p1=12; $summ_pp=$num_orders*$p1;
     //----------------------------------------------
      }elseif($num_orders>150 && $num_orders<=300){$p2=13;
      $num_orders2=$num_orders; 
      $summ_pp=($num_orders2*$p2); 

      //-------------------------------------------
      }elseif($num_orders>300 && $num_orders<=400){$p3=15;
      $num_orders3=$num_orders; 
     // $summ_pp12=(($num_orders-$num_orders3)*13.5);
      $summ_pp=($num_orders3*$p3); 
    
       //-------------------------------------------
      }elseif($num_orders>400 && $num_orders<=550){$p4=16;
      $num_orders4=$num_orders; 
     // $summ_pp13=(($num_orders-$num_orders4)*15.5);
      $summ_pp=($num_orders4*$p4); 
    
      
         //-------------------------------------------
      }elseif($num_orders>550){$p5=16;
      $num_orders5=$num_orders; 
      //$summ_pp14=(($num_orders-$num_orders5)*16.5);
      $summ_pp=($num_orders5*$p5); 
       
      }else{ } 
 
    }}

  }}
  //-----------------------------/hunger---------------------------------------------------------
  $sum_target_package=0;
  $sum_count_package=0;
$sql_ch2="SELECT * FROM package_target_tbl where id_num='$id_num' AND month='$month'AND year='$year' AND deleted='0'";
$result_ch2 = $conn2->query($sql_ch2);
if ($result_ch2->num_rows > 0) {
while($row_ch2 = $result_ch2->fetch_assoc()) {

$sum_count_package=$sum_count_package;
$sum_target_package=round($row_ch2['sum_target_package'],2);  
                         
}}

//echo 'sum_target_package / '.$sum_target_package;

//-----------------------------/pakagetarget---------------------------------------------------------
$count_all_orders=round($chfz_count_order+$jahez_count_order+$num_orders,2);
$sum_all_target=round($chfz_priceee3+$jahez_price+$summ_pp+$sum_target_package,2); 
if($target_matloop!=0){
    $nesba_mohkak=round($sum_all_target/$target_matloop*100,2);
}else{
  $target_matloop=1; 
  $nesba_mohkak=round($sum_all_target/$target_matloop*100,2);
}

if($nesba_mohkak>100){$takeem_delivery='good';}else{$takeem_delivery='bad';}

$response = array(
  'success' => true,
  'target_matloop' => strval($target_matloop),
  'count_all_orders' => strval($count_all_orders),
  'delevery_price' => strval($sum_all_target),
  'sum_all_debit' => strval($sum_all_debit),
  'sum_all_bouns' => strval($sum_all_bouns),
  'sum_all_credit' => strval($sum_all_credit),
  'sum_all_target' => strval($sum_all_target),
  'nesba_mohkak' => strval($nesba_mohkak),
  'takeem_delivery' => $takeem_delivery,
  'chfz_count_order' => strval($chfz_count_order),
  'chfz_after_vat' => strval($chfz_priceee3),
  'jahez_count_order' => strval($jahez_count_order),
  'jahez_price' => strval($jahez_price),
  'hunger_count_order' => strval($num_orders),
  'hunger_price' => strval($summ_pp),
  'sum_target_package' => strval($sum_target_package),

);
}}else{
  $response = array(
    'success' => false
   );
}
echo json_encode($response);
       
?>

















