<?php
/*=======================================================================
 *
 * find_unfinished_scan_ids
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/

if ($argv[1] == "") {
  echo "missing argument\n";
  exit();
}

include_once("../include/glob_defs.inc.php");

$mysql_db_default = mysql_connect($dbhost, $dblogin, $dbpassword) or die (mysql_error());
mysql_select_db("phoenix", $mysql_db_default) or die (mysql_error($mysql_db_default));
mysql_query('SET NAMES latin1', $mysql_db_default) or die ('SET NAMES latin1' . ": " . mysql_error($mysql_db_default));
$mysql_db_b2b = $mysql_db_default;

$mysql_db_b2b_history = mysql_connect($dbhost_history, $dblogin_history, $dbpassword_history) or die (mysql_error());
mysql_select_db("phoenix", $mysql_db_b2b_history) or die (mysql_error($mysql_db_b2b_history));
mysql_query('SET NAMES latin1', $mysql_db_b2b_history) or die ('SET NAMES latin1' . ": " . mysql_error($mysql_db_b2b_history));

$states = array(
  "102" => "1",  // HOTT
  "201" => "4",  // RAH 
  "202" => "2",  // LODT
  "301" => "9",  // OD
  "304" => "7",  // DF
  "351" => "9",  // OD (Pickup)
  "354" => "7",  // DF (Pickup)
  "359" => "11", // RTSPU
  "408" => "8"   // RTS
);

if ($argv[1] == "acapella7890") {
  $sql_query =
    "SELECT csc_id_related, jb.jb_id, LEFT(jb_ordertime, 10) as ordertime, jb_service, tr_commission_no" .
    " FROM phoenix.job AS jb, phoenix.genericdatacontainer AS gdc, phoenix.tour AS tr, jobpayment AS jbp" .
    " WHERE jb_storno IS NULL AND NOT (jb_service IN (6, 18)) AND jb.jb_id = tr.jb_id AND tr.tr_sort = 2 AND gdc_obj_id = jb.jb_id AND gdc_gen_fieldname = 'acquisitiontime'" .
    " AND jb.jb_id = jbp.jb_id AND jbpc_id = 0 " .
    " ORDER BY jb_ordertime, jb.jb_id";
} else {
  $sql_query =
    "SELECT csc_id_related, jb.jb_id, LEFT(jb_ordertime, 10) as ordertime, jb_service, tr_commission_no" .
    " FROM phoenix.job AS jb, phoenix.genericdatacontainer AS gdc, phoenix.tour AS tr" .
    " WHERE jb_storno IS NULL AND NOT (jb_service IN (6, 18)) AND jb.jb_id = tr.jb_id AND tr.tr_sort = 2 AND gdc_obj_id = jb.jb_id AND gdc_gen_fieldname = 'acquisitiontime'" .
    " AND tr_commission_no = '" . $argv[1] . "'" .
    " ORDER BY jb_ordertime, jb.jb_id";
}
writeLog($sql_query);
$res = exec_query($sql_query, $mysql_db_b2b, false);

while ($row = mysql_fetch_array($res, MYSQL_ASSOC)):
  $sql_query =
    "SELECT bo_obj_data, bo_createtime" .
    " FROM phoenix_log.b2b_objects WHERE bo_type = 300 AND bo_obj_data LIKE '" . $row["tr_commission_no"] . "|%' " . 
    " ORDER BY bo_createtime, bo_id";
  writeLog($sql_query);
  $res1 = exec_query($sql_query, $mysql_db_b2b, true);

  // Wenn nichts gefunden, dann in der Historie suchen (nur wenn job_details)
  if (mysql_num_rows($res1) == 0) {
    writeLog(str_replace("b2b_objects", "b2b_objects_2015", $sql_query));
    $res1 = exec_query(str_replace("b2b_objects", "b2b_objects_2015", $sql_query), $mysql_db_b2b_history, true);
  }

  // Wenn immer noch nichts gefunden, dann einen leeren Eintrag generieren, damit die nachfolgende Schleife einmal durchlaufen wird
  if (mysql_num_rows($res1) == 0) {
    $sql_query =
      "SELECT NULL AS bo_obj_data, NULL AS bo_ext_id2, NULL AS bo_createtime, NULL AS bo_state, NULL AS bo_id";
    writeLog($sql_query);
    $res1 = exec_query($sql_query, $mysql_db_b2b);
  }
  $output = true;
  while ($row1 = mysql_fetch_array($res1, MYSQL_ASSOC)):    
    $row["bo_obj_data"] = $row1["bo_obj_data"];
    $row["bo_createtime"] = $row1["bo_createtime"];
    $sequence_no = "";
    $event_no = "";
    if (!is_null($row1["bo_obj_data"]))
      list ($sequence_no, $event_no) = explode("|", $row["bo_obj_data"]);
    // wenn Auftrag ohne jegliche Events (durch LEFT JOIN hier vorhanden), dann die Nummer aus der tr_commission_no nehmen
    if ($sequence_no == "")
      $sequence_no = $row["tr_commission_no"];
  endwhile;
  mysql_free_result($res1);
  if (isset($states[$event_no])) {
    writeLog($row["csc_id_related"] . ";" . $row["jb_id"] . ";" . $row["ordertime"] . ";" . $row["jb_service"] . ";" . $row["tr_commission_no"] . ";" . $event_no . ";" . $row["bo_createtime"]);
    $gdc_gen_fieldname = get_first("SELECT gdc_gen_fieldname FROM genericdatacontainer WHERE gdc_obj_type = 'shp' AND gdc_gen_fieldname = 'shp_state' AND gdc_obj_id = '" . ltrim($row["tr_commission_no"], "HA0") . "'");
    if ($gdc_gen_fieldname != "shp_state") {
      $sql_query =
        "INSERT INTO genericdatacontainer" .
        " (gdc_obj_type, gdc_obj_id, gdc_gen_fieldname, gdc_content, gdc_context)" .
        " VALUES ('shp', '" . ltrim($row["tr_commission_no"], "HA0") . "', 'shp_state', '" . $states[$event_no] . "', '" . $row["tr_commission_no"] . "')\n";
      writeLog($sql_query);
//      exec_query($sql_query);
    } else {
      $sql_query =
        "UPDATE genericdatacontainer SET gdc_content = '" . $states[$event_no] . 
        "' WHERE gdc_obj_type = 'shp' AND gdc_gen_fieldname = 'shp_state' AND gdc_obj_id = " . ltrim($row["tr_commission_no"], "HA0") . " AND gdc_content != '" . $states[$event_no] . "'\n";
      writeLog($sql_query);
//      exec_query($sql_query);
    }
  }
endwhile;
mysql_free_result($res);

function exec_query($sqlquery, $mysql_db_id = NULL, $dummy = false) {
  global $mysql_db_default;

  if (is_null($mysql_db_id))
    $mysql_db_id = $mysql_db_default;
  $res = mysql_query($sqlquery, $mysql_db_id) or die ($sqlquery . ": " . mysql_error($mysql_db_id));
  return $res;
}

function get_first_row($sqlquery) {
  global $mysql_db_default;

  $res = mysql_query($sqlquery, $mysql_db_default) or die ($sqlquery . ": " . mysql_error($mysql_db_default));
  $row = mysql_fetch_array($res);
  mysql_free_result($res);
  return $row;
}

function get_first($sqlquery) {

  if ($row = get_first_row($sqlquery))
    return $row[0];
  return "";
}

function writeLog($log_text) {
  $fileHandle = @fopen("../log/set_scan_status_" . date("Ym") . ".log", 'a');
  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . $log_text .  "\n");
  @fclose($fileHandle);
  return;
}