<?php

include_once("../include/auth.inc.php");
include_once("../include/global.inc.php");
include_once("../include/inc_customer.inc.php");

getSecHttpVars("1",array("cs_id","csc_id","csc_id_ask_mail", "cs_id_ask_mail", "cs_id_photo", "hq_id", "vht_id", "csc_id_payer_cash"));

//echo "alert(" . $csId . "|" . $funcNo . "|" . $funcParms . ");\n";

$result = "''";

if ($cs_id != "") {
  $arr = getContactsOfCustomer($cs_id, "1", "0", array(101,102)); 
  //$today = getdate();
  //$fileHandle = @fopen("../log/ajaxReqJob_" . $today['year'] . sprintf("%02d", $today['mon']) . ".log", 'a');
  //@fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . "\$cs_id = " . $cs_id .  "\n");
  //@fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . "\$arr = " . json_encode($arr) .  "\n");
  //@fclose($fileHandle);
  sort($arr);
  
  $result = "new Array(";
  for ($i = 0; $i < count($arr); $i++) {
    if (substr(trim($arr[$i]), -1, 1) == ",")
      $arr[$i] = trim(substr(trim($arr[$i]), 0, -1));
    if ($result == "new Array(")
      $result .= "'" . rawurlencode($arr[$i]) . "'";
    else
      $result .= ",'" . rawurlencode($arr[$i]) . "'";
  }
  $result .= ")";
} elseif ($csc_id != "") {
  $sqlquery = "SELECT usr.usr_email, cs.cs_jbstatusmail2csc, cs.cs_jbstatusmail, cs.cs_jbstatusmail2, cs.cs_jbstatusmail3, cscad.cscad_email"
            . " FROM costcenter AS csc, customer AS cs, costcenteraddress AS cscad, employee AS emp, user AS usr"
            . " WHERE csc.csc_id = " . $csc_id . " AND"
            . "       csc.cs_id = cs.cs_id      AND"
            . "       cs.cs_admin = emp.emp_id  AND"
            . "       emp.usr_id = usr.usr_id   AND"
            . "       csc.csc_id = cscad.csc_id AND"
            . "       cscad.adt_id = '2'        AND"
            . "       ((usr.usr_email != '' AND cs.cs_jbstatusmail2csc = '0') OR"
            . "        (cscad.cscad_email != '' AND cs.cs_jbstatusmail2csc = '1'))";
  $res = $db->query($sqlquery);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
  $result = "'";
  if ($row = ($phpVersion >= "8.0" ? $res->fetch_assoc() : $res->fetchRow())):
    $cs_jbstatusmailstr = "";
    if ($row["cs_jbstatusmail"] == "1")
      $cs_jbstatusmailstr .= "Erledigung";
    if ($row["cs_jbstatusmail3"] == "1")
      $cs_jbstatusmailstr .= ($cs_jbstatusmailstr == "" ? "" : "/") . "Erfassung";
    if ($row["cs_jbstatusmail2"] == "1")
      $cs_jbstatusmailstr .= ($cs_jbstatusmailstr == "" ? "" : "/") . "Abholung";
    if ($cs_jbstatusmailstr != "") {
      $result .= "Automatische Mail bei " . $cs_jbstatusmailstr . " an &lt";
      if ($row["cs_jbstatusmail2csc"] == "0")
        $result .= str_replace(" ", "", $row["usr_email"]);
      else
        $result .= str_replace(" ", "", $row["cscad_email"]);
      $result .= "&gt";
    }
  endif;
  $res->free();
  $result .= "'";
} elseif (isset($csc_id_ask_mail) && $csc_id_ask_mail != "") {
  $cs_jbstatusmail_fields = getFieldValueFromId("customer", "cs_id", $cs_id_ask_mail, "cs_jbstatusmail_fields");
  $empIdActCC24State = false; 
  $empHqList = getParameterValue("0", "HEADQUARTERS_MULTIPLE_ACCESS_EMPLOYEES", "0") . "|300001";
  if ($empHqList != "") {
    $empIdActCC24State = isInParameterString($emp_id, $empHqList);
  }

  $result = "'";
  if ((substr($cs_jbstatusmail_fields, 3, 1) == "1" && (getParameterValue("0", "MASK_IGNORE_JBSTATUSMAIL2CSC", $hq_id) == "0" || $empIdActCC24State)) ||
    (getParameterValue("0", "MASK_IGNORE_JBSTATUSMAIL2CSC", $hq_id) != "0" && (getParameterValue($emp_id, "MODE_LATER_JOB") != "0" || $vht_id >= 7 /* Immer Status-Mails bei LKWs */ || getParameterValue("0", "MASK_IGNORE_JBSTATUSMAIL2CSC", $hq_id) == "2"))):
    $sqlquery = "SELECT usr.usr_email, cs.cs_jbstatusmail2csc, cs.cs_jbstatusmail, cscad.cscad_email"
              . " FROM costcenter AS csc, customer AS cs, costcenteraddress AS cscad, employee AS emp, user AS usr"
              . " WHERE csc.csc_id = " . ($csc_id_ask_mail == getParameterValue("0", "CSC_ID_PAYER_CASH", $hq_id) ? $csc_id_payer_cash : $csc_id_ask_mail) . " AND"
              . "       csc.cs_id = cs.cs_id                                   AND"
              . "       cs.cs_admin = emp.emp_id                               AND"
              . "       emp.usr_id = usr.usr_id                                AND"
              . "       csc.csc_id = cscad.csc_id                              AND"
              . "       cscad.adt_id = '2'";
  //echo $sqlquery . "<br>\n";
    $today = getdate();
//    $fileHandle = @fopen("../log/ajaxReqJob_" . $today['year'] . sprintf("%02d", $today['mon']) . ".log", 'a');
//    @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] \$jb_id = $jb_id [" . $sqlquery . "] . \n");
//    @fclose($fileHandle);
    $res = $db->query($sqlquery);
    if (DB::isError($res)) die ("$PHP_SELF: " . $res->getMessage());
    while ($row = ($phpVersion >= "8.0" ? $res->fetch_assoc() : $res->fetchRow())):
      $ask_for_statusmail =
        (getParameterValue("0", "MASK_IGNORE_JBSTATUSMAIL2CSC", $hq_id) == "0" || $empIdActCC24State) ||
        ((getParameterValue("0", "MASK_IGNORE_JBSTATUSMAIL2CSC", $hq_id) != "0" && (getParameterValue($emp_id, "MODE_LATER_JOB") == "1" || $vht_id >= 7 || getParameterValue("0", "MASK_IGNORE_JBSTATUSMAIL2CSC", $hq_id) == "2"))
          && ($row["cs_jbstatusmail"] == "1" || (substr($cs_jbstatusmail_fields, 3, 1) == "1"))
        );
      $statusmail2csc = $row["cs_jbstatusmail2csc"];
      if (trim($row["usr_email"]) != "" && $ask_for_statusmail && $statusmail2csc == "0")
        $result .= trim($row["usr_email"]);
      if (trim($row["cscad_email"]) != "" && $ask_for_statusmail && $statusmail2csc == "1")
        $result .= trim($row["cscad_email"]);
    endwhile;
    $res->free();
//    $fileHandle = @fopen("../log/ajaxReqJob_" . $today['year'] . sprintf("%02d", $today['mon']) . ".log", 'a');
//    @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] \$ask_for_statusmail = " . $ask_for_statusmail . ", \$statusmail2csc = " . $statusmail2csc . " \n");
//    @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] [" . $result . "] \n");
//    @fclose($fileHandle);
  endif;
  $result .= "'";
} elseif (isset($cs_id_photo) && $cs_id_photo != "") {
  list($cs_photo_disabled, $cs_photo_hq, $cs_photo_cs, $cs_photo_range_from, $cs_photo_range_to) = getCsPhotoConf($cs_id_photo);
  $result = "new Array('" . $cs_photo_disabled . "', '" . $cs_photo_hq . "', '" . $cs_photo_cs . "', '" . $cs_photo_range_from . "', '" . $cs_photo_range_to . "')";
}

echo "retValue = $result;";
?>  
