#include <infocon.style>

<page func=InfoCon title="Buchhaltung">
<calendar_init -5>

<style type="text/css">
span.in { color: green; }
span.out { color: red; }
span.in:hover, span.out:hover { background: yellow; border: 1px solid #ccc; }
</style>

<h3>Übersichten</h3>

<?
  $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
	 or die("Unable to connect to SQL server");
?>
<form method=POST action=list.php>
<input type=hidden name=blzkto value="<? echo $blzkto; ?>">
<b>Jahr:</b> <select name=year>
<option>
<?
  $query = "SELECT DISTINCT substr(date,0,5) AS year FROM sales ORDER BY substr(date,0,5) DESC";
  $sth = pg_exec ($dbh, $query);

  for ($nr=0; $nr <pg_NumRows ($sth); $nr++) {
    $cat = pg_fetch_array ($sth, $nr);
    printf ("<option value=\"%s\">%s", $cat["year"], $cat["year"]);
  }
?></select><br>
<b>Kategorie:</b> <select name=category>
<option>
<?
  $query = "SELECT DISTINCT category FROM sales ORDER BY category";
  $sth = pg_exec ($dbh, $query);

  for ($nr=0; $nr <pg_NumRows ($sth); $nr++) {
    $cat = pg_fetch_array ($sth, $nr);
    printf ("<option value=\"%s\">%s", $cat['category'], $cat['category']);
  }
?></select><br>
<b>Zahlung</b>: <input type=checkbox name=input value=1 checked> Einnahmen <input type=checkbox name=output value=1 checked> Ausgaben<br>

<p><center>
<input class=button type=submit name=submit value="List"> 
<input class=button type=submit name=submit value="Overview"> 
<input class=button type=submit name=submit value="Tally"> 
<input class=button type=reset value="Reset">
</center></p>
</form>

<?
  $form = 0;
  $query = "SELECT nr,date,description,price FROM sales_dm WHERE paid=0 AND visible=1 ORDER BY date,nr";
  $sth = pg_exec ($dbh, $query);

  if (pg_NumRows ($sth) > 0) {
    $form = 1;
    echo ("<h3>Nicht bezahlte Rechnungen</h3>");
    echo ("<form method=\"POST\" action=\"pay.php\">");
  }

  for ($nr=0; $nr <pg_NumRows ($sth); $nr++) {
    $row = pg_fetch_array ($sth, $nr);
    $date = substr ($row['date'],6,2) . "." . substr ($row['date'],4,2) . "." . substr ($row['date'],0,4);
    printf (" <input type=\"text\" id=\"date_%d\" name=\"date_%d\" size=\"7\" value=\"\">", $row['nr'], $row['nr']);
    printf (" <img class=\"calendar\" src=\"<root_prefix>pix/calendar.gif\" onclick=\"event.cancelBubble=true;popcalendar('date_%d');\" alt=\"\">", $row['nr']);
    printf (" %d. %s %s (%.2f&nbsp;DM)<br>", $row['nr'], $date, $row['description'], $row['price']);
  }

  $query = "SELECT nr,date,description,price FROM sales WHERE paid=0 AND visible=1 ORDER BY date,nr";
  $sth = pg_exec ($dbh, $query);

  if (!$form && pg_NumRows ($sth) > 0) {
    $form = 1;
    echo ("<h3>Nicht bezahlte Rechnungen</h3>");
    echo ("<form method=\"POST\" action=\"pay.php\">");
  }

  for ($nr=0; $nr <pg_NumRows ($sth); $nr++) {
    $row = pg_fetch_array ($sth, $nr);
    $date = substr ($row['date'],6,2) . "." . substr ($row['date'],4,2) . "." . substr ($row['date'],0,4);
    printf (" <input type=\"text\" id=\"date_%d\" name=\"date_%d\" size=\"6\" value=\"\">", $row['nr'], $row['nr']);
    printf (" <img class=\"calendar\" src=\"<root_prefix>pix/calendar.gif\" onclick=\"event.cancelBubble=true;popcalendar('date_%d');\" alt=\"\">", $row['nr']);
    printf (' <span class="%s">%d. %s %s (%.2f&nbsp;&euro;)</span><br>',
	    $row['price'] < 0 ? 'out' : 'in',
	    $row['nr'], $date, $row['description'], $row['price']);
  }

  if ($form > 0) {
    echo ("<p><input type=\"submit\" class=\"button\" value=\"Bezahlen\"> <input type=\"reset\" class=\"button\" value=\"Reset\"><p>");
    echo ("</form>");
  }
?>

</page>

# Local variables:
# mode: text
# mode: auto-fill
# end:
