<?php
header('Content-Type: application/json');
  include("dbConfig.php");
    $employ_id=$_GET["employ_id"];
    $dept_id=0;
    $job_id=0;
   $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'];
  
      if($lang=='ar'){$employ_name=$row_e['employ_name_ar'];}else{$employ_name=$row_e['employ_name_en'];} 

    $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;  
 
     $today_date=date("Y-m-d");
    $date_from=$today_date;
    $date_to=$today_date;
    $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);
 
$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;
   //-----------------------chfz------------------------------------------------------------------
    include("dbConfig_old.php");

   $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);
   
   }}
   
  //-----------------------------/chfz---------------------------------------------------------
    
   
   //-----------------------Jahez------------------------------------------------------------------
    include("dbConfig_old.php");

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

include("dbConfig_old.php");

$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()) { 
        
        $rider_id= $row244['rider_id'];  
           
           

$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_date'AND year='$year_date' AND deleted='0'";
$result_ch2 = $conn2->query($sql_ch2);
if ($result_ch2->num_rows > 0) {
while($row_ch2 = $result_ch2->fetch_assoc()) {

$sum_target_package=round($row_ch2['sum_target_package'],2).'<br>';  
                        
}}

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

$nesba_mohkak=round($sum_all_target/$target_matloop*100,2);

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

}}

  //-----------------------------pakagetarget---------------------------------------------------------
  
 
?>
<?php 

//   $month_date='03';
  
//   $year_date='2024';
  
  
    $month_date=date("m");
  
  $year_date=date("Y");
  
  
  
   //-----------------------chfz_month------------------------------------------------------------------
    include("dbConfig_old.php");

   $sql_p2_mo="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 MONTH(dispatch_time_tow)='$month_date' AND YEAR(dispatch_time_tow)='$year_date'AND (subscriber='The Chefz')  GROUP BY driver_username ";
   $result_p2_mo = $conn2->query($sql_p2_mo);
   if ($result_p2_mo->num_rows > 0) {
   while($row_p2_mo = $result_p2_mo->fetch_assoc()) {
   $chfz_count_order=$row_p2_mo["COUNT(did_id)"];   // echo 'Chefz Count Orders /'.$chfz_count_order.'<br>';
   
   $chefz_debit=$row_p2_mo["SUM(driver_debit_amount)"]; 
   $chefz_credit=$row_p2_mo["SUM(driver_credit_amount)"]; 
   $chefz_bonus=$row_p2_mo["SUM(bonus)"]; 
   
       
   $chfz_price_mo=$row_p2_mo["SUM(price)"];   // echo '$chfz_price_mo  / '.$chfz_price_mo;   echo '<br>';
   $chfz_pricee_mo=$chfz_price_mo/(1.15);
 
   $chfz_priceee3_mo=round($chfz_pricee_mo,2);
   
   }}
   
  //-----------------------------/chfz_month---------------------------------------------------------


   //-----------------------Jahez_mo------------------------------------------------------------------

   $sql_p1_mo="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  MONTH(dispatch_time_tow)='$month_date' AND YEAR(dispatch_time_tow)='$year_date' AND (subscriber='Jahez APP'OR subscriber='PiK' or subscriber='blu')  GROUP BY driver_username ";

   $result_p1_mo = $conn2->query($sql_p1_mo);
   if ($result_p1_mo->num_rows > 0) {
   while($row_p1_mo = $result_p1_mo->fetch_assoc()) {
   $jahez_count_order=$row_p1_mo["COUNT(did_id)"];   // echo 'Jahez Count Orders /'.$jahez_count_order.'<br>';
   
   
       
   $jahez_price_mo=round($row_p1_mo["SUM(price)"],2);
   
    $jahez_debit=$row_p1_mo["SUM(driver_debit_amount)"]; 
   $jahez_credit=$row_p1_mo["SUM(driver_credit_amount)"]; 
   $jahez_bonus=$row_p1_mo["SUM(bonus)"]; 

   }}
   
  //-----------------------------/Jahez_mo---------------------------------------------------------


  //-----------------------------/Hunger_mo---------------------------------------------------------

