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