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 ($opt_direction) {
172 if ($opt_direction eq "in") {
174 } elsif ($opt_direction eq "out") {
185 if ($where !~ /visible/) {
186 $where .= " AND " if $where;
187 $where .= "visible = 1";
190 $query = "SELECT nr,date,description,price FROM $table";
191 $query .= " WHERE $where" if ($where);
192 $query .= " ORDER by date,nr";
194 $sth = $dbh->prepare($query);
195 if ($sth && (my $rc = $sth->execute) > 0) {
196 print " Nr. Datum Bezeichnung Betrag\n";
197 print "----------------------------------------------------------------------------\n";
198 while (@row = $sth->fetchrow_array) {
199 $descr = substr($row[2],0,50);
200 printf "%4d %8s %-50s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
207 print "---------------------------------------------------------------------------\n"
208 if ($sum_neg > 0 || $sum_pos > 0) ;
209 printf " Zahlungseingänge %9.2f\n", $sum_pos
211 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
213 print "============================================================================\n";
214 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
226 $query = "SELECT DISTINCT category FROM $table ORDER by category";
227 $sth = $dbh->prepare($query);
228 if ($sth && (my $rc = $sth->execute) > 0) {
229 while (@row = $sth->fetchrow_array) {
230 push(@arr, $row[0]) if ($row[0]);
241 @categories = get_categories unless @categories;
243 printf "%s\n", join (", ",@categories);
251 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
253 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
255 my @arr = split(/\./, $ans);
257 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
258 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
259 return sprintf("%d.%d.%d", $arr[0], $arr[1],
260 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
268 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
269 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
272 if ($answers->{einaus} =~ /[ei]/i) {
273 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
274 $ans *= -1 if $ans < 0;
276 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
277 $ans *= -1 if $ans > 0;
283 sub complete_category
285 my ($text, $line, $start) = @_;
287 return () unless exists $answers->{category} && length $answers->{category};
289 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
291 $answers->{category},
293 my $sth = $dbh->prepare($sql);
296 while (my $row = $sth->fetchrow_hashref) {
297 $row->{description} = substr $row->{description}, $start if $start;
298 push @complete, $row->{description};
304 sub complete_categoryname
306 my ($text, $line, $start) = @_;
308 return () unless length $line;
310 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
313 my $sth = $dbh->prepare($sql);
316 while (my $row = $sth->fetchrow_hashref) {
317 $row->{category} = substr $row->{category}, $start if $start;
318 push @complete, $row->{category};
328 my @arr = split(/\./, $answers->{date});
339 if (exists $info->{default}) {
340 if ($info->{default} eq 'last') {
341 $default = $answers->{$name} if $answers->{$name};
342 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
343 $default = $info->{default}($answers);
344 } elsif ($info->{type} && $info->{type} eq 'boolean') {
345 if ($info->{default}) {
351 $default = $info->{default};
355 if ($info->{complete}) {
356 $term->{completion_function} = $info->{complete};
358 $term->{completion_function} = undef;
362 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
364 $ans = $term->readline ($info->{title} . ": ");
367 exit unless defined $ans;
369 if (!length $ans && defined $default) {
371 } elsif ($ans eq ".") {
375 return read_input($name, $info) unless length $ans || exists $info->{empty};
377 if ($ans eq '?' && exists $info->{lookup}) {
378 $info->{lookup}($name, $answers);
379 return read_input($name, $info);
382 if (exists $info->{type} && $info->{type} eq 'boolean') {
383 if ($ans =~ /[JY1]/i) {
388 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
389 $ans = $info->{validate}($ans);
391 $ans =~ s/ *$// if $ans;
394 if (!exists $info->{type} || $info->{type} ne 'boolean') {
395 $term->addhistory($ans);
401 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
402 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
403 # Nr. anschliessend bereits wieder vergeben ist.
412 $query = "SELECT nr FROM $table ORDER BY nr DESC";
413 $sth = $dbh->prepare($query);
416 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
427 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
433 'validate' => \&validate_date,
434 'default' => 'last'},
441 'default' => \&default_year},
443 'title' => 'Kategorie',
444 'lookup' => \&list_categories,
446 'complete' => \&complete_categoryname},
448 'title' => 'Beschreibung',
450 'complete' => \&complete_category},
452 'title' => 'Ein/Aus',
456 'title' => 'Steuersatz',
459 'title' => 'Umsatzsteuer',
463 'validate' => \&calculate_price},
465 'title' => 'bezahlt',
474 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
476 @categories = get_categories unless @categories;
478 $term = new Term::ReadLine '' unless $term;
480 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
481 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
483 print "Buchungseingabe\n\n";
484 while ($weiter =~ /[JjYy1]/) {
485 foreach my $f (@fields) {
486 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
489 } elsif ($f eq 'paid') {
490 if ($answers->{paid}) {
491 my @now = localtime(time);
492 $answers->{billing_date} = sprintf('%04d-%02d-%02d', $now[5]+1900, $now[4]+1, $now[3]);
494 $answers->{billing_date} = undef;
497 $ans = read_input($f, $fields->{$f});
498 $answers->{$f} = $ans;
501 $sth->execute(get_next_nr(),
502 date_to_string($answers->{date}),
505 $answers->{category},
506 $answers->{description},
507 $answers->{tax_percent},
508 $answers->{tax_assigned},
510 $answers->{billing_date},
513 $weiter = $answers->{weiter};
514 $answers->{tax_assigned} = 0.0;
523 sales_list("visible = 0");
525 sales_list("visible = 0");
532 sales_list("paid = 0");
538 print "infocon [options] [-h|--help] commands\n";
539 print " --buchung-category|-bc [category]\n";
540 print " --buchung-input|-bi\n";
541 print " --buchung-unpaid\n";
542 print " --buchung-hidden\n";
543 print " --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
544 print " --pay <nr> | --unpay <nr>\n";
545 print " --hide <nr> | --unhide <nr>\n";
546 print " --list-categories|-lc\n";
549 print " --verbose|-v\n";
550 print " --year|-y year\n";
551 print " --direction|--dir|-d in|out\n";
564 'buchung-input' => undef,
565 'buchung-unpaid' => undef,
566 'buchung-hidden' => undef,
567 'list-categories' => undef,
570 'buchung-category|bc:s' => \$data{category},
571 'pay=s' => \$data{pay},
572 'unpay=s' => \$data{unpay},
573 'hide=s' => \$data{hide},
574 'unhide=s' => \$data{unhide},
575 'year=i' => \$opt_year,
576 'date=s' => \$opt_date,
577 'direction|d=s' => \$opt_direction,
578 'mailto:s' => \$data{mailto},
580 'verbose' => \$opt_verbose,
582 'dm' => sub {$table = "sales_dm"},
583 'buchung-input|bi' => \$data{'buchung-input'},
584 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
585 'buchung-hidden|bh' => \$data{'buchung-hidden'},
586 'list-categories|lc' => \$data{'list-categories'},
589 my $cmdln = 'infocon ' . join (' ', @ARGV);
590 GetOptions(%options);
592 if ($opt_year != 0 && $opt_year < 2002) {
596 if (defined $opt_direction) {
597 usage unless $opt_direction =~ /^(in|out)$/i;
600 if (defined $data{mailto}) {
601 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
602 print "From: Joey Schulze <joey\@infodrom.org>\n";
603 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
604 printf "Subject: %s\n", $cmdln;
605 print "MIME-Version: 1.0\n";
606 print "Content-type: text/plain; charset=iso-8859-1\n";
607 print "Content-Disposition: inline\n";
608 print "Content-Transfer-Encoding: 8bit\n";
613 if (defined $data{category}) {
614 if (length($data{category})) {
615 sales_list("category = '".$data{category}."'");
620 } elsif (defined $data{'buchung-input'}) {
622 } elsif (defined $data{'buchung-unpaid'}) {
624 } elsif (defined $data{'buchung-hidden'}) {
626 } elsif (defined $data{'list-categories'}) {
628 } elsif (defined $data{pay}) {
629 pay_invoice($data{pay}, 1);
630 } elsif (defined $data{unpay}) {
631 pay_invoice($data{unpay}, 0);
632 } elsif (defined $data{hide}) {
633 hide_invoice($data{hide}, 1);
634 } elsif (defined $data{unhide}) {
635 hide_invoice($data{unhide}, 0);