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";
42 my $opt_direction = undef;
46 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
47 return sprintf ("%d.%02d.%02d", $3,$2,$1);
50 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
51 # Datenbank gespeichert werden kann.
57 return "" if (!$_[0]);
59 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
62 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
66 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
67 $day = $date_mday-1 if ($date_mday);
70 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
75 ($day,$mon,$year) = split(/\./, $_[0]);
83 } elsif ($year < 100) {
86 return sprintf("%4d%02d%02d", $year,$mon,$day);
97 $query = "UPDATE sales SET paid=1,billing_date=now() WHERE nr = $nr";
99 $query = "UPDATE sales SET paid=0,billing_date=NULL WHERE nr = $nr";
101 $sth = $dbh->do($query);
108 my $value = $hide==1?0:1;
112 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
113 $sth = $dbh->do($query);
127 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
129 $where .= " AND visible = 1";
131 $where .= "visible = 1";
136 $where .= " AND " if $where;
137 $where .= sprintf("year = %d", $opt_year);
140 if ($opt_direction) {
141 if ($opt_direction eq "in") {
143 } elsif ($opt_direction eq "out") {
154 if ($where !~ /visible/) {
155 $where .= " AND " if $where;
156 $where .= "visible = 1";
159 $query = "SELECT nr,date,description,price FROM $table";
160 $query .= " WHERE $where" if ($where);
161 $query .= " ORDER by date,nr";
163 $sth = $dbh->prepare($query);
164 if ($sth && (my $rc = $sth->execute) > 0) {
165 print " Nr. Datum Bezeichnung Betrag\n";
166 print "----------------------------------------------------------------------------\n";
167 while (@row = $sth->fetchrow_array) {
168 $descr = substr($row[2],0,50);
169 printf "%4d %8s %-50s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
176 print "---------------------------------------------------------------------------\n"
177 if ($sum_neg > 0 || $sum_pos > 0) ;
178 printf " Zahlungseingänge %9.2f\n", $sum_pos
180 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
182 print "============================================================================\n";
183 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
195 $query = "SELECT DISTINCT category FROM $table ORDER by category";
196 $sth = $dbh->prepare($query);
197 if ($sth && (my $rc = $sth->execute) > 0) {
198 while (@row = $sth->fetchrow_array) {
199 push(@arr, $row[0]) if ($row[0]);
210 @categories = get_categories unless @categories;
212 printf "%s\n", join (", ",@categories);
220 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
222 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
224 my @arr = split(/\./, $ans);
226 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
227 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
228 return sprintf("%d.%d.%d", $arr[0], $arr[1],
229 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
237 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
238 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
241 if ($answers->{einaus} =~ /[ei]/i) {
242 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
243 $ans *= -1 if $ans < 0;
245 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
246 $ans *= -1 if $ans > 0;
252 sub complete_category
254 my ($text, $line, $start) = @_;
256 return () unless exists $answers->{category} && length $answers->{category};
258 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
260 $answers->{category},
262 my $sth = $dbh->prepare($sql);
265 while (my $row = $sth->fetchrow_hashref) {
266 $row->{description} = substr $row->{description}, $start if $start;
267 push @complete, $row->{description};
273 sub complete_categoryname
275 my ($text, $line, $start) = @_;
277 return () unless length $line;
279 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
282 my $sth = $dbh->prepare($sql);
285 while (my $row = $sth->fetchrow_hashref) {
286 $row->{category} = substr $row->{category}, $start if $start;
287 push @complete, $row->{category};
297 my @arr = split(/\./, $answers->{date});
308 if (exists $info->{default}) {
309 if ($info->{default} eq 'last') {
310 $default = $answers->{$name} if $answers->{$name};
311 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
312 $default = $info->{default}($answers);
313 } elsif ($info->{type} && $info->{type} eq 'boolean') {
314 if ($info->{default}) {
320 $default = $info->{default};
324 if ($info->{complete}) {
325 $term->{completion_function} = $info->{complete};
327 $term->{completion_function} = undef;
331 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
333 $ans = $term->readline ($info->{title} . ": ");
336 exit unless defined $ans;
338 if (!length $ans && defined $default) {
340 } elsif ($ans eq ".") {
344 return read_input($name, $info) unless length $ans || exists $info->{empty};
346 if ($ans eq '?' && exists $info->{lookup}) {
347 $info->{lookup}($name, $answers);
348 return read_input($name, $info);
351 if (exists $info->{type} && $info->{type} eq 'boolean') {
352 if ($ans =~ /[JY1]/i) {
357 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
358 $ans = $info->{validate}($ans);
360 $ans =~ s/ *$// if $ans;
363 if (!exists $info->{type} || $info->{type} ne 'boolean') {
364 $term->addhistory($ans);
370 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
371 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
372 # Nr. anschliessend bereits wieder vergeben ist.
381 $query = "SELECT nr FROM $table ORDER BY nr DESC";
382 $sth = $dbh->prepare($query);
385 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
396 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
402 'validate' => \&validate_date,
403 'default' => 'last'},
410 'default' => \&default_year},
412 'title' => 'Kategorie',
413 'lookup' => \&list_categories,
415 'complete' => \&complete_categoryname},
417 'title' => 'Beschreibung',
419 'complete' => \&complete_category},
421 'title' => 'Ein/Aus',
425 'title' => 'Steuersatz',
428 'title' => 'Umsatzsteuer',
432 'validate' => \&calculate_price},
434 'title' => 'bezahlt',
443 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
445 @categories = get_categories unless @categories;
447 $term = new Term::ReadLine '' unless $term;
449 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
450 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
452 print "Buchungseingabe\n\n";
453 while ($weiter =~ /[JjYy1]/) {
454 foreach my $f (@fields) {
455 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
458 } elsif ($f eq 'paid') {
459 if ($answers->{paid}) {
460 my @now = localtime(time);
461 $answers->{billing_date} = sprintf('%04d-%02d-%02d', $now[5]+1900, $now[4]+1, $now[3]);
463 $answers->{billing_date} = undef;
466 $ans = read_input($f, $fields->{$f});
467 $answers->{$f} = $ans;
470 $sth->execute(get_next_nr(),
471 date_to_string($answers->{date}),
474 $answers->{category},
475 $answers->{description},
476 $answers->{tax_percent},
477 $answers->{tax_assigned},
479 $answers->{billing_date},
482 $weiter = $answers->{weiter};
483 $answers->{tax_assigned} = 0.0;
492 sales_list("visible = 0");
494 sales_list("visible = 0");
501 sales_list("paid = 0");
507 print "infocon [options] [-h|--help] commands\n";
508 print " --buchung-category|-bc [category]\n";
509 print " --buchung-input|-bi\n";
510 print " --buchung-unpaid\n";
511 print " --buchung-hidden\n";
512 print " --pay <nr> | --unpay <nr>\n";
513 print " --hide <nr> | --unhide <nr>\n";
514 print " --list-categories|-lc\n";
517 print " --verbose|-v\n";
518 print " --year|-y year\n";
519 print " --direction|--dir|-d in|out\n";
532 'buchung-input' => undef,
533 'buchung-unpaid' => undef,
534 'buchung-hidden' => undef,
535 'list-categories' => undef,
538 'buchung-category|bc:s' => \$data{category},
539 'pay=s' => \$data{pay},
540 'unpay=s' => \$data{unpay},
541 'hide=s' => \$data{hide},
542 'unhide=s' => \$data{unhide},
543 'year=i' => \$opt_year,
544 'direction|d=s' => \$opt_direction,
545 'mailto:s' => \$data{mailto},
547 'verbose' => \$opt_verbose,
549 'dm' => sub {$table = "sales_dm"},
550 'buchung-input|bi' => \$data{'buchung-input'},
551 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
552 'buchung-hidden|bh' => \$data{'buchung-hidden'},
553 'list-categories|lc' => \$data{'list-categories'},
556 my $cmdln = 'infocon ' . join (' ', @ARGV);
557 GetOptions(%options);
559 if ($opt_year != 0 && $opt_year < 2002) {
563 if (defined $opt_direction) {
564 usage unless $opt_direction =~ /^(in|out)$/i;
567 if (defined $data{mailto}) {
568 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
569 print "From: Joey Schulze <joey\@infodrom.org>\n";
570 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
571 printf "Subject: %s\n", $cmdln;
572 print "MIME-Version: 1.0\n";
573 print "Content-type: text/plain; charset=iso-8859-1\n";
574 print "Content-Disposition: inline\n";
575 print "Content-Transfer-Encoding: 8bit\n";
580 if (defined $data{category}) {
581 if (length($data{category})) {
582 sales_list("category = '".$data{category}."'");
587 } elsif (defined $data{'buchung-input'}) {
589 } elsif (defined $data{'buchung-unpaid'}) {
591 } elsif (defined $data{'buchung-hidden'}) {
593 } elsif (defined $data{'list-categories'}) {
595 } elsif (defined $data{pay}) {
596 pay_invoice($data{pay}, 1);
597 } elsif (defined $data{unpay}) {
598 pay_invoice($data{unpay}, 0);
599 } elsif (defined $data{hide}) {
600 hide_invoice($data{hide}, 1);
601 } elsif (defined $data{unhide}) {
602 hide_invoice($data{unhide}, 0);