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

include_once("../include/global.inc.php");
include_once("../include/caglobal.inc.php");
include_once("../include/global.inc.php");
include_once("../include/auth.inc.php");
include_once("inv.inc.php");

$pageTitel = "RECHNUNGS&Uuml;BERSICHT";
if ($usr_type == 3)
  $pageTitel = "AUFTR&Auml;GE";
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");

// customer or headquarter?
//$filter_costcenter_disabled = "disabled";
$costcenter_disabled_start = "";
$costcenter_disabled_end = "";
$_csc_id_ = "";
$_cs_eid_ = "&nbsp; ExtID:&nbsp;<input type=\"text\" class=\"f8np1\" name=\"cs_eid\" value=\"\" size=\"8\"> " .
  "&nbsp;Kundenname:&nbsp;<input type=\"text\" class=\"f8np1\" name=\"cmp_name\" value=\"\" size=\"12\">";
$_show_invoice_text_ =
  "&nbsp;&nbsp;<input type=\"checkbox\" name=\"show_invoice_text\" value=\"1\" title=\"Kundenrechnungstext für jeden Auftrag anzeigen\">&nbsp;Rechnungstext&nbsp;anzeigen\n";

if ($usr_type == 2):
  // customer (employee)
//  if ($csc_id == "") reportDie ("$PHP_SELF: Parameter 'csc_id' fehlt", false);
  $cmp_id = getFieldValueFromId("customer", "cs_id", "$cs_id", "cmp_id");
  $cmp_comp = getFieldValueFromId("company", "cmp_id", "$cmp_id", "cmp_comp");
  $csc_name = getFieldValueFromId("costcenter", "csc_id", "$csc_id", "csc_name");
  $kundenname = "$cmp_comp, Kostenstelle $csc_name";
//  $filter_costcenter_disabled = "";
  $_cs_eid_ = "";
  $_show_invoice_text_ = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
elseif ($usr_type == 1):
  // sysadmin
  $cmp_id = getFieldValueFromId("headquarters", "hq_id", "$hq_id", "cmp_id");
  $cmp_comp = getFieldValueFromId("company", "cmp_id", "$cmp_id", "cmp_comp");
  $kundenname = "Zentrale $cmp_comp";
elseif ($usr_type == 0):
  // headquarter
  $kundenname = "Systemadministrator";
elseif ($usr_type == 3):
  // courier
  $costcenter_disabled_start = "<!--";
  $costcenter_disabled_end = "-->";
  $_csc_id_ = "courier";
  $_cs_eid_ = "";
  if (INV_SHOW_JB_CR_REMARK == "1"):
    $_cs_eid_ = "&nbsp;&nbsp;&nbsp;Auftragsstatus:<input type=\"radio\" name=\"jb_status\" value=\"2\" checked>erledigt&nbsp;<input type=\"radio\" name=\"jb_status\" value=\"1\">laufend";
  endif;
  if (INV_SHOW_INVOICE_TEXT != "1"):
    $_show_invoice_text_ = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  endif;
  $courierListOptions = "";
  if (INV_SHOW_CR_ID_PARENT == "1"):
    $cr_id = getFieldValueFromId("courier", "usr_id", "$usr_id", "cr_id");
    $sqlquery = "SELECT cr_id FROM courier WHERE cr_id_parent = '$cr_id'";
    $res = $db->query($sqlquery);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
    $cr_id_clause = $cr_id;
    while ($row = $res->fetch_assoc()):
      $cr_id_clause .= "," . $row['cr_id'];
    endwhile;
    $res->free();
    // Kuriere zusammenstellen, damit danach selektiert werden kann
    $sqlquery = "SELECT crvh_sid FROM couriervehicle WHERE cr_id IN ($cr_id_clause) ORDER BY crvh_sid";
    $res = $db->query($sqlquery);
    if (DB::isError($res)) reportDie ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
    $courierListOptions = "Fahrzeug: <select class=\"f8np1\" name=\"cr_sid\">\n<option value=\"all\">alle</option>\n";    
    while ($row = $res->fetch_assoc()):
      $courierListOptions .= "<option value=" . $row["crvh_sid"] . ">" . $row["crvh_sid"] . "</option>\n";
    endwhile;
    $res->free();
    $courierListOptions .= "</select>\n";
    $_cs_eid_ = $courierListOptions . $_cs_eid_;
  endif;
else:
  reportDie ("$PHP_SELF: Ungültiger 'usr_type': '$usr_type'", false);
