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

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

$today = getdate();

$month = $today['mon'];
$year = $today['year'];
if ($today['mday'] < 15):
  $month--;
  if ($month == 0):
    $month = 12;
    $year--;
  endif;
endif;

//$month = 3;

$date_from1 = $year . "-" . sprintf("%02d", $month) . "-01 00:00:00";
$date_from1_text = substr($date_from1, 8, 2) . "." . substr($date_from1, 5, 2) . "." . substr($date_from1, 0, 4);
$date_to1 = $year . "-" . sprintf("%02d", $month) . "-" . daysPerMonth($year, $month) . " 23:59:59";
$date_to1_text = substr($date_to1, 8, 2) . "." . substr($date_to1, 5, 2) . "." . substr($date_to1, 0, 4);
$date_from2 = $year . "-" . sprintf("%02d", $month - 1) . "-01 00:00:00";
$date_to2 = $year . "-" . sprintf("%02d", $month + 1) . "-" . daysPerMonth($year, $month + 1) . " 23:59:59";

$cs_id = getFieldValueFromId("customer", "cs_eid", "$cs_eid1", "cs_id");
$csc_id = getFieldValueFromId("costcenter", "cs_id", "$cs_id", "csc_id");

$sqlquery = "SELECT jb_id, jb_ordertime, jb_fixprice, jb_freetext_2 FROM job WHERE csc_id_payer = $csc_id AND jb_ordertime > '$date_from1' AND jb_ordertime < '$date_to1' ORDER BY jb_id";
//echo $sqlquery . "<br>";
$res = $db2->query($sqlquery);
if (DB::isError($res))
  reportDie ("$PHP_SELF: '$sqlquery' : " . $res->getMessage());
// Lieferscheinnummern (delivery note numbers) herausprökeln
$dnnArr = array();
$dnnSum = 0;
while ($row = $res->fetch_assoc()):
  $jb_freetext_2Arr = explode("²", $row["jb_freetext_2"]);
  $jb_freetext_2dnnArr = explode("¹", $jb_freetext_2Arr[0]);
  $jb_freetext_2priceArr = explode("¹", $jb_freetext_2Arr[3]);
  for($i = 0; $i < count($jb_freetext_2dnnArr); $i++):
    $dnnArr[] = array($row['jb_id'],
      substr($row['jb_ordertime'], 8, 2) . "." . substr($row['jb_ordertime'], 5, 2) . "." . substr($row['jb_ordertime'], 0, 4) . " " . substr($row['jb_ordertime'], 11, 2) . ":" . substr($row['jb_ordertime'], 14, 2)/* . ":" . substr($row['jb_ordertime'], 17, 2)*/,
      trim($jb_freetext_2priceArr[$i]), trim($jb_freetext_2dnnArr[$i]), "-", "--.--.---- --:--", "-,--", "-");
    $dnnSum += str_replace(",", ".", trim($jb_freetext_2priceArr[$i]));
  endfor;
endwhile;
$res->free();
//Nach Lieferscheinnummer sortieren
usort ($dnnArr, "cmp");
//print_r($dnnArr);
//die();

$cs_id = getFieldValueFromId("customer", "cs_eid", "$cs_eid2", "cs_id");
//$csc_id = getFieldValueFromId("costcenter", "cs_id", "$cs_id", "csc_id");
$csc_id = $db->getOne("SELECT csc_id FROM costcenter WHERE cs_id = '$cs_id' AND csc_is_extern = 0");

$sqlquery = "SELECT jb_id, jb_ordertime, jb_fixprice, jb_freetext_2 FROM job WHERE csc_id_payer = $csc_id AND jb_ordertime > '$date_from2' AND jb_ordertime < '$date_to2' ORDER BY jb_id";
//echo $sqlquery . "<br>";

$res = $db2->query($sqlquery);
if (DB::isError($res))
  reportDie ("$PHP_SELF: '$sqlquery' : " . $res->getMessage());
$tmpSum = 0;
while ($row = $res->fetch_assoc()):
	$tmp_freetext_2Arr = explode("²", $row['jb_freetext_2']);
  $tmp_freetext_2dnnArr = explode("¹", $tmp_freetext_2Arr[0]);
  $tmp_freetext_2priceArr = explode("¹", $tmp_freetext_2Arr[3]);
  for($i = 0; $i < count($tmp_freetext_2dnnArr); $i++):
    for($j = 0; $j < count($dnnArr); $j++):
      if (trim($tmp_freetext_2dnnArr[$i]) == $dnnArr[$j][3] && $dnnArr[$j][7] == "-"):
