<?php

include_once ("../include/dbglobal.inc.php");
include_once ("../include/ranking.inc.php");     // Checks ranking to be lost or not

$today = getdate();
$start_time = date("Y-m-d H:i:s", mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year']));

//list ($zipcode, $jb_id, $hq_id_string) = getHttpVars(array("zipcode", "jb_id", "hq_id_string"));
$zipcode = getFieldValueFromId("address", "ad_id", getFieldValueFromClause("tour", "ad_id", "jb_id = '" . $jb_id . "' AND tr_sort = '1'"), "ad_zipcode");
$hq_id_string = $hq_id;
include("../jobs/job_courier.inc.php");
$vht_id_str_js = str_replace("var vht_id_str=new Array();\n", "", $vht_id_str_js);
//$js_args = "";
//if (isset($jb_id) && $jb_id != "")
//  $js_args = $jb_id . ", '" . $zipcode . "'" . ", '" . $hq_id_string . "'";

$companyIds = array();
$sqlquery =
  "SELECT cmp.cmp_id, cr.cr_sid"
  . " FROM company AS cmp, courier AS cr"
  . " WHERE "
  . "   cmp.cmp_id = cr.cmp_id AND"
  . "   cr.hq_id = '" . $hq_id . "' AND"
  . "   cr.cr_sid != '' AND NOT (cr.cr_sid IS NULL)";
$res = $db->query($sqlquery);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery'" . $res->getMessage());
while ($row = $res->fetch_assoc()):
  $companyIds[$row["cr_sid"]] = "companyId=" . ec($row["cmp_id"]);
endwhile;
$res->free();

$vhtIds = array();
$sqlquery =
  "SELECT mt_sort, mt_value FROM `metatype` WHERE mt_type = 'vehicletype'";
$res = $db->query($sqlquery);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery'" . $res->getMessage());
while ($row = $res->fetch_assoc()):
  $vhtIds[$row["mt_sort"]] = $row["mt_value"];
endwhile;
$res->free();

//$current_jobs = array();
//$sqlQuery = "SELECT jb_id, jb_ordertime, cr_sid FROM job WHERE jb_status = 1 ORDER BY jb_ordertime ASC";
//$res = $db->query($sqlQuery);
//if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlQuery': " . $res->getMessage());
//while ($row = $res->fetch_assoc()):
//  $current_jobs[$row["cr_sid"]][] = array($row["jb_id"], $row["jb_ordertime"]);
//endwhile;
//$res->free();

function formDate($sqlDate) {
	global $today;

  if ($today['mon'] == substr($sqlDate, 5, 2)
	  && $today['mday'] == substr($sqlDate, 8, 2)
	  && $today['year'] == substr($sqlDate, 0, 4)) {
    return substr($sqlDate, 11, 5) . " Uhr";
  }
  return substr($sqlDate, 11, 5) . " Uhr, " . substr($sqlDate, 8, 2) . "." . substr($sqlDate, 5, 2) . ".";
}

function mk_text($cr_locationzipcode, $cr_availabletime, $cr_gps_time, $cr_gps_type, $cr_available, $cr_sid, $vht_id, $cr_mobile_pda, $usr_phone, $usr_phone2, $jb_id) {
  global $db, $companyIds, $current_jobs, $customer_special, $courier_special, $vhtIds, $cs_id, $usr_id;

  $vhtType = $vhtIds[$vht_id];
  $cr_gps_type_text = "";
  if ($cr_gps_type == 0) {
    $cr_gps_type_text = "unbestimmt";
	} elseif ($cr_gps_type == 1) {
    $cr_gps_type_text = "LBS";
	} elseif ($cr_gps_type == 2) {
    $cr_gps_type_text = "GPS";
	} elseif ($cr_gps_type == 3) {
    $cr_gps_type_text = "Network";
	} elseif ($cr_gps_type == 9) {
    $cr_gps_type_text = "Ortung aus";
	}

  $detailLink = "";
  if (isset($current_jobs[$cr_sid]))
    foreach ($current_jobs[$cr_sid] AS $jobA) {
      $detailLink .= ($customer_special
        ? ""
        : "<a href=\"javascript:popupWindow(\'../admin/jb_detail.php?job_id=" . $jobA[0] . "\',\'Auftrag\',\'\');\">" . $jobA[0] . " (" . formDate($jobA[1]) . ")</a><br>\\n");
      $jb_tourdataA = explode("|", $jobA[4]);
      $plzA = explode(";", $jb_tourdataA[0]);
      $pickedUp1 = "";
      $pickedUp2 = "";
      if ($jobA[5] == 1):
        $pickedUp1 = "<span style=\"color:red\">(</span>";
        $pickedUp2 = "<span style=\"color:red\">)</span>";
      endif;
      $detailLink .= $pickedUp1 . $plzA[0] . $pickedUp2 . "&nbsp;-&gt;&nbsp;" . ((count($plzA) > 2) ? "...&nbsp;-&gt;&nbsp;" : "") . $plzA[count($plzA) - 1] . "<br>\\n";   
    }
  if ($detailLink != "")
    $detailLink .= "<br>";
  $cr_sid_link = "";
  if ($jb_id != "") {
	$jb_offer = $db->getOne("SELECT jb_offer FROM job WHERE jb_id = " . $jb_id);
	if ($jb_offer == 0) {
      $cr_sid_link = ($customer_special ? "" : "<center><b><a href=\"javascript:setCourier(\'" . $jb_id . "\',\'" . $cr_sid . "\');\">" . "[Auftrag zuweisen]" . "</a></b></center>");
    } else {
      $cr_sid_link = ($customer_special ? "" : "<center><b>" . "[Auftrag ist ein Angebot]" . "</b></center>");
	}
  }

  return
   "'" .
   "<center><b>" .
   ($customer_special ? "" : "<a href=\"../admin/courier_special.php?" . $companyIds[$cr_sid] . "\" target=\"_blank\">") .
    ereg_replace("[^[:digit:]+]", "", $cr_sid) . " ($vhtType)<\/a></b>" . 
   "<br>" . ($customer_special ? "Tel.: " . (trim($usr_phone) != "" ? $usr_phone : $usr_phone2) : $cr_mobile_pda) . 
   "<br>" . $cr_sid_link .
   "<br>" . $detailLink . "\\n" .
   ($customer_special ? "" : "<i>Freimeldung:</i> " . $cr_locationzipcode . "<br>" . formDate($cr_availabletime) . "<br>\\n" ) .
   "<i>Letzte Ortung:</i> " . formDate($cr_gps_time) . " (" . $cr_gps_type_text . ")" .
   ($customer_special ? "" :  "<br>" .
   ($cr_available == 1
     ? "<a href=\"../locating/locating.php?f_cr_sid=" . $cr_sid . "\" target=\"_blank\">(Neue Ortung)</a></center>"
     : "<span style=\"color:red\">(nicht online)</span>")) .
   "'";
}

?>