endif;

//include_once("../include/calendar.inc.php");
//$day_to = date("d");
//$month_to = date("m");
//$year_to = date("Y");
//list($year_from, $month_from) = addMonths($year_to, $month_to, -1);
//$day_from = $day_to + 1;
//if (daysPerMonth($year_from, $month_from) < $day_from)
//  $day_from = daysPerMonth($year_from, $month_from);
$day_to = date("d");
$day_from = 1;
$month_to = date("m");
$month_from = $month_to;
$year_to = date("Y");
$year_from = $year_to;

$_js_date_  = "  fillDateFields(1,\"inv_header\",\"invoiceOptions\",\"\",\"day_from\",\"month_from\",\"year_from\",\"\",\"\",\"\");\n";
$_js_date_ .= "  setDateTimeFields(\"inv_header\",\"invoiceOptions\",".$day_from.",".$month_from.",".$year_from.", \"\", \"\", \"day_from\", \"month_from\", \"year_from\", \"\", \"\", \"\");\n";
$_js_date_ .= "  fillDateFields(0, 'inv_header', 'invoiceOptions', 'year_from', 'day_from', 'month_from', 'year_from', '', '', '');\n";
$_js_date_ .= "  fillDateFields(1,\"inv_header\",\"invoiceOptions\", \"\", \"day_to\", \"month_to\", \"year_to\", \"\", \"\", \"\");\n";
$_js_date_ .= "  setDateTimeFields(\"inv_header\",\"invoiceOptions\",".$day_to.",".$month_to.",".$year_to.", \"\", \"\", \"day_to\", \"month_to\", \"year_to\", \"\", \"\", \"\");\n";
$_js_date_ .= "  fillDateFields(0, 'inv_header', 'invoiceOptions', 'year_to', 'day_to', 'month_to', 'year_to', '', '', '');\n";
//$_js_date_ .= ($_csc_id_ != "" ? "  csc_id = \"$_csc_id_\"\n" : "");

list ($jb_costcenter) = getHttpVars(array("jb_costcenter"));
$filter_costcenter = addOptionsFromTable("metatype", "mt_sort", "mt_value", "mt_sort",
  "mt_type = 'job_costcenter'", 2);
//$title = "&Uuml;bersicht der Rechnungsdaten";
?>

<html>
<head>
<title><?php echo $pageTitel; ?></title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>
<script src="../include/lib_global.js" type="text/javascript"></script>
<script type="text/javascript">

<!--

<?php echo $jsMenuOut; ?>

/* csc_id muss nach jedem Reload von 'inv_details.php' angepasst werden */
var csc_id = "<?php echo $_csc_id_; ?>";
var inv_list_params = "";
var prev_cs_eid = "";
var prev_cmp_name = "";

function initForm()
{
  if (top.frames.length <= 0)
  {
    alert ("Diese Seite ist Bestandteil eines Framesets und kann nicht direkt aufgerufen werden!\n\n" +
          "Bitte rufen Sie den entsprechenden Menüpunkt auf, damit die Inhalte dieser Seite im dazugehörigen Kontext angezeigt werden können."
    );
    var newURL = String(window.location.protocol) + "//" + String(window.location.hostname) + String(window.location.pathname);
    newURL = newURL.replace(/invoice\/inv_header.php/, "admin/start.php");
    window.location.href = newURL;
    return;
  }

  /*
    Initialisierung der Datumsfelder
    fillDateFields(init, frameIdx, formIdx, callerIdx, dayIdx, monIdx, yearIdx, hourIdx, minuteIdx, rhythmIdx)
  */
<!--
  <?php echo $_js_date_; ?>

  for (var i = 0; i < document.links.length; i++) {
    curHref = document.links[i].getAttribute('href');
    if (curHref.indexOf("currentNavigationItem") != -1)
      document.links[i].setAttribute("target", "_top", "false");
  }

  Event.observe('page_headline', 'click', function() {
    //  var rows = document.getElementById("winInvoice").rows;
    //  var myHeight = Number(rows.substr(0, rows.indexOf(",") - 1));
    var myHeight = 125
      + (((csc_id != "courier" && parent.document.body.clientWidth < 1400) || (csc_id == "courier" && parent.document.body.clientWidth < 1200)) ? 25 : 0)
      - ($('menu').visible() ? 0 : 65);  
    parent.document.getElementById("winInvoice").rows = String(myHeight) + ",*";
  });

  /*
    Die aktuellen Werte der Datumsfelder werden von inv_details.php gesetzt!
  */
}

