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

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

// - Einstellungen für Datum und jb_costcenter
// berücksichtigen bei Link auf 'inv_details'

//list ($csc_id_list) =
getSecHttpVars("1", array("csc_id_list"));
$csc_id_http_list = explode(",", $csc_id_list);

list ($day_from, $month_from, $year_from,
  $day_to, $month_to, $year_to, $cs_eid, $cmp_name) =
    getHttpVars(array("day_from", "month_from", "year_from",
      "day_to", "month_to", "year_to", "cs_eid", "cmp_name"));
if ($day_from == "" || $month_from == "" || $year_from == "" || $day_to == "" || $month_to == "" || $year_to == ""):
//  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;
endif;
$fromDateRange = $year_from . "-" . pad($month_from,2) . "-" . pad($day_from,2) . " 00:00:00";
$toDateRange = $year_to . "-" . pad($month_to,2) . "-" . pad($day_to,2) . " 23:59:59";
$dateSelectionStmt = " AND ($dbh_jb.jb_finishtime BETWEEN '$fromDateRange' AND '$toDateRange')";
$dateParms =
  "&day_from=".$day_from."&month_from=".$month_from."&year_from=".$year_from."&day_to=".$day_to."&month_to=".$month_to."&year_to=".$year_to;

if ($usr_type != 2):
  // for (usr_type == 2) the list was already made in 'inv.inc.php'
  mkCsc_id_customer_list_complete(1);
endif;
$csc_id_customer_list_hasChildrenList = array();
$content = "";
$_csc_id_finish_page_start_ = "";
mkCostcenterList();

function mkCostcenterList()
{
  global $csc_id_customer_list, $csc_id, $usr_type,
    $content, $_csc_id_finish_page_start_, $_inv_list_params_;

  $timeofday1 = gettimeofday();
  //echo $timeofday1["sec"] . "." . sprintf("%06d", $timeofday1["usec"]) . "<br>";

//  mkCsc_id_customer_list_complete();
  mkCsc_id_customer_list();
//print_r($csc_id_customer_list);

  if ($usr_type == 2):
    for ($i = 0; $i < count($csc_id_customer_list); $i++):
//    echo $csc_id_customer_list[$i]["csc_id"] . " == $csc_id . <br>";
      if ($csc_id_customer_list[$i]["csc_id"] == $csc_id):
        // save parent of costcenter of current user for
        // the starting point of the tree
        $csc_id_start_parent = $csc_id_customer_list[$i]["csc_pre_id"];
        $csc_id_start_only = $csc_id_customer_list[$i]["csc_id"];
        $csc_id_finish_page_start = $csc_id_customer_list[$i]["csc_id"];        
        break;
      endif;
    endfor;
  else:
    // usr_type 0 and 1
    $csc_id_start_parent = "";
    $csc_id_start_only = "";
    $csc_id_finish_page_start = "";
    for ($i = 0; $i < count($csc_id_customer_list); $i++):
      if ($csc_id_customer_list[$i]["csc_pre_id"] == ""):
        $csc_id_finish_page_start = $csc_id_customer_list[$i]["csc_id"];
        break;
      endif;
    endfor;
  endif;

  if (count($csc_id_customer_list) == 0):
    $content .= "&nbsp;<br><b>(Keine Kunden gefunden)</br>";
  else:
    mkCsc_id_customer_listHasChildrenList();
    writeCostcenterList($csc_id_start_parent, $csc_id_start_only, "");
    $csc_id_finish_page_start = ec($csc_id_finish_page_start);
    list($start) = getHttpVars(array("start"));
    if ($start != "")
      $csc_id_finish_page_start = $start;      
    list($csc_id_param, $csc_id_list_param) = getHttpVars(array("csc_id", "csc_id_list"));
    if ($csc_id_param == "")
      $csc_id_param = $csc_id_finish_page_start;
    $_csc_id_finish_page_start_ = $csc_id_finish_page_start;
    $_inv_list_params_ = "&csc_id=$csc_id_param&csc_id_list=$csc_id_list_param";
  endif;
}

