<?php

define("LOG_NAME", "edi_scnd");
include_once("../tools/edi.inc.php");

if (!isset($argv[1]) || $argv[1] == ""):
   die("missing parameter\n");
else:
  $sql_query = "SELECT trat_serialno FROM phoenix.tourarticle AS ta, phoenix.tourarticleprocess AS tp WHERE ta.trat_id = " . $argv[1] . " AND ta.trat_id = tp.trat_id";
  $res = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
  $row = mysql_fetch_row($res);
  $trat_serialno = $row[0];
  mysql_free_result($res);

  $sql_query = "SELECT count(*) FROM phoenix.tourarticle AS ta, phoenix.tourarticleprocess AS tp WHERE trat_serialno = '" . $trat_serialno . "' AND ta.trat_id = tp.trat_id AND tratp_type = 304";
  $res = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
  $row = mysql_fetch_row($res);
  $cnt  = $row[0];
  mysql_free_result($res);

  if ($cnt == 1):
//   Erste Auslieferung ohne Erfolg, zweite Auslieferung muss generiert werden
    copy_tour($argv[1]);
  elseif ($cnt > 1):
//  Zweite Auslieferung ohne Erfolg, keine weitere Auslieferung mehr
//"416" => "640", // "Undeliverable, return to sender (Amazon)",

    $sql_query = "SELECT tr.jb_id, tr.tr_sort, tr_comp, tr_person, tr_comp2, tr_hsno, tr_cs_freetext, trat_serialno, ad_zipcode, ad_city, ad_street, hq_id " .
      "  FROM phoenix.tourarticle AS ta, phoenix.tour AS tr, phoenix.address AS ad, phoenix.job AS jb WHERE trat_id = " . $argv[1] . " AND ta.jb_id = tr.jb_id AND ta.tr_sort = tr.tr_sort AND ad.ad_id = tr.ad_id AND tr.jb_id = jb.jb_id";
    $res = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
    $row = mysql_fetch_array($res, MYSQL_ASSOC);

    $mailObj = new htmlMimeMail();
    $mailObj->setFrom($mailaddr[$row["hq_id"]]["from"]);
//    $mailObj->setCc("leier@stadtbote.de");
    $mailObj->setBcc($mailaddr[$row["hq_id"]]["bcc"]);
    $mailObj->setSubject($row["trat_serialno"] . " - Zweite Zustellung gescheitert");
    $mailObj->setText(
      "Zweiter Auslieferungsversuch erfolglos:\n\n" .
      "Track-id: " . $row["trat_serialno"] . "\n" .
      "Adresse:  " . $row["tr_comp"] . ", " . $row["ad_street"] . " " . $row["tr_hsno"] . ", " . $row["ad_zipcode"] . " " . $row["ad_city"] . "\n" .
      "Auftrag:  " . $row["jb_id"] . ", " . "Station Nr. " . $row["tr_sort"] . "\n" .
      "Fahrer:   " . $row["tr_cs_freetext"] . "\n" .
      "Typ:      " . $row["tr_comp2"] ."\n");
    $mailResult = $mailObj->send($mailaddr[$row["hq_id"]]["sendto"], 'smtp');
//  $mailResult = $mailObj->send(array(), 'smtp');
    writeLog("result of sending mail for jb_id/trsort = '" . $row["jb_id"] . "/" . $row["tr_sort"] . "': '" . $mailResult . "'");
    mysql_free_result($res);
  endif;
endif;

function copy_tour($trat_id)
{
	global $jb_idA;

  $sql_query = "SELECT tr.jb_id, tr.tr_sort, ad_id, tr_comp, tr_person, tr_comp2, tr_phone, tr_hsno, tr_remark, tr.csc_id, tr_ware_from_to, tr_cs_freetext, trat_serialno, hq_id" .
    "  FROM phoenix.tourarticle AS ta, phoenix.tour AS tr, phoenix.job AS jb WHERE trat_id = $trat_id AND ta.jb_id = tr.jb_id AND ta.tr_sort = tr.tr_sort AND tr.jb_id = jb.jb_id";
  $res = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
  $row = mysql_fetch_array($res, MYSQL_ASSOC);
	$jb_id_new = $jb_idA[$row["hq_id"]][$row["tr_comp2"]];
	if ($row["tr_comp2"] == "STBX_TIMED")
    $jb_id_new = $jb_idA[$row["hq_id"]]["STBX_NEXT"];

  do {
    $sql_query = "SELECT jb_status FROM phoenix.job WHERE jb_id = $jb_id_new";
    $res1 = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
    $row1 = mysql_fetch_array($res1, MYSQL_ASSOC);
    if ($row1["jb_status"] == 997):
      writeLog("waiting for running 'edi_iftmin.php' to terminate...");
      sleep(10); // wait 10 seconds...
    endif;
    mysql_free_result($res1);
  } while ($row1["jb_status"] == 997);

  $sql_query = "SELECT trat_serialno FROM phoenix.tourarticle WHERE jb_id = $jb_id_new AND trat_serialno = '" . $row["trat_serialno"] . "'";
  $res2 = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
writeLog("[$sql_query]");
  if ($row2 = mysql_fetch_row($res2)):
    writeLog("not copying undelivered tourstop: trat_id = $trat_id, jb_id = '" . $row["jb_id"] . "', tr_sort = '" . $row["tr_sort"] . "': '" . $row["trat_serialno"] . "' was already previously copied to jb_id = '$jb_id_new'");
  else:
    $sql_query = "SELECT MAX(tr_sort) FROM phoenix.tour WHERE jb_id = " . $jb_id_new;
    $res1 = mysql_query($sql_query) or die ($sql_query . ": " . mysql_error());
    $row1 = mysql_fetch_row($res1);
    $tr_sort_new = $row1[0] + 1;
    if ($tr_sort_new == 1)
      $tr_sort_new = 2;
    mysql_free_result($res1);

    writeLog("copying undelivered tourstop: trat_id = $trat_id, jb_id = '" . $row["jb_id"] . "', tr_sort = '" . $row["tr_sort"] . "' => jb_id_new = '$jb_id_new', tr_sort_new = '$tr_sort_new'");
    exec_query("UPDATE phoenix.job SET jb_status = 999 WHERE jb_id = $jb_id_new AND jb_status != 999");
//      writeLog("UPDATE phoenix.job SET jb_status = 999 WHERE jb_id = $jb_id_new AND jb_status != 999");
    exec_query("INSERT INTO phoenix.tour (jb_id, ad_id, tr_sort, tr_comp, tr_person, tr_comp2, tr_phone, tr_hsno, tr_remark, csc_id, tr_ware_from_to, tr_status, tr_cs_freetext, tr_mediationarea_id)" .
      "  VALUES (" . $jb_id_new . ", " . $row["ad_id"] . ", " . $tr_sort_new . ", '*" . my_str_check_edi($row["tr_comp"]) . "', '" . my_str_check_edi($row["tr_person"]) . "', '" . $row["tr_comp2"] . "', '" .
      $row["tr_phone"] . "', '" . my_str_check_edi($row["tr_hsno"]) . "', '" . my_str_check_edi($row["tr_remark"]) . "', " . $row["csc_id"] . ", " . $row["tr_ware_from_to"] . ", 0, '" . $row["tr_cs_freetext"] . "', 0)");
    exec_query("INSERT INTO phoenix.tourarticle (jb_id, tr_sort, trat_serialno) VALUES ($jb_id_new, $tr_sort_new, '" . $row["trat_serialno"] . "')");
  endif;
  mysql_free_result($res2);
  mysql_free_result($res);
}

?>