function finishPageDoIt(csc_id_param)
{
  var addParams = "";
  var selectedCr_sid = "";
  var selectedJb_status = "";
  var selected_show_invoice_text = "";

  if (typeof document.invoiceOptions.show_invoice_text != "undefined")
    selected_show_invoice_text = document.invoiceOptions.show_invoice_text.checked;

  if (csc_id != "courier") // nur wenn csc_id auch wirklich eine verschlüsselte Kostenstellennumer enthält
  {
    if (csc_id_param == 0) csc_id_param = csc_id;
    if (typeof document.invoiceOptions.cs_eid != "undefined")
      addParams = "&cs_eid=" + document.invoiceOptions.cs_eid.value;
    if (typeof document.invoiceOptions.cmp_name != "undefined")
      addParams = addParams + "&cmp_name=" + document.invoiceOptions.cmp_name.value;

    parent.frames[2].location.href="inv_details.php" +
      "?csc_id=" + csc_id_param +
      "&day_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_from') +
      "&month_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_from') +
      "&year_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_from') +
      "&day_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_to') +
      "&month_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_to') +
      "&year_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_to') +
      "&jb_costcenter=" + getSelectedVal('inv_header', 'invoiceOptions', 'jb_costcenter_select') +
      "&show_invoice_text=" + selected_show_invoice_text +
      addParams +
      "&dbhistory=" + document.invoiceOptions.dbhistory.value;
  } else {
    if (typeof document.invoiceOptions.cr_sid != "undefined")
      selectedCr_sid = getSelectedVal('inv_header', 'invoiceOptions', 'cr_sid');
    if (typeof document.invoiceOptions.jb_status != "undefined") {
      if (document.invoiceOptions.jb_status[0].checked)
        selectedJb_status = document.invoiceOptions.jb_status[0].value;
      if (document.invoiceOptions.jb_status[1].checked)
        selectedJb_status = document.invoiceOptions.jb_status[1].value;
    }
    parent.frames[1].location.href="inv_details_cr.php" +
      "?csc_id=" + csc_id_param +
      "&day_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_from') +
      "&month_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_from') +
      "&year_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_from') +
      "&day_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_to') +
      "&month_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_to') +
      "&year_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_to') +
      "&cr_sid=" + selectedCr_sid +
      "&jb_status=" + selectedJb_status +
      "&show_invoice_text=" + selected_show_invoice_text +
      "&dbhistory=" + document.invoiceOptions.dbhistory.value +
      "&deactivateMenu=1";
  }
}

function finishPageDoItFull()
{
  var addParams = "";

  if (csc_id == "courier") {
    finishPageDoIt(0);
    return;
  }

  if (typeof document.invoiceOptions.cs_eid != "undefined")
  {
    addParams = "&cs_eid=" + document.invoiceOptions.cs_eid.value;
    if (prev_cs_eid != document.invoiceOptions.cs_eid.value)
    {
      prev_cs_eid = document.invoiceOptions.cs_eid.value;
      csc_id = "";
    }
  }
  if (typeof document.invoiceOptions.cmp_name != "undefined")
  {
    addParams = addParams + "&cmp_name=" + document.invoiceOptions.cmp_name.value;
    if (prev_cmp_name != document.invoiceOptions.cmp_name.value)
    {
      prev_cmp_name = document.invoiceOptions.cmp_name.value;
      csc_id = "";
    }
  }

  parent.frames[1].location.href="inv_list.php" +
    "?day_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_from') +
    "&month_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_from') +
    "&year_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_from') +
    "&day_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_to') +
    "&month_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_to') +
    "&year_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_to') +
    "&start=" + csc_id +
    inv_list_params +
    addParams +
    "&dbhistory=" + document.invoiceOptions.dbhistory.value;
}

function finishPage(csc_id_param)
{
//  var csc_id = csc_id_param;
  // wg. Timingproblemen wird eine Pause von 100 Millisekunden zwischengeschaltet
  setTimeout("finishPageDoIt('" + csc_id_param + "')", 100);
}

function finishPageFull()
{
  setTimeout("finishPageDoItFull()", 200);
}

