Ditch the currency, i.e. only support current sales from 2003 in Euro
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / list.wml
1 #include <infocon.style>
2
3 <page func=InfoCon title="Buchhaltung">
4
5 <table class=font width=100% border=0 cellpadding=2 cellspacing=0>
6 <tr bgcolor=#<colh>>
7   <th width=15%>Datum</th>
8   <th width=20%>Kategorie</th>
9   <th width=50%>Verwendungszweck</th>
10   <th width=15%>Betrag</th>
11 </tr>
12 <?
13   $dbh = pg_pconnect ("<dbhost>", "<dbport>", "<dbname>")
14                or die("Unable to connect to SQL server");
15
16   pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
17
18   $table = "sales";
19
20   if (strlen($_POST[year])) {
21     # $where[] = "date >= '$year-01-01'";
22     # $year++;
23     # $where[] = "date < '$year-01-01'";
24     $where[] = "date ~* '$_POST[year]'";
25   }
26   if (strlen($_POST[category])) {
27     $where[] = "category = '$_POST[category]'";
28   }
29   if ($_POST[input] && !$_POST[output]) {
30     $where[] = "price > 0.0";
31   } elseif ($_POST[output] && !$_POST[input]) {
32     $where[] = "price < 0.0";
33   }
34
35   $query = "SELECT date,oid,category,description,price FROM $table ";
36   if (count($where) > 0) {
37     $query .= " WHERE " . implode ($where, " AND ");
38   }
39   $query .= " ORDER BY date,oid";
40   $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
41
42   $sum = 0.0;
43   $color = "<cold>";
44   for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
45     $row = pg_fetch_array ($sth, $nr);
46     $color = $color=="<cold>"?"<coln>":"<cold>";
47
48     # $date = explode (" ", $row['date']);
49     # $date = explode ("-", $date[0]);
50     # $date = sprintf ("%d.%d.%d", $date[2], $date[1], $date[0]);
51     $date = substr ($row['date'],6,2) . "." . substr ($row['date'],4,2) . "." . substr ($row['date'],0,4);
52
53     printf ("<tr bgcolor=\"#%s\">", $color);
54     printf ("<td width=\"15%%\" align=\"right\">%s</td>", $date);
55     printf ("<td width=\"20%%\">%s</td>", $row['category']);
56     printf ("<td width=\"50%%\"><a href=\"edit.php?oid=%d\">%s</a></td>",
57       $row['oid'], $row['description']);
58     printf ("<td width=\"15%%\" align=\"right\">%5.2f</td>", $row['price']);
59     $sum += $row['price'];
60     echo ("</tr>");
61   }
62   echo ("<tr>");
63   echo ("<td width=\"85%\" colspan=\"3\"><b>Summe</b></td>");
64   printf ("<td width=\"15%%\" align=\"right\"><b>%5.2f</b></td>", $sum);
65   echo ("</tr>");
66   echo ("</table><p>");
67
68   if (!strlen($_POST[category])) {
69
70     $query = "SELECT DISTINCT category FROM $table ";
71     if (strlen($_POST[year])) {
72       $query .= "WHERE date ~* '$_POST[year]'";
73     }
74
75     $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
76
77     for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
78       $row = pg_fetch_array ($sth, $nr);
79       $catz[] = $row['category'];
80     }
81
82     if (count ($catz) > 0) {
83       echo ("<h3>Nach Kategorien sortiert</h3>");
84       echo ("<div align=\"center\"><table class=\"font\" width=\"80%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" summary=\"\">");
85       echo ("<tr bgcolor=\"#<colh>\">"
86            ."<th width=\"70%\">Kategorie</th>"
87            ."<th width=\"10%\">Einnahmen</th>"
88            ."<th width=\"10%\">Ausgaben</th>"
89            ."<th width=\"10%\">Summe</th></tr>");
90       sort ($catz);
91
92       $color = "<cold>";
93       $sum_output = 0;
94       for ($i=0; $i < count ($catz); $i++) {
95         $query = sprintf ("SELECT SUM (price) FROM $table WHERE category = '%s' AND price > 0.0", $catz[$i]);
96         $color = $color=="<cold>"?"<coln>":"<cold>";
97
98         if (strlen($_POST[year])) {
99           $query .= " AND date ~* '$_POST[year]'";
100         }
101         $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
102         $row = pg_fetch_array ($sth, 0);
103         $input = $row[0]; $sum_input += $input;
104         $query = sprintf ("SELECT SUM (price) FROM $table WHERE category = '%s' AND price < 0.0", $catz[$i]);
105         if (strlen($_POST[year])) {
106           $query .= " AND date ~* '$_POST[year]'";
107         }
108         $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
109         $row = pg_fetch_array ($sth, 0);
110         $output = -$row[0]; $sum_output += $output;
111
112         printf ("<tr bgcolor=\"#%s\"><td width=\"70%%\">%s</td>"
113                ."<td align=\"right\" width=\"10%%\">%.2f</td>"
114                ."<td align=\"right\" width=\"10%%\">%.2f</td>"
115                ."<td align=\"right\" width=\"10%%\">%.2f</td></tr>",
116                $color, $catz[$i], $input, $output, $input - $output);
117       }
118
119       printf ("<tr bgcolor=\"#%s\"><td width=\"70%%\"><b>Summe</b></td>"
120              ."<td align=\"right\" width=\"10%%\"><b>%.2f</b></td>"
121              ."<td align=\"right\" width=\"10%%\"><b>%.2f</b></td>"
122              ."<td align=\"right\" width=\"10%%\"><b>%.2f</b></td></tr>",
123              $color, $sum_input, $sum_output, $sum_input - $sum_output);
124       echo ("</table></div>");
125
126     }
127   }
128 ?>
129
130 </page>
131
132 # Local variables:
133 # mode: text
134 # mode: auto-fill
135 # end: