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";
43 my $opt_direction = undef;
47 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
48 return sprintf ("%d.%02d.%02d", $3,$2,$1);
51 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
52 # Datenbank gespeichert werden kann.
58 return "" if (!$_[0]);
60 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
63 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
67 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
68 $day = $date_mday-1 if ($date_mday);
71 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
76 ($day,$mon,$year) = split(/\./, $_[0]);
84 } elsif ($year < 100) {
87 return sprintf("%4d%02d%02d", $year,$mon,$day);
93 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
95 return sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday) unless defined $date;
97 if ($date =~ /^(\d+)\.(\d+)\.?$/) {
98 if ($mon == 0 && $2 > 9) {
99 $date = sprintf('%04d-%02d-%02d', $year+1900-1, $2, $1);
101 $date = sprintf('%04d-%02d-%02d', $year+1900, $2, $1);
103 } elsif ($date =~ /^(\d+)\.(\d+)\.(\d+)?$/) {
104 if (length $3 == 2) {
105 $date = sprintf('20%02d-%02d-%02d', $3, $2, $1);
107 $date = sprintf('%04d-%02d-%02d', $3, $2, $1);
109 } elsif ($date !~ /^(\d+)-(\d+)-(\d+)$/) {
123 my $date = valid_isodate $opt_date;
125 die "Invalid date\n" unless defined $date;
127 $query = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
128 defined $date ? $dbh->quote($date) : 'now()', $nr);
130 $query = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
132 $sth = $dbh->do($query);
139 my $value = $hide==1?0:1;
143 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
144 $sth = $dbh->do($query);
158 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
160 $where .= " AND visible = 1";
162 $where .= "visible = 1";
167 $where .= " AND " if $where;
168 $where .= sprintf("year = %d", $opt_year);
171 if (defined $data{category} && length $data{category}) {
172 $where .= " AND " if $where;
173 $where .= sprintf("category = %s", $dbh->quote($data{category}));
176 if ($opt_direction) {
177 if ($opt_direction eq "in") {
179 } elsif ($opt_direction eq "out") {
190 if ($where !~ /visible/) {
191 $where .= " AND " if $where;
192 $where .= "visible = 1";
195 $query = "SELECT nr,date,description,price FROM $table";
196 $query .= " WHERE $where" if ($where);
197 $query .= " ORDER by date,nr";
199 $sth = $dbh->prepare($query);
200 if ($sth && (my $rc = $sth->execute) > 0) {
201 print " Nr. Datum Bezeichnung Betrag\n";
202 print "----------------------------------------------------------------------------\n";
203 while (@row = $sth->fetchrow_array) {
204 $descr = substr($row[2],0,50);
205 printf "%4d %8s %-50s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
212 print "---------------------------------------------------------------------------\n"
213 if ($sum_neg > 0 || $sum_pos > 0) ;
214 printf " Zahlungseingänge %9.2f\n", $sum_pos
216 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
218 print "============================================================================\n";
219 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
231 $query = "SELECT DISTINCT category FROM $table ORDER by category";
232 $sth = $dbh->prepare($query);
233 if ($sth && (my $rc = $sth->execute) > 0) {
234 while (@row = $sth->fetchrow_array) {
235 push(@arr, $row[0]) if ($row[0]);
246 @categories = get_categories unless @categories;
248 printf "%s\n", join (", ",@categories);
256 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
258 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
260 my @arr = split(/\./, $ans);
262 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
263 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
264 return sprintf("%d.%d.%d", $arr[0], $arr[1],
265 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
273 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
274 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
277 if ($answers->{einaus} =~ /[ei]/i) {
278 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
279 $ans *= -1 if $ans < 0;
281 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
282 $ans *= -1 if $ans > 0;
288 sub complete_category
290 my ($text, $line, $start) = @_;
292 return () unless exists $answers->{category} && length $answers->{category};
294 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
296 $answers->{category},
298 my $sth = $dbh->prepare($sql);
301 while (my $row = $sth->fetchrow_hashref) {
302 $row->{description} = substr $row->{description}, $start if $start;
303 push @complete, $row->{description};
309 sub complete_categoryname
311 my ($text, $line, $start) = @_;
313 return () unless length $line;
315 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
318 my $sth = $dbh->prepare($sql);
321 while (my $row = $sth->fetchrow_hashref) {
322 $row->{category} = substr $row->{category}, $start if $start;
323 push @complete, $row->{category};
333 my @arr = split(/\./, $answers->{date});
344 if (exists $info->{default}) {
345 if ($info->{default} eq 'last') {
346 $default = $answers->{$name} if $answers->{$name};
347 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
348 $default = $info->{default}($answers);
349 } elsif ($info->{type} && $info->{type} eq 'boolean') {
350 if ($info->{default}) {
356 $default = $info->{default};
360 if ($info->{complete}) {
361 $term->{completion_function} = $info->{complete};
363 $term->{completion_function} = undef;
367 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
369 $ans = $term->readline ($info->{title} . ": ");
372 exit unless defined $ans;
374 if (!length $ans && defined $default) {
376 } elsif ($ans eq ".") {
380 return read_input($name, $info) unless length $ans || exists $info->{empty};
382 if ($ans eq '?' && exists $info->{lookup}) {
383 $info->{lookup}($name, $answers);
384 return read_input($name, $info);
387 if (exists $info->{type} && $info->{type} eq 'boolean') {
388 if ($ans =~ /[JY1]/i) {
393 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
394 $ans = $info->{validate}($ans);
396 $ans =~ s/ *$// if $ans;
399 if (!exists $info->{type} || $info->{type} ne 'boolean') {
400 $term->addhistory($ans);
406 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
407 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
408 # Nr. anschliessend bereits wieder vergeben ist.
417 $query = "SELECT nr FROM $table ORDER BY nr DESC";
418 $sth = $dbh->prepare($query);
421 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
432 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
438 'validate' => \&validate_date,
439 'default' => 'last'},
446 'default' => \&default_year},
448 'title' => 'Kategorie',
449 'lookup' => \&list_categories,
451 'complete' => \&complete_categoryname},
453 'title' => 'Beschreibung',
455 'complete' => \&complete_category},
457 'title' => 'Ein/Aus',
461 'title' => 'Steuersatz',
464 'title' => 'Umsatzsteuer',
468 'validate' => \&calculate_price},
470 'title' => 'bezahlt',
476 'validate' => \&validate_date},
483 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
485 @categories = get_categories unless @categories;
487 $term = new Term::ReadLine '' unless $term;
489 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
490 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
492 print "Buchungseingabe\n\n";
493 while ($weiter =~ /[JjYy1]/) {
494 foreach my $f (@fields) {
495 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
499 $ans = read_input($f, $fields->{$f});
500 $answers->{$f} = $ans;
503 if ($answers->{paid}) {
504 $fields->{'billing_date'}{'default'} = $answers->{'date'};
505 $ans = read_input('billing_date', $fields->{'billing_date'});
506 $answers->{billing_date} = $ans;
508 $answers->{billing_date} = undef;
513 $sth->execute(get_next_nr(),
514 date_to_string($answers->{date}),
517 $answers->{category},
518 $answers->{description},
519 $answers->{tax_percent},
520 $answers->{tax_assigned},
522 defined $answers->{billing_date} ? date_to_string($answers->{billing_date}) : undef,
525 $weiter = $answers->{weiter};
526 $answers->{tax_assigned} = 0.0;
535 sales_list("visible = 0");
537 sales_list("visible = 0");
544 sales_list("paid = 0");
550 print "infocon [options] [-h|--help] commands\n";
551 print " --buchung-category|-bc [category]\n";
552 print " --buchung-input|-bi\n";
553 print " --buchung-unpaid\n";
554 print " --buchung-hidden\n";
555 print " --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
556 print " --pay <nr> | --unpay <nr>\n";
557 print " --hide <nr> | --unhide <nr>\n";
558 print " --list-categories|-lc\n";
561 print " --verbose|-v\n";
562 print " --year|-y year\n";
563 print " --direction|--dir|-d in|out\n";
576 'buchung-input' => undef,
577 'buchung-unpaid' => undef,
578 'buchung-hidden' => undef,
579 'list-categories' => undef,
582 'buchung-category|bc:s' => \$data{category},
583 'pay=s' => \$data{pay},
584 'unpay=s' => \$data{unpay},
585 'hide=s' => \$data{hide},
586 'unhide=s' => \$data{unhide},
587 'year=i' => \$opt_year,
588 'date=s' => \$opt_date,
589 'direction|d=s' => \$opt_direction,
590 'mailto:s' => \$data{mailto},
592 'verbose' => \$opt_verbose,
594 'dm' => sub {$table = "sales_dm"},
595 'buchung-input|bi' => \&buchung_input,
596 'buchung-unpaid|bu' => \&buchung_unpaid,
597 'buchung-hidden|bh' => \&buchung_hidden,
598 'list-categories|lc' => \$data{'list-categories'},
601 my $cmdln = 'infocon ' . join (' ', @ARGV);
602 GetOptions(%options);
604 if ($opt_year != 0 && $opt_year < 2002) {
608 if (defined $opt_direction) {
609 usage unless $opt_direction =~ /^(in|out)$/i;
612 if (defined $data{mailto}) {
613 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
614 print "From: Joey Schulze <joey\@infodrom.org>\n";
615 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
616 printf "Subject: %s\n", $cmdln;
617 print "MIME-Version: 1.0\n";
618 print "Content-type: text/plain; charset=iso-8859-1\n";
619 print "Content-Disposition: inline\n";
620 print "Content-Transfer-Encoding: 8bit\n";
625 if (defined $data{category}) {
628 } elsif (defined $data{'list-categories'}) {
630 } elsif (defined $data{pay}) {
631 pay_invoice($data{pay}, 1);
632 } elsif (defined $data{unpay}) {
633 pay_invoice($data{unpay}, 0);
634 } elsif (defined $data{hide}) {
635 hide_invoice($data{hide}, 1);
636 } elsif (defined $data{unhide}) {
637 hide_invoice($data{unhide}, 0);