<?php
/*=======================================================================
 *
 * get_saved_tour.php
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/

include_once("../include/caglobal.inc.php");
include_once("../include/global.inc.php");
include_once("../include/auth.inc.php");
//$phpVersion = substr(phpversion(), 0, 3);
//if ($phpVersion >= "7.0"):
//  require_once("../PEAR/HTML/Template/IT.php");
//else:
//  require_once("HTML/IT.php");
//endif;

// Decision to use the archive or normal tables
list ($dbhistory) = getHttpVars(array("dbhistory"));
getDBNames($dbhistory);
if ($dbh_jb == "")
  $dbh_jb = "job";
if ($dbh_tr == "")
  $dbh_tr = "tour";

// Name der Tabellen
if (!(isset($_copy_jobs_flag) && $_copy_jobs_flag == true) || (isset($jb_storno) && $jb_storno > 0) ||
  (isset($_copy_jobs_flag_ori) && $_copy_jobs_flag_ori == true)):
  $_job_table = $dbh_jb;
  $_tour_table = $dbh_tr;
else:
  $_job_table = "job_backup";
  $_tour_table = "tour_backup";
endif;
// Job-ID des Auftrags ist Parameter
// checken, ob jb_id schon definiert (gesetzt, wenn von "check_jb_permanent.php" aufgerufen)
if (!(isset($check_jb_permanent_flag) && $check_jb_permanent_flag == true))
  list ($jb_id, $cr_sid_none, $consider_manual_disp, $jb_copy, $no_dates) = getHttpVars(array("jb_id", "cr_sid", "consider_manual_disp", "jb_copy", "no_dates"));
if ($jb_id == "") reportDie ("$PHP_SELF: Parameter 'jb_id' fehlt!", false);

// gespeicherte Job-Daten holen
list($csc_id, $vht_id, $jb_weight, $jb_crvh_length, $jb_crvh_width, $jb_crvh_height, $jb_crvh_position, $jb_ordertime, $jb_reserv, $jb_permanent, $vht_id,
  $cr_sid, $cr_id_order, $jb_waitstorno, $jb_waittime, $jb_fixprice, $jb_serviceprice, $jb_cr_price, $csc_id_payer, $csc_id_payer_cash,
  /*$jb_type,*/ $jb_globaljob, $jb_freetext_1, $jb_payment, $jb_permanent2,
  $jb_sales_tax_rate, $jb_sales_tax_rate_sign, $jp_permenddat, $jb_cr_filter, $jb_cr_filter_opt,
  $jb_locktime, $jb_lockuser, $jb_status, $cr_id_permanent, $jb_incomplete, $jb_export_time, $jb_dispoinfo, $jb_commission_no) = // $jb_status wird bei der nachträglichen Änderung einer Tour benötigt
  getFieldsValueFromId($_job_table, "jb_id", $jb_id,
    array("csc_id", "vht_id", "jb_weight", "jb_crvh_length", "jb_crvh_width", "jb_crvh_height", "jb_crvh_position", "jb_ordertime", "jb_reserv", "jb_permanent",
      "vht_id", "cr_sid", "cr_id_order", "jb_waitstorno", "jb_waittime", "jb_fixprice", "jb_serviceprice", "jb_cr_subprice", "csc_id_payer", "csc_id_payer_cash",
      /*"jb_type",*/ "jb_globaljob", "jb_freetext_1", "jb_payment", "jb_permanent2",
      "jb_sales_tax_rate", "jb_sales_tax_rate_sign", "jp_permenddat", "jb_cr_filter", "jb_cr_filter_opt",
      "jb_locktime", "jb_lockuser", "jb_status", "cr_id_permanent", "jb_incomplete", "jb_export_time", "jb_dispoinfo", "jb_commission_no"));
if (MASK_CR_PRICE_MODE == "1") {
  $jb_cr_serviceprice = $db->getOne("SELECT gdc_content FROM genericdatacontainer WHERE gdc_obj_type = 'jb' AND gdc_obj_id = $jb_id AND gdc_gen_fieldname = 'jb_cr_serviceprice'");
}
$jb_price_km = $db->getOne("SELECT jbprc_price FROM jobprice WHERE jb_id = " . $jb_id . " AND mt_sort = 15");
$job_is_exported = (trim($jb_export_time) != "" ? true : false);
$jb_job_jam = $db->getOne("SELECT gdc_content FROM genericdatacontainer WHERE gdc_obj_type = 'jb' AND gdc_obj_id = " . $jb_id . " AND gdc_gen_fieldname = 'jb_job_jam'");
$jb_jam_waittime = "";
if (trim($jb_job_jam) != ""):
  if ($jb_reserv == 1):
    $tmpCsIdPayer = getFieldValueFromId("costcenter", "csc_id", $csc_id_payer, "cs_id");
    $jb_jam_waittime = $db->getOne("SELECT cs_jb_jam_waittime_minutes from customer WHERE cs_id = " . $tmpCsIdPayer);
    if ($jb_jam_waittime == 0):
      $jb_jam_waittime = getParameterValue("0", "CS_JB_JAM_WAITTIME_AD_HOC_DEFAULT", "0"); endif;
  else:
    $jb_jam_waittime = (strtotime($jb_job_jam) - strtotime($jb_ordertime)) / 60;
  endif;
endif;

// Prüfen, ob Datensatz gesperrt bzw. neue Sperre setzen
if (!(isset($_copy_jobs_flag) && $_copy_jobs_flag == true)):
  $job_is_locked = false;
  $locktime = date("Y-m-d H:i:s");
  if ($jb_locktime != "" && $jb_locktime != "0000-00-00 00:00:00" && $jb_lockuser != $usr_id):
//echo "'$jb_lockuser' == '$usr_id'";
    // Datensatz ist gesperrt; überprüfen, ob Sperrzeit bereits abgelaufen
    $jb_locktime_arr = getValsFromDate($jb_locktime);
    $locktime_expired = date("Y-m-d H:i:s",
      mktime($jb_locktime_arr[3], $jb_locktime_arr[4] + MASK_LOCKTIME_TIMEOUT, $jb_locktime_arr[5], $jb_locktime_arr[1], $jb_locktime_arr[2], $jb_locktime_arr[0]));
    if ($locktime <= $locktime_expired):
      // Timeout noch nicht abgelaufen: Datensatz ist gesperrt
      $job_is_locked = true;
    endif;
  endif;
  if ($job_is_locked != true):
    // Datensatz ist noch nicht gespert, jetzt aber für anderweitige Benutzer sperren
    updateStmt("job", "jb_id", $jb_id, array("jb_locktime", $locktime, "jb_lockuser", $usr_id));
  endif;
endif;

if ($jb_weight == "") $jb_weight = "0";
if ($jb_payment == 0) $jb_payment = "";
if ($jb_fixprice == 0) $jb_fixprice = "";
if ($jb_serviceprice == 0) $jb_serviceprice = "";
if ($jb_cr_price == 0) $jb_cr_price = "";
//$jb_fixprice = str_replace(".00", "", $jb_fixprice);
$jb_fixprice = str_replace(".", ",", $jb_fixprice);
$jb_serviceprice = str_replace(".", ",", $jb_serviceprice);

if (!isset($csc_id_order) || $csc_id_order == 0) $csc_id_order = "";
if ($jb_waittime != "")
  list($jb_waittime_jahr, $jb_waittime_monat, $jb_waittime_tag,
    $jb_waittime_hour, $jb_waittime_minute) = getValsFromDate($jb_waittime);
else
  $jahr = $monat = $tag = $stunde = $minute = "";
if ($no_dates != true && ($jb_reserv == 1 || $jb_permanent != "")):
  list($jahr, $monat, $tag, $stunde, $minute) = getValsFromDate($jb_ordertime);
  list($endejahr, $endemonat, $endetag, $dummy1, $dummy2) = getValsFromDate($jp_permenddat);
else:
  $jahr = $monat = $tag = $stunde = $minute = "";
  $endejahr = $endemonat = $endetag = "";
endif;

// Zoneneinträge holen
$km_price_params = "";
$sqlquery = "SELECT trs_srv_name FROM tourservice WHERE trs_srvt_name = 'z' AND jb_id = " . $jb_id;
$res = $db->query($sqlquery);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
while ($row = $res->fetch_assoc()):
  $km_price_params .= ($km_price_params != "" ? " + " : "") . $row['trs_srv_name'];
endwhile;
$res->free();

