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