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

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

// TOBEDONE:
// - csc_inv_interval (Rechnungsuntervall)
// - csc_inv_type (Art der Rechnungstellung)
// Verschiedene Sortierungen
// Rechnungslauf für headquarter

if ($usr_type == 2):
  // customer (employee)
  if ($csc_id == ""): reportDie ("$PHP_SELF: Parameter 'csc_id' fehlt", false); endif;
elseif ($usr_type == 1 || $usr_type == 0):
  // headquarter and sysadmin see all jobs of current customer
  if ($csc_id != "" && $csc_id != "undefined"):
    $cs_id = getFieldValueFromId("costcenter", "csc_id", "$csc_id", "cs_id");
  else:
    $_tableHeader_ = "&nbsp;<br><center><b>Bitte links einen Kunden ausw&auml;hlen!</b></center>";
    $_csc_id_ = "''";
    $content = "";
  endif;
  // rebuild $csc_id_customer_list_complete
//  $csc_id_customer_list_complete = array();
  mkCsc_id_customer_list_complete(1);
else:
  reportDie ("$PHP_SELF: Ungültiger 'usr_type': '$usr_type'", false);
endif;

if (($usr_type == 1 || $usr_type == 0 || $usr_type == 2) && ($csc_id != "" && $csc_id != "undefined")):
  mkCsc_id_customer_list();
  // get list of all costcenters that are to be shown for current csc_id (without externals)
  $csc_id_list_sql = "";
  for ($i = 0; $i < count($csc_id_customer_list); $i++):
    if (costcenterIsParent($csc_id, $csc_id_customer_list[$i]["csc_id"])):
      // Only if the costcenter of the current csc_id
      // is parent of the current costcenter, then take it
      $csc_id_list_sql .= $csc_id_customer_list[$i]["csc_id"] . ",";
    endif;
  endfor;
  $csc_id_list_sql = trim($csc_id_list_sql, ",");

  check_daterange();

  // check costcenterrange
  $cscSelectionStmt = "";
  if ($jb_costcenter == "")
    // default: including sub-costcenters
    $jb_costcenter = "2";
  if ($jb_costcenter == "1")
    // selected costcenter exclusively
    $cscSelectionStmt = " AND (($dbh_jb.csc_id_payer = $csc_id)" .
      " OR ($dbh_jb.csc_id_payer = 0 AND $dbh_jb.jb_id = $dbh_tr.jb_id AND $dbh_tr.csc_id = $csc_id))";

  // find all finished jobs for current costcenter / headquarter
  // finished jobs in the table 'job' aren't invoiced yet,
  // invoiced jobs live in jobhistory (happy until they die...)

  // wenn Root-Kostenstelle, dann Name aus company nehmen      
  if (trim($db2->getOne("SELECT csc_pre_id FROM costcenter WHERE csc_id = '" . $csc_id . "'")) == ""):
    $csc_name = $db2->getOne(
      "SELECT company.cmp_comp FROM company, customer, costcenter" .
      " WHERE customer.cs_id = costcenter.cs_id AND company.cmp_id = customer.cmp_id " .
      " AND costcenter.csc_id = '" . $csc_id . "'");
  else:
    // wenn Unterkostenstelle, dann Kostenstellennamen
    $csc_name = $db2->getOne("SELECT csc_name FROM costcenter WHERE csc_id = '" . $csc_id . "'");
  endif;

  $_tableHeader_ =
    "<td align=\"center\" colspan=\"8\">" .
    "<span class=\"verysmall\"> &nbsp;<br>&nbsp;<br></span>" .
    "<b>$csc_name" .
    (($jb_costcenter == "2") ? " (inkl. untergeordnete KST)" : "") .
    "</b><br><br>";

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

  // get all finished jobs where csc_id_payer is in list of costcenters to be shown
  // if csc_id_payer = 0, then costsplit: csc_id of the tour stations must be in this list
  $sqlquery = "SELECT DISTINCT $dbh_jb.jb_id, $dbh_jb.csc_id, $dbh_jb.jb_ordertime, " .
    "$dbh_jb.cr_id, $dbh_jb.cr_sid, $dbh_jb.jb_finishtime, $dbh_jb.jb_booktime, $dbh_jb.emp_id, $dbh_jb.jb_fixprice, $dbh_jb.csc_id_payer, $dbh_jb.jb_storno, $dbh_jb.jb_invtext, " .
    "$dbh_jb.jb_incomplete, $dbh_jb.jb_export_time, $dbh_tr.csc_id AS csc_id_tour " .
    "FROM $dbh_jb, $dbh_tr " .
    "WHERE $dbh_jb.hq_id = '$hq_id' " .
    "AND $dbh_jb.jb_id = $dbh_tr.jb_id " .
    "AND (($dbh_jb.csc_id_payer IN ($csc_id_list_sql)) " .
    "OR ($dbh_jb.csc_id_payer = 0 AND $dbh_jb.jb_id = $dbh_tr.jb_id AND $dbh_tr.csc_id IN ($csc_id_list_sql))" .
    ") " .
    "AND $dbh_jb.jb_status = 2 AND ($dbh_jb.jb_storno IS NULL OR $dbh_jb.jb_storno < 2)" .
    $dateSelectionStmt . $cscSelectionStmt .
    " ORDER BY $dbh_jb.jb_finishtime, $dbh_tr.jb_id, $dbh_tr.tr_sort";
//echo $sqlquery;

  list ($show_invoice_text) = getHttpVars(array("show_invoice_text"));
  $job_list = mk_job_list($sqlquery, $show_invoice_text);
  $content = "";
  print_job_list($job_list, $content);
  $_csc_id_ = "'" . ec($csc_id) . "'";
endif;

?>

<html>
<head>
<title>AUFTR&Auml;GE</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<!--
<link rel="stylesheet" type="text/css" href="../css/phoenix-print.css">
-->
<script src="../include/lib_global.js" type="text/javascript">
</script>
<script type="text/javascript">
<!--

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_details.php/, "admin/start.php");
    window.location.href = newURL;
    return;
  }

  parent.frames[0].csc_id = <?php echo $_csc_id_;?>
}

-->
</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="initForm()">
  <table border="0" width="100%" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
    <tr>
      <td>
        &nbsp;
      </td>
      <td align="center">
        <table width="100%" align="left" cellspacing="0" cellpadding="4" vspace="0" hspace="0">
          <tr>
              <?php echo $_tableHeader_;?>
          </tr>

          <?php echo $content;?>

          <tr>
            <td>
              &nbsp;
            </td>
          </tr>
        </table>
      </td>
      <td>
        &nbsp;
      </td>
    </tr>
  </table>
</body>
</html>
