Ignore old sales_dm table for unpaid receipts
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index a1c6819..ff346ba 100755 (executable)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 
 #  infocon - Administration tool for InfoCon
-#  Copyright (c) 1998-2003,2005-8  Martin Schulze <joey@infodrom.org>
+#  Copyright (c) 1998-2003,2005-8,10,11  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
@@ -23,6 +23,7 @@ use strict;
 use warnings;
 
 use DBI;
+use Scalar::Util qw/reftype/;
 use Term::ReadLine;
 use Getopt::Long;
 
@@ -131,11 +132,8 @@ sub sales_list
     }
 
     if ($opt_year) {
-       if ($where) {
-           $where .= " AND date ~* '$opt_year'";
-       } else {
-           $where .= "date ~* '$opt_year'";
-       }
+       $where .= " AND " if $where;
+       $where .= sprintf("year = %d", $opt_year);
     }
 
     if ($opt_direction) {
@@ -152,9 +150,15 @@ sub sales_list
        }
     }
 
+    if ($where !~ /visible/) {
+       $where .= " AND " if $where;
+       $where .= "visible = 1";
+    }
+
     $query  = "SELECT nr,date,description,price FROM $table";
     $query .= " WHERE $where" if ($where);
     $query .= " ORDER by date,nr";
+
     $sth = $dbh->prepare($query);
     if ($sth && (my $rc = $sth->execute) > 0) {
        print " Nr.   Datum  Bezeichnung                                 Betrag\n";
@@ -180,17 +184,14 @@ sub sales_list
     $data{'done'} = 1;
 }
 
