<?php
/*=======================================================================
 *
 * job_courier.inc.php
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/
$phpVersion = substr(phpversion(), 0, 3);

// Sortierfunktion für Arrays aus ranking.inc.php für usort-Sortierung
function cmp_usort ($a, $b) {
//global $jb_longhaul;

//  if (isset($a[12]) && isset($b[12]) && $jb_longhaul == 1) {
//    return ($a[12] < $b[12]) ? -1 : 1;
//  } else {
    // erst PLZ-Vergleich
    if (trim($a[4]) == trim($b[4])):
      // dann Uhrzeit-Vergleich
      if (trim($a[5]) == trim($b[5])):
        return 0;
      else:
        return (trim($a[5]) < trim($b[5])) ? -1 : 1;
      endif;
    endif;
    return (trim($a[4]) < trim($b[4])) ? -1 : 1;
//  }
}

// Vergleich zweier Arrays aus ranking.inc.php anhand der cr_ids
function my_array_diff($a, $b)
{
  $ret_arr = array();
  $found = false;

  foreach($a as $a_val) {
    $found = false;
    foreach($b as $b_val) {
      if (trim($a_val[1]) == trim($b_val[1])):
        $found = true;
        break;
      endif;
    }
    if (!$found)
      $ret_arr[] = $a_val;
  }
  return($ret_arr);
}

//function cmp_udiff ($a, $b) {
//  // cr_sid-Vergleich
//  if (trim($a[1]) == trim($b[1])):
//    return 0;
//  endif;
//  return (trim($a[1]) < trim($b[1])) ? -1 : 1;
//}

// Die Liste der favorisierten und geblockten Kuriere liefert leider nur cr_ids zurück,
// deshalb muss die vollständige Liste hier erstellt werden
function get_complete_courier_data($cr_id_order_list_temp)
{
  global $db, $hq_id, $phpVersion;

  $sqlInClause = "";
  if ($cr_id_order_list_temp == "" || count($cr_id_order_list_temp) == 0):
    $cr_id_order_list_temp = Array();
  else:
    $sqlInClause = " cr.cr_id IN (" . implode(",", $cr_id_order_list_temp) . ") AND";
    $sqlQuery =
      "SELECT cr.cr_id, cr.cr_sid, cr.cr_eid, mt.mt_value, cr.cr_locationzipcode, cr.cr_availabletime, REPLACE(REPLACE(CONCAT(crvh.crvh_filter, ',', cr.cr_filter), ',,,', ',,'), ',,', ',') AS all_filter, cr.cr_occupied"
      . " FROM courier AS cr, couriervehicle AS crvh, metatype AS mt, company AS cmp"
      . " WHERE " . $sqlInClause
      . "       cr.cr_sid != ''                               AND"
      . "       cr.cmp_id = cmp.cmp_id                        AND"
      . "       cr.cr_sid = crvh.crvh_sid                     AND"
      . "       crvh.vht_id = mt.mt_sort                      AND"
      . "       mt.mt_type = 'vehicletype'                       "
      . " ORDER BY cr.cr_available ASC, cr.cr_locationzipcode ASC, cr.cr_availabletime ASC";
    $res = $db->query($sqlQuery);
    $cr_id_order_list_temp = array();
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlQuery'" . $res->getMessage());
    while ($row = ($phpVersion >= "8.0" ? $res->fetch_assoc() : $res->fetchRow())):
      $cr_id_order_list_temp[] = array(
        $row["cr_id"],
        $row["cr_sid"],
        $row["cr_eid"],
        $row["mt_value"],
        $row["cr_locationzipcode"],
        $row["cr_availabletime"],
        $row["all_filter"],
        $row["cr_occupied"],
      );
    endwhile;
    $res->free();
  endif;
  return $cr_id_order_list_temp;
}

//function get_longhaul_courier_data($cr_id_order_list_temp, $ad_zipcode, $ad_city, $ad_street, $tr_hsno) {
//  global $db;
//
//  writeLog_("../log/job_courier_", "get_longhaul_courier_data: count(\$cr_id_order_list_temp) = " . count($cr_id_order_list_temp) . ", " . var_export($cr_id_order_list_temp, true));
//  if ($cr_id_order_list_temp == "" || count($cr_id_order_list_temp) == 0):
//    $cr_id_order_list_temp = Array();
//  else:
//    for($i = 0; $i < count($cr_id_order_list_temp); $i++) {
//      list($cr_gps_long, $cr_gps_lat) =
//        getFieldsValueFromId("courier", "cr_id", $cr_id_order_list_temp[$i][0], array("cr_gps_long", "cr_gps_lat"));
//      // $ad_country ist noch nicht sauber!
//      $dest_coords = get_coords("", $ad_zipcode, $ad_city, $ad_street, $tr_hsno);
//      $starting_sec = get_distance_google(array($cr_gps_lat, $cr_gps_long), $dest_coords, $force_date = '0000-00-00 00:00:00', "km", true);
//      $starting_min = round($starting_sec / 60);
//      $starting_m = get_distance_google(array($cr_gps_lat, $cr_gps_long), $dest_coords, $force_date = '0000-00-00 00:00:00', "km");
////      $starting_km = round($starting_m / 1000);
//      if (!isset($cr_id_order_list_temp[$i][8]))
//        $cr_id_order_list_temp[$i][8] = 0;
//      $cr_id_order_list_temp[$i][9] = $cr_gps_lat;
//      $cr_id_order_list_temp[$i][10] = $cr_gps_long;
////      $cr_id_order_list_temp[$i][11] = $dest_coords[0];
////      $cr_id_order_list_temp[$i][12] = $dest_coords[1];
//      $cr_id_order_list_temp[$i][11] = $starting_min;
//      $cr_id_order_list_temp[$i][12] = $starting_m;
//    }
//  endif;
//  writeLog_("../log/job_courier_", "get_longhaul_courier_data finished");
//  return $cr_id_order_list_temp;
//}

?>
