<?php
/*=======================================================================
 *
 * inv_courier.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: Link auf Auftragsdetails (Popup)
// - include Start/Ziel
// - make it sortable

$hq_id = $HTTP_SESSION_VARS['hq_id'];
$hq_cmp_id = getFieldValueFromId("headquarters", "hq_id", "$hq_id", "cmp_id");
$hq_cmp_comp = getFieldValueFromId("company", "cmp_id", "$hq_cmp_id", "cmp_comp");
$usr_id = $HTTP_SESSION_VARS['usr_id'];
$usr_type = getFieldValueFromId("user", "usr_id", "$usr_id", "usr_type");
if ($usr_type == 3):
  $cr_id = getFieldValueFromId("courier", "usr_id", "$usr_id", "cr_id");
  $cr_sid = getFieldValueFromId("courier", "usr_id", "$usr_id", "cr_sid");
  $cmp_id = getFieldValueFromId("courier", "usr_id", "$usr_id", "cmp_id");
else:
  reportDie ("$PHP_SELF: Ungültiger 'usr_type': '$usr_type'", false);
endif;

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

$jb_status = 2;
if (INV_SHOW_JB_CR_REMARK == "1"):
  list ($jb_status, $jb_id, $jb_cr_remark) = getHttpVars(array("jb_status", "jb_id", "jb_cr_remark"));
  if ($jb_status == "")
    $jb_status = 2;
  if ($jb_id != ""):
    $sqlStmt = "UPDATE job SET jb_cr_remark = '$jb_cr_remark' WHERE jb_id = '$jb_id'";
    $result = $db->query($sqlStmt);
    if (DB::isError($result))
      reportDie ("$PHP_SELF: <br>$sqlStmt<br>" . $result->getMessage());
  endif;
endif;

$cmp_name = getFieldValueFromId("company", "cmp_id", "$cmp_id", "cmp_comp");
$tableHeader =
  "<td align=\"center\" colspan=\"8\"><b>$cmp_name, $hq_cmp_comp: " . ($jb_status == 1 ? "Laufende" : "Erledigte") . " Auftr&auml;ge" .
  "</b><br><br>";

$price_title = "Preis";
check_daterange();
if (INV_SHOW_JB_CR_REMARK == "1"):
  if ($jb_status == "1")
    $dateSelectionStmt = str_replace("jb_finishtime", "jb_ordertime", $dateSelectionStmt);
endif;

$cr_id_clause = "$dbh_jb.cr_id = $cr_id";
// check whether sub-couriers are to be shown
if (INV_SHOW_CR_ID_PARENT == "1"):
  $cr_id_clause = $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());
  while ($row = $res->fetch_assoc()):
    $cr_id_clause .= "," . $row['cr_id'];
  endwhile;
  $res->free();
  $cr_id_clause = " $dbh_jb.cr_id IN ($cr_id_clause)";
  list ($sel_cr_sid) = getHttpVars(array("cr_sid"));
  if ($sel_cr_sid != "" && $sel_cr_sid != "all")
    $cr_id_clause .= " AND $dbh_jb.cr_sid = '$sel_cr_sid'";
endif;
//echo $cr_id_clause;
// get all finished jobs of current hq of current courier
//$sqlquery = "SELECT DISTINCT $dbh_jb.jb_id, $dbh_jb.csc_id, $dbh_jb.jb_ordertime, " .
//  "$dbh_jb.cr_id, $dbh_jb.jb_finishtime, $dbh_jb.emp_id, $dbh_jb.jb_fixprice, $dbh_jb.csc_id_payer, $dbh_tr.csc_id AS csc_id_tour " .
//  "FROM $dbh_jb, $dbh_tr WHERE $dbh_jb.hq_id = '$hq_id' " .
//  "AND $dbh_jb.cr_id = $cr_id AND $dbh_jb.jb_id = $dbh_tr.jb_id " .
//  "AND $dbh_jb.jb_status = 2" .
//  " ORDER BY $dbh_jb.jb_finishtime, $dbh_tr.tr_sort";
$sqlquery = "SELECT $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_cr_filter, " .
  (INV_JB_INVOICE_CR == "1" ? "$dbh_jb.jb_invtext_cr AS jb_invtext, " : "$dbh_jb.jb_invtext, ") .  
  "$dbh_jb.jb_cr_remark, $dbh_tr.csc_id AS csc_id_tour, $dbh_tr.tr_comp, $dbh_tr.tr_person, $dbh_tr.tr_remark, $dbh_tr.tr_hsno, " .
  "address.ad_street, address.ad_zipcode, address.ad_city " .
  "FROM $dbh_jb, $dbh_tr, address WHERE " . /* $dbh_jb.hq_id = '$hq_id' AND . */
  $cr_id_clause . " AND $dbh_jb.jb_id = $dbh_tr.jb_id" .
  " AND $dbh_jb.jb_status = $jb_status " . $dateSelectionStmt .
  " AND $dbh_tr.ad_id = address.ad_id " .
  " ORDER BY $dbh_jb.jb_finishtime, $dbh_jb.jb_id";
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, "courier");
?>

<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>
<?php include_once ("../include/js_framework.inc.php"); ?>
<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_cr.php/, "admin/start.php");
    window.location.href = newURL;
    return;
  }

  parent.frames[0].csc_id = "courier";
}

function mkJb_cr_remark(form_id_hide,form_id_show) {
  myhide(form_id_hide);
  myshow(form_id_show);
//  $(form_id_hide).hide();
//  $(form_id_show).show();
};

function doDelete(elem_id){
  document.getElementById(elem_id).value = "";
}

function doSave(form_id){
  document.getElementById(form_id).submit();
}

// Quelle: http://www.nak-webmaster.de/modules.php?name=News&file=article&sid=71\n" +
function countDigits(elem_id){
  var maxDigits = 255;   //maximale Zeichenanzahl

  if (document.getElementById(elem_id).value.length > maxDigits - 1) {
    //die folgende Zeile entfernt alle Zeichen die  > maxDigits sind
    document.getElementById(elem_id).value = document.getElementById(elem_id).value.substring(0, maxDigits);
  }
}

-->
</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>