-sub get_descriptions
+sub get_categories
 {
     my $query;
     my @row;
     my $sth;
     my @arr = ();
-    my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
-       = localtime;
 
-    $query  = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ORDER by description",
-                     $table, $date_year+1900);
+    $query  = "SELECT DISTINCT category FROM $table ORDER by category";
     $sth = $dbh->prepare($query);
     if ($sth && (my $rc = $sth->execute) > 0) {
        while (@row = $sth->fetchrow_array) {
@@ -200,49 +201,159 @@ sub get_descriptions
     return @arr;
 }
 
-sub get_categories
+sub categories
 {
-    my $query;
-    my @row;
-    my $sth;
-    my @arr = ();
+    my ($text, $line, $start) = @_;
 
-    $query  = "SELECT DISTINCT category FROM $table ORDER by category";
-    $sth = $dbh->prepare($query);
-    if ($sth && (my $rc = $sth->execute) > 0) {
-       while (@row = $sth->fetchrow_array) {
-           push(@arr, $row[0]) if ($row[0]);
-       }
-    }
-    return @arr;
+    return () if $line =~ /\s/;
+
+    @categories = get_categories unless @categories;
+
+    return @categories;
 }
 
 sub list_categories
 {
+    my $field = shift;
+    my $answers = shift;
+
     @categories = get_categories unless @categories;
 
     printf "%s\n", join (", ",@categories);
 
-    exit;
+    exit unless $field;
+}
+
+sub validate_date
+{
+    my $ans = shift;
+    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
+
+    return sprintf("%d.%d.%d", $mday, $mon+1, $year+1900) unless length $ans;
+
+    my @arr = split(/\./, $ans);
+
+    return sprintf("%d.%d.%d", $ans, $mon+1, $year+1900) if scalar @arr == 1;
+    return sprintf("%d.%d.%d", $arr[0], $arr[1], $year+1900) if scalar @arr == 2;
+    return sprintf("%d.%d.%d", $arr[0], $arr[1],
+                  length $arr[2] > 2 ? $arr[2] : $arr[2] + 2000);
+}
+
+my $answers;
+sub calculate_price
+{
+    my $ans = shift;
+
+    if (!defined $answers->{tax_assigned} || !length $answers->{tax_assigned}) {
+       $answers->{tax_assigned} = $ans - ($ans / ((100+$answers->{tax_percent})/100));
+    }
+
+    if ($answers->{einaus} =~ /ei/i) {
+       $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} < 0;
+       $ans *= -1 if $ans < 0;
+    } else {
+       $answers->{tax_assigned} *= -1 if $answers->{tax_assigned} > 0;
+       $ans *= -1 if $ans > 0;
+    }
+
+    return $ans;
+}
+
+sub complete_category
+{
+    my ($text, $line, $start) = @_;
+
+    return () unless exists $answers->{category} && length $answers->{category};
+
+    my $sql = sprintf("SELECT DISTINCT description FROM %s WHERE category = '%s' AND description LIKE '%s%%' ORDER BY description",
+                     $table,
+                     $answers->{category},
+                     $line);
+    my $sth = $dbh->prepare($sql);
+    $sth->execute;
+    my @complete;
+    while (my $row = $sth->fetchrow_hashref) {
+       $row->{description} = substr $row->{description}, $start if $start;
+       push @complete, $row->{description};
+    }
+
+    return @complete;
+}
+
+sub default_year
+{
+    my $answers = shift;
+
+    my @arr = split(/\./, $answers->{date});
+    return $arr[2];
 }
 
 sub read_input
 {
-    my $prompt = shift;
-    my $default = shift;
+    my $name = shift;
+    my $info = shift;
+    my $default;
     my $ans;
 
+    if (exists $info->{default}) {
+       if ($info->{default} eq 'last') {
+           $default = $answers->{$name} if $answers->{$name};
+       } elsif (reftype $info->{default} && reftype $info->{default} eq 'CODE') {
+           $default = $info->{default}($answers);
+       } elsif ($info->{type} && $info->{type} eq 'boolean') {
+           if ($info->{default}) {
+               $default = 'J';
+           } else {
+               $default = 'N';
+           }
+       } else {
+           $default = $info->{default};
+       }
+    }
+
+    if ($info->{complete}) {
+       $term->{completion_function} = $info->{complete};
+    } else {
+       $term->{completion_function} = undef;
+    }
+
     if ($default) {
-       $ans = $term->readline ($prompt . " [" . $default . "]: ");
+       $ans = $term->readline ($info->{title} . " [" . $default . "]: ");
     } else {
-       $ans = $term->readline ($prompt . ": ");
+       $ans = $term->readline ($info->{title} . ": ");
     }
-    if (length ($ans) == 0) {
+
+    exit unless defined $ans;
+
+    if (!length $ans && defined $default) {
        $ans = $default;
     } elsif ($ans eq ".") {
        $ans = '';
     }
-    $ans =~ s/ *$// if $ans;
+
+    return read_input($name, $info) unless length $ans || exists $info->{empty};
+
+    if ($ans eq '?' && exists $info->{lookup}) {
+       $info->{lookup}($name, $answers);
+       return read_input($name, $info);
+    }
+
+    if (exists $info->{type} && $info->{type} eq 'boolean') {
+       if ($ans =~ /[JY1]/i) {
+           $ans = 1;
+       } else {
+           $ans = 0;
+       }
+    } elsif (exists $info->{validate} && reftype $info->{validate} eq 'CODE') {
+       $ans = $info->{validate}($ans);
+    } else {
+       $ans =~ s/ *$// if $ans;
+    }
+
+    if (!exists $info->{type} || $info->{type} ne 'boolean') {
+       $term->addhistory($ans);
+    }
+
     return $ans;
 }
 
@@ -270,90 +381,83 @@ sub get_next_nr
 
 sub buchung_input
 {
-    my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
-    my @input = ();
     my $weiter = 'y';
-    my $i;
     my $ans;
-    my $query;
     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
        = localtime;
 
+    my $fields = {
+       'date' => {
+           'title' => 'Datum',
+           'validate' => \&validate_date,
+           'default' => 'last'},
+       'pdf' => {
+           'title' => 'PDF',
+           'type' => 'boolean',
+           'default' => 0},
+       'year' => {
+           'title' => 'Jahr',
+           'default' => \&default_year},
+       'category' => {
+           'title' => 'Kategorie',
+           'lookup' => \&list_categories,
+           'default' => 'last',
+           'complete' => \&categories},
+       'description' => {
+           'title' => 'Beschreibung',
+           'default' => 'last',
+           'complete' => \&complete_category},
+       'einaus' => {
+           'title' => 'Ein/Aus',
+           'default' => 'a',
+           'save' => 0},
+       'tax_percent' => {
+           'title' => 'Steuersatz',
+           'default' => '19'},
+       'tax_assigned' => {
+           'title' => 'Umsatzsteuer',
+           'empty' => 1},
+       'price' => {
+           'title' => 'Betrag',
+           'validate' => \&calculate_price},
+       'paid' => {
+           'title' => 'bezahlt',
+           'type' => 'boolean',
+           'default' => 0},
+       'weiter' => {
+           'title' => 'Weiter',
+           'type' => 'boolean',
+           'default' => 1,
+           'save' => 0},
+    };
+    my @fields = ('date','year','pdf','category','description','einaus','tax_percent','tax_assigned','price','paid','weiter');
+
     @categories = get_categories unless @categories;
-    my @descriptions = get_descriptions;
 
     $term = new Term::ReadLine '' unless $term;
 
-    $term->addhistory($_) foreach (@categories);
-    $term->addhistory($_) foreach (@descriptions);
-
-    my $attribs = $term->Attribs;
-
-    my $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
+    my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,paid) VALUES (?,?,?,?,?,?,?,?,?,?)");
 
     print "Buchungseingabe\n\n";
     while ($weiter =~ /[JjYy1]/) {
-       $i=0;while ($i <= $#fieldname) {
-           if ($fieldname[$i] eq "Category") {
-               $attribs->{completion_entry_function} = $attribs->{list_completion_function};
-               $attribs->{completion_word} = \@categories;
-           } elsif ($fieldname[$i] eq "Description") {
-               $attribs->{completion_entry_function} = $attribs->{list_completion_function};
-               $attribs->{completion_word} = \@descriptions;
-           } elsif ($fieldname[$i] eq "Tax assigned") {
-               if ($input[$i-1] == 0) {
-                   $input[$i++] = 0;
-                   next;
-               }
-           } else {
-               $attribs->{completion_word} = undef;
-           }
-           $ans = read_input($fieldname[$i],$input[$i]);
-           if ($fieldname[$i] eq "Category" && $ans eq "?") {
-               printf "  %s\n", join (", ",@categories);
-           } elsif ($fieldname[$i] eq "Datum") {
-               if ($ans =~ /^\d+\.\d+.\d+$/) {
-                   $input[$i] = $ans;
-                   $i++;
-               } elsif ($ans =~ /^\d+\.\d+.$/) {
-                   $ans .= $date_year + 1900;
-                   $input[$i] = $ans;
-                   $i++;
-               } elsif ($ans =~ /^\d+\.$/) {
-                   $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
-                   $input[$i] = $ans;
-                   $i++;
-               }
-           } elsif ($fieldname[$i] eq "Paid") {
-               if ($ans =~ /[1jJyY]/) {
-                   $input[$i] = 1;
-               } else {
-                   $input[$i] = 0;
-               }
-               $i++;
-           } else {
-               $input[$i] = $ans;
-               $i++;
-           }
-       }
-       if (!$input[5]) {       # USt selbst berechnen
-           if ($input[4] != 0) {
-               $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
-           }
-       }
-
-       if ($input[3] =~ /[EeIi\+]/) {
-           $input[5] *= -1 if ($input[5] < 0);
-           $input[6] *= -1 if ($input[6] < 0);
-       } else {
-           $input[5] *= -1 if ($input[5] > 0);
-           $input[6] *= -1 if ($input[6] > 0);
+       foreach my $f (@fields) {
+           $ans = read_input($f, $fields->{$f});
+           $answers->{$f} = $ans;
        }
 
-       $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
-                         $input[5], $input[6], 1, $input[7]);
-       $weiter = read_input("Weiter",'j');
-       $input[5] = 0.0;
+       $sth->execute(get_next_nr(),
+                     date_to_string($answers->{date}),
+                     $answers->{pdf},
+                     $answers->{year},
+                     $answers->{category},
+                     $answers->{description},
+                     $answers->{tax_percent},
+                     $answers->{tax_assigned},
+                     $answers->{price},
+                     $answers->{paid});
+
+       $weiter = $answers->{weiter};
+       $answers->{tax_assigned} = 0.0;
     }
 
     exit;
@@ -370,8 +474,6 @@ sub buchung_hidden
 
 sub buchung_unpaid
 {
-    $table = "sales_dm";
-    sales_list("paid = 0");
     $table = "sales";
     sales_list("paid = 0");
     exit;
@@ -403,6 +505,11 @@ sub usage
     'unpay' => undef,
     'hide' => undef,
     'unhide' => undef,
+    'mailto' => undef,
+    'buchung-input' => undef,
+    'buchung-unpaid' => undef,
+    'buchung-hidden' => undef,
+    'list-categories' => undef,
     );
 my %options = (
     'buchung-category|bc:s' => \$data{category},
@@ -411,17 +518,19 @@ my %options = (
     'hide=s' => \$data{hide},
     'unhide=s' => \$data{unhide},
     'year=i' => \$opt_year,
-    'direction=s' => \$opt_direction,
+    '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' => \&buchung_input,
-    'buchung-unpaid|bu' => \&buchung_unpaid,
-    'buchung-hidden|bh' => \&buchung_hidden,
-    'list-categories|lc' => \&list_categories,
+    'buchung-input|bi' => \$data{'buchung-input'},
+    'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
+    'buchung-hidden|bh' => \$data{'buchung-hidden'},
+    'list-categories|lc' => \$data{'list-categories'},
     );
 
+my $cmdln = 'infocon ' . join (' ', @ARGV);
 GetOptions(%options);
 
 if ($opt_year != 0 && $opt_year < 2002) {
@@ -432,6 +541,19 @@ if (defined $opt_direction) {
     usage unless $opt_direction =~ /^(in|out)$/i;
 }
 
+if (defined $data{mailto}) {
+    if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
+       print  "From: Joey Schulze <joey\@infodrom.org>\n";
+       printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
+       printf "Subject: %s\n", $cmdln;
+       print  "MIME-Version: 1.0\n";
+       print  "Content-type: text/plain; charset=iso-8859-1\n";
+       print  "Content-Disposition: inline\n";
+       print  "Content-Transfer-Encoding: 8bit\n";
+       print  "\n";
+    }
+}
+
 if (defined $data{category}) {
     if (length($data{category})) {
        sales_list("category = '".$data{category}."'");
@@ -439,14 +561,22 @@ if (defined $data{category}) {
        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}) {
-    pay_invoce($data{pay}, 1);
+    pay_invoice($data{pay}, 1);
 } elsif (defined $data{unpay}) {
-    pay_invoce($data{unpay}, 0);
+    pay_invoice($data{unpay}, 0);
 } elsif (defined $data{hide}) {
-    hide_invoce($data{hide}, 1);
+    hide_invoice($data{hide}, 1);
 } elsif (defined $data{unhide}) {
-    hide_invoce($data{unhide}, 0);
+    hide_invoice($data{unhide}, 0);
+} else {
+    usage;
 }
-
-usage;