+#include <infocon.style>
+
+<page title="Kontoführung">
+
+<headline "Buchhaltung">
+
+<table width=100% border=1 cellpadding=2 cellspacing=0>
+<tr>
+ <th width=15%>Datum</th>
+ <th width=20%>Kategorie</th>
+ <th width=50%>Verwendungszweck</th>
+ <th width=15%>Betrag</th>
+</tr>
+<?
+ $dbh = pg_pconnect ("kuolema", "5432", "<db>")
+ or die("Unable to connect to SQL server");
+
+ pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
+
+ if (strlen($year)) {
+ # $where[] = "date >= '$year-01-01'";
+ # $year++;
+ # $where[] = "date < '$year-01-01'";
+ $where[] = "date ~* '$year'";
+ }
+ if (strlen($category)) {
+ $where[] = "category = '$category'";
+ }
+ if ($input && !$output) {
+ $where[] = "price > 0.0";
+ } elseif ($output && !$input) {
+ $where[] = "price < 0.0";
+ }
+
+ $query = "SELECT date,oid,category,description,price FROM sales ";
+ if (count($where) > 0) {
+ $query .= " WHERE " . implode ($where, " AND ");
+ }
+ $query .= " ORDER BY date,oid";
+ $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
+
+ $sum = 0.0;
+ for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
+ $row = pg_fetch_array ($sth, $nr);
+
+ # $date = explode (" ", $row['date']);
+ # $date = explode ("-", $date[0]);
+ # $date = sprintf ("%d.%d.%d", $date[2], $date[1], $date[0]);
+ $date = substr ($row['date'],6,2) . "." . substr ($row['date'],4,2) . "." . substr ($row['date'],0,4);
+
+ echo ("<tr>");
+ printf ("<td width=\"15%%\" align=\"right\">%s</td>", $date);
+ printf ("<td width=\"20%%\">%s</td>", $row['category']);
+ printf ("<td width=\"50%%\"><a href=\"edit.php3?oid=%d\">%s</a></td>", $row['oid'], $row['description']);
+ printf ("<td width=\"15%%\" align=\"right\">%5.2f</td>", $row['price']);
+ $sum += $row['price'];
+ echo ("</tr>");
+ }
+ echo ("<tr>");
+ echo ("<td width=\"85%\" colspan=\"3\">Summe</td>");
+ printf ("<td width=\"15%%\" align=\"right\">%5.2f</td>", $sum);
+ echo ("</tr>");
+
+ if (!strlen($category)) {
+
+ $query = "SELECT DISTINCT category FROM sales ";
+ if (strlen($year)) {
+ $query .= "WHERE date ~* '$year'";
+ }
+
+ $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
+
+ for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
+ $row = pg_fetch_array ($sth, $nr);
+ $catz[] = $row['category'];
+ }
+
+ if (count ($catz) > 0) {
+ echo ("<h3>Nach Kategorien sortiert</h3>");
+ echo ("<table width=\"100%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">");
+ echo ("<tr><th width=\"70%\">Kategorie</th><th width=\"10%\">Einnahmen</th><th width=\"10%\">Ausgaben</th>"
+ ."<th width=\"10%\">Summe</th></tr>");
+ sort ($catz);
+
+ for ($i=0; $i < count ($catz); $i++) {
+ $query = sprintf ("SELECT SUM (price) FROM sales WHERE category = '%s' AND price > 0.0", $catz[$i]);
+ if (strlen($year)) {
+ $query .= " AND date ~* '$year'";
+ }
+ $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
+ $row = pg_fetch_array ($sth, 0);
+ $input = $row[0]; $sum_input += $input;
+ $query = sprintf ("SELECT SUM (price) FROM sales WHERE category = '%s' AND price < 0.0", $catz[$i]);
+ if (strlen($year)) {
+ $query .= " AND date ~* '$year'";
+ }
+ $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
+ $row = pg_fetch_array ($sth, 0);
+ $output = -$row[0]; $sum_output += $output;
+
+ printf ("<tr><td width=\"70%%\">%s</td><td align=\"right\" width=\"10%%\">%.2f</td>"
+ ."<td align=\"right\" width=\"10%%\">%.2f</td>"
+ ."<td align=\"right\" width=\"10%%\">%.2f</td></tr>",$catz[$i], $input, $output, $input - $output);
+ }
+
+ printf ("<tr><td width=\"70%%\">Summe</td><td align=\"right\" width=\"10%%\">%.2f</td>"
+ ."<td align=\"right\" width=\"10%%\">%.2f</td>"
+ ."<td align=\"right\" width=\"10%%\">%.2f</td></tr>",$sum_input, $sum_output, $sum_input - $sum_output);
+ echo ("</table>");
+
+ }
+ }
+?>
+</table><p>
+
+<a href="edit.php3">Neuer Eintrag</a><p>
+
+
+</page>
+
+# Local variables:
+# mode: text
+# mode: auto-fill
+# mode: iso-accents
+# end: