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

$check_jb_permanent_flag = true;
include_once("../include/dbglobal.inc.php");
include_once("../include/inc_check_publicholiday.inc.php");
include_once("../include/caglobal.inc.php");
include_once("../include/global.inc.php");
trace_execution_time_start();

$hq_id = HQ_ID_DEFAULT;
  
$sqlquery = "SELECT tax.tx_id, tax.tx_value FROM tax";
$res = $db->query($sqlquery);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
$sales_tax_rate_sign_list = "var sales_tax_rate_sign_list = [";
$i = 0;
while ($row = $res->fetch_assoc()):
  if ($i++ > 0)
    $sales_tax_rate_sign_list .= ",";
  $sales_tax_rate_sign_list .= "[\"" . $row["tx_id"] . "\", \"" . $row["tx_value"] . "\"]";
endwhile;
$res->free();
$sales_tax_rate_sign_list .= "];\n";

$sqlquery = "SELECT crf_short, crf_text, crf_status FROM courierfilter ORDER BY crf_short";
$res = $db->query($sqlquery);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
// die Kurier-Filter sollen alphabetisch von oben nach unten statt von links nach rechts sortiert sein...
// deshalb erst einmal die Daten aus der Abfrage übernehmen...
$crf_rows = array();
while ($row = $res->fetch_assoc()):
  $crf_rows[] = $row;
endwhile;
$res->free();
// ... und dann umsortieren ...
// die Filter werden von rechts nach links aufgebaut, also müssen sie hier
// erst in die richtige Reihenfolge gebracht werden, so dass am Schluss
// die sichtbare Reihenfolge von oben nach unten ist
$courierfilter_list = "var cfl=[";
$crf_rows_rows = 4; // 4 Spalten
$crf_rows_lines = ceil(count($crf_rows) / $crf_rows_rows); // ergibt x Zeilen
for ($j = 0; $j < $crf_rows_lines; $j++):
  for ($k = 0; $k < $crf_rows_rows; $k++):
    $i = ($k * $crf_rows_lines + $j);
    if ($courierfilter_list != "var cfl=[")
      $courierfilter_list .= ",\n";
    if ($i < count($crf_rows)):
      $courierfilter_list .= 
        "[\"" . $crf_rows[$i]["crf_short"] . "\", \"" . $crf_rows[$i]["crf_text"] . "\", \"" . $crf_rows[$i]["crf_status"] . "\", 0]";
    else:
      $courierfilter_list .= 
        "[\"" . "&nbsp;" . "\", \"" . "&nbsp;" . "\", \"0\", 0]";
    endif;
  endfor;
endfor;
$courierfilter_list .= "];\n";

$ph_list = getPublicHolidays(getDateTime("year"), "1", "0");
$js_ph_list = "var ph=[";
$js_ph_list_prev = "";
$j = 0;
for ($i = 0; $i < count($ph_list); $i++):
  $js_ph_list_cur = $ph_list[$i][0] . sprintf("%02d", $ph_list[$i][1]) . sprintf("%02d", $ph_list[$i][2]);
  if ($js_ph_list_prev != $js_ph_list_cur):
    if ($i > 0)
      $js_ph_list .= ",\n";
    $js_ph_list .= "\"" . $js_ph_list_cur . "\"";
    $js_ph_list_prev = $js_ph_list_cur;
  endif;
endfor;
$js_ph_list .= "];\n";

// get fake-zipcodes from DB
$sqlquery = "SELECT distinct srvp1.srvp_plz AS srvp_plz_source, srvp2.srvp_plz AS srvp_plz_target, srva_id, srva2_id, srvsam_name" .
  " FROM serviceplz AS srvp1, serviceplz AS srvp2, servicesubareamapping AS srvsam" .
  " WHERE srvp1.srvp_id = srvsam.srva_id AND srvp2.srvp_id = srvsam.srva2_id ORDER BY srvp1.srvp_plz, srvsam_sort";
$res = $db->query($sqlquery);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
$i = 0;
$js_fz_list = "var fz=[";
while ($row = $res->fetch_assoc()):
  if ($i++ > 0)
    $js_fz_list .= ",\n";
  $js_fz_list .= "[\"" . $row['srvp_plz_source'] . "\", \"" . $row['srvp_plz_target'] . "\", \""
    . $row['srva2_id'] . "\", \"" . $row['srvsam_name'] . "\"]";
endwhile;
$res->free();
$js_fz_list .= "];\n";

//  // wenn MASK_AUTO_REFRESH == 0, dann wird die Kurierliste dynamisch (bei Aufruf) aufgebaut
//  // wenn MASK_AUTO_REFRESH == 1, dann wird die Kurierliste statisch (hier) aufgebaut
//  if (MASK_AUTO_REFRESH == "1"):
include("job_courier.inc.php");
$cr_id_order_list .=
  "var cr_id_order_list2=[];\n" .
  "var vht_id_str=[];\n";
//  endif;

$fileHandle = @fopen("../jobs/job_options.js", 'w');
@fwrite($fileHandle, $courierfilter_list . $sales_tax_rate_sign_list . $js_ph_list . $js_fz_list . $cr_id_order_list);
@fclose($fileHandle);
writeLog_("../log/job_options.js.inc_", trace_execution_time_stop() . " '../jobs/job_options.js' updated");

?>
