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+)$/) {
117 my $date = valid_isodate $opt_date;
119 die "Invalid date\n" unless defined $date;
121 $query = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
122 defined $date ? $dbh->quote($date) : 'now()', $nr);
124 $query = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
126 $sth = $dbh->do($query);
133 my $value = $hide==1?0:1;
137 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
138 $sth = $dbh->do($query);
152 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
154 $where .= " AND visible = 1";
156 $where .= "visible = 1";
161 $where .= " AND " if $where;
162 $where .= sprintf("year = %d", $opt_year);
165 if ($opt_direction) {
166 if ($opt_direction eq "in") {
168 } elsif ($opt_direction eq "out") {
179 if ($where !~ /visible/) {
180 $where .= " AND " if $where;
181 $where .= "visible = 1";
184 $query = "SELECT nr,date,description,price FROM $table";
185 $query .= " WHERE $where" if ($where);
186 $query .= " ORDER by date,nr";
188 $sth = $dbh->prepare($query);
189 if ($sth && (my $rc = $sth->execute) > 0) {
190 print " Nr. Datum Bezeichnung Betrag\n";
191 print "----------------------------------------------------------------------------\n";
192 while (@row = $sth->fetchrow_array) {
193 $descr = substr($row[2],0,50);
194 printf "%4d %8s %-50s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
201 print "---------------------------------------------------------------------------\n"
202 if ($sum_neg > 0 || $sum_pos > 0) ;
203 printf " Zahlungseingänge %9.2f\n", $sum_pos
205 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
207 print "============================================================================\n";
208 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
220 $query = "SELECT DISTINCT category FROM $table ORDER by category";
221 $sth = $dbh->prepare($query);
222 if ($sth && (my $rc = $sth->execute) > 0) {
223 while (@row = $sth->fetchrow_array) {
224 push(@arr, $row[0]) if ($row[0]);
235 @categories = get_categories unless @categories;
237 printf "%s\n", join (", ",@categories);
245 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
247 return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
249 my @arr = split(/\./, $ans);
251 return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
252 return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
253 return sprintf("%d.%d.%d", $arr[0], $arr[1],
254 length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
262 if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
263 $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
266 if ($answers->{einaus} =~ /[ei]/i) {
267 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
268 $ans *= -1 if $ans < 0;
270 $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
271 $ans *= -1 if $ans > 0;
277 sub complete_category
279 my ($text, $line, $start) = @_;
281 return () unless exists $answers->{category} && length $answers->{category};
283 my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
285 $answers->{category},
287 my $sth = $dbh->prepare($sql);
290 while (my $row = $sth->fetchrow_hashref) {
291 $row->{description} = substr $row->{description}, $start if $start;
292 push @complete, $row->{description};
298 sub complete_categoryname
300 my ($text, $line, $start) = @_;
302 return () unless length $line;
304 my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
307 my $sth = $dbh->prepare($sql);
310 while (my $row = $sth->fetchrow_hashref) {
311 $row->{category} = substr $row->{category}, $start if $start;
312 push @complete, $row->{category};
322 my @arr = split(/\./, $answers->{date});
333 if (exists $info->{default}) {
334 if ($info->{default} eq 'last') {
335 $default = $answers->{$name} if $answers->{$name};
336 } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
337 $default = $info->{default}($answers);
338 } elsif ($info->{type} && $info->{type} eq 'boolean') {
339 if ($info->{default}) {
345 $default = $info->{default};
349 if ($info->{complete}) {
350 $term->{completion_function} = $info->{complete};
352 $term->{completion_function} = undef;
356 $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
358 $ans = $term->readline ($info->{title} . ": ");
361 exit unless defined $ans;
363 if (!length $ans && defined $default) {
365 } elsif ($ans eq ".") {
369 return read_input($name, $info) unless length $ans || exists $info->{empty};
371 if ($ans eq '?' && exists $info->{lookup}) {
372 $info->{lookup}($name, $answers);
373 return read_input($name, $info);
376 if (exists $info->{type} && $info->{type} eq 'boolean') {
377 if ($ans =~ /[JY1]/i) {
382 } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
383 $ans = $info->{validate}($ans);
385 $ans =~ s/ *$// if $ans;
388 if (!exists $info->{type} || $info->{type} ne 'boolean') {
389 $term->addhistory($ans);
395 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
396 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
397 # Nr. anschliessend bereits wieder vergeben ist.
406 $query = "SELECT nr FROM $table ORDER BY nr DESC";
407 $sth = $dbh->prepare($query);
410 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
421 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
427 'validate' => \&validate_date,
428 'default' => 'last'},
435 'default' => \&default_year},
437 'title' => 'Kategorie',
438 'lookup' => \&list_categories,
440 'complete' => \&complete_categoryname},
442 'title' => 'Beschreibung',
444 'complete' => \&complete_category},
446 'title' => 'Ein/Aus',
450 'title' => 'Steuersatz',
453 'title' => 'Umsatzsteuer',
457 'validate' => \&calculate_price},
459 'title' => 'bezahlt',
468 my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
470 @categories = get_categories unless @categories;
472 $term = new Term::ReadLine '' unless $term;
474 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
475 "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
477 print "Buchungseingabe\n\n";
478 while ($weiter =~ /[JjYy1]/) {
479 foreach my $f (@fields) {
480 if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
483 } elsif ($f eq 'paid') {
484 if ($answers->{paid}) {
485 my @now = localtime(time);
486 $answers->{billing_date} = sprintf('%04d-%02d-%02d', $now[5]+1900, $now[4]+1, $now[3]);
488 $answers->{billing_date} = undef;
491 $ans = read_input($f, $fields->{$f});
492 $answers->{$f} = $ans;
495 $sth->execute(get_next_nr(),
496 date_to_string($answers->{date}),
499 $answers->{category},
500 $answers->{description},
501 $answers->{tax_percent},
502 $answers->{tax_assigned},
504 $answers->{billing_date},
507 $weiter = $answers->{weiter};
508 $answers->{tax_assigned} = 0.0;
517 sales_list("visible = 0");
519 sales_list("visible = 0");
526 sales_list("paid = 0");
532 print "infocon [options] [-h|--help] commands\n";
533 print " --buchung-category|-bc [category]\n";
534 print " --buchung-input|-bi\n";
535 print " --buchung-unpaid\n";
536 print " --buchung-hidden\n";
537 print " --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
538 print " --pay <nr> | --unpay <nr>\n";
539 print " --hide <nr> | --unhide <nr>\n";
540 print " --list-categories|-lc\n";
543 print " --verbose|-v\n";
544 print " --year|-y year\n";
545 print " --direction|--dir|-d in|out\n";
558 'buchung-input' => undef,
559 'buchung-unpaid' => undef,
560 'buchung-hidden' => undef,
561 'list-categories' => undef,
564 'buchung-category|bc:s' => \$data{category},
565 'pay=s' => \$data{pay},
566 'unpay=s' => \$data{unpay},
567 'hide=s' => \$data{hide},
568 'unhide=s' => \$data{unhide},
569 'year=i' => \$opt_year,
570 'date=s' => \$opt_date,
571 'direction|d=s' => \$opt_direction,
572 'mailto:s' => \$data{mailto},
574 'verbose' => \$opt_verbose,
576 'dm' => sub {$table = "sales_dm"},
577 'buchung-input|bi' => \$data{'buchung-input'},
578 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
579 'buchung-hidden|bh' => \$data{'buchung-hidden'},
580 'list-categories|lc' => \$data{'list-categories'},
583 my $cmdln = 'infocon ' . join (' ', @ARGV);
584 GetOptions(%options);
586 if ($opt_year != 0 && $opt_year < 2002) {
590 if (defined $opt_direction) {
591 usage unless $opt_direction =~ /^(in|out)$/i;
594 if (defined $data{mailto}) {
595 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
596 print "From: Joey Schulze <joey\@infodrom.org>\n";
597 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
598 printf "Subject: %s\n", $cmdln;
599 print "MIME-Version: 1.0\n";
600 print "Content-type: text/plain; charset=iso-8859-1\n";
601 print "Content-Disposition: inline\n";
602 print "Content-Transfer-Encoding: 8bit\n";
607 if (defined $data{category}) {
608 if (length($data{category})) {
609 sales_list("category = '".$data{category}."'");
614 } elsif (defined $data{'buchung-input'}) {
616 } elsif (defined $data{'buchung-unpaid'}) {
618 } elsif (defined $data{'buchung-hidden'}) {
620 } elsif (defined $data{'list-categories'}) {
622 } elsif (defined $data{pay}) {
623 pay_invoice($data{pay}, 1);
624 } elsif (defined $data{unpay}) {
625 pay_invoice($data{unpay}, 0);
626 } elsif (defined $data{hide}) {
627 hide_invoice($data{hide}, 1);
628 } elsif (defined $data{unhide}) {
629 hide_invoice($data{unhide}, 0);