3 # infocon - Administration tool for InfoCon
4 # Copyright (c) 1998-2003,2005-8,10,11 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.
26 use Scalar::Util qw/reftype/;
31 my $engine = "dbi:Pg:dbname=infocon";
32 my $dbh = DBI->connect($engine);
34 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);
52 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
53 # Datenbank gespeichert werden kann.
59 return "" if (!$_[0]);
61 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
64 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
68 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
69 $day = $date_mday-1 if ($date_mday);
72 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
77 ($day,$mon,$year) = split(/\./, $_[0]);
85 } elsif ($year < 100) {
88 return sprintf("%4d%02d%02d", $year,$mon,$day);
95 my $value = $pay==1?1:0;
99 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
100 $sth = $dbh->do($query);
107 my $value = $hide==1?0:1;
111 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
112 $sth = $dbh->do($query);
126 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
128 $where .= " AND visible = 1";
130 $where .= "visible = 1";
135 $where .= " AND " if $where;
136 $where .= sprintf("year = %d", $opt_year);
139 if ($opt_direction) {
140 if ($opt_direction eq "in") {
142 } elsif ($opt_direction eq "out") {
153 if ($where !~ /visible/) {
154 $where .= " AND " if $where;
155 $where .= "visible = 1";
158 $query = "SELECT nr,date,description,price FROM $table";
159 $query .= " WHERE $where" if ($where);
160 $query .= " ORDER by date,nr";
162 $sth = $dbh->prepare($query);
163 if ($sth && (my $rc = $sth->execute) > 0) {
164 print " Nr. Datum Bezeichnung Betrag\n";
165 print "----------------------------------------------------------------------------\n";
166 while (@row = $sth->fetchrow_array) {
167 $descr = substr($row[2],0,50);
168 printf "%4d %8s %-50s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
175 print "---------------------------------------------------------------------------\n"
176 if ($sum_neg > 0 || $sum_pos > 0) ;
177 printf " Zahlungseingänge %9.2f\n", $sum_pos
179 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
181 print "============================================================================\n";
182 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
194 $query = "SELECT DISTINCT category FROM $table ORDER by category";
195 $sth = $dbh->prepare($query);
196 if ($sth && (my $rc = $sth->execute) > 0) {
197 while (@row = $sth->fetchrow_array) {
198 push(@arr, $row[0]) if ($row[0]);
206 my ($text, $line, $start) = @_;
208 return () if $line =~ /\s/;
210 @categories = get_categories unless @categories;
220 @categories = get_categories unless @categories;
222 printf "%s\n", join (", ",@categories);
230 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
232 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
234 my @arr = split(/\./, $ans);
236 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
237 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
238 return sprintf("%d.%d.%d", $arr[0], $arr[1],
239 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
247 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
248 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
251 if ($answers->{einaus} =~ /[ei]/i) {
252 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
253 $ans *= -1 if $ans < 0;
255 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
256 $ans *= -1 if $ans > 0;
262 sub complete_category
264 my ($text, $line, $start) = @_;
266 return () unless exists $answers->{category} && length $answers->{category};
268 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
270 $answers->{category},
272 my $sth = $dbh->prepare($sql);
275 while (my $row = $sth->fetchrow_hashref) {
276 $row->{description} = substr $row->{description}, $start if $start;
277 push @complete, $row->{description};
287 my @arr = split(/\./, $answers->{date});
298 if (exists $info->{default}) {
299 if ($info->{default} eq 'last') {
300 $default = $answers->{$name} if $answers->{$name};
301 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
302 $default = $info->{default}($answers);
303 } elsif ($info->{type} && $info->{type} eq 'boolean') {
304 if ($info->{default}) {
310 $default = $info->{default};
314 if ($info->{complete}) {
315 $term->{completion_function} = $info->{complete};
317 $term->{completion_function} = undef;
321 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
323 $ans = $term->readline ($info->{title} . ": ");
326 exit unless defined $ans;
328 if (!length $ans && defined $default) {
330 } elsif ($ans eq ".") {
334 return read_input($name, $info) unless length $ans || exists $info->{empty};
336 if ($ans eq '?' && exists $info->{lookup}) {
337 $info->{lookup}($name, $answers);
338 return read_input($name, $info);
341 if (exists $info->{type} && $info->{type} eq 'boolean') {
342 if ($ans =~ /[JY1]/i) {
347 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
348 $ans = $info->{validate}($ans);
350 $ans =~ s/ *$// if $ans;
353 if (!exists $info->{type} || $info->{type} ne 'boolean') {
354 $term->addhistory($ans);
360 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
361 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
362 # Nr. anschliessend bereits wieder vergeben ist.
371 $query = "SELECT nr FROM $table ORDER BY nr DESC";
372 $sth = $dbh->prepare($query);
375 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
386 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
392 'validate' => \&validate_date,
393 'default' => 'last'},
400 'default' => \&default_year},
402 'title' => 'Kategorie',
403 'lookup' => \&list_categories,
405 'complete' => \&categories},
407 'title' => 'Beschreibung',
409 'complete' => \&complete_category},
411 'title' => 'Ein/Aus',
415 'title' => 'Steuersatz',
418 'title' => 'Umsatzsteuer',
422 'validate' => \&calculate_price},
424 'title' => 'bezahlt',
433 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
435 @categories = get_categories unless @categories;
437 $term = new Term::ReadLine '' unless $term;
439 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,paid) VALUES (?,?,?,?,?,?,?,?,?,?)");
441 print "Buchungseingabe\n\n";
442 while ($weiter =~ /[JjYy1]/) {
443 foreach my $f (@fields) {
444 $ans = read_input($f, $fields->{$f});
445 $answers->{$f} = $ans;
448 $sth->execute(get_next_nr(),
449 date_to_string($answers->{date}),
452 $answers->{category},
453 $answers->{description},
454 $answers->{tax_percent},
455 $answers->{tax_assigned},
459 $weiter = $answers->{weiter};
460 $answers->{tax_assigned} = 0.0;
469 sales_list("visible = 0");
471 sales_list("visible = 0");
478 sales_list("paid = 0");
484 print "infocon [options] [-h|--help] commands\n";
485 print " --buchung-category|-bc [category]\n";
486 print " --buchung-input|-bi\n";
487 print " --buchung-unpaid\n";
488 print " --buchung-hidden\n";
489 print " --pay <nr> | --unpay <nr>\n";
490 print " --hide <nr> | --unhide <nr>\n";
491 print " --list-categories|-lc\n";
494 print " --verbose|-v\n";
495 print " --year|-y year\n";
496 print " --direction|--dir|-d in|out\n";
509 'buchung-input' => undef,
510 'buchung-unpaid' => undef,
511 'buchung-hidden' => undef,
512 'list-categories' => undef,
515 'buchung-category|bc:s' => \$data{category},
516 'pay=s' => \$data{pay},
517 'unpay=s' => \$data{unpay},
518 'hide=s' => \$data{hide},
519 'unhide=s' => \$data{unhide},
520 'year=i' => \$opt_year,
521 'direction|d=s' => \$opt_direction,
522 'mailto:s' => \$data{mailto},
524 'verbose' => \$opt_verbose,
526 'dm' => sub {$table = "sales_dm"},
527 'buchung-input|bi' => \$data{'buchung-input'},
528 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
529 'buchung-hidden|bh' => \$data{'buchung-hidden'},
530 'list-categories|lc' => \$data{'list-categories'},
533 my $cmdln = 'infocon ' . join (' ', @ARGV);
534 GetOptions(%options);
536 if ($opt_year != 0 && $opt_year < 2002) {
540 if (defined $opt_direction) {
541 usage unless $opt_direction =~ /^(in|out)$/i;
544 if (defined $data{mailto}) {
545 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
546 print "From: Joey Schulze <joey\@infodrom.org>\n";
547 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
548 printf "Subject: %s\n", $cmdln;
549 print "MIME-Version: 1.0\n";
550 print "Content-type: text/plain; charset=iso-8859-1\n";
551 print "Content-Disposition: inline\n";
552 print "Content-Transfer-Encoding: 8bit\n";
557 if (defined $data{category}) {
558 if (length($data{category})) {
559 sales_list("category = '".$data{category}."'");
564 } elsif (defined $data{'buchung-input'}) {
566 } elsif (defined $data{'buchung-unpaid'}) {
568 } elsif (defined $data{'buchung-hidden'}) {
570 } elsif (defined $data{'list-categories'}) {
572 } elsif (defined $data{pay}) {
573 pay_invoice($data{pay}, 1);
574 } elsif (defined $data{unpay}) {
575 pay_invoice($data{unpay}, 0);
576 } elsif (defined $data{hide}) {
577 hide_invoice($data{hide}, 1);
578 } elsif (defined $data{unhide}) {
579 hide_invoice($data{unhide}, 0);