5d2474798e7b35358f6565dcc4cc14be6ce97ad4
[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,2003  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 @row;
85     my $sth;
86
87     $query  = "UPDATE sales SET paid=$value WHERE nr = $nr";
88     $sth = $dbh->do($query);
89 }
90
91 sub sales_list
92 {
93     my $where = shift;
94     my $descr;
95     my $sum_pos=0;
96     my $sum_neg=0;
97     my $query;
98     my @row;
99     my $sth;
100     my $d;
101
102     if ($opt_year) {
103         if ($where) {
104             $where .= " AND date ~* '$opt_year'";
105         } else {
106             $where .= "date ~* '$opt_year'";
107         }
108     }
109
110     if ($opt_direction) {
111         if ($opt_direction eq "in") {
112             $d = "price >= 0"
113         } elsif ($opt_direction eq "out") {
114             $d = "price <= 0"
115         }
116
117         if ($where) {
118             $where .= " AND $d";
119         } else {
120             $where .= "$d";
121         }
122     }
123
124     $query  = "SELECT nr,date,description,price FROM $table";
125     $query .= " WHERE $where" if ($where);
126     $query .= " ORDER by date,nr";
127     $sth = $dbh->prepare($query);
128     if ($sth && ($rc = $sth->execute) > 0) {
129         print " Nr.   Datum  Bezeichnung                                 Betrag\n";
130         print "------------------------------------------------------------------\n";
131         while (@row = $sth->fetchrow_array) {
132             $descr = substr($row[2],0,40);
133             printf "%4d %8s %-40s  %9.2f\n", $row[0], &sdate($row[1]), $descr, $row[3];
134             if ($row[3] < 0.0) {
135                 $sum_neg -= $row[3];
136             } else {
137                 $sum_pos += $row[3];
138             }
139         }
140         print "-----------------------------------------------------------------\n"
141             if ($sum_neg > 0 || $sum_pos > 0) ;
142         printf " Zahlungseingänge                                       %9.2f\n", $sum_pos
143             if ($sum_pos > 0);
144         printf " Zahlungsausgänge                                       %9.2f\n", -$sum_neg
145             if ($sum_neg > 0);
146         print "==================================================================\n";
147         printf " Summe                                                  %9.2f\n\n", $sum_pos - $sum_neg;
148     }
149 }
150
151 sub get_categories
152 {
153     my $query;
154     my @row;
155     my $sth;
156     my @arr = ();
157
158     $query  = "SELECT DISTINCT category FROM $table ORDER by category";
159     $sth = $dbh->prepare($query);
160     if ($sth && ($rc = $sth->execute) > 0) {
161         while (@row = $sth->fetchrow_array) {
162             push(@arr, $row[0]) if ($row[0]);
163         }
164     }
165     return @arr;
166 }
167
168 sub list_categories
169 {
170     @categories = &get_categories() if ($#categories);
171
172     printf "%s\n", join (", ",@categories);
173 }
174
175 sub read_input
176 {
177     my $prompt = shift;
178     my $default = shift;
179     my $ans;
180
181     if ($default) {
182         $ans = $term->readline ($prompt . " [" . $default . "]: ");
183     } else {
184         $ans = $term->readline ($prompt . ": ");
185     }
186     if (length ($ans) == 0) {
187         $ans = $default;
188     } elsif ($ans eq ".") {
189         $ans = '';
190     }
191     return $ans;
192 }
193
194 # Gibt die naechste freie Nr. in der Datenbank zurueck.  Wenn der
195 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
196 # Nr. anschliessend bereits wieder vergeben ist.
197 #
198 sub get_next_nr
199 {
200     my $query;
201     my $sth;
202     my $rc;
203     my @row;
204
205     $query = "SELECT nr FROM $table ORDER BY nr DESC";
206     $sth = $dbh->prepare($query);
207     if ($sth) {
208         $rc = $sth->execute;
209         if ($rc > 0 && (@row = $sth->fetchrow_array)) {
210             return $row[0] + 1;
211         }
212     }
213     return 1;
214 }
215
216 sub buchung_input
217 {
218     my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
219     my @input = ();
220     my $weiter = 'y';
221     my $i;
222     my $query;
223     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
224         = localtime();
225
226     print "Buchungseingabe\n\n";
227     while ($weiter =~ /[JjYy]/) {
228         $i=0;while ($i <= $#fieldname) {
229             $ans = &read_input($fieldname[$i],$input[$i]);
230             if ($fieldname[$i] eq "Category" && $ans eq "?") {
231                 @categories = &get_categories() if ($#categories);
232                 printf "  %s\n", join (", ",@categories);
233             } elsif ($fieldname[$i] eq "Datum") {
234                 if ($ans =~ /^\d+\.\d+.\d+$/) {
235                     $input[$i] = $ans;
236                     $i++;
237                 } elsif ($ans =~ /^\d+\.\d+.$/) {
238                     $ans .= $date_year + 1900;
239                     $input[$i] = $ans;
240                     $i++;
241                 } elsif ($ans =~ /^\d+\.$/) {
242                     $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
243                     $input[$i] = $ans;
244                     $i++;
245                 }
246             } else {
247                 $input[$i] = $ans;
248                 $i++;
249             }
250         }
251         if (!$input[5]) {       # USt selbst berechnen
252             if ($input[4] != 0) {
253                 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
254             }
255         }
256
257         if ($input[3] =~ /[EeIi\+]/) {
258             $input[5] *= -1 if ($input[5] < 0);
259             $input[6] *= -1 if ($input[6] < 0);
260         } else {
261             $input[5] *= -1 if ($input[5] > 0);
262             $input[6] *= -1 if ($input[6] > 0);
263         }
264
265         $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d)",
266                           &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
267                           $input[5], $input[6], $input[7]);
268         $sth = $dbh->do($query);
269         $weiter = &read_input("Weiter",'j');
270         $input[5] = 0.0;
271     }
272 }
273
274 sub usage
275 {
276     print "infocon [options] [-h|--help] commands\n";
277     print "  --buchung-category|-bc [category]\n";
278     print "  --buchung-input|-bi\n";
279     print "  --buchung-unpaid\n";
280     print "  --pay <nr> | --unpay <nr>\n";
281     print "  --list-categories|-lc\n";
282     print "  Options:\n";
283     print "    --verbose|-v\n";
284     print "    --year|-y year\n";
285     print "    --direction|--dir|-d in|out\n";
286     print "    --dm\n";
287     exit 0;
288 }
289
290 $term = new Term::ReadLine '';
291 $i = 0;
292 $opt_verbose = 0;
293 $opt_year = 0;
294 &usage() if ($#ARGV == -1);
295 while ($i <= $#ARGV) {
296     # Some aliases
297     if ($ARGV[$i] eq "-bc") {
298         $ARGV[$i] = "--buchung-category";
299     } elsif ($ARGV[$i] eq "-bi") {
300         $ARGV[$i] = "--buchung-input";
301     } elsif ($ARGV[$i] eq "-lc") {
302         $ARGV[$i] = "--list-categories";
303     }
304
305     if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
306         &usage();
307     } elsif ($ARGV[$i] =~ /^--list-/) {
308         $ARGV[$i] =~ s/^--list-//;
309         if ($ARGV[$i] eq "categories") {
310             &list_categories();
311         } else {
312             &usage();
313         }
314     } elsif ($ARGV[$i] =~ /^--buchung-/) {
315         $ARGV[$i] =~ s/^--buchung-//;
316         if ($ARGV[$i] eq "category") {
317             if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
318                 $i++;
319                 &sales_list("category = '$ARGV[$i]'");
320             } else {
321                 &sales_list();
322             }
323         } elsif ($ARGV[$i] eq "input") {
324             &buchung_input();
325         } elsif ($ARGV[$i] eq "unpaid") {
326             $saved_table = $table;
327             $table = "sales_dm";
328             &sales_list("paid = 0");
329             $table = "sales";
330             &sales_list("paid = 0");
331             $table = $table_saved;
332         } else {
333             &usage();
334         }
335     } elsif ($ARGV[$i] eq "--pay") {
336         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
337             && ($ARGV[$i+1] =~ /^\d+$/)) {
338             $i++;
339             &pay_invoice ($ARGV[$i], 1);
340         }
341     } elsif ($ARGV[$i] eq "--unpay") {
342         if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
343             && ($ARGV[$i+1] =~ /^\d+$/)) {
344             $i++;
345             &pay_invoice ($ARGV[$i], 0);
346         }
347     } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
348         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
349             $i++;
350             $opt_year = $1;
351             if ($opt_year < 70) {
352                 $opt_year += 2000;
353             } elsif ($opt_year < 100) {
354                 $opt_year += 1900;
355             }
356             if ($opt_year < 2002) {
357                 $table = "sales_dm";
358             } elsif ($opt_year > 2001) {
359                 $table = "sales";
360             }
361         }
362     } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
363              || $ARGV[$i] eq "-d") {
364         if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
365             $i++;
366             $opt_direction = $1;
367         }
368     } elsif ($ARGV[$i] eq "--dm") {
369         $table = "sales_dm";
370     } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {
371         $opt_verbose = 1;
372     }
373     $i++;
374 }