//echo "'" . trim($tmp_freetext_2dnnArr[$i]) .  "' ? '" . $dnnArr[$j][3] ."'<br>\n";
        $dnnArr[$j][4] = $row["jb_id"];
        $dnnArr[$j][5] = substr($row['jb_ordertime'], 8, 2) . "." . substr($row['jb_ordertime'], 5, 2) . "." . substr($row['jb_ordertime'], 0, 4) . " " . substr($row['jb_ordertime'], 11, 2) . ":" . substr($row['jb_ordertime'], 14, 2) /*. ":" . substr($row['jb_ordertime'], 17, 2)*/;
        $dnnArr[$j][6] = trim($tmp_freetext_2priceArr[$i]);
        $dnnArr[$j][7] = trim($tmp_freetext_2dnnArr[$i]);
        $tmpSum += str_replace(",", ".",trim($tmp_freetext_2priceArr[$i]));
        break;
      endif;
    endfor;
  endfor;
endwhile;
$res->free();

//print_r($dnnArr);
//die();

foreach($dnnArr as $dnn) {
  $content .= "<tr><td><div class=\"f7np1\">&nbsp;" . $dnn[7] . " / " . $dnn[4] . "&nbsp;</td><td><div class=\"f7np1\">&nbsp;" . $dnn[5] . "&nbsp;</div></td><td align=right><div class=\"f7np1\">&nbsp;" . $dnn[6] .
    "&nbsp;</div></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><div class=\"f7np1\">&nbsp;" .
    $dnn[3] . " / " . $dnn[0] . "&nbsp;</div></td><td><div class=\"f7np1\">&nbsp;" . $dnn[1] . "&nbsp;</div></td><td align=right><div class=\"f7np1\">&nbsp;" . $dnn[2] . "&nbsp;</div></td></tr>\n";
}

$content .= "</div><td colspan=\"7\"> <div class=\"verysmall\">" .
 "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" .
 "</div></td><div class=\"f7np1\">";

$content .= "<tr><td><div class=\"f7np1\">&nbsp;<b>Summe</b>&nbsp;</div></td><td><div class=\"f7np1\">&nbsp;&nbsp;</div></td><td align=right><div class=\"f7np1\">&nbsp;<b>" .
 str_replace(".", ",", sprintf("%01.2f", trim($tmpSum))) .
 "</b>&nbsp;</div></td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><div class=\"f7np1\">&nbsp;" .
 "<b>Summe</b>&nbsp;</div></td><td><div class=\"f7np1\">&nbsp;&nbsp;</div></td><td align=right><div class=\"f7np1\">&nbsp;<b>" .
 str_replace(".", ",", sprintf("%01.2f", trim($dnnSum))) .
 "</b>&nbsp;</div></td></tr>\n";

function cmp ($a, $b) {
    if ($a[3] == $b[3]):
      if ($a[0] == $b[0]) return 0;
      return ($a[0] < $b[0]) ? -1 : 1;
    endif;
    return ($a[3] < $b[3]) ? -1 : 1;
}

?>

<html>
<head>
<title>LIEFERSCHEIN&Uuml;BERSICHT</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<script src="../include/lib_global.js" type="text/javascript">
</script>
<style type="text/css">
<!--
.verysmall
{ font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 2pt; font-weight: normal; padding: 0px}
-->
</style>
</head>
<body>
  <table border="0" width="100%" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
    <tr>
      <td>
        &nbsp;
      </td>
      <td align="center">
        <table border="0" align="left" cellspacing="0" cellpadding="0" vspace="0" hspace="0">
          <tr>
          	<td align="center" colspan="7"> <div class="f10bp1_blue">
              Lieferschein&uuml;bersicht für <?php echo $cs_eid2;?> vom <?php echo $date_from1_text;?> bis <?php echo $date_to1_text;?></div>
            </td>
          </tr>
          <tr>
            <td colspan="7"> <div class="f12bp1_blue">
              &nbsp;
            </div></td>
          </tr>
          <tr>
          	<td>
          		<div class="f7np1">&nbsp;<b>Lieferschein-/Auftragsnr.</b>&nbsp;</div></td><td><div class="f7np1">&nbsp;<b><b>Auftragsdatum</b>&nbsp;</div></td><td align=right><div class="f7np1">&nbsp;<b>EUR</b>&nbsp;</div></td><td>&nbsp;</td><td>
        	    <div class="f7np1">&nbsp;<b>Lieferschein-/Auftragsnr.</b>&nbsp;</div></td><td><div class="f7np1">&nbsp;<b><b>Auftragsdatum</b>&nbsp;</div></td><td align=right><div class="f7np1">&nbsp;<b>EUR</b>&nbsp;</div>
            </td>  
          </tr>
          <tr>
            <td colspan="7"> <div class="verysmall">
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            </div></td>
          </tr>
          <tr>
            <?php echo $content;?>
          </tr>
        </table>
      </td>
      <td>
        &nbsp;
      </td>
    </tr>
  </table>
</body>
</html>
