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