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