8147a3ec49579c7aabb5b9ecf9c40eea3f1652fd
[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 $category = shift;
186     my $query;
187     my @row;
188     my $sth;
189     my @arr = ();
190     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
191         = localtime;
192
193     $query  = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ",
194                       $table, $date_year+1900);
195     $query .= sprintf("AND category = '%s' ", $category) if defined $category;
196     $query .= "ORDER by description";
197     $sth = $dbh->prepare($query);
198     if ($sth && (my $rc = $sth->execute) > 0) {
199         while (@row = $sth->fetchrow_array) {
200             push(@arr, $row[0]) if ($row[0]);
201         }
202     }
203     return @arr;
204 }
205
206 sub get_categories
207 {
208     my $query;
209     my @row;
210     my $sth;
211     my @arr = ();
212
213     $query  = "SELECT DISTINCT category FROM $table ORDER by category";
214     $sth = $dbh->prepare($query);
215     if ($sth && (my $rc = $sth->execute) > 0) {
216         while (@row = $sth->fetchrow_array) {
217             push(@arr, $row[0]) if ($row[0]);
218         }
219     }
220     return @arr;
221 }
222
223 sub list_categories
224 {
225     @categories = get_categories unless @categories;
226
227     printf "%s\n", join (", ",@categories);
228
229     exit;
230 }
231
232 sub read_input
233 {
234     my $prompt = shift;
235     my $default = shift;
236     my $ans;
237
238     if ($default) {
239         $ans = $term->readline ($prompt . " [" . $default . "]: ");
240     } else {
241         $ans = $term->readline ($prompt . ": ");
242     }
243     if (length ($ans) == 0) {
244         $ans = $default;
245     } elsif ($ans eq ".") {
246         $ans = '';
247     }
248     $ans =~ s/ *$// if $ans;
249     return $ans;
250 }
251
252 # Gibt die naechste freie Nr. in der Datenbank zurueck.  Wenn der
253 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
254 # Nr. anschliessend bereits wieder vergeben ist.
255 #
256 sub get_next_nr
257 {
258     my $query;
259     my $sth;
260     my $rc;
261     my @row;
262
263     $query = "SELECT nr FROM $table ORDER BY nr DESC";
264     $sth = $dbh->prepare($query);
265     if ($sth) {
266         $rc = $sth->execute;
267         if ($rc > 0 && (@row = $sth->fetchrow_array)) {
268             return $row[0] + 1;
269         }
270     }
271     return 1;
272 }
273
274 sub buchung_input
275 {
276     my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
277     my @input = ();
278     my $weiter = 'y';
279     my $i;
280     my $ans;
281     my $query;
282     my @descriptions;
283     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
284         = localtime;
285
286     @categories = get_categories unless @categories;
287     @descriptions = get_descriptions;
288
289     $term = new Term::ReadLine '' unless $term;
290
291     $term->addhistory($_) foreach (@categories);
292     $term->addhistory($_) foreach (@descriptions);
293
294     my $attribs = $term->Attribs;
295
296     my $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
297
298     print "Buchungseingabe\n\n";
299     while ($weiter =~ /[JjYy1]/) {
300         $i=0;while ($i <= $#fieldname) {
301             if ($fieldname[$i] eq "Category") {
302                 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
303                 $attribs->{completion_word} = \@categories;
304             } elsif ($fieldname[$i] eq "Description") {
305                 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
306                 @descriptions = get_descriptions $input[1];
307                 $attribs->{completion_word} = \@descriptions;
308             } elsif ($fieldname[$i] eq "Tax assigned") {
309                 if ($input[$i-1] == 0) {
310                     $input[$i++] = 0;
311                     next;
312                 }
313             } else {
314                 $attribs->{completion_word} = undef;
315             }
316             $ans = read_input($fieldname[$i],$input[$i]);
317             if ($fieldname[$i] eq "Category" && $ans eq "?") {
318                 printf "  %s\n", join (", ",@categories);
319             } elsif ($fieldname[$i] eq "Datum") {
320                 if ($ans =~ /^\d+\.\d+.\d+$/) {
321                     $input[$i] = $ans;
322                     $i++;
323                 } elsif ($ans =~ /^\d+\.\d+.$/) {
324                     $ans .= $date_year + 1900;
325                     $input[$i] = $ans;
326                     $i++;
327                 } elsif ($ans =~ /^\d+\.$/) {
328                     $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
329                     $input[$i] = $ans;
330                     $i++;
331                 }
332             } elsif ($fieldname[$i] eq "Paid") {
333                 if ($ans =~ /[1jJyY]/) {
334                     $input[$i] = 1;
335                 } else {
336                     $input[$i] = 0;
337                 }
338                 $i++;
339             } else {
340                 $input[$i] = $ans;
341                 $i++;
342             }
343         }
344         if (!$input[5]) {       # USt selbst berechnen
345             if ($input[4] != 0) {
346                 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
347             }
348         }
349
350         if ($input[3] =~ /[EeIi\+]/) {
351             $input[5] *= -1 if ($input[5] < 0);
352             $input[6] *= -1 if ($input[6] < 0);
353         } else {
354             $input[5] *= -1 if ($input[5] > 0);
355             $input[6] *= -1 if ($input[6] > 0);
356         }
357
358         $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
359                           $input[5], $input[6], 1, $input[7]);
360         $weiter = read_input("Weiter",'j');
361         $input[5] = 0.0;
362     }
363
364     exit;
365 }
366
367 sub buchung_hidden
368 {
369     $table = "sales_dm";
370     sales_list("visible = 0");
371     $table = "sales";
372     sales_list("visible = 0");
373     exit;
374 }
375
376 sub buchung_unpaid
377 {
378     $table = "sales_dm";
379     sales_list("paid = 0");
380     $table = "sales";
381     sales_list("paid = 0");
382     exit;
383 }
384
385 sub usage
386 {
387     print "infocon [options] [-h|--help] commands\n";
388     print "  --buchung-category|-bc [category]\n";
389     print "  --buchung-input|-bi\n";
390     print "  --buchung-unpaid\n";
391     print "  --buchung-hidden\n";
392     print "  --pay <nr> | --unpay <nr>\n";
393     print "  --hide <nr> | --unhide <nr>\n";
394     print "  --list-categories|-lc\n";
395     print "  Options:\n";
396     print "    --all|-a\n";
397     print "    --verbose|-v\n";
398     print "    --year|-y year\n";
399     print "    --direction|--dir|-d in|out\n";
400     print "    --dm\n";
401     exit 0;
402 }
403
404 %data = (
405     'category' => undef,
406     'done' => undef,
407     'pay' => undef,
408     'unpay' => undef,
409     'hide' => undef,
410     'unhide' => undef,
411     'mailto' => undef,
412     'buchung-input' => undef,
413     'buchung-unpaid' => undef,
414     'buchung-hidden' => undef,
415     'list-categories' => undef,
416     );
417 my %options = (
418     'buchung-category|bc:s' => \$data{category},
419     'pay=s' => \$data{pay},
420     'unpay=s' => \$data{unpay},
421     'hide=s' => \$data{hide},
422     'unhide=s' => \$data{unhide},
423     'year=i' => \$opt_year,
424     'direction=s' => \$opt_direction,
425     'mailto:s' => \$data{mailto},
426     'all' => \$opt_all,
427     'verbose' => \$opt_verbose,
428     'help' => \&usage,
429     'dm' => sub {$table = "sales_dm"},
430     'buchung-input|bi' => \$data{'buchung-input'},
431     'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
432     'buchung-hidden|bh' => \$data{'buchung-hidden'},
433     'list-categories|lc' => \$data{'list-categories'},
434     );
435
436 my $cmdln = 'infocon ' . join (' ', @ARGV);
437 GetOptions(%options);
438
439 if ($opt_year != 0 && $opt_year < 2002) {
440     $table = "sales_dm";
441 }
442
443 if (defined $opt_direction) {
444     usage unless $opt_direction =~ /^(in|out)$/i;
445 }
446
447 if (defined $data{mailto}) {
448     if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
449         print  "From: Joey Schulze <joey\@infodrom.org>\n";
450         printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
451         printf "Subject: %s\n", $cmdln;
452         print  "MIME-Version: 1.0\n";
453         print  "Content-type: text/plain; charset=iso-8859-1\n";
454         print  "Content-Disposition: inline\n";
455         print  "Content-Transfer-Encoding: 8bit\n";
456         print  "\n";
457     }
458 }
459
460 if (defined $data{category}) {
461     if (length($data{category})) {
462         sales_list("category = '".$data{category}."'");
463     } else {
464         sales_list;
465     }
466     exit;
467 } elsif (defined $data{'buchung-input'}) {
468     buchung_input;
469 } elsif (defined $data{'buchung-unpaid'}) {
470     buchung_unpaid;
471 } elsif (defined $data{'buchung-hidden'}) {
472     buchung_hidden;
473 } elsif (defined $data{'list-categories'}) {
474     list_categories;
475 } elsif (defined $data{pay}) {
476     pay_invoce($data{pay}, 1);
477 } elsif (defined $data{unpay}) {
478     pay_invoce($data{unpay}, 0);
479 } elsif (defined $data{hide}) {
480     hide_invoce($data{hide}, 1);
481 } elsif (defined $data{unhide}) {
482     hide_invoce($data{unhide}, 0);
483 }
484
485 usage;