// Km-Preis Parameter holen
$javascript_zone = "";
if ($km_price_params != "") {
  $tr_mediationarea_names = "";
  $sqlquery = "SELECT tr_mediationarea_name FROM tour WHERE jb_id = " . $jb_id;
  $res = $db->query($sqlquery);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
  while ($row = $res->fetch_assoc()):
    $tr_mediationarea_names .= ($tr_mediationarea_names != "" ? "," : "") . $row['tr_mediationarea_name'];
  endwhile;
  $res->free();
  $km_price_params = $jb_price_km . "|" . $km_price_params . "||zone|" . $tr_mediationarea_names;
  $javascript_zone = "opener.parent.job_options.fixprice_was_zone = true;\n";
  if (isset($no_dates) && $no_dates == "true") {
    $jb_fixprice = "";
    $jb_cr_price = "";
  }
} else {
  $km_price_params = $db->getOne("SELECT trs_srvt_name FROM tourservice WHERE trs_srv_name = 'Fixpreis' AND jb_id = " . $jb_id);
}
//$cr_sid = "";
//if ($cr_id_order != "")
//  $cr_sid = $db->getOne(
//    "SELECT crvh_sid FROM courier_vehicle WHERE cr_id = '$cr_id_order'");
////    "SELECT crvh_sid FROM courier_vehicle WHERE cr_id = '$cr_id_order' AND hq_id = '$hq_id'");
if ((getParameterValue($emp_id, "MODE_COPY_JOB")) == "1" && $cr_sid != "0")
  $cr_sid_none = $cr_sid;

// Javascript-Funktionen im temporären Fenster ändern die Werte im Eingabeformular
// Job-Tour Frame
$jb_sales_tax_rate_sign = $db->getOne("SELECT tx_id FROM tax WHERE tx_sign = '$jb_sales_tax_rate_sign'");
$javascript = setJavascriptOpenerFormVals("job_options", "tourOptions",
  array("cr_id_order" => ($cr_sid_none == "none" ? "" : $cr_sid), "vht_id" => $vht_id, "jb_weight" => $jb_weight,
    "jb_crvh_length" => $jb_crvh_length, "jb_crvh_width" => $jb_crvh_width, "jb_crvh_height" => $jb_crvh_height, "jb_crvh_position" => $jb_crvh_position,
    "jb_permanent" => $jb_permanent, "savedTour" => "", "jb_fixprice" => $jb_fixprice,
    "jb_serviceprice" => $jb_serviceprice, "jb_cr_price" => $jb_cr_price,
    "jb_tourname_hidden" => getFieldValueFromId($_job_table, "jb_id", $jb_id, "jb_tourname"),
    "jb_tourname_hidden_vht_id" => $vht_id,
    "jb_freetext_1" => my_str_check($jb_freetext_1), "jb_cr_filter" => $jb_cr_filter,
    "jb_dispoinfo" => my_str_check($jb_dispoinfo),
    "jb_cr_filter_opt" => $jb_cr_filter_opt, "jb_permanent2_hidden" => $jb_permanent2,
    "jb_sales_tax_rate" => $jb_sales_tax_rate, "jb_sales_tax_rate_sign" => $jb_sales_tax_rate_sign,
    "km_price_params" => $km_price_params
    ));

if (MASK_CR_PRICE_MODE == "1") {
  $javascript .= setJavascriptOpenerFormVals("job_options", "tourOptions", array("jb_cr_serviceprice" => $jb_cr_serviceprice));
}
$javascript .=
  "if (typeof(opener.parent.job_options.document.tourOptions.jb_permanent) != 'undefined') {\n" .
  "  opener.checkJb_permanent();\n" .
  "  opener.setDateTimeFields(\"job_options\", \"tourOptions\", \"$tag\", \"$monat\", \"$jahr\", \"$stunde\", \"$minute\", \"tag\", \"monat\", \"jahr\", \"stunde\", \"minute\", \"jb_permanent\");\n" .
  "} else {\n" .
  "  opener.setDateTimeFields(\"job_options\", \"tourOptions\", \"$tag\", \"$monat\", \"$jahr\", \"$stunde\", \"$minute\", \"tag\", \"monat\", \"jahr\", \"stunde\", \"minute\", \"\");\n" .
  "}\n" .
  "if (typeof(opener.parent.job_options.document.tourOptions.endetag) != 'undefined')\n" .
  "  opener.setDateTimeFields(\"job_options\", \"tourOptions\", \"$endetag\", \"$endemonat\", \"$endejahr\", \"\", \"\", \"endetag\", \"endemonat\", \"endejahr\", \"\", \"\", \"\");\n" .
  "if(typeof(opener.parent.job_tour.document.tourForm.jb_freetextButton) != 'undefined') {" .
  "  if (\"" . my_str_check($jb_freetext_1) . "\" == \"\")\n" .
  "    opener.parent.job_tour.document.tourForm.jb_freetextButton.value = \"Text\";\n" .
  "  else\n" .
  "    opener.parent.job_tour.document.tourForm.jb_freetextButton.value = \"Text*\"\n;" .
  "}";
