Need to increment the counter
[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-6  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_categories
171 {
172     my $query;
173     my @row;
174     my $sth;
175     my @arr = ();
176
177     $query  = "SELECT DISTINCT category FROM $table ORDER by category";
178     $sth = $dbh->prepare($query);
179     if ($sth && ($rc = $sth->execute) > 0) {
180         while (@row = $sth->fetchrow_array) {
181             push(@arr, $row[0]) if ($row[0]);
182         }
183     }
184     return @arr;
185 }
186
187 sub list_categories
188 {
189     @categories = &get_categories() if ($#categories);
190
191     printf "%s\n", join (", ",@categories);
192 }
193
194 sub read_input
195 {
196     my $prompt = shift;
197     my $default = shift;
198     my $ans;
199
200     if ($default) {
201         $ans = $term->readline ($prompt . " [" . $default . "]: ");
202     } else {
203         $ans = $term->readline ($prompt . ": ");
204     }
205     if (length ($ans) == 0) {
206         $ans = $default;
207     } elsif ($ans eq ".") {
208         $ans = '';
209     }
210     return $ans;
211 }
212
213 # Gibt die naechste freie Nr. in der Datenbank zurueck.  Wenn der
214 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
215 # Nr. anschliessend bereits wieder vergeben ist.
216 #
217 sub get_next_nr
218 {
219     my $query;
220     my $sth;
221     my $rc;
222     my @row;
223
224     $query = "SELECT nr FROM $table ORDER BY nr DESC";
225     $sth = $dbh->prepare($query);
226     if ($sth) {
227         $rc = $sth->execute;
228         if ($rc > 0 && (@row = $sth->fetchrow_array)) {
229             return $row[0] + 1;
230         }
231     }
232     return 1;
233 }
234
235 sub buchung_input
236 {
237     my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
238     my @input = ();
239     my $weiter = 'y';
240     my $i;
241     my $query;
242     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
243         = localtime();
244
245     print "Buchungseingabe\n\n";
246     while ($weiter =~ /[JjYy1]/) {
247         $i=0;while ($i <= $#fieldname) {
248             $ans = &read_input($fieldname[$i],$input[$i]);
249             if ($fieldname[$i] eq "Category" && $ans eq "?") {
250                 @categories = &get_categories() if ($#categories);
251                 printf "  %s\n", join (", ",@categories);
252             } elsif ($fieldname[$i] eq "Datum") {
253                 if ($ans =~ /^\d+\.\d+.\d+$/) {
254                     $input[$i] = $ans;
255                     $i++;
256                 } elsif ($ans =~ /^\d+\.\d+.$/) {
257                     $ans .= $date_year + 1900;
258                     $input[$i] = $ans;
259                     $i++;
260                 } elsif ($ans =~ /^\d+\.$/) {
261                     $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
262                     $input[$i] = $ans;
263                     $i++;
264                 }
265             } elsif ($fieldname[$i] eq "Paid") {
266                 if ($ans =~ /[1jJyY]/) {
267                     $input[$i] = 1;
268                 } else {
269                     $input[$i] = 0;
270                 }
271                 $i++;
272             } else {
273                 $input[$i] = $ans;
274                 $i++;
275             }
276         }
277         if (!$input[5]) {       # USt selbst berechnen
278             if ($input[4] != 0) {
279                 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
280             }
281         }
282
283         if ($input[3] =~ /[EeIi\+]/) {
284             $input[5] *= -1 if ($input[5] < 0);
285             $input[6] *= -1 if ($input[6] < 0);
286         } else {
287             $input[5] *= -1 if ($input[5] > 0);
288             $input[6] *= -1 if ($input[6] > 0);
289         }
290
291         $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d,%d)",
292                           &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
293                           $input[5], $input[6], 1, $input[7]);
294         $sth = $dbh->do($query);
295         $weiter = &read_input("Weiter",'j');
296         $input[5] = 0.0;
297     }
298 }
299
300 sub usage
301 {
302     print "infocon [options] [-h|--help] commands\n";
303     print "  --buchung-category|-bc [category]\n";
304     print "  --buchung-input|-bi\n";
305     print "  --buchung-unpaid\n";
306     print "  --buchung-hidden\n";
307     print "  --pay <nr> | --unpay <nr>\n";
308     print "  --hide <nr> | --unhide <nr>\n";
309     print "  --list-categories|-lc\n";
310     print "  Options:\n";
311     print "    --all|-a\n";
312     print "    --verbose|-v\n";
313     print "    --year|-y year\n";
314     print "    --direction|--dir|-d in|out\n";
315     print "    --dm\n";
316     exit 0;
317 }
318
319 $term = new Term::ReadLine '';
320 $i = 0;
321 $opt_all = 0;
322 $opt_verbose = 0;
323 $opt_year = 0;
324 &usage() if ($#ARGV == -1);
325 while ($i <= $#ARGV) {
326     # Some aliases
327     if ($ARGV[$i] eq "-bc") {
328         $ARGV[$i] = "--buchung-category";
329     } elsif ($ARGV[$i] eq "-bi") {
330         $ARGV[$i] = "--buchung-input";
331     } elsif ($ARGV[$i] eq "-bh") {
332         $ARGV[$i] = "--buchung-hidden";
333     } elsif ($ARGV[$i] eq "-bu") {
334         $ARGV[$i] = "--buchung-unpaid";
335     } elsif ($ARGV[$i] eq "-lc") {
336         $ARGV[$i] = "--list-categories";
337     }
338
339     if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
340         &usage();
341     } elsif ($ARGV[$i] =~ /^--list-/) {
342         $ARGV[$i] =~ s/^--list-//;
343         if ($ARGV[$i] eq "categories") {
344             &list_categories();
345         } else {
346             &usage();
347         }
348     } elsif ($ARGV[$i] =~ /^--buchung-/) {
349         $ARGV[$i] =~ s/^--buchung-//;
350         if ($ARGV[$i] eq "category") {
351             if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
352                 $i++;
353                 &sales_list("category = '$ARGV[$i]'");
354             } else {
355                 &sales_list();
356             }
357         } elsif ($ARGV[$i] eq "hidden") {
358             $saved_table = $table;
359             $table = "sales_dm";
360             &sales_list("visible = 0");
361             $table = "sales";
362             &sales_list("visible = 0");
363             $table = $table_saved;
364         } elsif ($ARGV[$i] eq "input") {
365             &buchung_input();
366         } elsif ($ARGV[$i] eq "unpaid") {
367             $saved_table = $table;
368             $table = "sales_dm";
369             &sales_list("paid = 0");
370             $table = "sales";
371             &sales_list("paid = 0");
372             $table = $table_saved;
373         } else {
374             &usage();
375         }
376     } elsif ($ARGV[$i] eq "--pay") {
377         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
378             && ($ARGV[$i+1] =~ /^\d+$/)) {
379             $i++;
380             &pay_invoice ($ARGV[$i], 1);
381         }
382     } elsif ($ARGV[$i] eq "--unpay") {
383         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
384             && ($ARGV[$i+1] =~ /^\d+$/)) {
385             $i++;
386             &pay_invoice ($ARGV[$i], 0);
387         }
388     } elsif ($ARGV[$i] eq "--hide") {
389         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
390             && ($ARGV[$i+1] =~ /^\d+$/)) {
391             $i++;
392             &hide_invoice ($ARGV[$i], 1);
393         }
394     } elsif ($ARGV[$i] eq "--unhide") {
395         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
396             && ($ARGV[$i+1] =~ /^\d+$/)) {
397             $i++;
398             &hide_invoice ($ARGV[$i], 0);
399         }
400     } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
401         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
402             $i++;
403             $opt_year = $1;
404             if ($opt_year < 70) {
405                 $opt_year += 2000;
406             } elsif ($opt_year < 100) {
407                 $opt_year += 1900;
408             }
409             if ($opt_year < 2002) {
410                 $table = "sales_dm";
411             } elsif ($opt_year > 2001) {
412                 $table = "sales";
413             }
414         }
415     } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
416              || $ARGV[$i] eq "-d") {
417         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
418             $i++;
419             $opt_direction = $1;
420         }
421     } elsif ($ARGV[$i] eq "--dm") {
422         $table = "sales_dm";
423     } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
424         $opt_all = 1;
425     } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {
426         $opt_verbose = 1;
427     }
428     $i++;
429 }