Improve work without category
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index d1c7592..c10413b 100755 (executable)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 
 #  infocon - Administration tool for InfoCon
-#  Copyright (c) 1998-2003,2005-8,10,11  Martin Schulze <joey@infodrom.org>
+#  Copyright (c) 1998-2003,2005-8,10,11,12,14,15,16  Martin Schulze <joey@infodrom.org>
 #
 #  This program is free software; you can redistribute it and/or modify
 #  it under the terms of the GNU General Public License as published by
@@ -17,8 +17,6 @@
 #  along with this program; if not, write to the Free Software
 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
 
-# $Id$
-
 use strict;
 use warnings;
 
@@ -41,6 +39,7 @@ my $term = undef;
 my $opt_all = 0;
 my $opt_verbose = 0;
 my $opt_year = 0;
+my $opt_date = undef;
 my $opt_direction = undef;
 
 sub sdate
@@ -88,15 +87,48 @@ sub date_to_string
     return sprintf("%4d%02d%02d", $year,$mon,$day);
 }
 
+sub valid_isodate
+{
+    my $date = shift;
+    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
+
+    return sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday) unless defined $date;
+
+    if ($date =~ /^(\d+)\.(\d+)\.?$/) {
+       if ($mon == 0 && $2 > 9) {
+           $date = sprintf('%04d-%02d-%02d', $year+1900-1, $2, $1);
+       } else {
+           $date = sprintf('%04d-%02d-%02d', $year+1900, $2, $1);
+       }
+    } elsif ($date =~ /^(\d+)\.(\d+)\.(\d+)?$/) {
+       if (length $3 == 2) {
+           $date = sprintf('20%02d-%02d-%02d', $3, $2, $1);
+       } else {
+           $date = sprintf('%04d-%02d-%02d', $3, $2, $1);
+       }
+    } elsif ($date !~ /^(\d+)-(\d+)-(\d+)$/) {
+       return undef;
+    }
+    return $date;
+}
+
 sub pay_invoice
 {
     my $nr = shift;
     my $pay = shift;
-    my $value = $pay==1?1:0;
     my $query;
     my $sth;
 
-    $query  = "UPDATE sales SET paid=$value WHERE nr = $nr";
+    if ($pay) {
+       my $date = valid_isodate $opt_date;
+
+       die "Invalid date\n" unless defined $date;
+
+       $query  = sprintf("UPDATE sales SET paid=1,billing_date=%s WHERE nr = %d",
+                         defined $date ? $dbh->quote($date) : 'now()', $nr);
+    } else {
+       $query  = sprintf("UPDATE sales SET paid=0,billing_date=NULL WHERE nr = %d", $nr);
+    }
     $sth = $dbh->do($query);
 }
 
@@ -136,6 +168,11 @@ sub sales_list
        $where .= sprintf("year = %d", $opt_year);
     }
 
+    if (defined $data{category} && length $data{category}) {
+       $where .= " AND " if $where;
+       $where .= sprintf("category = %s", $dbh->quote($data{category}));
+    }
+
     if ($opt_direction) {
        if ($opt_direction eq "in") {
            $d = "price >= 0"
@@ -161,25 +198,25 @@ sub sales_list
 
     $sth = $dbh->prepare($query);
     if ($sth && (my $rc = $sth->execute) > 0) {
-       print " Nr.   Datum  Bezeichnung                                 Betrag\n";
-       print "------------------------------------------------------------------\n";
+       print " Nr.   Datum  Bezeichnung                                           Betrag\n";
+       print "----------------------------------------------------------------------------\n";
        while (@row = $sth->fetchrow_array) {
-           $descr = substr($row[2],0,40);
-           printf "%4d %8s %-40s  %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
+           $descr = substr($row[2],0,50);
+           printf "%4d %8s %-50s  %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
            if ($row[3] < 0.0) {
                $sum_neg -= $row[3];
            } else {
                $sum_pos += $row[3];
            }
        }
-       print "-----------------------------------------------------------------\n"
+       print "---------------------------------------------------------------------------\n"
            if ($sum_neg > 0 || $sum_pos > 0) ;
-       printf " Zahlungseingänge                                       %9.2f\n", $sum_pos
+       printf " Zahlungseingänge                                                 %9.2f\n", $sum_pos
            if ($sum_pos > 0);
-       printf " Zahlungsausgänge                                       %9.2f\n", -$sum_neg
+       printf " Zahlungsausgänge                                                 %9.2f\n", -$sum_neg
            if ($sum_neg > 0);
-       print "==================================================================\n";
-       printf " Summe                                                  %9.2f\n\n", $sum_pos - $sum_neg;
+       print "============================================================================\n";
+       printf " Summe                                                            %9.2f\n\n", $sum_pos - $sum_neg;
     }
     $data{'done'} = 1;
 }
@@ -201,17 +238,6 @@ sub get_categories
     return @arr;
 }
 
-sub categories
-{
-    my ($text, $line, $start) = @_;
-
-    return () if $line =~ /\s/;
-
-    @categories = get_categories unless @categories;
-
-    return @categories;
-}
-
 sub list_categories
 {
     my $field = shift;
@@ -248,7 +274,7 @@ sub calculate_price
        $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
     }
 
