4e2ba984ed5d1096b6337b7ad46d9fd9ba2a7b75
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
1 #! /usr/bin/perl
2
3 #  infocon - Admin-Tool for InfoCon
4 #  Copyright (c) 1998-2003,2005-7  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 use Term::ReadLine;
24
25 $table = "sales";
26 $engine  = "dbi:Pg:dbname=infocon";
27 $dbh = DBI->connect($engine);
28 if (!$dbh) {
29     print "Access to database denied!\n";
30     return 1;
31 }
32
33 @categories = ();
34
35 sub sdate
36 {
37     $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
38     return sprintf ("%d.%02d.%02d", $3,$2,$1);
39 }
40
41 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
42 # Datenbank gespeichert werden kann.
43 #
44 sub date_to_string
45 {
46     return "" if (!$_[0]);
47
48     ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
49         = localtime;
50
51     if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
52         $day = $date_mday;
53         $mon = $date_mon+1;
54         $year = $date_year;
55     } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
56         $day = $date_mday-1 if ($date_mday);
57         $mon = $date_mon+1;
58         $year = $date_year;
59     } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
60         $day = $date_mday+1;
61         $mon = $date_mon+1;
62         $year = $date_year;
63     } else {
64         ($day,$mon,$year) = split(/\./, $_[0]);
65         if (!$year) {    
66             $year = $date_year;
67         }
68     }
69
70     if ($year < 70) {
71         $year += 2000;
72     } elsif ($year < 100) {
73         $year += 1900;
74     }
75     return sprintf("%4d%02d%02d", $year,$mon,$day);
76 }
77
78 sub pay_invoice
79 {
80     my $nr = shift;
81     my $pay = shift;
82     my $value = $pay==1?1:0;
83     my $query;
84     my $sth;
85
86     $query  = "UPDATE sales SET paid=$value WHERE nr = $nr";
87     $sth = $dbh->do($query);
88 }
89
90 sub hide_invoice
91 {
92     my $nr = shift;
93     my $hide = shift;
94     my $value = $hide==1?0:1;
95     my $query;
96     my $sth;
97
98     $query  = "UPDATE sales SET visible=$value WHERE nr = $nr";
99     $sth = $dbh->do($query);
100 }
101
102 sub sales_list
103 {
104     my $where = shift;
105     my $descr;
106     my $sum_pos=0;
107     my $sum_neg=0;
108     my $query;
109     my @row;
110     my $sth;
111     my $d;
112
113     if ($where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
114         if ($where) {
115             $where .= " AND visible = 1";
116         } else {
117             $where .= "visible = 1";
118         }
119     }
120
121     if ($opt_year) {
122         if ($where) {
123             $where .= " AND date ~* '$opt_year'";
124         } else {
125             $where .= "date ~* '$opt_year'";
126         }
127     }
128
129     if ($opt_direction) {
130         if ($opt_direction eq "in") {
131             $d = "price >= 0"
132         } elsif ($opt_direction eq "out") {
133             $d = "price <= 0"
134         }
135
136         if ($where) {
137             $where .= " AND $d";
138         } else {
139             $where .= "$d";
140         }
141     }
142
143     $query  = "SELECT nr,date,description,price FROM $table";
144     $query .= " WHERE $where" if ($where);
145     $query .= " ORDER by date,nr";
146     $sth = $dbh->prepare($query);
147     if ($sth && ($rc = $sth->execute) > 0) {
148         print " Nr.   Datum  Bezeichnung                                 Betrag\n";
149         print "------------------------------------------------------------------\n";
150         while (@row = $sth->fetchrow_array) {
151             $descr = substr($row[2],0,40);
152             printf "%4d %8s %-40s  %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
153             if ($row[3] < 0.0) {
154                 $sum_neg -= $row[3];
155             } else {
156                 $sum_pos += $row[3];
157             }
158         }
159         print "-----------------------------------------------------------------\n"
160             if ($sum_neg > 0 || $sum_pos > 0) ;
161         printf " Zahlungseingänge                                       %9.2f\n", $sum_pos
162             if ($sum_pos > 0);
163         printf " Zahlungsausgänge                                       %9.2f\n", -$sum_neg
164             if ($sum_neg > 0);
165         print "==================================================================\n";
166         printf " Summe                                                  %9.2f\n\n", $sum_pos - $sum_neg;
167     }
168 }
169
170 sub get_descriptions
171 {
172     my $query;
173     my @row;
174     my $sth;
175     my @arr = ();
176     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
177         = localtime;
178
179     $query  = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ORDER by description",
180                       $table, $date_year+1900);
181     $sth = $dbh->prepare($query);
182     if ($sth && ($rc = $sth->execute) > 0) {
183         while (@row = $sth->fetchrow_array) {
184             push(@arr, $row[0]) if ($row[0]);
185         }
186     }
187     return @arr;
188 }
189
190 sub get_categories
191 {
192     my $query;
193     my @row;
194     my $sth;
195     my @arr = ();
196
197     $query  = "SELECT DISTINCT category FROM $table ORDER by category";
198     $sth = $dbh->prepare($query);
199     if ($sth && ($rc = $sth->execute) > 0) {
200         while (@row = $sth->fetchrow_array) {
201             push(@arr, $row[0]) if ($row[0]);
202         }
203     }
204     return @arr;
205 }
206
207 sub list_categories
208 {
209     @categories = get_categories unless @categories;
210
211     printf "%s\n", join (", ",@categories);
212 }
213
214 sub read_input
215 {
216     my $prompt = shift;
217     my $default = shift;
218     my $ans;
219
220     if ($default) {
221         $ans = $term->readline ($prompt . " [" . $default . "]: ");
222     } else {
223         $ans = $term->readline ($prompt . ": ");
224     }
225     if (length ($ans) == 0) {
226         $ans = $default;
227     } elsif ($ans eq ".") {
228         $ans = '';
229     }
230     return $ans;
231 }
232
233 # Gibt die naechste freie Nr. in der Datenbank zurueck.  Wenn der
234 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
235 # Nr. anschliessend bereits wieder vergeben ist.
236 #
237 sub get_next_nr
238 {
239     my $query;
240     my $sth;
241     my $rc;
242     my @row;
243
244     $query = "SELECT nr FROM $table ORDER BY nr DESC";
245     $sth = $dbh->prepare($query);
246     if ($sth) {
247         $rc = $sth->execute;
248         if ($rc > 0 && (@row = $sth->fetchrow_array)) {
249             return $row[0] + 1;
250         }
251     }
252     return 1;
253 }
254
255 sub buchung_input
256 {
257     my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
258     my @input = ();
259     my $weiter = 'y';
260     my $i;
261     my $query;
262     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
263         = localtime;
264
265     @categories = get_categories unless @categories;
266     my @descriptions = get_descriptions;
267
268     $term->addhistory($_) foreach (@categories);
269     $term->addhistory($_) foreach (@descriptions);
270
271     my $attribs = $term->Attribs;
272
273     $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
274
275     print "Buchungseingabe\n\n";
276     while ($weiter =~ /[JjYy1]/) {
277         $i=0;while ($i <= $#fieldname) {
278             if ($fieldname[$i] eq "Category") {
279                 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
280                 $attribs->{completion_word} = \@categories;
281             } elsif ($fieldname[$i] eq "Description") {
282                 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
283                 $attribs->{completion_word} = \@descriptions;
284             } else {
285                 $attribs->{completion_word} = undef;
286             }
287             $ans = read_input($fieldname[$i],$input[$i]);
288             if ($fieldname[$i] eq "Category" && $ans eq "?") {
289                 printf "  %s\n", join (", ",@categories);
290             } elsif ($fieldname[$i] eq "Datum") {
291                 if ($ans =~ /^\d+\.\d+.\d+$/) {
292                     $input[$i] = $ans;
293                     $i++;
294                 } elsif ($ans =~ /^\d+\.\d+.$/) {
295                     $ans .= $date_year + 1900;
296                     $input[$i] = $ans;
297                     $i++;
298                 } elsif ($ans =~ /^\d+\.$/) {
299                     $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
300                     $input[$i] = $ans;
301                     $i++;
302                 }
303             } elsif ($fieldname[$i] eq "Paid") {
304                 if ($ans =~ /[1jJyY]/) {
305                     $input[$i] = 1;
306                 } else {
307                     $input[$i] = 0;
308                 }
309                 $i++;
310             } else {
311                 $input[$i] = $ans;
312                 $i++;
313             }
314         }
315         if (!$input[5]) {       # USt selbst berechnen
316             if ($input[4] != 0) {
317                 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
318             }
319         }
320
321         if ($input[3] =~ /[EeIi\+]/) {
322             $input[5] *= -1 if ($input[5] < 0);
323             $input[6] *= -1 if ($input[6] < 0);
324         } else {
325             $input[5] *= -1 if ($input[5] > 0);
326             $input[6] *= -1 if ($input[6] > 0);
327         }
328
329         $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
330                           $input[5], $input[6], 1, $input[7]);
331         $weiter = read_input("Weiter",'j');
332         $input[5] = 0.0;
333     }
334 }
335
336 sub usage
337 {
338     print "infocon [options] [-h|--help] commands\n";
339     print "  --buchung-category|-bc [category]\n";
340     print "  --buchung-input|-bi\n";
341     print "  --buchung-unpaid\n";
342     print "  --buchung-hidden\n";
343     print "  --pay <nr> | --unpay <nr>\n";
344     print "  --hide <nr> | --unhide <nr>\n";
345     print "  --list-categories|-lc\n";
346     print "  Options:\n";
347     print "    --all|-a\n";
348     print "    --verbose|-v\n";
349     print "    --year|-y year\n";
350     print "    --direction|--dir|-d in|out\n";
351     print "    --dm\n";
352     exit 0;
353 }
354
355 $term = new Term::ReadLine '';
356 $i = 0;
357 $opt_all = 0;
358 $opt_verbose = 0;
359 $opt_year = 0;
360 usage unless @ARGV;
361 while ($i <= $#ARGV) {
362     # Some aliases
363     if ($ARGV[$i] eq "-bc") {
364         $ARGV[$i] = "--buchung-category";
365     } elsif ($ARGV[$i] eq "-bi") {
366         $ARGV[$i] = "--buchung-input";
367     } elsif ($ARGV[$i] eq "-bh") {
368         $ARGV[$i] = "--buchung-hidden";
369     } elsif ($ARGV[$i] eq "-bu") {
370         $ARGV[$i] = "--buchung-unpaid";
371     } elsif ($ARGV[$i] eq "-lc") {
372         $ARGV[$i] = "--list-categories";
373     }
374
375     if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
376         usage;
377     } elsif ($ARGV[$i] =~ /^--list-/) {
378         $ARGV[$i] =~ s/^--list-//;
379         if ($ARGV[$i] eq "categories") {
380             list_categories;
381         } else {
382             usage;
383         }
384     } elsif ($ARGV[$i] =~ /^--buchung-/) {
385         $ARGV[$i] =~ s/^--buchung-//;
386         if ($ARGV[$i] eq "category") {
387             if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
388                 $i++;
389                 sales_list("category = '$ARGV[$i]'");
390             } else {
391                 sales_list;
392             }
393         } elsif ($ARGV[$i] eq "hidden") {
394             $saved_table = $table;
395             $table = "sales_dm";
396             sales_list("visible = 0");
397             $table = "sales";
398             sales_list("visible = 0");
399             $table = $table_saved;
400         } elsif ($ARGV[$i] eq "input") {
401             buchung_input;
402         } elsif ($ARGV[$i] eq "unpaid") {
403             $saved_table = $table;
404             $table = "sales_dm";
405             sales_list("paid = 0");
406             $table = "sales";
407             sales_list("paid = 0");
408             $table = $table_saved;
409         } else {
410             usage;
411         }
412     } elsif ($ARGV[$i] eq "--pay") {
413         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
414             && ($ARGV[$i+1] =~ /^\d+$/)) {
415             $i++;
416             pay_invoice ($ARGV[$i], 1);
417         }
418     } elsif ($ARGV[$i] eq "--unpay") {
419         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
420             && ($ARGV[$i+1] =~ /^\d+$/)) {
421             $i++;
422             pay_invoice ($ARGV[$i], 0);
423         }
424     } elsif ($ARGV[$i] eq "--hide") {
425         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
426             && ($ARGV[$i+1] =~ /^\d+$/)) {
427             $i++;
428             hide_invoice ($ARGV[$i], 1);
429         }
430     } elsif ($ARGV[$i] eq "--unhide") {
431         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
432             && ($ARGV[$i+1] =~ /^\d+$/)) {
433             $i++;
434             hide_invoice ($ARGV[$i], 0);
435         }
436     } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
437         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
438             $i++;
439             $opt_year = $1;
440             if ($opt_year < 70) {
441                 $opt_year += 2000;
442             } elsif ($opt_year < 100) {
443                 $opt_year += 1900;
444             }
445             if ($opt_year < 2002) {
446                 $table = "sales_dm";
447             } elsif ($opt_year > 2001) {
448                 $table = "sales";
449             }
450         }
451     } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
452              || $ARGV[$i] eq "-d") {
453         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
454             $i++;
455             $opt_direction = $1;
456         }
457     } elsif ($ARGV[$i] eq "--dm") {
458         $table = "sales_dm";
459     } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
460         $opt_all = 1;
461     } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {
462         $opt_verbose = 1;
463     }
464     $i++;
465 }