Replacement of the percent sign
[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 ($_POST["submit"] == "List") {
21   if (strlen($_POST[year])) {
22     # $where[] = "date >= '$year-01-01'";
23     # $year++;
24     # $where[] = "date < '$year-01-01'";
25     $where[] = "date ~* '$_POST[year]'";
26   }
27   if (strlen($_POST[category])) {
28     $where[] = "category = '$_POST[category]'";
29   }
30   if ($_POST[input] && !$_POST[output]) {
31     $where[] = "price > 0.0";
32   } elseif ($_POST[output] && !$_POST[input]) {
33     $where[] = "price < 0.0";
34   }
35
36   $query = "SELECT date,oid,category,description,price FROM $table ";
37   if (count($where) > 0) {
38     $query .= " WHERE " . implode ($where, " AND ");
39   }
40   $query .= " ORDER BY date,oid";
41   $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
42
43   $sum = 0.0;
44   $color = "<cold>";
45   for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
46     $row = pg_fetch_array ($sth, $nr);
47     $color = $color=="<cold>"?"<coln>":"<cold>";
48
49     # $date = explode (" ", $row['date']);
50     # $date = explode ("-", $date[0]);
51     # $date = sprintf ("%d.%d.%d", $date[2], $date[1], $date[0]);
52     $date = substr ($row['date'],6,2) . "." . substr ($row['date'],4,2) . "." . substr ($row['date'],0,4);
53
54     printf ("<tr bgcolor=\"#%s\">", $color);
55     printf ("<td width=\"15%%\" align=\"right\">%s</td>", $date);
56     printf ("<td width=\"20%%\">%s</td>", $row['category']);
57     printf ("<td width=\"50%%\"><a href=\"edit.php?oid=%d\">%s</a></td>",
58       $row['oid'], $row['description']);
59     printf ("<td width=\"15%%\" align=\"right\">%5.2f</td>", $row['price']);
60     $sum += $row['price'];
61     echo ("</tr>");
62   }
63   echo ("<tr>");
64   echo ("<td width=\"85%\" colspan=\"3\"><b>Summe</b></td>");
65   printf ("<td width=\"15%%\" align=\"right\"><b>%5.2f</b></td>", $sum);
66   echo ("</tr>");
67   echo ("</table><p>");
68
69 } elseif ($_POST["submit"] == "Overview") {
70
71     $query = "SELECT DISTINCT category FROM $table ";
72     if (strlen($_POST[year]))
73       $query .= "WHERE date ~* '$_POST[year]'";
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   } elseif ($_POST["submit"] == "Tally") {
128     if (strlen($_POST[year]))
129       $where = "date ~* '$_POST[year]' AND ";
130     else
131       $where = '';
132
133     echo ("<h3>Abrechnung</h3>");
134
135     $output = tally ($dbh, "Aus", $where . "price < 0.0");
136     $input = tally ($dbh, "Ein", $where . "price > 0.0");
137
138     printf ("<p>Summe Steuern: %.2f", $input["tax"] - $output["tax"]);
139     printf ("<br>Summe netto: %.2f", $input["netto"] - $output["netto"]);
140     printf ("<br>Summe btutto: %.2f</p>", $input["brutto"] - $output["brutto"]);
141   }
142
143 function tally ($dbh, $head, $where)
144 {
145   echo ("<div align=\"center\"><table class=\"font\" width=\"80%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" summary=\"\">");
146   printf ("<tr bgcolor=\"#<colh>\">"
147           ."<th width=\"10%%\">%s</th>"
148           ."<th width=\"30%%\">Steuer</th>"
149           ."<th width=\"30%%\">netto</th>"
150           ."<th width=\"30%%\">brutto</th></tr>",
151           $head);
152   
153   $query = "SELECT tax_percent,sum(tax_assigned) AS tax,sum(price-tax_assigned) AS netto,sum(price) AS brutto"
154     . " FROM sales WHERE " . $where
155     . " GROUP BY tax_percent ORDER BY tax_percent";
156
157   $color = "<cold>";
158   $sum = array();
159
160   $sth = pg_exec ($dbh, $query); // or die("Datenbank-Abfrage!");
161   for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
162     $row = pg_fetch_array ($sth, $nr);
163     $color = $color=="<cold>"?"<coln>":"<cold>";
164
165     if ($row["brutto"] < 0) {
166       $row["tax"] = -$row["tax"];
167       $row["netto"] = -$row["netto"];
168       $row["brutto"] = -$row["brutto"];
169     }
170
171     printf ("<tr bgcolor=\"#%s\"><td align=\"right\" width=\"10%%\">%d%%</td>"
172             ."<td align=\"right\" width=\"30%%\">%.2f</td>"
173             ."<td align=\"right\" width=\"30%%\">%.2f</td>"
174             ."<td align=\"right\" width=\"30%%\">%.2f</td></tr>",
175             $color,
176             $row["tax_percent"], $row["tax"], $row["netto"], $row["brutto"]);
177     $sum["tax"] += $row["tax"];
178     $sum["netto"] += $row["netto"];
179     $sum["brutto"] += $row["brutto"];
180   }
181
182   printf ("<tr bgcolor=\"#%s\"><td width=\"10%%\"><b>Sum</b></td>"
183           ."<td align=\"right\" width=\"30%%\"><b>%.2f</b></td>"
184           ."<td align=\"right\" width=\"30%%\"><b>%.2f</b></td>"
185           ."<td align=\"right\" width=\"30%%\"><b>%.2f</b></td></tr>",
186           $color,
187           $sum["tax"], $sum["netto"], $sum["brutto"]);
188
189   echo ("</table></div>\n");
190
191   return $sum;
192 }
193
194 ?>
195
196 </page>
197
198 # Local variables:
199 # mode: php
200 # end: