<?php
/*=======================================================================
 *
 * premium_details.php
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/

//error_reporting(E_ALL ^ (E_DEPRECATED | E_NOTICE | E_STRICT));
include_once("../include/auth.inc.php");
include_once("../include/global.inc.php");
$log_file_name = "../log/premium_details_" . date("Ym") . ".log";

if (!authCheckEmployeeRights($emp_id, "1")) : die ("$PHP_SELF: Access denied!"); endif;
$httpVarsArray = array("crvh_id");
getSecHttpVars("1", $httpVarsArray);

if (trim($crvh_id) == "") {
  $crvh_id = isset($_POST["f_crvh_id"]) ? trim($_POST["f_crvh_id"]) : (isset($_GET["crvh_id"]) ? trim($_GET["crvh_id"]) : 0);
}
$crvh_sid = "";
if ($crvh_id != 0) {
  $crvh_sid = $db->getOne("SELECT crvh_sid FROM couriervehicle WHERE crvh_id = '" . $crvh_id . "'");
}
if ($crvh_sid == "") {
  $crvh_sid = "(Fahrzeug fehlt)";
  $error_text = "<b>Es wurde kein Fahrzeug übergeben!<b>";
}

$sql_query = "SELECT usr_account, usr_firstname, usr_name FROM user WHERE usr_id = " . $usr_id;
$res = $db->query($sql_query);
if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query'" . $res->getMessage());
$row = $res->fetch_assoc();
myWriteLog($row["usr_firstname"] . " " . $row["usr_name"] . " (" . $row["usr_account"] . ")");
$res->free();

$content = "";
$error_text .= "<br><br>";

if ($crvh_sid != "(Fahrzeug fehlt)") {
  $sql_query = "SELECT gdc_content, gdc_context" .
    " FROM genericdatacontainer WHERE gdc_obj_id = " . $crvh_id . " AND gdc_obj_type = 'crvh' AND gdc_gen_fieldname = 'crvh_premium' AND gdc_context = ''" . 
    " ORDER BY gdc_content";
  $res = query_and_log($sql_query, $db);
  $nextElem = 1;
  $sum = 0;
  while ($row = $res->fetch_assoc()) {
    $crvh_premium_data = explode("|", $row["gdc_content"]);
    $content .= "<tr><td align=left ". '><input name="crvh_premium_text_' . $crvh_premium_data[0] . '" type="text" size="40" maxlength="60" value = "' . $crvh_premium_data[1] .'" class="input inputBig">' . "</td>";
    $content .= "<td align=right ". '>&nbsp;<input name="crvh_premium_amount_' . $crvh_premium_data[0] . '" type="text" size="8" maxlength="10" value = "' . $crvh_premium_data[2] . '" class="input inputBigRight" onchange="checkAmount(' . $crvh_premium_data[0] . ')">' . "</td>";
    $content .= "<td align=center ". '><input type="checkbox" name="crvh_premium_repeat_' . $crvh_premium_data[0] . '" value="1" class="input inputBigCenter"' . ($crvh_premium_data[3] == "true" ? ' checked' : '') . '>' . "</td>";
    $content .= "<td align=left ". '><input name="crvh_premium_comment_' . $crvh_premium_data[0] . '" type="text" size="40" maxlength="60" value = "' . $crvh_premium_data[4] . '" class="input inputBig">' . "</td>";
    $content .= '<td><a href="javascript:save_row(' . $crvh_id . ', 3, ' . $crvh_premium_data[0] . ')">&nbsp;<img src=../images/trash.jpg alt="löschen" title="löschen"></a>' . "</td></tr>\n";
    $sum += str_replace(",", ".", $crvh_premium_data[2]);
    $nextElem = $crvh_premium_data[0] + 1;
  }
  $res->free();
  $content .= "<tr><td align=left ". '><input name="crvh_premium_text_' . $nextElem . '" type="text" size="40" maxlength="60" value = "" class="input inputBig"">' . "</td>";
  $content .= "<td align=right ". '>&nbsp;<input name="crvh_premium_amount_' . $nextElem . '" type="text" size="8" maxlength="10" value = "" class="input inputBigRight" onchange="checkAmount(' . $nextElem . ')">' . "</td>";
  $content .= "<td align=center ". '><input type="checkbox" name="crvh_premium_repeat_' . $nextElem . '" value="1" class="input inputBigCenter">' . "</td>";
  $content .= "<td align=left ". '><input name="crvh_premium_comment_' . $nextElem . '" type="text" size="40" maxlength="60" value = "" class="input inputBig">' . "</td></tr>\n";
  $content .= "<tr><td align=left><span class=\"textBig\">Summe:</span></td><td align=right><span class=\"textBig\" style=\"color:red\">" . str_replace(".", ",", sprintf("%01.2f", $sum)) . "</span></td><td></td><td align=right>" .
    '<button type="button" class="buttonAction" name="action_save" onMouseover="document.getElementsByName(\'action_save\')[0].style.color=\'#97bcFF\'" onMouseout="document.getElementsByName(\'action_save\')[0].style.color=\'#FFFFFF\'" ' .
    'onClick="save_rows(' . $crvh_id . ', ' . $nextElem . ')">Speichern</button>' . "</td></tr>\n";

  query_and_log("UPDATE couriervehicle SET crvh_partner_premium = " . $sum . " WHERE crvh_id = " . $crvh_id, $db);
  
  $sql_query = "SELECT gdc_content, gdc_context" .
    " FROM genericdatacontainer WHERE gdc_obj_id = " . $crvh_id . " AND gdc_obj_type = 'crvh' AND gdc_gen_fieldname = 'crvh_premium' AND gdc_context != ''" .
    " ORDER BY gdc_context DESC";
  $res = query_and_log($sql_query, $db);
  $rowCnt = 0;
  $sum = 0;
  $prev_gdc_context = "";
  while ($row = $res->fetch_assoc()) {
    if ($rowCnt % 2 == 0) { $cC = "bgcolor=\"#DDDDDD\""; $cC2 = "bgcolor=\"#EEEEEE\""; }
    if ($rowCnt++ % 2 == 1) { $cC = "bgcolor=\"#BBBBFF\""; $cC2 = "bgcolor=\"#CCCCFF\""; }
    if ($prev_gdc_context != $row["gdc_context"]) {
	  if ($prev_gdc_context != "")
        $content .= "<tr><td align=left><span class=\"textBig\">Summe:</span></td><td align=right><span class=\"textBig\" style=\"color:red\">" . str_replace(".", ",", sprintf("%01.2f", $sum)) . "</span></td><td></td><td></td><tr>\n";
      $content .= "<tr><td><span class=\"textBig\">&nbsp;</span></td><td></td><td></td><td></td><td></td></tr>\n";
      $sum = 0;
      $prev_gdc_context = $row["gdc_context"];
    }
    $colCnt = 0;
    $crvh_premium_data = explode("|", $row["gdc_content"]);
    $content .= "<tr><td align=left " . ($colCnt++ % 2 == 1 ? $cC : $cC2) . '><span class="textBig">' . $crvh_premium_data[1] . "</span></td>";
    $content .= "<td align=right " . ($colCnt++ % 2 == 1 ? $cC : $cC2) . '><span class="textBig">' . $crvh_premium_data[2] . "</span></td>";
    $content .= "<td align=center " . ($colCnt++ % 2 == 1 ? $cC : $cC2) . '><span class="textBig">' . ($crvh_premium_data[3] == "true" ? 'ja' : '') . "</span></td>";
    $content .= "<td align=left " . ($colCnt++ % 2 == 1 ? $cC : $cC2) . '><span class="textBig">' . $crvh_premium_data[4] . "</span></td>";
    $content .= "<td align=left " . ($colCnt++ % 2 == 1 ? $cC : $cC2) . '><span class="textBig">' . formDate($row["gdc_context"]) . "</span></td></tr>\n";
    $sum += str_replace(",", ".", $crvh_premium_data[2]);
  }
  $res->free();
  $content .= "<tr><td align=left><span class=\"textBig\">Summe:</span></td><td align=right><span class=\"textBig\" style=\"color:red\">" . str_replace(".", ",", sprintf("%01.2f", $sum)) . "</span></td><td></td><td></td><tr>\n";

  $content = 
    "<table border=0 cellspacing=0 cellpadding=0 vspace=0 hspace=0>\n" .
    "<tr><td align=left><span class=\"textBig\">Text</span></td><td align=left><span class=\"textBig\">Betrag in &#8364</span></td>" .
    "<td align=center><span class=\"textBig\">wiederholen</span></td><td align=center><span class=\"textBig\">Bemerkung</span></td>" .
    "<td align=center><span class=\"textBig\">Export</span></td></tr>\n" .
    $content .
    "</table>\n";
}

function formDate($sqlDate) {
  return substr($sqlDate, 8, 2) . "." . substr($sqlDate, 5, 2) . "." . substr($sqlDate, 0, 4). "&nbsp;" . substr($sqlDate, 10, 9);
}

function query_and_log($sql_query, $db) {
//echo $sql_query . "<br>";
//  if (substr($sql_query, 0, 6) != "SELECT")
    myWriteLog("\$sql_query = $sql_query");
  $res = $db->query($sql_query);
  if (DB::isError($res)) reportDie ("$PHP_SELF: '$sql_query' : " . $res->getMessage());
  return $res;
};

function myWriteLog($log_text) {
  global $log_file_name;

  $fileHandle = @fopen($log_file_name, 'a');
  @fwrite($fileHandle, "[" . date("Y-m-d H:i:s") . "] " . $log_text .  "\n");
  @fclose($fileHandle);
}

/**************************************************************************************************************************************************/
// HTML-Output

