<?php
/*=======================================================================
 *
 * job_courier.inc.php
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/

// Sortierfunktion für Arrays aus ranking.inc.php für usort-Sortierung
function cmp_usort ($a, $b) {
  // 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;

  $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, crvh.crvh_filter, cr.cr_occupied"
      . " FROM courier AS cr, couriervehicle AS crvh, metatype AS mt, company AS cmp"
      . " WHERE " . $sqlInClause
      . "       cr.hq_id = '" . $hq_id . "'                   AND"
      . "       cr.cr_sid != ''                               AND"
      . "       cr.cmp_id = cmp.cmp_id                        AND"
      . "       cr.cr_id = crvh.cr_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 = $res->fetch_assoc()):
      $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["crvh_filter"],
        $row["cr_occupied"],
      );
    endwhile;
    $res->free();
  endif;
  return $cr_id_order_list_temp;
}

?>