function writeCostcenterList($csc_id_show_parent, $csc_id_show_only, $depth_str)
{
  global $content, $cs_id, $csc_id, $csc_id_customer_list, $csc_id_http_list, $csc_id_list,
    $csc_id_customer_list_hasChildrenList, $dateParms, $cs_eid, $cmp_name;

  // write costcenterlist of current parent
  // (being the first element in costcenter path list)
  for ($i = 0; $i < count($csc_id_customer_list); $i++):
    $followup = false;
//echo ("$i '" . $csc_id_show_parent . "' '" . $csc_id_customer_list[$i]["csc_pre_id"]) . "'<br>";
    if ($csc_id_customer_list[$i]["csc_pre_id"] == $csc_id_show_parent &&
      ($csc_id_show_only == "" || $csc_id_customer_list[$i]["csc_id"] == $csc_id_show_only)):
      // current costcenter is child of csc_id to be shown
      if (in_array($csc_id_customer_list[$i]["csc_id"], $csc_id_http_list)):
        // current costcenter has previously been opened by click
        // - it gets the "can be closed"-mark
        // (below, showing its children follows)
        $childrenlink =
          "<a href=\"$PHP_SELF?csc_id=" . ec($csc_id) . "&csc_id_list=" .
          ec(trim(
            str_replace("," . $csc_id_customer_list[$i]["csc_id"] . ",", ",", "," . $csc_id_list . ","),
            ",")) .
          "$dateParms&cs_eid=$cs_eid&cmp_name=$cmp_name&start=" . $csc_id_customer_list[$i]["csc_id"] . "#$i\"><img src=\"../images/m.gif\" border=0></a>";
        $followup = true;
      elseif ($csc_id_customer_list_hasChildrenList[$csc_id_customer_list[$i]["csc_id"]] == true):
        // current costcenter has children but is not opened
        // - it gets the "can be opened"-mark
        $childrenlink =
          "<a href=\"$PHP_SELF?csc_id=" . ec($csc_id) . "&csc_id_list=" .
          ec($csc_id_list .
            (($csc_id_list == "") ? "" : ",") .
            $csc_id_customer_list[$i]["csc_id"]) .
          "$dateParms&cs_eid=$cs_eid&cmp_name=$cmp_name&start=" . $csc_id_customer_list[$i]["csc_id"] . "#$i\"><img src=\"../images/p.gif\" border=0></a>";
      else:
        // current costcenter has no children
        // it gets the "cannot be opened" - mark
        $childrenlink = "<img src=\"../images/o.gif\">";
      endif;
      // write current costcenter
      // calling inv_details.php shall not forget the options set
      // in the header-frame
      $content .= "<tr><td align=\"left\">" .
        "<a name=\"$i\">" . $depth_str . "</a>" . $childrenlink .
        "&nbsp;<a href=\"javascript:finishPage('" .
        ec($csc_id_customer_list[$i]["csc_id"]) . "')\">" .
        $csc_id_customer_list[$i]["csc_name"] . "</a>" .
        "</td></tr>\n";
// echo "$i: " . ($csc_id_customer_list[$i]["csc_id"]) . "<br>";
      // if current costcenter is opened, continue there
      if ($followup):
        writeCostcenterList($csc_id_customer_list[$i]["csc_id"], "" ,$depth_str . "&nbsp;&nbsp;&nbsp;");
      endif;
    else:

    endif;
  endfor;
}

//function getCostcenterParentList($csc_id_child)
//{
//  global $csc_id_root, $csc_id_customer_list;
//
//  if ($csc_id_child == $csc_id_root)
//    return "";
//  for($i = 0; $i < count($csc_id_customer_list); $i++):
//    if ($csc_id_customer_list[$i]["csc_id"] == $csc_id_child)
//      return $csc_id_customer_list[$i]["csc_pre_id"] . " " .
//        getCostcenterParentList($csc_id_customer_list[$i]["csc_pre_id"]);
//  endfor;
//}

function mkCsc_id_customer_listHasChildrenList()
{
  global $csc_id_customer_list, $csc_id_customer_list_hasChildrenList;

  for($i = 0; $i < count($csc_id_customer_list); $i++):
    $csc_id_customer_list_hasChildrenList[$csc_id_customer_list[$i]["csc_id"]] = false;
  endfor;
  for($i = 0; $i < count($csc_id_customer_list); $i++):
    $csc_id_customer_list_hasChildrenList[$csc_id_customer_list[$i]["csc_pre_id"]] = true;
  endfor;
}

//function costcenterHasChildren($csc_id_parent)
//{
//  global $csc_id_customer_list;
//echo date("H:i:s") . "<br>";
//  for($i = 0; $i < count($csc_id_customer_list); $i++):
//    if ($csc_id_customer_list[$i]["csc_pre_id"] == $csc_id_parent)
//      return true;
//  endfor;
//  return false;
//}
?>

<html>
<head>
<title>KUNDEN</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<script type="text/javascript">
<!--

function finishPage(csc_id)
{
//  parent.frames[0].csc_id = csc_id;
  parent.frames[0].finishPage(csc_id);
//  parent.frames[0].finishPage(csc_id);
  return;
}

function init(csc_id, inv_list_params)
{
  parent.frames[0].inv_list_params = inv_list_params;
  if (csc_id != 0 && csc_id != "____") {
    finishPage(csc_id);
  }
  return;
}

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

-->
</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>
</head>
<body onload="init('<?php echo $_csc_id_finish_page_start_;?>','<?php echo $_inv_list_params_;?>')">
  <table border=0 width=100% cellspacing=0 cellpadding=0 vspace=0 hspace=0>
    <tr>
      <td>
        &nbsp;
      </td>
      <td align="center">
        <table border=0 width=100% cellspacing=0 cellpadding=0 vspace=0 hspace=0>
          <?php echo $content;?>
        </table>
      </td>
      <td>
        &nbsp;
      </td>
    </tr>
  </table>
</body>
</html>