?>
<html>
<head>
<title>Partnerprämie für <?php echo $crvh_sid;?></title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  .textBig        {font-size: 12pt}
  .inputBig       {font-size: 12pt; margin: 0px}
  .inputBigRight  {font-size: 12pt; margin: 0px; text-align: right}
  .inputBigCenter {font-size: 12pt; margin: 0px; text-align: center}
  .buttonAction   {font-size: 12pt; font-weight:bold; font-style:normal; width:100px; height:24px; background:#1b12b9; color:#FFFFFF}
//  .buttonAction   {width:100px; height:22px; background:#1b12b9; color:#FFFFFF; font-size:10pt; font-weight:bold; font-style:normal; font-family:Helvetica,Arial}

//.button {
//  background-color: #4CAF50; /* Green */
//  border: none;
//  color: white;
//  padding: 15px 32px;
//  text-align: center;
//  text-decoration: none;
//  display: inline-block;
//  font-size: 16px;
//  margin: 4px 2px;
//  cursor: pointer;
//}

</style>
<script src="../js/jquery.js"></script>
<script type="text/javascript">
<!--

function checkAmount(no) {
  if ((document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim() != "") &&
    isNaN(document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim().replace(/,/,"."))) {
    alert("Betrag bitte in der Form '999999,99' eingeben.");
    document.getElementsByName("crvh_premium_amount_" + no)[0].value = "";
    document.getElementsByName("crvh_premium_amount_" + no)[0].focus();
  }
  // Sicherstellen, dass nur höchstens zwei Nachkommastellen eingegeben werden.
  var parts = document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim().replace(/,/,".").split(".");
  if (parts.length > 1) {
    if (parts[1].length > 2) {
      alert("Betrag bitte in der Form '999999,99' eingeben.");
      document.getElementsByName("crvh_premium_amount_" + no)[0].value = "";
      document.getElementsByName("crvh_premium_amount_" + no)[0].focus();
    } else {
      if (parts[1].length < 1) {
        document.getElementsByName("crvh_premium_amount_" + no)[0].value = document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim() + "00";
      } else {
        if (parts[1].length < 2) {
          document.getElementsByName("crvh_premium_amount_" + no)[0].value = document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim() + "0";
        }
      }
    }
  } else {
    if (parts[0].length > 0) {
      document.getElementsByName("crvh_premium_amount_" + no)[0].value = document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim() + ",00";
    }
  }
}

function ajaxRequestGet(url, data) {
  $.ajax({
     type: "GET",
     url: url,
     data: data,
     async: false,
     cache: false,
     success: function(msg){eval(msg);}
  });
}

function save_rows(crvh_id, no) {
  for (var i = 1; i < no; i++) {
    if (typeof(document.getElementsByName("crvh_premium_text_" + i)[0]) != 'undefined')
      save_row(crvh_id, 2, i);
  }
  if (document.getElementsByName("crvh_premium_text_" + no)[0].value.trim() != "" ||
    document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim() != "")
      save_row(crvh_id, 1, no);
  document.premium_details.submit();
}

function save_row(crvh_id, act, no) {

  if (act == 3)
    if (!confirm("Eintrag wirklich löschen?"))
      return;

  var crvh_premium_text = document.getElementsByName("crvh_premium_text_" + no)[0].value.trim();
  var crvh_premium_amount = document.getElementsByName("crvh_premium_amount_" + no)[0].value.trim();
  var crvh_premium_repeat = document.getElementsByName("crvh_premium_repeat_" + no)[0].checked;
  var crvh_premium_comment = document.getElementsByName("crvh_premium_comment_" + no)[0].value.trim();

  if ((act == 1 || act == 2) && (crvh_premium_text == "" || crvh_premium_amount == "")) {
    alert("Bitte mindestens die ersten beiden Felder ausfüllen!");
    return;
  }

//alert('act=' + act + '&crvh_id=' + crvh_id + '&no=' + no + '&gdc_content=' + no + "|" + crvh_premium_text + "|" + crvh_premium_amount + "|" + crvh_premium_repeat + "|" + crvh_premium_comment)
  ajaxRequestGet('../include/ajaxReqPremium_details.php', 'act=' + act + '&crvh_id=' + crvh_id + '&no=' + no + '&gdc_content=' + no + "|" + crvh_premium_text + "|" + crvh_premium_amount + "|" + crvh_premium_repeat + "|" + crvh_premium_comment);

  if (act == 3)
    document.premium_details.submit();
}

-->
</script>
</head>
<body>
  <div style="width:100%; height:10px;"> </div>  
  <div class="f10bp1_blue" style="text-align: center; font-size: 14pt">Abrechnung Export für <?php echo $crvh_sid;?></div>  
  <div style="width:100%; height:20px;"> </div>
  <table border="0" width="100%" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
    <tr>
      <td>
        &nbsp;
      </td>
      <td align="center">
        <table border="0" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
          <tr>
            <td align="center">              
              <span style="color:red;font-size:12pt"> <?php echo $error_text; ?> </span>
              <form name="premium_details" action="premium_details.php" method="post">
                <input type="hidden" name="f_crvh_id" value="<?php echo $crvh_id;?>">
                <?php echo $content; ?>
              </form>
            </td>
          </tr>
        </table>
      </td>
      <td>
        &nbsp;
      </td>
    </tr>
  </table>
</body>
</html>
