3 # infocon - Administration tool for InfoCon
4 # Copyright (c) 1998-2003,2005-8,10,11,12,14,15,16 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";
44 my $opt_direction = undef;
48 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
49 return sprintf ("%d.%02d.%02d", $3,$2,$1);
54 $_[0] =~ /(\d{4})(\d{2})(\d{2})/;
55 return sprintf ("%04d-%02d-%02d", $1,$2,$3);
58 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
59 # Datenbank gespeichert werden kann.
65 return "" if (!$_[0]);
67 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
70 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
74 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
75 $day = $date_mday-1 if ($date_mday);
78 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
83 ($day,$mon,$year) = split(/\./, $_[0]);
91 } elsif ($year < 100) {
94 return sprintf("%4d%02d%02d", $year,$mon,$day);
100 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
102 return sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday) unless defined $date;
104 if ($date =~ /^(\d+)\.(\d+)\.?$/) {
105 if ($mon == 0 && $2 > 9) {
106 $date = sprintf('%04d-%02d-%02d', $year+1900-1, $2, $1);
108 $date = sprintf('%04d-%02d-%02d', $year+1900, $2, $1);
110 } elsif ($date =~ /^(\d+)\.(\d+)\.(\d+)?$/) {
111 if (length $3 == 2) {
112 $date = sprintf('20%02d-%02d-%02d', $3, $2, $1);
114 $date = sprintf('%04d-%02d-%02d', $3, $2, $1);
116 } elsif ($date !~ /^(\d+)-(\d+)-(\d+)$/) {
130 my $date = valid_isodate $opt_date;
132 die "Invalid date\n" unless defined $date;
134 $query = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
135 defined $date ? $dbh->quote($date) : 'now()', $nr);
137 $query = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
139 $sth = $dbh->do($query);
146 my $value = $hide==1?0:1;
150 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
151 $sth = $dbh->do($query);
165 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
167 $where .= " AND visible = 1";
169 $where .= "visible = 1";
174 $where .= " AND " if $where;
175 $where .= sprintf("year = %d", $opt_year);
178 if (defined $data{category} && length $data{category}) {
179 $where .= " AND " if $where;
180 $where .= sprintf("category = %s", $dbh->quote($data{category}));
183 if ($opt_direction) {
184 if ($opt_direction eq "in") {
186 } elsif ($opt_direction eq "out") {
197 if ($where !~ /visible/) {
198 $where .= " AND " if $where;
199 $where .= "visible = 1";
202 $query = "SELECT nr,date,description,price,tax_percent,tax_assigned FROM $table";
203 $query .= " WHERE $where" if ($where);
204 $query .= " ORDER by date,nr";
206 $sth = $dbh->prepare($query);
207 if ($sth && (my $rc = $sth->execute) > 0) {
209 print '"nr","date","description","taxrate","tax","value"'."\n";
211 print " Nr. Datum Bezeichnung Betrag\n";
212 print "----------------------------------------------------------------------------\n";
214 while ($row = $sth->fetchrow_hashref) {
215 $descr = substr($row->{description},0,50);
217 printf '"%d","%s","%s","%.2f","%.2f","%.2f"'."\n", $row->{nr}, isodate($row->{date}), $descr,
218 $row->{tax_percent}, $row->{tax_assigned}, $row->{price};
220 printf "%4d %8s %-50s %9.2f\n", $row->{nr}, sdate($row->{date}), $descr, $row->{price};
222 if ($row->{price} < 0.0) {
223 $sum_neg -= $row->{price};
225 $sum_pos += $row->{price};
229 print "---------------------------------------------------------------------------\n"
230 if ($sum_neg > 0 || $sum_pos > 0) ;
231 printf " Zahlungseingänge %9.2f\n", $sum_pos
233 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
235 print "============================================================================\n";
236 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
249 $query = "SELECT DISTINCT category FROM $table ORDER by category";
250 $sth = $dbh->prepare($query);
251 if ($sth && (my $rc = $sth->execute) > 0) {
252 while (@row = $sth->fetchrow_array) {
253 push(@arr, $row[0]) if ($row[0]);
264 @categories = get_categories unless @categories;
266 printf "%s\n", join (", ",@categories);
274 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
276 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
278 my @arr = split(/\./, $ans);
280 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
281 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
282 return sprintf("%d.%d.%d", $arr[0], $arr[1],
283 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
291 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
292 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
295 if ($answers->{einaus} =~ /[ei]/i) {
296 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
297 $ans *= -1 if $ans < 0;
299 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
300 $ans *= -1 if $ans > 0;
306 sub complete_category
308 my ($text, $line, $start) = @_;
310 return () unless exists $answers->{category} && length $answers->{category};
312 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
314 $answers->{category},
316 my $sth = $dbh->prepare($sql);
319 while (my $row = $sth->fetchrow_hashref) {
320 $row->{description} = substr $row->{description}, $start if $start;
321 push @complete, $row->{description};
327 sub complete_categoryname
329 my ($text, $line, $start) = @_;
331 return () unless length $line;
333 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
336 my $sth = $dbh->prepare($sql);
339 while (my $row = $sth->fetchrow_hashref) {
340 $row->{category} = substr $row->{category}, $start if $start;
341 push @complete, $row->{category};
351 my @arr = split(/\./, $answers->{date});
362 if (exists $info->{default}) {
363 if ($info->{default} eq 'last') {
364 $default = $answers->{$name} if $answers->{$name};
365 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
366 $default = $info->{default}($answers);
367 } elsif ($info->{type} && $info->{type} eq 'boolean') {
368 if ($info->{default}) {
374 $default = $info->{default};
378 if ($info->{complete}) {
379 $term->{completion_function} = $info->{complete};
381 $term->{completion_function} = undef;
385 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
387 $ans = $term->readline ($info->{title} . ": ");
390 exit unless defined $ans;
392 if (!length $ans && defined $default) {
394 } elsif ($ans eq ".") {
398 return read_input($name, $info) unless length $ans || exists $info->{empty};
400 if ($ans eq '?' && exists $info->{lookup}) {
401 $info->{lookup}($name, $answers);
402 return read_input($name, $info);
405 if (exists $info->{type} && $info->{type} eq 'boolean') {
406 if ($ans =~ /[JY1]/i) {
411 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
412 $ans = $info->{validate}($ans);
414 $ans =~ s/ *$// if $ans;
417 if (!exists $info->{type} || $info->{type} ne 'boolean') {
418 $term->addhistory($ans);
424 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
425 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
426 # Nr. anschliessend bereits wieder vergeben ist.
435 $query = "SELECT nr FROM $table ORDER BY nr DESC";
436 $sth = $dbh->prepare($query);
439 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
450 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
456 'validate' => \&validate_date,
457 'default' => 'last'},
464 'default' => \&default_year},
466 'title' => 'Kategorie',
467 'lookup' => \&list_categories,
469 'complete' => \&complete_categoryname},
471 'title' => 'Beschreibung',
473 'complete' => \&complete_category},
475 'title' => 'Ein/Aus',
479 'title' => 'Steuersatz',
482 'title' => 'Umsatzsteuer',
486 'validate' => \&calculate_price},
488 'title' => 'bezahlt',
494 'validate' => \&validate_date},
501 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
503 @categories = get_categories unless @categories;
505 $term = new Term::ReadLine '' unless $term;
507 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
508 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
510 print "Buchungseingabe\n\n";
511 while ($weiter =~ /[JjYy1]/) {
512 foreach my $f (@fields) {
513 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
517 $ans = read_input($f, $fields->{$f});
518 $answers->{$f} = $ans;
521 if ($answers->{paid}) {
522 $fields->{'billing_date'}{'default'} = $answers->{'date'};
523 $ans = read_input('billing_date', $fields->{'billing_date'});
524 $answers->{billing_date} = $ans;
526 $answers->{billing_date} = undef;
531 $sth->execute(get_next_nr(),
532 date_to_string($answers->{date}),
535 $answers->{category},
536 $answers->{description},
537 $answers->{tax_percent},
538 $answers->{tax_assigned},
540 defined $answers->{billing_date} ? date_to_string($answers->{billing_date}) : undef,
543 $weiter = $answers->{weiter};
544 $answers->{tax_assigned} = 0.0;
553 sales_list("visible = 0");
555 sales_list("visible = 0");
562 sales_list("paid = 0");
568 print "infocon [options] [-h|--help] commands\n";
569 print " --buchung-category|-bc [category]\n";
570 print " --buchung-input|-bi\n";
571 print " --buchung-unpaid\n";
572 print " --buchung-hidden\n";
573 print " --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
574 print " --pay <nr> | --unpay <nr>\n";
575 print " --hide <nr> | --unhide <nr>\n";
576 print " --list-categories|-lc\n";
580 print " --verbose|-v\n";
581 print " --year|-y year\n";
582 print " --direction|--dir|-d in|out\n";
595 'buchung-input' => undef,
596 'buchung-unpaid' => undef,
597 'buchung-hidden' => undef,
598 'list-categories' => undef,
601 'buchung-category|bc:s' => \$data{category},
602 'pay=s' => \$data{pay},
603 'unpay=s' => \$data{unpay},
604 'hide=s' => \$data{hide},
605 'unhide=s' => \$data{unhide},
606 'year=i' => \$opt_year,
607 'date=s' => \$opt_date,
608 'direction|d=s' => \$opt_direction,
609 'mailto:s' => \$data{mailto},
612 'verbose' => \$opt_verbose,
614 'dm' => sub {$table = "sales_dm"},
615 'buchung-input|bi' => \&buchung_input,
616 'buchung-unpaid|bu' => \&buchung_unpaid,
617 'buchung-hidden|bh' => \&buchung_hidden,
618 'list-categories|lc' => \$data{'list-categories'},
621 my $cmdln = 'infocon ' . join (' ', @ARGV);
622 GetOptions(%options);
624 if ($opt_year != 0 && $opt_year < 2002) {
628 if (defined $opt_direction) {
629 usage unless $opt_direction =~ /^(in|out)$/i;
632 if (defined $data{mailto}) {
633 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
634 print "From: Joey Schulze <joey\@infodrom.org>\n";
635 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
636 printf "Subject: %s\n", $cmdln;
637 print "MIME-Version: 1.0\n";
638 print "Content-type: text/plain; charset=iso-8859-1\n";
639 print "Content-Disposition: inline\n";
640 print "Content-Transfer-Encoding: 8bit\n";
645 if (defined $data{category}) {
648 } elsif (defined $data{'list-categories'}) {
650 } elsif (defined $data{pay}) {
651 pay_invoice($data{pay}, 1);
652 } elsif (defined $data{unpay}) {
653 pay_invoice($data{unpay}, 0);
654 } elsif (defined $data{hide}) {
655 hide_invoice($data{hide}, 1);
656 } elsif (defined $data{unhide}) {
657 hide_invoice($data{unhide}, 0);