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/;
25 use Date::Calc qw/Add_Delta_YM/;
30 my $engine = "dbi:Pg:dbname=infocon";
31 my $dbh = DBI->connect($engine);
33 print "Access to database denied!\n";
46 my $opt_direction = undef;
50 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
51 return sprintf ("%d.%02d.%02d", $3,$2,$1);
56 $_[0] =~ /(\d{4})(\d{2})(\d{2})/;
57 return sprintf ("%04d-%02d-%02d", $1,$2,$3);
60 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
61 # Datenbank gespeichert werden kann.
67 return "" if (!$_[0]);
69 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
72 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
76 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
77 $day = $date_mday-1 if ($date_mday);
80 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
85 ($day,$mon,$year) = split(/\./, $_[0]);
93 } elsif ($year < 100) {
96 return sprintf("%4d%02d%02d", $year,$mon,$day);
102 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
104 return sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday) unless defined $date;
106 if ($date =~ /^(\d+)\.(\d+)\.?$/) {
107 if ($mon == 0 && $2 > 9) {
108 $date = sprintf('%04d-%02d-%02d', $year+1900-1, $2, $1);
110 $date = sprintf('%04d-%02d-%02d', $year+1900, $2, $1);
112 } elsif ($date =~ /^(\d+)\.(\d+)\.(\d+)?$/) {
113 if (length $3 == 2) {
114 $date = sprintf('20%02d-%02d-%02d', $3, $2, $1);
116 $date = sprintf('%04d-%02d-%02d', $3, $2, $1);
118 } elsif ($date !~ /^(\d+)-(\d+)-(\d+)$/) {
132 my $date = valid_isodate $opt_date;
134 die "Invalid date\n" unless defined $date;
136 $query = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
137 defined $date ? $dbh->quote($date) : 'now()', $nr);
139 $query = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
141 $sth = $dbh->do($query);
148 my $value = $hide==1?0:1;
152 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
153 $sth = $dbh->do($query);
167 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
169 $where .= " AND visible = 1";
171 $where .= "visible = 1";
176 $where .= " AND " if $where;
177 $where .= sprintf("year = %d", $opt_year);
180 if (defined $data{category} && length $data{category}) {
181 $where .= " AND " if $where;
182 $where .= sprintf("category = %s", $dbh->quote($data{category}));
185 if ($opt_direction) {
186 if ($opt_direction eq "in") {
188 } elsif ($opt_direction eq "out") {
200 $where .= " AND " if $where;
201 $where .= sprintf("description ILIKE '%%%s%%'", $opt_grep);
204 if (defined $where && $where !~ /visible/) {
205 $where .= " AND " if $where;
206 $where .= "visible = 1";
209 $query = "SELECT nr,date,description,price,tax_percent,tax_assigned FROM $table";
210 $query .= " WHERE $where" if ($where);
211 $query .= " ORDER by date,nr";
213 $sth = $dbh->prepare($query);
214 if ($sth && (my $rc = $sth->execute) > 0) {
216 print '"nr","date","description","taxrate","tax","value"'."\n";
218 print " Nr. Datum Bezeichnung Betrag\n";
219 print "----------------------------------------------------------------------------\n";
221 while ($row = $sth->fetchrow_hashref) {
222 $descr = substr($row->{description},0,50);
224 printf '"%d","%s","%s","%.2f","%.2f","%.2f"'."\n", $row->{nr}, isodate($row->{date}), $descr,
225 $row->{tax_percent}, $row->{tax_assigned}, $row->{price};
227 printf "%4d %8s %-50s %9.2f\n", $row->{nr}, sdate($row->{date}), $descr, $row->{price};
229 if ($row->{price} < 0.0) {
230 $sum_neg -= $row->{price};
232 $sum_pos += $row->{price};
236 print "---------------------------------------------------------------------------\n"
237 if ($sum_neg > 0 || $sum_pos > 0) ;
238 printf " Zahlungseingänge %9.2f\n", $sum_pos
240 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
242 print "============================================================================\n";
243 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
256 $query = "SELECT DISTINCT category FROM $table ORDER by category";
257 $sth = $dbh->prepare($query);
258 if ($sth && (my $rc = $sth->execute) > 0) {
259 while (@row = $sth->fetchrow_array) {
260 push(@arr, $row[0]) if ($row[0]);
271 @categories = get_categories unless @categories;
273 printf "%s\n", join (", ",@categories);
281 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
283 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
285 my @arr = split(/\./, $ans);
287 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
288 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
289 return sprintf("%d.%d.%d", $arr[0], $arr[1],
290 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
298 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
299 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
302 if ($answers->{einaus} =~ /[ei]/i) {
303 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
304 $ans *= -1 if $ans < 0;
306 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
307 $ans *= -1 if $ans > 0;
313 sub complete_category
315 my ($text, $line, $start) = @_;
317 return () unless exists $answers->{category} && length $answers->{category};
319 my @now = localtime();
320 my ($year,$month,$day) = Add_Delta_YM($now[5]+1900, $now[4]+1, $now[3], -3, 0);
322 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND date > '%04d%02d%02d' AND description LIKE '%s%%' ORDER BY description",
324 $answers->{category},
327 my $sth = $dbh->prepare($sql);
330 while (my $row = $sth->fetchrow_hashref) {
331 $row->{description} = substr $row->{description}, $start if $start;
332 push @complete, $row->{description};
338 sub complete_categoryname
340 my ($text, $line, $start) = @_;
342 return () unless length $line;
344 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
347 my $sth = $dbh->prepare($sql);
350 while (my $row = $sth->fetchrow_hashref) {
351 $row->{category} = substr $row->{category}, $start if $start;
352 push @complete, $row->{category};
362 my @arr = split(/\./, $answers->{date});
373 if (exists $info->{default}) {
374 if ($info->{default} eq 'last') {
375 $default = $answers->{$name} if $answers->{$name};
376 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
377 $default = $info->{default}($answers);
378 } elsif ($info->{type} && $info->{type} eq 'boolean') {
379 if ($info->{default}) {
385 $default = $info->{default};
389 if ($info->{complete}) {
390 $term->{completion_function} = $info->{complete};
392 $term->{completion_function} = undef;
396 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
398 $ans = $term->readline ($info->{title} . ": ");
401 exit unless defined $ans;
403 if (!length $ans && defined $default) {
405 } elsif ($ans eq ".") {
409 return read_input($name, $info) unless length $ans || exists $info->{empty};
411 if ($ans eq '?' && exists $info->{lookup}) {
412 $info->{lookup}($name, $answers);
413 return read_input($name, $info);
416 if (exists $info->{type} && $info->{type} eq 'boolean') {
417 if ($ans =~ /[JY1]/i) {
422 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
423 $ans = $info->{validate}($ans);
425 $ans =~ s/ *$// if $ans;
428 if (!exists $info->{type} || $info->{type} ne 'boolean') {
429 $term->addhistory($ans);
435 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
436 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
437 # Nr. anschliessend bereits wieder vergeben ist.
446 $query = "SELECT nr FROM $table ORDER BY nr DESC";
447 $sth = $dbh->prepare($query);
450 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
461 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
467 'validate' => \&validate_date,
468 'default' => 'last'},
475 'default' => \&default_year},
477 'title' => 'Kategorie',
478 'lookup' => \&list_categories,
480 'complete' => \&complete_categoryname},
482 'title' => 'Beschreibung',
484 'complete' => \&complete_category},
486 'title' => 'Ein/Aus',
490 'title' => 'Steuersatz',
493 'title' => 'Umsatzsteuer',
497 'validate' => \&calculate_price},
499 'title' => 'bezahlt',
505 'validate' => \&validate_date},
512 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
514 @categories = get_categories unless @categories;
516 $term = new Term::ReadLine '' unless $term;
518 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
519 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
521 print "Buchungseingabe\n\n";
522 while ($weiter =~ /[JjYy1]/) {
523 foreach my $f (@fields) {
524 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
528 $ans = read_input($f, $fields->{$f});
529 $answers->{$f} = $ans;
532 if ($answers->{paid}) {
533 $fields->{'billing_date'}{'default'} = $answers->{'date'};
534 $ans = read_input('billing_date', $fields->{'billing_date'});
535 $answers->{billing_date} = $ans;
537 $answers->{billing_date} = undef;
542 $sth->execute(get_next_nr(),
543 date_to_string($answers->{date}),
546 $answers->{category},
547 $answers->{description},
548 $answers->{tax_percent},
549 $answers->{tax_assigned},
551 defined $answers->{billing_date} ? date_to_string($answers->{billing_date}) : undef,
554 $weiter = $answers->{weiter};
555 $answers->{tax_assigned} = 0.0;
564 sales_list("visible = 0");
566 sales_list("visible = 0");
573 sales_list("paid = 0");
579 print "infocon [options] [-h|--help] commands\n";
580 print " --buchung-category|-bc [category]\n";
581 print " --buchung-input|-bi\n";
582 print " --buchung-unpaid|-bu\n";
583 print " --buchung-hidden\n";
584 print " --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
585 print " --pay <nr> | --unpay <nr>\n";
586 print " --hide <nr> | --unhide <nr>\n";
587 print " --list-categories|-lc\n";
591 print " --verbose|-v\n";
592 print " --grep|-g keyword\n";
593 print " --year|-y year\n";
594 print " --direction|--dir|-d in|out\n";
607 'buchung-input' => undef,
608 'buchung-unpaid' => undef,
609 'buchung-hidden' => undef,
610 'list-categories' => undef,
613 'buchung-category|bc:s' => \$data{category},
614 'pay=s' => \$data{pay},
615 'unpay=s' => \$data{unpay},
616 'hide=s' => \$data{hide},
617 'unhide=s' => \$data{unhide},
618 'year=i' => \$opt_year,
619 'date=s' => \$opt_date,
620 'direction|d=s' => \$opt_direction,
621 'grep|g=s' => \$opt_grep,
622 'mailto:s' => \$data{mailto},
625 'verbose' => \$opt_verbose,
627 'dm' => sub {$table = "sales_dm"},
628 'buchung-input|bi' => \&buchung_input,
629 'buchung-unpaid|bu' => \&buchung_unpaid,
630 'buchung-hidden|bh' => \&buchung_hidden,
631 'list-categories|lc' => \$data{'list-categories'},
634 my $cmdln = 'infocon ' . join (' ', @ARGV);
635 GetOptions(%options);
637 if ($opt_year != 0 && $opt_year < 2002) {
641 if (defined $opt_direction) {
642 usage unless $opt_direction =~ /^(in|out)$/i;
645 if (defined $data{mailto}) {
646 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
647 print "From: Joey Schulze <joey\@infodrom.org>\n";
648 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
649 printf "Subject: %s\n", $cmdln;
650 print "MIME-Version: 1.0\n";
651 print "Content-type: text/plain; charset=iso-8859-1\n";
652 print "Content-Disposition: inline\n";
653 print "Content-Transfer-Encoding: 8bit\n";
658 if (defined $data{category}) {
661 } elsif (defined $data{'list-categories'}) {
663 } elsif (defined $data{pay}) {
664 pay_invoice($data{pay}, 1);
665 } elsif (defined $data{unpay}) {
666 pay_invoice($data{unpay}, 0);
667 } elsif (defined $data{hide}) {
668 hide_invoice($data{hide}, 1);
669 } elsif (defined $data{unhide}) {
670 hide_invoice($data{unhide}, 0);