3 # infocon - Administration tool for InfoCon
4 # Copyright (c) 1998-2003,2005-8,10,11,12,14,15,16,18 Martin Schulze <joey@infodrom.org>
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.
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.
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.
24 use Scalar::Util qw/reftype/;
29 my $engine = "dbi:Pg:dbname=infocon";
30 my $dbh = DBI->connect($engine);
32 print "Access to database denied!\n";
45 my $opt_direction = undef;
49 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
50 return sprintf ("%d.%02d.%02d", $3,$2,$1);
55 $_[0] =~ /(\d{4})(\d{2})(\d{2})/;
56 return sprintf ("%04d-%02d-%02d", $1,$2,$3);
59 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
60 # Datenbank gespeichert werden kann.
66 return "" if (!$_[0]);
68 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
71 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
75 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
76 $day = $date_mday-1 if ($date_mday);
79 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
84 ($day,$mon,$year) = split(/\./, $_[0]);
92 } elsif ($year < 100) {
95 return sprintf("%4d%02d%02d", $year,$mon,$day);
101 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
103 return sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday) unless defined $date;
105 if ($date =~ /^(\d+)\.(\d+)\.?$/) {
106 if ($mon == 0 && $2 > 9) {
107 $date = sprintf('%04d-%02d-%02d', $year+1900-1, $2, $1);
109 $date = sprintf('%04d-%02d-%02d', $year+1900, $2, $1);
111 } elsif ($date =~ /^(\d+)\.(\d+)\.(\d+)?$/) {
112 if (length $3 == 2) {
113 $date = sprintf('20%02d-%02d-%02d', $3, $2, $1);
115 $date = sprintf('%04d-%02d-%02d', $3, $2, $1);
117 } elsif ($date !~ /^(\d+)-(\d+)-(\d+)$/) {
131 my $date = valid_isodate $opt_date;
133 die "Invalid date\n" unless defined $date;
135 $query = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
136 defined $date ? $dbh->quote($date) : 'now()', $nr);
138 $query = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
140 $sth = $dbh->do($query);
147 my $value = $hide==1?0:1;
151 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
152 $sth = $dbh->do($query);
166 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
168 $where .= " AND visible = 1";
170 $where .= "visible = 1";
175 $where .= " AND " if $where;
176 $where .= sprintf("year = %d", $opt_year);
179 if (defined $data{category} && length $data{category}) {
180 $where .= " AND " if $where;
181 $where .= sprintf("category = %s", $dbh->quote($data{category}));
184 if ($opt_direction) {
185 if ($opt_direction eq "in") {
187 } elsif ($opt_direction eq "out") {
199 $where .= " AND " if $where;
200 $where .= sprintf("description ILIKE '%%%s%%'", $opt_grep);
203 if ($where !~ /visible/) {
204 $where .= " AND " if $where;
205 $where .= "visible = 1";
208 $query = "SELECT nr,date,description,price,tax_percent,tax_assigned FROM $table";
209 $query .= " WHERE $where" if ($where);
210 $query .= " ORDER by date,nr";
212 $sth = $dbh->prepare($query);
213 if ($sth && (my $rc = $sth->execute) > 0) {
215 print '"nr","date","description","taxrate","tax","value"'."\n";
217 print " Nr. Datum Bezeichnung Betrag\n";
218 print "----------------------------------------------------------------------------\n";
220 while ($row = $sth->fetchrow_hashref) {
221 $descr = substr($row->{description},0,50);
223 printf '"%d","%s","%s","%.2f","%.2f","%.2f"'."\n", $row->{nr}, isodate($row->{date}), $descr,
224 $row->{tax_percent}, $row->{tax_assigned}, $row->{price};
226 printf "%4d %8s %-50s %9.2f\n", $row->{nr}, sdate($row->{date}), $descr, $row->{price};
228 if ($row->{price} < 0.0) {
229 $sum_neg -= $row->{price};
231 $sum_pos += $row->{price};
235 print "---------------------------------------------------------------------------\n"
236 if ($sum_neg > 0 || $sum_pos > 0) ;
237 printf " Zahlungseingänge %9.2f\n", $sum_pos
239 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
241 print "============================================================================\n";
242 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
255 $query = "SELECT DISTINCT category FROM $table ORDER by category";
256 $sth = $dbh->prepare($query);
257 if ($sth && (my $rc = $sth->execute) > 0) {
258 while (@row = $sth->fetchrow_array) {
259 push(@arr, $row[0]) if ($row[0]);
270 @categories = get_categories unless @categories;
272 printf "%s\n", join (", ",@categories);
280 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
282 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
284 my @arr = split(/\./, $ans);
286 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
287 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
288 return sprintf("%d.%d.%d", $arr[0], $arr[1],
289 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
297 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
298 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
301 if ($answers->{einaus} =~ /[ei]/i) {
302 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
303 $ans *= -1 if $ans < 0;
305 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
306 $ans *= -1 if $ans > 0;
312 sub complete_category
314 my ($text, $line, $start) = @_;
316 return () unless exists $answers->{category} && length $answers->{category};
318 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
320 $answers->{category},
322 my $sth = $dbh->prepare($sql);
325 while (my $row = $sth->fetchrow_hashref) {
326 $row->{description} = substr $row->{description}, $start if $start;
327 push @complete, $row->{description};
333 sub complete_categoryname
335 my ($text, $line, $start) = @_;
337 return () unless length $line;
339 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
342 my $sth = $dbh->prepare($sql);
345 while (my $row = $sth->fetchrow_hashref) {
346 $row->{category} = substr $row->{category}, $start if $start;
347 push @complete, $row->{category};
357 my @arr = split(/\./, $answers->{date});
368 if (exists $info->{default}) {
369 if ($info->{default} eq 'last') {
370 $default = $answers->{$name} if $answers->{$name};
371 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
372 $default = $info->{default}($answers);
373 } elsif ($info->{type} && $info->{type} eq 'boolean') {
374 if ($info->{default}) {
380 $default = $info->{default};
384 if ($info->{complete}) {
385 $term->{completion_function} = $info->{complete};
387 $term->{completion_function} = undef;
391 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
393 $ans = $term->readline ($info->{title} . ": ");
396 exit unless defined $ans;
398 if (!length $ans && defined $default) {
400 } elsif ($ans eq ".") {
404 return read_input($name, $info) unless length $ans || exists $info->{empty};
406 if ($ans eq '?' && exists $info->{lookup}) {
407 $info->{lookup}($name, $answers);
408 return read_input($name, $info);
411 if (exists $info->{type} && $info->{type} eq 'boolean') {
412 if ($ans =~ /[JY1]/i) {
417 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
418 $ans = $info->{validate}($ans);
420 $ans =~ s/ *$// if $ans;
423 if (!exists $info->{type} || $info->{type} ne 'boolean') {
424 $term->addhistory($ans);
430 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
431 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
432 # Nr. anschliessend bereits wieder vergeben ist.
441 $query = "SELECT nr FROM $table ORDER BY nr DESC";
442 $sth = $dbh->prepare($query);
445 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
456 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
462 'validate' => \&validate_date,
463 'default' => 'last'},
470 'default' => \&default_year},
472 'title' => 'Kategorie',
473 'lookup' => \&list_categories,
475 'complete' => \&complete_categoryname},
477 'title' => 'Beschreibung',
479 'complete' => \&complete_category},
481 'title' => 'Ein/Aus',
485 'title' => 'Steuersatz',
488 'title' => 'Umsatzsteuer',
492 'validate' => \&calculate_price},
494 'title' => 'bezahlt',
500 'validate' => \&validate_date},
507 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
509 @categories = get_categories unless @categories;
511 $term = new Term::ReadLine '' unless $term;
513 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
514 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
516 print "Buchungseingabe\n\n";
517 while ($weiter =~ /[JjYy1]/) {
518 foreach my $f (@fields) {
519 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
523 $ans = read_input($f, $fields->{$f});
524 $answers->{$f} = $ans;
527 if ($answers->{paid}) {
528 $fields->{'billing_date'}{'default'} = $answers->{'date'};
529 $ans = read_input('billing_date', $fields->{'billing_date'});
530 $answers->{billing_date} = $ans;
532 $answers->{billing_date} = undef;
537 $sth->execute(get_next_nr(),
538 date_to_string($answers->{date}),
541 $answers->{category},
542 $answers->{description},
543 $answers->{tax_percent},
544 $answers->{tax_assigned},
546 defined $answers->{billing_date} ? date_to_string($answers->{billing_date}) : undef,
549 $weiter = $answers->{weiter};
550 $answers->{tax_assigned} = 0.0;
559 sales_list("visible = 0");
561 sales_list("visible = 0");
568 sales_list("paid = 0");
574 print "infocon [options] [-h|--help] commands\n";
575 print " --buchung-category|-bc [category]\n";
576 print " --buchung-input|-bi\n";
577 print " --buchung-unpaid|-bu\n";
578 print " --buchung-hidden\n";
579 print " --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
580 print " --pay <nr> | --unpay <nr>\n";
581 print " --hide <nr> | --unhide <nr>\n";
582 print " --list-categories|-lc\n";
586 print " --verbose|-v\n";
587 print " --grep|-g keyword\n";
588 print " --year|-y year\n";
589 print " --direction|--dir|-d in|out\n";
602 'buchung-input' => undef,
603 'buchung-unpaid' => undef,
604 'buchung-hidden' => undef,
605 'list-categories' => undef,
608 'buchung-category|bc:s' => \$data{category},
609 'pay=s' => \$data{pay},
610 'unpay=s' => \$data{unpay},
611 'hide=s' => \$data{hide},
612 'unhide=s' => \$data{unhide},
613 'year=i' => \$opt_year,
614 'date=s' => \$opt_date,
615 'direction|d=s' => \$opt_direction,
616 'grep|g=s' => \$opt_grep,
617 'mailto:s' => \$data{mailto},
620 'verbose' => \$opt_verbose,
622 'dm' => sub {$table = "sales_dm"},
623 'buchung-input|bi' => \&buchung_input,
624 'buchung-unpaid|bu' => \&buchung_unpaid,
625 'buchung-hidden|bh' => \&buchung_hidden,
626 'list-categories|lc' => \$data{'list-categories'},
629 my $cmdln = 'infocon ' . join (' ', @ARGV);
630 GetOptions(%options);
632 if ($opt_year != 0 && $opt_year < 2002) {
636 if (defined $opt_direction) {
637 usage unless $opt_direction =~ /^(in|out)$/i;
640 if (defined $data{mailto}) {
641 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
642 print "From: Joey Schulze <joey\@infodrom.org>\n";
643 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
644 printf "Subject: %s\n", $cmdln;
645 print "MIME-Version: 1.0\n";
646 print "Content-type: text/plain; charset=iso-8859-1\n";
647 print "Content-Disposition: inline\n";
648 print "Content-Transfer-Encoding: 8bit\n";
653 if (defined $data{category}) {
656 } elsif (defined $data{'list-categories'}) {
658 } elsif (defined $data{pay}) {
659 pay_invoice($data{pay}, 1);
660 } elsif (defined $data{unpay}) {
661 pay_invoice($data{unpay}, 0);
662 } elsif (defined $data{hide}) {
663 hide_invoice($data{hide}, 1);
664 } elsif (defined $data{unhide}) {
665 hide_invoice($data{unhide}, 0);