function show_history()
{
  if (getSelectedVal('inv_header', 'invoiceOptions', 'cr_sid') == "all") {
    alert("Bitte ein Fahrzeug wählen!");
  } else {
    parent.frames[1].location.href="crHistoryUN.php" +
      "?day_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_from') +
      "&month_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_from') +
      "&year_from=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_from') +
      "&day_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'day_to') +
      "&month_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'month_to') +
      "&year_to=" + getSelectedNumVal('inv_header', 'invoiceOptions', 'year_to') +
      "&cr_sid=" + getSelectedVal('inv_header', 'invoiceOptions', 'cr_sid');
  }
}

-->
</script>
<noscript>
  <center>
  <b><br>JavaScript ist nicht verf&uuml;gbar. Bitte aktivieren Sie JavaScript<br><br>
        in Ihrem Browser, damit diese Seite ordnungsgem&auml;&szlig; funktioniert!</b><br><br>
  </center>
</noscript>
<style type="text/css">
<!--
.verysmall
{ font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 2pt; font-weight: normal; padding: 0px}
-->
</style>
</head>
<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>initForm();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

  <div style="float:left;">

  <form name="invoiceOptions" action="inv_details.php" method="post" target="inv_details">

<?php echo $costcenter_disabled_start ?>

  <div style="float:left; padding-top:5px;">
    <select class="f8np1" name="jb_costcenter_select" <?php echo isset($filter_costcenter_disabled) ? $filter_costcenter_disabled : ""; ?>>
      <?php echo $filter_costcenter ?>
    </select>
  </div>

<?php echo $costcenter_disabled_end ?>

  <div style="float:left; padding-top:5px;">
     <?php echo $_cs_eid_ ?>
  </div>
  
  </div>
  <div style="float:right;">

  <div style="float:left; padding-top:5px;">
    &nbsp;&nbsp;Datum erledigt von:&nbsp;
      <select name="day_from" class="f8np1"
        onchange="fillDateFieldsCheck(0, 'inv_header', 'invoiceOptions', 'day_from', 'day_from', 'month_from', 'year_from', '', '', '', 'day_to', 'month_to', 'year_to', '', '', '')">
      </select>
      <select name="month_from" class="f8np1"
        onchange="fillDateFieldsCheck(0, 'inv_header', 'invoiceOptions', 'month_from', 'day_from', 'month_from', 'year_from', '', '', '', 'day_to', 'month_to', 'year_to', '', '', '')">
      </select>
      <select name="year_from" class="f8np1"
        onchange="fillDateFieldsCheck(0, 'inv_header', 'invoiceOptions', 'year_from', 'day_from', 'month_from', 'year_from', '', '', '', 'day_to', 'month_to', 'year_to', '', '', '')">
      </select>
   &nbsp;bis:&nbsp;
      <select name="day_to" class="f8np1"
        onchange="fillDateFieldsCheck(0, 'inv_header', 'invoiceOptions', 'day_to', 'day_from', 'month_from', 'year_from', '', '', '', 'day_to', 'month_to', 'year_to', '', '', '')">
      </select>
      <select name="month_to" class="f8np1"
        onchange="fillDateFieldsCheck(0, 'inv_header', 'invoiceOptions', 'month_to', 'day_from', 'month_from', 'year_from', '', '', '', 'day_to', 'month_to', 'year_to', '', '', '')">
      </select>
      <select name="year_to" class="f8np1"
        onchange="fillDateFieldsCheck(0, 'inv_header', 'invoiceOptions', 'year_to', 'day_from', 'month_from', 'year_from', '', '', '', 'day_to', 'month_to', 'year_to', '', '', '')">
      </select>
      <input type="hidden" name="dbhistory" value="<?php echo isset($_db_history) ? $_db_history : ""; ?>">
  </div> 

  <div style="float:left; padding-top:5px;">
    <?php echo $_show_invoice_text_ ?>
  </div>

  <div style="float:left; padding-top:5px;">
    &nbsp;&nbsp;
  </div>
  <div style="float:left; padding-top:5px;">
  <?php echo defineButton("Anzeigen", "submit", "finishPageFull()", "left", "", "", "", "", "", "", "", "10pt", "", "", "", "Anzeigen (alt+a)", "a", false); ?>
  &nbsp;&nbsp;</div>
  <div style="float:left; padding-top:5px;">
  <?php echo defineButton("Log anzeigen", "submit_log", "show_history()", "left", "", "", "", "", "", "", "", "10pt", "", "", "", "Fahrzeug-Log anzeigen (alt+f)", "f", false); ?>
  &nbsp;&nbsp;
  </div>
  <div style="float:left; padding-top:5px;">
    &nbsp;
  </div>

  </form>
</div>

</div>

</body>
</html>
