Bring code into the next century
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index da37618..45e64f5 100755 (executable)
@@ -1,18 +1,18 @@
 #! /usr/bin/perl
 
 #  infocon - Admin-Tool for InfoCon
-#  Copyright (c) 1998-2002  Martin Schulze <joey@infodrom.org>
-
+#  Copyright (c) 1998-2003,2005-6,7  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
 #  the Free Software Foundation; either version 2 of the License, or
 #  (at your option) any later version.
-
+#
 #  This program is distributed in the hope that it will be useful,
 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #  GNU General Public License for more details.
-
+#
 #  You should have received a copy of the GNU General Public License
 #  along with this program; if not, write to the Free Software
 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
@@ -20,6 +20,7 @@
 # $Id$
 
 use DBI;
+use Term::ReadLine;
 
 $table = "sales";
 $engine  = "dbi:Pg:dbname=infocon";
@@ -45,7 +46,7 @@ sub date_to_string
     return "" if (!$_[0]);
 
     ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
-       = localtime();
+       = localtime;
 
     if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
        $day = $date_mday;
@@ -80,13 +81,24 @@ sub pay_invoice
     my $pay = shift;
     my $value = $pay==1?1:0;
     my $query;
-    my @row;
     my $sth;
 
     $query  = "UPDATE sales SET paid=$value WHERE nr = $nr";
     $sth = $dbh->do($query);
 }
 
+sub hide_invoice
+{
+    my $nr = shift;
+    my $hide = shift;
+    my $value = $hide==1?0:1;
+    my $query;
+    my $sth;
+
+    $query  = "UPDATE sales SET visible=$value WHERE nr = $nr";
+    $sth = $dbh->do($query);
+}
+
 sub sales_list
 {
     my $where = shift;
@@ -98,6 +110,14 @@ sub sales_list
     my $sth;
     my $d;
 
+    if ($where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
+       if ($where) {
+           $where .= " AND visible = 1";
+       } else {
+           $where .= "visible = 1";
+       }
+    }
+
     if ($opt_year) {
        if ($where) {
            $where .= " AND date ~* '$opt_year'";
@@ -129,7 +149,7 @@ sub sales_list
        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];
+           printf "%4d %8s %-40s  %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
            if ($row[3] < 0.0) {
                $sum_neg -= $row[3];
            } else {
@@ -166,7 +186,7 @@ sub get_categories
 
 sub list_categories
 {
-    @categories = &get_categories() if ($#categories);
+    @categories = get_categories unless @categories;
 
     printf "%s\n", join (", ",@categories);
 }
@@ -177,11 +197,11 @@ sub read_input
     my $default = shift;
     my $ans;
 
-    print $prompt;
-    printf " [%s]", $default if ($default);
-    print ": ";
-    $ans = <STDIN>;
-    chop ($ans) if ($ans);
+    if ($default) {
+       $ans = $term->readline ($prompt . " [" . $default . "]: ");
+    } else {
+       $ans = $term->readline ($prompt . ": ");
+    }
     if (length ($ans) == 0) {
        $ans = $default;
     } elsif ($ans eq ".") {
@@ -220,14 +240,16 @@ sub buchung_input
     my $i;
     my $query;
     my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
-       = localtime();
+       = localtime;
+
+    $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
 
     print "Buchungseingabe\n\n";
-    while ($weiter =~ /[JjYy]/) {
+    while ($weiter =~ /[JjYy1]/) {
        $i=0;while ($i <= $#fieldname) {
-           $ans = &read_input($fieldname[$i],$input[$i]);
+           $ans = read_input($fieldname[$i],$input[$i]);
            if ($fieldname[$i] eq "Category" && $ans eq "?") {
-               @categories = &get_categories() if ($#categories);
+               @categories = get_categories unless @categories;
                printf "  %s\n", join (", ",@categories);
            } elsif ($fieldname[$i] eq "Datum") {
                if ($ans =~ /^\d+\.\d+.\d+$/) {
@@ -242,6 +264,13 @@ sub buchung_input
                    $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++;
@@ -261,11 +290,9 @@ sub buchung_input
            $input[6] *= -1 if ($input[6] > 0);
        }
 
-       $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d)",
-                         &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
-                         $input[5], $input[6], $input[7]);
-       $sth = $dbh->do($query);
-       $weiter = &read_input("Weiter",'j');
+       $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;
     }
 }
@@ -276,9 +303,12 @@ sub usage
     print "  --buchung-category|-bc [category]\n";
     print "  --buchung-input|-bi\n";
     print "  --buchung-unpaid\n";
+    print "  --buchung-hidden\n";
     print "  --pay <nr> | --unpay <nr>\n";
+    print "  --hide <nr> | --unhide <nr>\n";
     print "  --list-categories|-lc\n";
     print "  Options:\n";
+    print "    --all|-a\n";
     print "    --verbose|-v\n";
     print "    --year|-y year\n";
     print "    --direction|--dir|-d in|out\n";
@@ -286,61 +316,86 @@ sub usage
     exit 0;
 }
 
+$term = new Term::ReadLine '';
 $i = 0;
+$opt_all = 0;
 $opt_verbose = 0;
 $opt_year = 0;
-&usage() if ($#ARGV == -1);
+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";
     }
 
     if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
-       &usage();
+       usage;
     } elsif ($ARGV[$i] =~ /^--list-/) {
        $ARGV[$i] =~ s/^--list-//;
        if ($ARGV[$i] eq "categories") {
-           &list_categories();
+           list_categories;
        } else {
-           &usage();
+           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]'");
+               sales_list("category = '$ARGV[$i]'");
            } else {
-               &sales_list();
+               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();
+           buchung_input;
        } elsif ($ARGV[$i] eq "unpaid") {
            $saved_table = $table;
            $table = "sales_dm";
-           &sales_list("paid = 0");
+           sales_list("paid = 0");
            $table = "sales";
-           &sales_list("paid = 0");
+           sales_list("paid = 0");
            $table = $table_saved;
        } else {
-           &usage();
+           usage;
        }
     } elsif ($ARGV[$i] eq "--pay") {
        if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
            && ($ARGV[$i+1] =~ /^\d+$/)) {
            $i++;
-           &pay_invoice ($ARGV[$i], 1);
+           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);
+           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+)$/)) {
@@ -365,6 +420,8 @@ while ($i <= $#ARGV) {
        }
     } 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;
     }