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