<?php

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

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

$RemarkToTSPList = array();
checkRemarkToTSP(file_get_contents('../log/orderReq_20150308.log', true));
checkRemarkToTSP(file_get_contents('../log/orderReq_20150310.log', true));
checkRemarkToTSP(file_get_contents('../log/orderReq_20150315.log', true));
checkRemarkToTSP(file_get_contents('../log/orderReq_20150322.log', true));
checkRemarkToTSP(file_get_contents('../log/orderReq_20150328.log', true));
checkRemarkToTSP(file_get_contents('../log/orderReq_20150330.log', true));
checkRemarkToTSP(file_get_contents('../log/orderReq.log', true));

foreach($RemarkToTSPList as $sequence_no => $RemarkToTSPArr) {
  foreach($RemarkToTSPArr as $RemarkToTSP) {
    echo $sequence_no . $RemarkToTSP . "\n";
  }
}

function checkRemarkToTSP($orderReqLog) {
  global $RemarkToTSPList;

  $offs = 0;
  $pos = strpos($orderReqLog, "<xml><order>", $offs);
  while (!($pos === false)) {
    $pos1 = strpos($orderReqLog, "</order></xml>", $offs);
    
    $orderReq = substr($orderReqLog, $pos, $pos1 - $pos + strlen("</xml></order>"));
    $pos2 = strpos($orderReq, "RemarkToTSP", 0);
      if (!($pos2 === false)) {
      	$pos3 = strpos($orderReq, "</operation><no>", 0);
      	$pos4 = strpos($orderReq, "</no>", $pos3 + 1);
      	$sequence_no = substr($orderReq, $pos3 + strlen("</operation><no>"), $pos4 - strlen("</operation><no>") - $pos3);
      	$pos5 = strpos($orderReq, ";RemarkToTSP", $pos4 + 1);
      	$pos6 = strpos($orderReq, ";", $pos5 + 1);
      	$RemarkToTSP = substr($orderReq, $pos5 + strlen(";RemarkToTSP"), $pos6 - strlen(";RemarkToTSP") - $pos5);
      	$RemarkToTSP = ", " .
          get_first("SELECT gdc_content FROM genericdatacontainer, tour WHERE genericdatacontainer.gdc_obj_id = tour.jb_id AND gdc_gen_fieldname = 'info_0' AND tr_commission_no = '" . $sequence_no . "'") .
        	"(# " . get_first("SELECT jb_id FROM tour WHERE tr_commission_no = '" . $sequence_no . "'") . ")" . $RemarkToTSP;
      	if (!in_array($sequence_no, array_keys($RemarkToTSPList))) {
      	  $RemarkToTSPList[$sequence_no] = array();
      	}
      	
      	if (!in_array($RemarkToTSP, array_values($RemarkToTSPList[$sequence_no]))) {
      	  $RemarkToTSPList[$sequence_no][] = $RemarkToTSP;
      	}
      }
    $offs = $pos1 + 1;
    $pos = strpos($orderReqLog, "<xml><order>", $offs);
  }
}

function get_first_row($sqlquery) {
  global $mysql_db;
  
  $res = mysql_query($sqlquery, $mysql_db) or die ($sqlquery . ": " . mysql_error($mysql_db));
  $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 "";
}

?>