$sum_raseed_all=0;
$summ_pp_month=0;
 $sqlhung="SELECT *,SUM(number_orders_delivered) FROM driver_hunger_orders_tbl where rider_id ='$rider_id' AND MONTH(report_date)='$month_date'AND YEAR(report_date)='$year_date' 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_month=$rowhung["SUM(number_orders_delivered)"];
      if($num_orders_month>=1 && $num_orders_month<=150){$p1=12; $summ_pp_month=$num_orders_month*$p1;
     //----------------------------------------------
      }elseif($num_orders_month>150 && $num_orders_month<=300){$p2=13;
      $num_orders2=$num_orders_month; 
      $summ_pp_month=($num_orders2*$p2); 
    
      
      //-------------------------------------------
      }elseif($num_orders_month>300 && $num_orders_month<=400){$p3=15;
      $num_orders3=$num_orders_month; 
     // $summ_pp12=(($num_orders-$num_orders3)*13.5);
      $summ_pp_month=($num_orders3*$p3); 
    
      
       //-------------------------------------------
      }elseif($num_orders_month>400 && $num_orders_month<=550){$p4=16;
      $num_orders4=$num_orders_month; 
     // $summ_pp13=(($num_orders-$num_orders4)*15.5);
      $summ_pp_month=($num_orders4*$p4); 
    
      
         //-------------------------------------------
      }elseif($num_orders_month>550){$p5=16;
      $num_orders5=$num_orders_month; 
      //$summ_pp14=(($num_orders-$num_orders5)*16.5);
      $summ_pp_month=($num_orders5*$p5); 
       
      }else{ } 
 
    }}

        $sum_target_package_month=0;
        $sum_count_package_month=0;
	  $sql_ch2="SELECT * FROM package_target_tbl where id_num='$id_num' AND month='$month_date'AND year='$year_date' AND deleted='0'";
      $result_ch2 = $conn2->query($sql_ch2);
      if ($result_ch2->num_rows > 0) {
      while($row_ch2 = $result_ch2->fetch_assoc()) {
    
    $sum_target_package_month=round($row_ch2['sum_target_package'],2);  
                               
   }}
  
  //-----------------------------/pakagetarget_month---------------------------------------------------------

  $summ_all_price_all_project_month=round($chfz_priceee3_mo+$jahez_price_mo+$summ_pp_month+$sum_target_package_month,2); 

  ?>
  
  
  <?php 
 
// تحديد التاريخ
$year = date('Y');
$month = date('n'); // الشهر الحالي


$year =$year_date;
$month = $month_date; 

// العدادات
$friday_count = 0;
$other_day_count = 0;

// حلقة لحساب أيام الجمعة وأيام الأخرى في الشهر
for ($day = 1; $day <= cal_days_in_month(CAL_GREGORIAN, $month, $year); $day++) {
    // إنشاء تاريخ ليتم فحصه
    $date = strtotime("$year-$month-$day");
    
    // التحقق مما إذا كان اليوم يوم جمعة أم لا
    if (date('l', $date) === 'Friday') {
        $friday_count++;
    } else {
        $other_day_count++;
    }
}

  $target_matloop_month=$other_day_count*$employ_target;
  $nesba_mohkak_month=round($summ_all_price_all_project_month/$target_matloop_month*100,2);

  $response = array(
    'success' => true,
    'count_all_orders' => strval($count_all_orders),
    '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),
    'nesba_mohkak_month' => strval($nesba_mohkak_month),
    'summ_all_price_all_project_month' => strval($summ_all_price_all_project_month),
    'sum_target_package_month_all' => strval($sum_target_package_month),
  
  );
  echo json_encode($response);
 ?>
 
 