if (MASK_WAITTIME_ENABLED == "1"):
  $javascript .= setJavascriptOpenerFormVals("job_options", "tourOptions",
    array(
      "jb_waittime_hour" => $jb_waittime_hour, "jb_waittime_minute" => $jb_waittime_minute
     ));
endif;
$javascript .= $javascript_zone;

// Job-Options Frame initialisieren
$javascript .=
  setJavascriptOpenerDisabled("job_options", "tourOptions", "jb_tourname_overwrite",
    ((trim(getFieldValueFromId($_job_table, "jb_id", $jb_id, "jb_tourname")) != "") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_waitstorno",
    (($jb_waitstorno != "1") ? "false" : "true")) .
  setJavascriptOpenerDisabled("job_options", "tourOptions", "jb_permanent",
    (($jb_reserv == 1 || $jb_permanent != "") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_0",
    ((substr($jb_permanent2, 0, 1) != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_1",
    ((substr($jb_permanent2, 1, 1) != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_2",
    ((substr($jb_permanent2, 2, 1) != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_3",
    ((substr($jb_permanent2, 3, 1) != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_4",
    ((substr($jb_permanent2, 4, 1) != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_5",
    ((substr($jb_permanent2, 5, 1) != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_permanent2_6",
    ((substr($jb_permanent2, 6, 1) != "1") ? "false" : "true")) .
//  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_type",
//    (($jb_type != "1") ? "false" : "true")) .
  setJavascriptOpenerChecked("job_options", "tourOptions", "jb_globaljob",
    (($jb_globaljob != "1") ? "false" : "true")) .
  "if (typeof(opener.parent.job_tour.document.tourForm.jb_permanent) != 'undefined') \n" .
  "  opener.checkJb_permanent2();\n" .
  "opener.initJobList();\n" .
  "if (typeof(opener.parent.job_options.document.tourOptions.cr_id_order) != 'undefined') \n" .
  "  opener.checkCr_id_order();\n";

$i = 1;
$payer_found = false;
do {
  // gespeicherte Tour-Daten holen
  if (existsEntry($_tour_table, array("jb_id", $jb_id, "tr_sort", $i))):
    $tourFound = 1;
    list($csc_id, $tr_comp, $tr_person, $ad_street, $tr_hsno,
      $ad_zipcode, $ad_city, $ad_country, $tr_remark, $tr_status, $tr_ware_from_to, $tr_commission_no, $tr_mediationarea_id, $tr_status, $tr_sign, $tr_signname, $tr_finishtime, $tr_id) =
      getTourData($jb_id, $i, $dbh_tr);
    // 17.02.2026: Bei Konserven soll die erste Ko.-Nr. als $jb_commission_no übernommen werden wenn vorhanden und letztere leer
    if ((isset($no_dates) && $no_dates == "true") && $jb_commission_no == "" && my_str_check($tr_commission_no) != "")
      $jb_commission_no = my_str_check($tr_commission_no);
    $tr_tracking = "";
    $gdc_content = $db->getOne("SELECT gdc_content FROM genericdatacontainer WHERE gdc_obj_type = 'tr' AND gdc_gen_fieldname = 'tr_tracking' AND gdc_obj_id = " . $tr_id);
    if ($gdc_content != "") {
      $gdc_content_arr = explode("|", $gdc_content);
      $tr_tracking = $gdc_content_arr[0];
	}
    $is_payer = (($csc_id_payer == $csc_id && $csc_id_payer != CSC_ID_PAYER_CASH) || $csc_id_payer_cash == $csc_id) ? "true" : "false";
//$javascript .= 
//"alert($is_payer + \",\" + $csc_id + \",\" + $csc_id_payer + \",\" + $csc_id_payer_cash);";
    $javascript .= setJavascriptOpenerJobList($i - 1,
      array(JL_CSC_ID => $csc_id, JL_TR_COMP => $tr_comp, JL_TR_PERSON => $tr_person,
        JL_AD_STREET => $ad_street, JL_TR_HSNO => $tr_hsno, JL_AD_COUNTRY => $ad_country, JL_AD_ZIPCODE => $ad_zipcode,
        JL_AD_CITY => $ad_city, JL_TR_REMARK => $tr_remark, JL_CSC_ID_PAYER => $is_payer,
        JL_TR_COMMISSION_NO => my_str_check($tr_commission_no),
        JL_TR_TRACKING => my_str_check($tr_tracking),
        JL_TR_MEDIATIONAREA_ID => $tr_mediationarea_id,
        JL_TR_WARE_TO => ($tr_ware_from_to == 1 || $tr_ware_from_to == 3 ? "true" : "false"),
        JL_TR_WARE_FROM => ($tr_ware_from_to == 2 || $tr_ware_from_to == 3 ? "true" : "false")));
    if ($is_payer == "true"): $payer_found = true; endif;
    // wenn von "check_jb_permanent.php" aufgerufen, muss joblist-Array aufgebaut werden
    if (isset($check_jb_permanent_flag) && $check_jb_permanent_flag == true):
      setJobList($i, JL_CSC_ID, $csc_id);
      setJobList($i, JL_TR_COMP, $tr_comp);
      setJobList($i, JL_TR_PERSON, $tr_person);
      setJobList($i, JL_AD_STREET, $ad_street);
      setJobList($i, JL_TR_HSNO, $tr_hsno);
      setJobList($i, JL_AD_COUNTRY, $ad_country);
      setJobList($i, JL_AD_ZIPCODE, $ad_zipcode);
      setJobList($i, JL_AD_CITY, $ad_city);
      setJobList($i, JL_TR_REMARK, $tr_remark);
      setJobList($i, JL_TR_TRACKING, my_str_check($tr_tracking));
      setJobList($i, JL_TR_WARE_TO, ($tr_ware_from_to == 1 || $tr_ware_from_to == 3 ? "true" : "false"));
      setJobList($i, JL_TR_WARE_FROM, ($tr_ware_from_to == 2 || $tr_ware_from_to == 3 ? "true" : "false"));
      setJobList($i, JL_CSC_ID_PAYER, $is_payer);
//      setJobList($i, JL_TR_COMMISSION_NO, $tr_commission_no);
      setJobList($i, JL_HIDE_TR_REMARK, "true");
      if (defined('JL_MEDIATIONAREA_ID'))
        setJobList($i, JL_MEDIATIONAREA_ID, $tr_mediationarea_id);
      if ($jb_payment == 0):
        $jb_costsplit = "false";
        $jb_cash = "false";
      endif;
      if ($jb_payment == 1):
        $jb_costsplit = "true";
        $jb_cash = "false";
      endif;
      if ($jb_payment == 2):
        $jb_costsplit = "false";
        $jb_cash = "true";
      endif;
      if ($javascript_zone != ""):
        $jb_fixprice = 0;
        $jb_cr_price = 0;
      endif;
    else:
      $javascript .= javascriptAdd_cs_eid($csc_id, "opener.");
    endif;
  else:
    $tourFound = 0;
  endif;
  $i++;
} while ($tourFound == 1);
$javascript .=
  "if (typeof(opener.parent.job_tour.document.tourForm.jb_commission_no) != 'undefined') {\n" .
  "  opener.parent.job_tour.document.tourForm.jb_commission_no.value = \"" . $jb_commission_no . "\";\n" .
  "  opener.check_jb_commission_no();\n" .
  "}\n" .
  setJavascriptOpenerFormVals("job_options", "tourOptions",
  array(
    "jb_commission_no_hidden" => $jb_commission_no
   )
 );

// möglichen Kostensplit beachten
/* für Zentrale Hansetrans: Barzahlung */
if ($jb_payment == 1 || ($jb_payment == 0 && MASK_CASH_PAYER_SELECT == "1"))
  $javascript .=
    "if (typeof(opener.parent.job_tour.document.tourForm.jb_costsplit) != 'undefined') {" .
    "  opener.parent.job_tour.document.tourForm.jb_costsplit.checked = true;\n" .
    "  opener.checkCostsplit();\n}\n";
// Kostenteilung, Barzahlungsarie berücksichtigen
$comp_payer = "";
if ($csc_id_payer != -1 && $csc_id_payer != 0 && (CSC_ID_PAYER_CASH != "" && $csc_id_payer != CSC_ID_PAYER_CASH)):
  // wenn der Payer nicht in der Joblist gefunden wurde und
  // csc_id_payer weder Kostenteilung noch Barzahlung enthält
  // dann ist der Zahler in csc_id_payer der Drittzahler!!!
  // Da muss nun auch noch ein Name gefunden werden.
  if (!$payer_found):
    $comp_payer = $db->getOne(
      "SELECT cmp.cmp_comp FROM company AS cmp, customer AS cs, costcenter AS csc" .
      " WHERE cmp.cmp_id = cs.cmp_id AND cs.cs_id = csc.cs_id AND csc.csc_id = '$csc_id_payer'");
    $comp_payer = my_str_check_js($comp_payer);
    $javascript .= javascriptAdd_cs_eid($csc_id_payer, "opener.");
  endif;
  $javascript .=
    "if (opener.parent.job_tour.is_customer) {opener.setCsc_id_payer('$csc_id_payer', opener.get_comp('$csc_id_payer'));}\n";
endif;

// Kurierfilter berücksichtigen
$jb_cr_filter_list = array_merge(explode(",", $jb_cr_filter), explode(",", $jb_cr_filter_opt));

// Erst alle Kurierfilter zurücksetzen
$javascript .=
  "for (var i = 0; i < opener.parent.job_options.cfl.length; i++)\n" .
  "  opener.parent.job_options.cfl[i][3] = 0;\n" .
  "var filterStr = \"Filter\";";
// Alle Kurierfilter neu setzen
for ($i = 0; $i < count($jb_cr_filter_list); $i++)
{
  $javascript .=
    "for (var i = 0; i < opener.parent.job_options.cfl.length; i++)\n" .
    "{\n" .
    "  if (opener.parent.job_options.cfl[i][0] == \"" . trim($jb_cr_filter_list[$i]) . "\")\n" .
    "  {\n" .
    "    opener.parent.job_options.cfl[i][3] = 1;\n" .
    "    if (opener.parent.job_options.is_in_customer_filters(opener.parent.job_options.cfl[i][0]))\n" .
    "      filterStr = \"Filter*\";" .
    "    break;\n" .
    "  }\n" .
    "}\n";
}

$javascript .=
"if (typeof(opener.parent.job_options.document.tourOptions.jb_cr_filterButton) != 'undefined')\n {" .  
  "opener.parent.job_options.document.tourOptions.jb_cr_filterButton.value = filterStr;\n" .
"}";

// Wenn Barzahlung, dann Checkbox ankreuzen
if ($jb_payment == 2)
  $javascript .=
  "if (typeof(opener.parent.job_tour.document.tourForm.jb_cash) != 'undefined')\n {" .  
   "  opener.parent.job_tour.document.tourForm.jb_cash.checked = true;\n" .
   "  opener.checkCash();\n" .
   "}";
else
  $javascript .=
   "if (typeof(opener.parent.job_tour.document.tourForm.jb_cash) != 'undefined')\n {" .  
   "  opener.parent.job_tour.document.tourForm.jb_cash.checked = false;\n" .
   "  opener.checkCash();\n" .
   "}";

$waittimefiels = "";
if (MASK_WAITTIME_ENABLED == "1"):
  $waittimefiels =
    "opener.parent.job_options.document.tourOptions.jb_waittime_hour.disabled = false;\n" .
    "opener.parent.job_options.document.tourOptions.jb_waittime_minute.disabled = false;\n";
endif;

// Wenn der Originalaufrag einen Festpreis hatte und ein Discount berücksichtigt wurde,
// dann muss die entsprechende Checkbox gesetzt sein!
  $javascript .=
    "opener.parent.job_options.discount_default = true;\n";
if ($jb_fixprice != "0" || $jb_serviceprice != "0"):
  if ($db->getOne("SELECT trs_discount FROM tourservice WHERE jb_id = '$jb_id' AND (trs_srv_name = 'Fixpreis' OR trs_srv_name = 'Servicepreis') AND trs_discount > 0") > 0):
//    $javascript .=
//      "opener.parent.job_options.document.tourOptions.jb_discount.disabled = false;\n" .
//      "opener.parent.job_options.document.tourOptions.jb_discount.checked = true;\n";
  else:
    $javascript .=
      "opener.parent.job_options.discount_default = false;\n";
  endif;
endif;

$javascript .= 
  "opener.parent.job_options.is_exported = " . ($job_is_exported ? "true" : "false") . ";\n";

$javascript .=
  "opener.parent.job_options.document.tourOptions.tag.disabled = false;\n" .
  "opener.parent.job_options.document.tourOptions.monat.disabled = false;\n" .
  "opener.parent.job_options.document.tourOptions.jahr.disabled = false;\n" .
  "opener.parent.job_options.document.tourOptions.stunde.disabled = false;\n" .
  "opener.parent.job_options.document.tourOptions.minute.disabled = false;\n" .
  "if (typeof(opener.parent.job_options.document.tourOptions.jb_permanent) != 'undefined')\n" .  
  "  opener.parent.job_options.document.tourOptions.jb_permanent.disabled = false;\n" .
  $waittimefiels .
  "if (typeof(opener.parent.job_options.document.tourOptions.jb_globaljob) != 'undefined')\n {" .  
  "  opener.parent.job_options.document.tourOptions.jb_globaljob.checked = false;\n" .
  "  opener.parent.job_options.document.tourOptions.jb_globaljob.disabled = false; }\n" .
  "if (typeof(opener.parent.job_options.document.tourOptions.jb_status_manual) != 'undefined')\n" .   
  "  opener.parent.job_options.document.tourOptions.jb_status_manual.disabled = false;\n" .
  (($consider_manual_disp == 1 && MASK_MANUAL_DISPOSITION == "1")
    ? "if (typeof(opener.parent.job_options.document.tourOptions.jb_status_manual) != 'undefined')\n" .
      "  opener.parent.job_options.document.tourOptions.jb_status_manual.checked = true;\n"
    : "if (typeof(opener.parent.job_options.document.tourOptions.jb_status_manual) != 'undefined')\n" .
      "  opener.parent.job_options.document.tourOptions.jb_status_manual.checked = false;\n") .
  "opener.checkAfterwards();\n" .
  ($jb_incomplete == 1
    ? "if (typeof(opener.parent.job_options.document.tourOptions.jb_incomplete) != 'undefined')\n" .
      "opener.parent.job_options.document.tourOptions.jb_incomplete.checked = true;\n"
    : "if (typeof(opener.parent.job_options.document.tourOptions.jb_incomplete) != 'undefined')\n" .
      "opener.parent.job_options.document.tourOptions.jb_incomplete.checked = false;\n") .
//  "opener.parent.job_options.document.tourOptions.crSidList.disabled = false;\n" .
//  "opener.parent.job_options.document.tourOptions.cr_id_order.disabled = false;\n" .
//  "opener.parent.job_options.document.tourOptions.jb_waitstorno.disabled = false;\n" .
//"alert(\"$jb_incomplete\");\n" .
//"alert(opener.parent.job_options.document.tourOptions.jb_dispoinfo.value);\n" .
  "if (opener.parent.job_tour.is_customer) {opener.resetSignificantPriceValues();}\n" .
  "opener.parent.job_options.document.tourOptions.jb_jam_waittime.value = '" . $jb_jam_waittime . "';\n" .
  "opener.writeJob();\n" .
  "var curTourNo = 0;\n" .  
  ($job_is_locked ? "opener.parent.job_options.setTimeout('job_is_locked()', 100);\n" : "") .
  ($jb_copy == "true" ? "opener.parent.job_options.setTimeout('duplicateJob()', 100);\n" : "")
    . javascriptOpenerRefreshAndClose();

//
// checken ob von "check_jb_permanent.php" aufgerufen
// wenn ja, dann keine HTML-Ausgabe
//
if (!(isset($check_jb_permanent_flag) && $check_jb_permanent_flag == true)):
// Inhalt des temporären Fensters erzeugen und ausgeben
//  if ($phpVersion >= "7.0"):
//    $tpl = new HTML_Template_IT();
//  else:
//    $tpl = new IntegratedTemplate();
//  endif;
//  $tpl->loadTemplatefile(GETDATAHTMLTPL, true, true);
//  $tpl->setCurrentBlock("javascript");
//  $tpl->setVariable("_javascript_", $javascript);
////echo $javascript; die();
//  $tpl->parseCurrentBlock("javascript");
//  $tpl->show();
  $output = file_get_contents(GETDATAHTMLTPL);
  $output = str_replace("{_javascript_}", $javascript, $output);
  echo $output;
endif;

// Für 'check_jb_permanent.php' muss jb_freetext clean sein!
$jb_freetext_1 = my_str_check($jb_freetext_1);


?>
