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