-    if ($answers->{einaus} =~ /ei/i) {
+    if ($answers->{einaus} =~ /[ei]/i) {
        $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
        $ans *= -1 if $ans < 0;
     } else {
@@ -280,6 +306,26 @@ sub complete_category
     return @complete;
 }
 
+sub complete_categoryname
+{
+    my ($text, $line, $start) = @_;
+
+    return () unless length $line;
+
+    my $sql = sprintf("SELECT DISTINCT category FROM %s WHERE category LIKE '%s%%' ORDER BY category",
+                     $table,
+                     $line);
+    my $sth = $dbh->prepare($sql);
+    $sth->execute;
+    my @complete;
+    while (my $row = $sth->fetchrow_hashref) {
+       $row->{category} = substr $row->{category}, $start if $start;
+       push @complete, $row->{category};
+    }
+
+    return @complete;
+}
+
 sub default_year
 {
     my $answers = shift;
@@ -402,7 +448,7 @@ sub buchung_input
            'title' => 'Kategorie',
            'lookup' => \&list_categories,
            'default' => 'last',
-           'complete' => \&categories},
+           'complete' => \&complete_categoryname},
        'description' => {
            'title' => 'Beschreibung',
            'default' => 'last',
@@ -424,6 +470,10 @@ sub buchung_input
            'title' => 'bezahlt',
            'type' => 'boolean',
            'default' => 0},
+       'billing_date' => {
+           'title' => 'wann',
+           'type' => 'date',
+           'validate' => \&validate_date},
        'weiter' => {
            'title' => 'Weiter',
            'type' => 'boolean',
@@ -436,13 +486,28 @@ sub buchung_input
 
     $term = new Term::ReadLine '' unless $term;
 
-    my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,paid) VALUES (?,?,?,?,?,?,?,?,?,?)");
+    my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,billing_date,paid) " .
+                            "VALUES (?,?,?,?,?,?,?,?,?,?,?)");
 
     print "Buchungseingabe\n\n";
     while ($weiter =~ /[JjYy1]/) {
        foreach my $f (@fields) {
+           if ($f eq 'tax_assigned' && $answers->{'tax_percent'} == 0) {
+               $answers->{$f} = 0;
+               next;
+           }
            $ans = read_input($f, $fields->{$f});
            $answers->{$f} = $ans;
+
+           if ($f eq 'paid') {
+               if ($answers->{paid}) {
+                   $fields->{'billing_date'}{'default'} = $answers->{'date'};
+                   $ans = read_input('billing_date', $fields->{'billing_date'});
+                   $answers->{billing_date} = $ans;
+               } else {
+                   $answers->{billing_date} = undef;
+               }
+           }
        }
 
        $sth->execute(get_next_nr(),
@@ -454,6 +519,7 @@ sub buchung_input
                      $answers->{tax_percent},
                      $answers->{tax_assigned},
                      $answers->{price},
+                     defined $answers->{billing_date} ? date_to_string($answers->{billing_date}) : undef,
                      $answers->{paid});
 
        $weiter = $answers->{weiter};
@@ -474,8 +540,6 @@ sub buchung_hidden
 
 sub buchung_unpaid
 {
-    $table = "sales_dm";
-    sales_list("paid = 0");
     $table = "sales";
     sales_list("paid = 0");
     exit;
@@ -488,6 +552,7 @@ sub usage
     print "  --buchung-input|-bi\n";
     print "  --buchung-unpaid\n";
     print "  --buchung-hidden\n";
+    print "  --date [yyyy-mm-dd|dd.mm.] (for --pay)\n";
     print "  --pay <nr> | --unpay <nr>\n";
     print "  --hide <nr> | --unhide <nr>\n";
     print "  --list-categories|-lc\n";
@@ -520,15 +585,16 @@ my %options = (
     'hide=s' => \$data{hide},
     'unhide=s' => \$data{unhide},
     'year=i' => \$opt_year,
+    'date=s' => \$opt_date,
     'direction|d=s' => \$opt_direction,
     'mailto:s' => \$data{mailto},
     'all' => \$opt_all,
     'verbose' => \$opt_verbose,
     'help' => \&usage,
     'dm' => sub {$table = "sales_dm"},
-    'buchung-input|bi' => \$data{'buchung-input'},
-    'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
-    'buchung-hidden|bh' => \$data{'buchung-hidden'},
+    'buchung-input|bi' => \&buchung_input,
+    'buchung-unpaid|bu' => \&buchung_unpaid,
+    'buchung-hidden|bh' => \&buchung_hidden,
     'list-categories|lc' => \$data{'list-categories'},
     );
 
@@ -557,18 +623,8 @@ if (defined $data{mailto}) {
 }
 
 if (defined $data{category}) {
-    if (length($data{category})) {
-       sales_list("category = '".$data{category}."'");
-    } else {
-       sales_list;
-    }
+    sales_list;
     exit;
-} elsif (defined $data{'buchung-input'}) {
-    buchung_input;
-} elsif (defined $data{'buchung-unpaid'}) {
-    buchung_unpaid;
-} elsif (defined $data{'buchung-hidden'}) {
-    buchung_hidden;
 } elsif (defined $data{'list-categories'}) {
     list_categories;
 } elsif (defined $data{pay}) {