Switch to indirect function calls
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index 4e2ba98..49cd230 100755 (executable)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 
-#  infocon - Admin-Tool for InfoCon
-#  Copyright (c) 1998-2003,2005-7  Martin Schulze <joey@infodrom.org>
+#  infocon - Administration tool for InfoCon
+#  Copyright (c) 1998-2003,2005-8  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
 
 # $Id$
 
+use strict;
+use warnings;
+
 use DBI;
 use Term::ReadLine;
+use Getopt::Long;
 
-$table = "sales";
-$engine  = "dbi:Pg:dbname=infocon";
-$dbh = DBI->connect($engine);
+my $table = "sales";
+my $engine  = "dbi:Pg:dbname=infocon";
+my $dbh = DBI->connect($engine);
 if (!$dbh) {
     print "Access to database denied!\n";
     return 1;
 }
 
-@categories = ();
+my %data;
+my @categories = ();
+my $term = undef;
+my $opt_all = 0;
+my $opt_verbose = 0;
+my $opt_year = 0;
+my $opt_direction = undef;
 
 sub sdate
 {
@@ -43,9 +53,11 @@ sub sdate
 #
 sub date_to_string
 {
+    my ($day,$mon,$year);
+
     return "" if (!$_[0]);
 
-    ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
+    my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
        = localtime;
 
     if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
@@ -110,7 +122,7 @@ sub sales_list
     my $sth;
     my $d;
 
-    if ($where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
+    if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
        if ($where) {
            $where .= " AND visible = 1";
        } else {
@@ -144,7 +156,7 @@ sub sales_list
     $query .= " WHERE $where" if ($where);
     $query .= " ORDER by date,nr";
     $sth = $dbh->prepare($query);
-    if ($sth && ($rc = $sth->execute) > 0) {
+    if ($sth && (my $rc = $sth->execute) > 0) {
        print " Nr.   Datum  Bezeichnung                                 Betrag\n";
        print "------------------------------------------------------------------\n";
        while (@row = $sth->fetchrow_array) {
@@ -165,10 +177,12 @@ sub sales_list
        print "==================================================================\n";
        printf " Summe                                                  %9.2f\n\n", $sum_pos - $sum_neg;
     }
+    $data{'done'} = 1;
 }
 
 sub get_descriptions
 {
+    my $category = shift;
     my $query;
     my @row;
     my $sth;
@@ -176,10 +190,12 @@ sub get_descriptions
     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",
+    $query  = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ",
                      $table, $date_year+1900);
+    $query .= sprintf("AND category = '%s' ", $category) if defined $category;
+    $query .= "ORDER by description";
     $sth = $dbh->prepare($query);
-    if ($sth && ($rc = $sth->execute) > 0) {
+    if ($sth && (my $rc = $sth->execute) > 0) {
        while (@row = $sth->fetchrow_array) {
            push(@arr, $row[0]) if ($row[0]);
        }
@@ -196,7 +212,7 @@ sub get_categories
 
     $query  = "SELECT DISTINCT category FROM $table ORDER by category";
     $sth = $dbh->prepare($query);
-    if ($sth && ($rc = $sth->execute) > 0) {
+    if ($sth && (my $rc = $sth->execute) > 0) {
        while (@row = $sth->fetchrow_array) {
            push(@arr, $row[0]) if ($row[0]);
        }
@@ -209,6 +225,8 @@ sub list_categories
     @categories = get_categories unless @categories;
 
     printf "%s\n", join (", ",@categories);
+
+    exit;
 }
 
 sub read_input
@@ -227,6 +245,7 @@ sub read_input
     } elsif ($ans eq ".") {
        $ans = '';
     }
+    $ans =~ s/ *$// if $ans;
     return $ans;
 }
 
@@ -258,19 +277,23 @@ sub buchung_input
     my @input = ();
     my $weiter = 'y';
     my $i;
+    my $ans;
     my $query;
+    my @descriptions;
     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
        = localtime;
 
     @categories = get_categories unless @categories;
-    my @descriptions = get_descriptions;
+    @descriptions = get_descriptions;
+
+    $term = new Term::ReadLine '' unless $term;
 
     $term->addhistory($_) foreach (@categories);
     $term->addhistory($_) foreach (@descriptions);
 
     my $attribs = $term->Attribs;
 
-    $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
+    my $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
 
     print "Buchungseingabe\n\n";
     while ($weiter =~ /[JjYy1]/) {
@@ -280,7 +303,13 @@ sub buchung_input
                $attribs->{completion_word} = \@categories;
            } elsif ($fieldname[$i] eq "Description") {
                $attribs->{completion_entry_function} = $attribs->{list_completion_function};
+               @descriptions = get_descriptions $input[1];
                $attribs->{completion_word} = \@descriptions;
+           } elsif ($fieldname[$i] eq "Tax assigned") {
+               if ($input[$i-1] == 0) {
+                   $input[$i++] = 0;
+                   next;
+               }
            } else {
                $attribs->{completion_word} = undef;
            }
@@ -331,6 +360,26 @@ sub buchung_input
        $weiter = read_input("Weiter",'j');
        $input[5] = 0.0;
     }
+
+    exit;
+}
+
+sub buchung_hidden
+{
+    $table = "sales_dm";
+    sales_list("visible = 0");
+    $table = "sales";
+    sales_list("visible = 0");
+    exit;
+}
+
+sub buchung_unpaid
+{
+    $table = "sales_dm";
+    sales_list("paid = 0");
+    $table = "sales";
+    sales_list("paid = 0");
+    exit;
 }
 
 sub usage
@@ -352,114 +401,69 @@ sub usage
     exit 0;
 }
 
-$term = new Term::ReadLine '';
-$i = 0;
-$opt_all = 0;
-$opt_verbose = 0;
-$opt_year = 0;
-usage unless @ARGV;
-while ($i <= $#ARGV) {
-    # Some aliases
-    if ($ARGV[$i] eq "-bc") {
-       $ARGV[$i] = "--buchung-category";
-    } elsif ($ARGV[$i] eq "-bi") {
-       $ARGV[$i] = "--buchung-input";
-    } elsif ($ARGV[$i] eq "-bh") {
-       $ARGV[$i] = "--buchung-hidden";
-    } elsif ($ARGV[$i] eq "-bu") {
-       $ARGV[$i] = "--buchung-unpaid";
-    } elsif ($ARGV[$i] eq "-lc") {
-       $ARGV[$i] = "--list-categories";
-    }
+%data = (
+    'category' => undef,
+    'done' => undef,
+    'pay' => undef,
+    'unpay' => undef,
+    'hide' => undef,
+    'unhide' => undef,
+    'buchung-input' => undef,
+    'buchung-unpaid' => undef,
+    'buchung-hidden' => undef,
+    'list-categories' => undef,
+    );
+my %options = (
+    'buchung-category|bc:s' => \$data{category},
+    'pay=s' => \$data{pay},
+    'unpay=s' => \$data{unpay},
+    'hide=s' => \$data{hide},
+    'unhide=s' => \$data{unhide},
+    'year=i' => \$opt_year,
+    'direction=s' => \$opt_direction,
+    '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'},
+    'list-categories|lc' => \$data{'list-categories'},
+    );
+
+GetOptions(%options);
+
+if ($opt_year != 0 && $opt_year < 2002) {
+    $table = "sales_dm";
+}
 
-    if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
-       usage;
-    } elsif ($ARGV[$i] =~ /^--list-/) {
-       $ARGV[$i] =~ s/^--list-//;
-       if ($ARGV[$i] eq "categories") {
-           list_categories;
-       } else {
-           usage;
-       }
-    } elsif ($ARGV[$i] =~ /^--buchung-/) {
-       $ARGV[$i] =~ s/^--buchung-//;
-       if ($ARGV[$i] eq "category") {
-           if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
-               $i++;
-               sales_list("category = '$ARGV[$i]'");
-           } else {
-               sales_list;
-           }
-       } elsif ($ARGV[$i] eq "hidden") {
-           $saved_table = $table;
-           $table = "sales_dm";
-           sales_list("visible = 0");
-           $table = "sales";
-           sales_list("visible = 0");
-           $table = $table_saved;
-       } elsif ($ARGV[$i] eq "input") {
-           buchung_input;
-       } elsif ($ARGV[$i] eq "unpaid") {
-           $saved_table = $table;
-           $table = "sales_dm";
-           sales_list("paid = 0");
-           $table = "sales";
-           sales_list("paid = 0");
-           $table = $table_saved;
-       } else {
-           usage;
-       }
-    } elsif ($ARGV[$i] eq "--pay") {
-       if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
-           && ($ARGV[$i+1] =~ /^\d+$/)) {
-           $i++;
-           pay_invoice ($ARGV[$i], 1);
-       }
-    } elsif ($ARGV[$i] eq "--unpay") {
-       if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
-           && ($ARGV[$i+1] =~ /^\d+$/)) {
-           $i++;
-           pay_invoice ($ARGV[$i], 0);
-       }
-    } elsif ($ARGV[$i] eq "--hide") {
-       if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
-           && ($ARGV[$i+1] =~ /^\d+$/)) {
-           $i++;
-           hide_invoice ($ARGV[$i], 1);
-       }
-    } elsif ($ARGV[$i] eq "--unhide") {
-       if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
-           && ($ARGV[$i+1] =~ /^\d+$/)) {
-           $i++;
-           hide_invoice ($ARGV[$i], 0);
-       }
-    } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
-       if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
-           $i++;
-           $opt_year = $1;
-           if ($opt_year < 70) {
-               $opt_year += 2000;
-           } elsif ($opt_year < 100) {
-               $opt_year += 1900;
-           }
-           if ($opt_year < 2002) {
-               $table = "sales_dm";
-           } elsif ($opt_year > 2001) {
-               $table = "sales";
-           }
-       }
-    } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
-            || $ARGV[$i] eq "-d") {
-       if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
-           $i++;
-           $opt_direction = $1;
-       }
-    } elsif ($ARGV[$i] eq "--dm") {
-       $table = "sales_dm";
-    } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
-       $opt_all = 1;
-    } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {
-       $opt_verbose = 1;
+if (defined $opt_direction) {
+    usage unless $opt_direction =~ /^(in|out)$/i;
+}
+
+if (defined $data{category}) {
+    if (length($data{category})) {
+       sales_list("category = '".$data{category}."'");
+    } else {
+       sales_list;
     }
-    $i++;
+    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);
+} elsif (defined $data{unpay}) {
+    pay_invoce($data{unpay}, 0);
+} elsif (defined $data{hide}) {
+    hide_invoce($data{hide}, 1);
+} elsif (defined $data{unhide}) {
+    hide_invoce($data{unhide}, 0);
 }
+
+usage;