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