3 # infocon - Administration tool for InfoCon
4 # Copyright (c) 1998-2003,2005-8,10,11,12,14,15 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);
99 $query = "UPDATE sales SET paid=1,billing_date=now() WHERE nr = $nr";
101 $query = "UPDATE sales SET paid=0,billing_date=NULL WHERE nr = $nr";
103 $sth = $dbh->do($query);
110 my $value = $hide==1?0:1;
114 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
115 $sth = $dbh->do($query);
129 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
131 $where .= " AND visible = 1";
133 $where .= "visible = 1";
138 $where .= " AND " if $where;
139 $where .= sprintf("year = %d", $opt_year);
142 if ($opt_direction) {
143 if ($opt_direction eq "in") {
145 } elsif ($opt_direction eq "out") {
156 if ($where !~ /visible/) {
157 $where .= " AND " if $where;
158 $where .= "visible = 1";
161 $query = "SELECT nr,date,description,price FROM $table";
162 $query .= " WHERE $where" if ($where);
163 $query .= " ORDER by date,nr";
165 $sth = $dbh->prepare($query);
166 if ($sth && (my $rc = $sth->execute) > 0) {
167 print " Nr. Datum Bezeichnung Betrag\n";
168 print "----------------------------------------------------------------------------\n";
169 while (@row = $sth->fetchrow_array) {
170 $descr = substr($row[2],0,50);
171 printf "%4d %8s %-50s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
178 print "---------------------------------------------------------------------------\n"
179 if ($sum_neg > 0 || $sum_pos > 0) ;
180 printf " Zahlungseingänge %9.2f\n", $sum_pos
182 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
184 print "============================================================================\n";
185 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
197 $query = "SELECT DISTINCT category FROM $table ORDER by category";
198 $sth = $dbh->prepare($query);
199 if ($sth && (my $rc = $sth->execute) > 0) {
200 while (@row = $sth->fetchrow_array) {
201 push(@arr, $row[0]) if ($row[0]);
212 @categories = get_categories unless @categories;
214 printf "%s\n", join (", ",@categories);
222 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
224 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
226 my @arr = split(/\./, $ans);
228 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
229 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
230 return sprintf("%d.%d.%d", $arr[0], $arr[1],
231 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
239 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
240 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
243 if ($answers->{einaus} =~ /[ei]/i) {
244 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
245 $ans *= -1 if $ans < 0;
247 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
248 $ans *= -1 if $ans > 0;
254 sub complete_category
256 my ($text, $line, $start) = @_;
258 return () unless exists $answers->{category} && length $answers->{category};
260 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
262 $answers->{category},
264 my $sth = $dbh->prepare($sql);
267 while (my $row = $sth->fetchrow_hashref) {
268 $row->{description} = substr $row->{description}, $start if $start;
269 push @complete, $row->{description};
275 sub complete_categoryname
277 my ($text, $line, $start) = @_;
279 return () unless length $line;
281 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
284 my $sth = $dbh->prepare($sql);
287 while (my $row = $sth->fetchrow_hashref) {
288 $row->{category} = substr $row->{category}, $start if $start;
289 push @complete, $row->{category};
299 my @arr = split(/\./, $answers->{date});
310 if (exists $info->{default}) {
311 if ($info->{default} eq 'last') {
312 $default = $answers->{$name} if $answers->{$name};
313 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
314 $default = $info->{default}($answers);
315 } elsif ($info->{type} && $info->{type} eq 'boolean') {
316 if ($info->{default}) {
322 $default = $info->{default};
326 if ($info->{complete}) {
327 $term->{completion_function} = $info->{complete};
329 $term->{completion_function} = undef;
333 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
335 $ans = $term->readline ($info->{title} . ": ");
338 exit unless defined $ans;
340 if (!length $ans && defined $default) {
342 } elsif ($ans eq ".") {
346 return read_input($name, $info) unless length $ans || exists $info->{empty};
348 if ($ans eq '?' && exists $info->{lookup}) {
349 $info->{lookup}($name, $answers);
350 return read_input($name, $info);
353 if (exists $info->{type} && $info->{type} eq 'boolean') {
354 if ($ans =~ /[JY1]/i) {
359 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
360 $ans = $info->{validate}($ans);
362 $ans =~ s/ *$// if $ans;
365 if (!exists $info->{type} || $info->{type} ne 'boolean') {
366 $term->addhistory($ans);
372 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
373 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
374 # Nr. anschliessend bereits wieder vergeben ist.
383 $query = "SELECT nr FROM $table ORDER BY nr DESC";
384 $sth = $dbh->prepare($query);
387 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
398 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
404 'validate' => \&validate_date,
405 'default' => 'last'},
412 'default' => \&default_year},
414 'title' => 'Kategorie',
415 'lookup' => \&list_categories,
417 'complete' => \&complete_categoryname},
419 'title' => 'Beschreibung',
421 'complete' => \&complete_category},
423 'title' => 'Ein/Aus',
427 'title' => 'Steuersatz',
430 'title' => 'Umsatzsteuer',
434 'validate' => \&calculate_price},
436 'title' => 'bezahlt',
445 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
447 @categories = get_categories unless @categories;
449 $term = new Term::ReadLine '' unless $term;
451 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,paid) VALUES (?,?,?,?,?,?,?,?,?,?)");
453 print "Buchungseingabe\n\n";
454 while ($weiter =~ /[JjYy1]/) {
455 foreach my $f (@fields) {
456 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
460 $ans = read_input($f, $fields->{$f});
461 $answers->{$f} = $ans;
464 $sth->execute(get_next_nr(),
465 date_to_string($answers->{date}),
468 $answers->{category},
469 $answers->{description},
470 $answers->{tax_percent},
471 $answers->{tax_assigned},
475 $weiter = $answers->{weiter};
476 $answers->{tax_assigned} = 0.0;
485 sales_list("visible = 0");
487 sales_list("visible = 0");
494 sales_list("paid = 0");
500 print "infocon [options] [-h|--help] commands\n";
501 print " --buchung-category|-bc [category]\n";
502 print " --buchung-input|-bi\n";
503 print " --buchung-unpaid\n";
504 print " --buchung-hidden\n";
505 print " --pay <nr> | --unpay <nr>\n";
506 print " --hide <nr> | --unhide <nr>\n";
507 print " --list-categories|-lc\n";
510 print " --verbose|-v\n";
511 print " --year|-y year\n";
512 print " --direction|--dir|-d in|out\n";
525 'buchung-input' => undef,
526 'buchung-unpaid' => undef,
527 'buchung-hidden' => undef,
528 'list-categories' => undef,
531 'buchung-category|bc:s' => \$data{category},
532 'pay=s' => \$data{pay},
533 'unpay=s' => \$data{unpay},
534 'hide=s' => \$data{hide},
535 'unhide=s' => \$data{unhide},
536 'year=i' => \$opt_year,
537 'direction|d=s' => \$opt_direction,
538 'mailto:s' => \$data{mailto},
540 'verbose' => \$opt_verbose,
542 'dm' => sub {$table = "sales_dm"},
543 'buchung-input|bi' => \$data{'buchung-input'},
544 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
545 'buchung-hidden|bh' => \$data{'buchung-hidden'},
546 'list-categories|lc' => \$data{'list-categories'},
549 my $cmdln = 'infocon ' . join (' ', @ARGV);
550 GetOptions(%options);
552 if ($opt_year != 0 && $opt_year < 2002) {
556 if (defined $opt_direction) {
557 usage unless $opt_direction =~ /^(in|out)$/i;
560 if (defined $data{mailto}) {
561 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
562 print "From: Joey Schulze <joey\@infodrom.org>\n";
563 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
564 printf "Subject: %s\n", $cmdln;
565 print "MIME-Version: 1.0\n";
566 print "Content-type: text/plain; charset=iso-8859-1\n";
567 print "Content-Disposition: inline\n";
568 print "Content-Transfer-Encoding: 8bit\n";
573 if (defined $data{category}) {
574 if (length($data{category})) {
575 sales_list("category = '".$data{category}."'");
580 } elsif (defined $data{'buchung-input'}) {
582 } elsif (defined $data{'buchung-unpaid'}) {
584 } elsif (defined $data{'buchung-hidden'}) {
586 } elsif (defined $data{'list-categories'}) {
588 } elsif (defined $data{pay}) {
589 pay_invoice($data{pay}, 1);
590 } elsif (defined $data{unpay}) {
591 pay_invoice($data{unpay}, 0);
592 } elsif (defined $data{hide}) {
593 hide_invoice($data{hide}, 1);
594 } elsif (defined $data{unhide}) {
595 hide_invoice($data{unhide}, 0);