Finally added support for half typed date strings which really should
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
1 #! /usr/bin/perl
2
3 #  infocon - Admin-Tool for InfoCon
4 #  Copyright (c) 1998-2002  Martin Schulze <joey@infodrom.org>
5
6 #  This program is free software; you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation; either version 2 of the License, or
9 #  (at your option) any later version.
10
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19
20 # $Id$
21
22 use DBI;
23
24 $table = "sales";
25 $engine  = "dbi:Pg:dbname=infocon";
26 $dbh = DBI->connect($engine);
27 if (!$dbh) {
28     print "Access to database denied!\n";
29     return 1;
30 }
31
32 @categories = ();
33
34 sub sdate
35 {
36     $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
37     return sprintf ("%d.%02d.%02d", $3,$2,$1);
38 }
39
40 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
41 # Datenbank gespeichert werden kann.
42 #
43 sub date_to_string
44 {
45     return "" if (!$_[0]);
46
47     ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
48         = localtime();
49
50     if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
51         $day = $date_mday;
52         $mon = $date_mon+1;
53         $year = $date_year;
54     } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
55         $day = $date_mday-1 if ($date_mday);
56         $mon = $date_mon+1;
57         $year = $date_year;
58     } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
59         $day = $date_mday+1;
60         $mon = $date_mon+1;
61         $year = $date_year;
62     } else {
63         ($day,$mon,$year) = split(/\./, $_[0]);
64         if (!$year) {    
65             $year = $date_year;
66         }
67     }
68
69     if ($year < 70) {
70         $year += 2000;
71     } elsif ($year < 100) {
72         $year += 1900;
73     }
74     return sprintf("%4d%02d%02d", $year,$mon,$day);
75 }
76
77 sub pay_invoice
78 {
79     my $nr = shift;
80     my $pay = shift;
81     my $value = $pay==1?1:0;
82     my $query;
83     my @row;
84     my $sth;
85
86     $query  = "UPDATE sales SET paid=$value WHERE nr = $nr";
87     $sth = $dbh->do($query);
88 }
89
90 sub sales_list
91 {
92     my $where = shift;
93     my $descr;
94     my $sum_pos=0;
95     my $sum_neg=0;
96     my $query;
97     my @row;
98     my $sth;
99     my $d;
100
101     if ($opt_year) {
102         if ($where) {
103             $where .= " AND date ~* '$opt_year'";
104         } else {
105             $where .= "date ~* '$opt_year'";
106         }
107     }
108
109     if ($opt_direction) {
110         if ($opt_direction eq "in") {
111             $d = "price >= 0"
112         } elsif ($opt_direction eq "out") {
113             $d = "price <= 0"
114         }
115
116         if ($where) {
117             $where .= " AND $d";
118         } else {
119             $where .= "$d";
120         }
121     }
122
123     $query  = "SELECT nr,date,description,price FROM $table";
124     $query .= " WHERE $where" if ($where);
125     $query .= " ORDER by date,nr";
126     $sth = $dbh->prepare($query);
127     if ($sth && ($rc = $sth->execute) > 0) {
128         print " Nr.   Datum  Bezeichnung                                 Betrag\n";
129         print "------------------------------------------------------------------\n";
130         while (@row = $sth->fetchrow_array) {
131             $descr = substr($row[2],0,40);
132             printf "%4d %8s %-40s  %9.2f\n", $row[0], &sdate($row[1]), $descr, $row[3];
133             if ($row[3] < 0.0) {
134                 $sum_neg -= $row[3];
135             } else {
136                 $sum_pos += $row[3];
137             }
138         }
139         print "-----------------------------------------------------------------\n"
140             if ($sum_neg > 0 || $sum_pos > 0) ;
141         printf " Zahlungseingänge                                       %9.2f\n", $sum_pos
142             if ($sum_pos > 0);
143         printf " Zahlungsausgänge                                       %9.2f\n", -$sum_neg
144             if ($sum_neg > 0);
145         print "==================================================================\n";
146         printf " Summe                                                  %9.2f\n\n", $sum_pos - $sum_neg;
147     }
148 }
149
150 sub get_categories
151 {
152     my $query;
153     my @row;
154     my $sth;
155     my @arr = ();
156
157     $query  = "SELECT DISTINCT category FROM $table ORDER by category";
158     $sth = $dbh->prepare($query);
159     if ($sth && ($rc = $sth->execute) > 0) {
160         while (@row = $sth->fetchrow_array) {
161             push(@arr, $row[0]) if ($row[0]);
162         }
163     }
164     return @arr;
165 }
166
167 sub list_categories
168 {
169     @categories = &get_categories() if ($#categories);
170
171     printf "%s\n", join (", ",@categories);
172 }
173
174 sub read_input
175 {
176     my $prompt = shift;
177     my $default = shift;
178     my $ans;
179
180     print $prompt;
181     printf " [%s]", $default if ($default);
182     print ": ";
183     $ans = <STDIN>;
184     chop ($ans) if ($ans);
185     if (length ($ans) == 0) {
186         $ans = $default;
187     } elsif ($ans eq ".") {
188         $ans = '';
189     }
190     return $ans;
191 }
192
193 # Gibt die naechste freie Nr. in der Datenbank zurueck.  Wenn der
194 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
195 # Nr. anschliessend bereits wieder vergeben ist.
196 #
197 sub get_next_nr
198 {
199     my $query;
200     my $sth;
201     my $rc;
202     my @row;
203
204     $query = "SELECT nr FROM $table ORDER BY nr DESC";
205     $sth = $dbh->prepare($query);
206     if ($sth) {
207         $rc = $sth->execute;
208         if ($rc > 0 && (@row = $sth->fetchrow_array)) {
209             return $row[0] + 1;
210         }
211     }
212     return 1;
213 }
214
215 sub buchung_input
216 {
217     my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
218     my @input = ();
219     my $weiter = 'y';
220     my $i;
221     my $query;
222     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
223         = localtime();
224
225     print "Buchungseingabe\n\n";
226     while ($weiter =~ /[JjYy]/) {
227         $i=0;while ($i <= $#fieldname) {
228             $ans = &read_input($fieldname[$i],$input[$i]);
229             if ($fieldname[$i] eq "Category" && $ans eq "?") {
230                 @categories = &get_categories() if ($#categories);
231                 printf "  %s\n", join (", ",@categories);
232             } elsif ($fieldname[$i] eq "Datum") {
233                 if ($ans =~ /^\d+\.\d+.\d+$/) {
234                     $input[$i] = $ans;
235                     $i++;
236                 } elsif ($ans =~ /^\d+\.\d+.$/) {
237                     $ans .= $date_year + 1900;
238                     $input[$i] = $ans;
239                     $i++;
240                 } elsif ($ans =~ /^\d+\.$/) {
241                     $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
242                     $input[$i] = $ans;
243                     $i++;
244                 }
245             } else {
246                 $input[$i] = $ans;
247                 $i++;
248             }
249         }
250         if (!$input[5]) {       # USt selbst berechnen
251             if ($input[4] != 0) {
252                 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
253             }
254         }
255
256         if ($input[3] =~ /[EeIi\+]/) {
257             $input[5] *= -1 if ($input[5] < 0);
258             $input[6] *= -1 if ($input[6] < 0);
259         } else {
260             $input[5] *= -1 if ($input[5] > 0);
261             $input[6] *= -1 if ($input[6] > 0);
262         }
263
264         $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d)",
265                           &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
266                           $input[5], $input[6], $input[7]);
267         $sth = $dbh->do($query);
268         $weiter = &read_input("Weiter",'j');
269         $input[5] = 0.0;
270     }
271 }
272
273 sub usage
274 {
275     print "infocon [options] [-h|--help] commands\n";
276     print "  --buchung-category|-bc [category]\n";
277     print "  --buchung-input|-bi\n";
278     print "  --buchung-unpaid\n";
279     print "  --pay <nr> | --unpay <nr>\n";
280     print "  --list-categories|-lc\n";
281     print "  Options:\n";
282     print "    --verbose|-v\n";
283     print "    --year|-y year\n";
284     print "    --direction|--dir|-d in|out\n";
285     print "    --dm\n";
286     exit 0;
287 }
288
289 $i = 0;
290 $opt_verbose = 0;
291 $opt_year = 0;
292 &usage() if ($#ARGV == -1);
293 while ($i <= $#ARGV) {
294     # Some aliases
295     if ($ARGV[$i] eq "-bc") {
296         $ARGV[$i] = "--buchung-category";
297     } elsif ($ARGV[$i] eq "-bi") {
298         $ARGV[$i] = "--buchung-input";
299     } elsif ($ARGV[$i] eq "-lc") {
300         $ARGV[$i] = "--list-categories";
301     }
302
303     if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
304         &usage();
305     } elsif ($ARGV[$i] =~ /^--list-/) {
306         $ARGV[$i] =~ s/^--list-//;
307         if ($ARGV[$i] eq "categories") {
308             &list_categories();
309         } else {
310             &usage();
311         }
312     } elsif ($ARGV[$i] =~ /^--buchung-/) {
313         $ARGV[$i] =~ s/^--buchung-//;
314         if ($ARGV[$i] eq "category") {
315             if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
316                 $i++;
317                 &sales_list("category = '$ARGV[$i]'");
318             } else {
319                 &sales_list();
320             }
321         } elsif ($ARGV[$i] eq "input") {
322             &buchung_input();
323         } elsif ($ARGV[$i] eq "unpaid") {
324             $saved_table = $table;
325             $table = "sales_dm";
326             &sales_list("paid = 0");
327             $table = "sales";
328             &sales_list("paid = 0");
329             $table = $table_saved;
330         } else {
331             &usage();
332         }
333     } elsif ($ARGV[$i] eq "--pay") {
334         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
335             && ($ARGV[$i+1] =~ /^\d+$/)) {
336             $i++;
337             &pay_invoice ($ARGV[$i], 1);
338         }
339     } elsif ($ARGV[$i] eq "--unpay") {
340         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
341             && ($ARGV[$i+1] =~ /^\d+$/)) {
342             $i++;
343             &pay_invoice ($ARGV[$i], 0);
344         }
345     } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
346         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
347             $i++;
348             $opt_year = $1;
349             if ($opt_year < 70) {
350                 $opt_year += 2000;
351             } elsif ($opt_year < 100) {
352                 $opt_year += 1900;
353             }
354             if ($opt_year < 2002) {
355                 $table = "sales_dm";
356             } elsif ($opt_year > 2001) {
357                 $table = "sales";
358             }
359         }
360     } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
361              || $ARGV[$i] eq "-d") {
362         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
363             $i++;
364             $opt_direction = $1;
365         }
366     } elsif ($ARGV[$i] eq "--dm") {
367         $table = "sales_dm";
368     } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {
369         $opt_verbose = 1;
370     }
371     $i++;
372 }