<?php

$cs_eid_src = isset($_POST[f_cs_eid_src]) ? $_POST[f_cs_eid_src] : "";
$cs_eid_dest = isset($_POST[f_cs_eid_dest]) ? $_POST[f_cs_eid_dest] : "";
$srvph_validfrom = isset($_POST[f_srvph_validfrom]) ? $_POST[f_srvph_validfrom] : "";
if ($srvph_validfrom == "")
  $srvph_validfrom = date("Y-m-d H:i:s", time());
$do_it = isset($_POST[f_do_it]) ? $_POST[f_do_it] : "";
$msgTxt = "";
$do_it_txt = "";

if ($cs_eid_src != "" && $cs_eid_dest != "") {
  mysql_connect("172.16.0.102:3391", "phoenix", "AdAdgkS13") or die (mysql_error());
  mysql_select_db("phoenix") or die (mysql_error());
  mysql_query('SET NAMES latin1') or die ('SET NAMES latin1');
  list ($hq_id_src, $cs_id_src) = getCs_id($cs_eid_src);
  $prices_src = getPrices($cs_id_src);
  list ($hq_id_dest, $cs_id_dest) = getCs_id($cs_eid_dest);
//  $prices_dest = getPrices($cs_id_src);
//  $msgTxtJobs = $prices_src . $prices_dest;
  if (count($prices_src) > 0) {
    $do_it_txt = "Preise kopieren <input type=\"checkbox\" name=\"f_do_it\" value=\"1\">";
    for ($i = 0; $i < count($prices_src); $i++) {
      $msgTxtJobs .= ($i+1) . ": " .  $prices_src[$i][6] . " -&gt; " . $prices_src[$i][7] . "<br>\n";
      exec_query("INSERT INTO serviceplzcustomer (srvp_id, srvp2_id, hq_id, cs_id, srvpc_price, srvpc_discount)" .
        " VALUES (" . $prices_src[$i][0] . ", " . $prices_src[$i][1] . ", " . $hq_id_dest . ", " . $cs_id_dest . ", " . $prices_src[$i][4] . ", " . $prices_src[$i][5] . ");");
      exec_query("INSERT INTO serviceplzhistory (srvp_id, srvp2_id, hq_id, cs_id, usr_id, srvph_validfrom, srvph_price, srvph_discount)" .
        " VALUES (" . $prices_src[$i][0] . ", " . $prices_src[$i][1] . ", " . $hq_id_dest . ", " . $cs_id_dest . ", 0, '" . $srvph_validfrom . "', " . $prices_src[$i][4] . ", " . $prices_src[$i][5] . ");");
    }
    $msgTxt .= "<span style=\"color:red\">" . "$i Preise gefunden" . "</span><br><br>\n" . $msgTxtJobs;
  } else {
    $msgTxt .= "<span style=\"color:red\">" . "Für " . $cs_eid_src . " wurden keine kundenspezifischen Preise gefunden!" . "</span><br>\n";
  }
} else {
  $msgTxt .= "<span style=\"color:red\">" . "Bitte beide Kundennumern eingeben!" . "</span><br>\n";
}

function getCs_id($cs_eid) {
  $res = mysql_query("SELECT hq_id, cs_id FROM customer WHERE cs_eid = '$cs_eid'");
  if (!$res) die (mysql_error());
  $row = mysql_fetch_row($res);
  return array($row[0], $row[1]);
}

function getPrices($cs_id) {
  $srvpc = array();
  $res = mysql_query("SELECT srvpc.srvp_id, srvpc.srvp2_id, hq_id, cs_id, srvpc_price, srvpc_discount, srvp.srvp_plz AS srvp_plz, srvp2.srvp_plz AS srvp_plz2
    FROM serviceplzcustomer AS srvpc, serviceplz AS srvp, serviceplz AS srvp2
    WHERE srvp.srvp_id = srvpc.srvp_id AND srvp2.srvp_id = srvp2_id AND cs_id = " . $cs_id);
  if (!$res) die (mysql_error());
  while ($row = mysql_fetch_assoc($res)) {
    $srvpc[] = array($row["srvp_id"], $row["srvp2_id"], $row["hq_id"], $row["cs_id"], $row["srvpc_price"], $row["srvpc_discount"], $row["srvp_plz"], $row["srvp_plz2"]);
  }
  mysql_free_result($res);
  return $srvpc;
}

function exec_query($sqlquery) {
  global $msgTxtJobs, $do_it;

  $msgTxtJobs .= "[" . $sqlquery . "]";
  if ($do_it == "1") {
//    mysql_query($sqlquery) or die($sqlquery . ": " . mysql_error());    
    $msgTxtJobs .= "<span style=\"color:red\">" . " - ausgeführt!" . "</span>";
  }
  $msgTxtJobs .= "<br>\n";
}

?>

<html>
<head>
  <title>Kopie der kompletten PLZ-Preismatrix</title>
  <link rel="stylesheet" type="text/css" href="../css/phoenix.css"> 
</head>

<body>
  <form name="import_form" action="copy_prices.php" method="post">
  	<center>
  		&nbsp;<br>
      Quelle (Kd.-Nr.): <input name="f_cs_eid_src" type="text" size="6" maxlength="10" class="input" value="<?php echo $cs_eid_src;?>">&nbsp;
      Ziel (Kd.-Nr.): <input name="f_cs_eid_dest" type="text" size="6" maxlength="10" class="input" value="<?php echo $cs_eid_dest;?>">&nbsp;
      g&uuml;ltig ab: <input name="f_srvph_validfrom" type="text" size="19" maxlength="19" class="input" value="<?php echo $srvph_validfrom;?>">&nbsp;
      <input type="submit" name="formsubmit" size="2" value="Submit" class="submit"><br>
      <?php echo $do_it_txt; ?>
  		&nbsp;<br>
  		&nbsp;<br>
      <?php echo $msgTxt; ?>
    </center>
  </form>

</body>
</html>
