Added support for visible/invisible items
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index c634977..767b93e 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-2002,2003  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";
@@ -74,13 +75,33 @@ sub date_to_string
     return sprintf("%4d%02d%02d", $year,$mon,$day);
 }
 
+# --buchung-visible
+#
+sub toggle_visibility
+{
+    my $nr = shift;
+    my $query;
+    my @row;
+    my $sth;
+    my $value;
+
+    $query = "SELECT nr,visible FROM sales WHERE nr = $nr";
+    $sth = $dbh->prepare($query);
+    if ($sth && ($rc = $sth->execute) > 0) {
+       @row = $sth->fetchrow_array;
+       $value = $row[1]==1?0:1;
+
+       $query  = "UPDATE sales SET visible=$value WHERE nr = $nr";
+       $sth = $dbh->do($query);
+    }
+}
+
 sub pay_invoice
 {
     my $nr = shift;
     my $pay = shift;
     my $value = $pay==1?1:0;
     my $query;
-    my @row;
     my $sth;
 
     $query  = "UPDATE sales SET paid=$value WHERE nr = $nr";
@@ -98,6 +119,14 @@ sub sales_list
     my $sth;
     my $d;
 
+    if (!$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'";
@@ -177,11 +206,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 ".") {
@@ -219,6 +248,8 @@ sub buchung_input
     my $weiter = 'y';
     my $i;
     my $query;
+    my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
+       = localtime();
 
     print "Buchungseingabe\n\n";
     while ($weiter =~ /[JjYy]/) {
@@ -227,6 +258,19 @@ sub buchung_input
            if ($fieldname[$i] eq "Category" && $ans eq "?") {
                @categories = &get_categories() if ($#categories);
                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++;
+               }
            } else {
                $input[$i] = $ans;
                $i++;
@@ -261,9 +305,11 @@ sub usage
     print "  --buchung-category|-bc [category]\n";
     print "  --buchung-input|-bi\n";
     print "  --buchung-unpaid\n";
+    print "  --buchung-visible|-bv <nr>\n";
     print "  --pay <nr> | --unpay <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";
@@ -271,7 +317,9 @@ sub usage
     exit 0;
 }
 
+$term = new Term::ReadLine '';
 $i = 0;
+$opt_all = 0;
 $opt_verbose = 0;
 $opt_year = 0;
 &usage() if ($#ARGV == -1);
@@ -281,6 +329,8 @@ while ($i <= $#ARGV) {
        $ARGV[$i] = "--buchung-category";
     } elsif ($ARGV[$i] eq "-bi") {
        $ARGV[$i] = "--buchung-input";
+    } elsif ($ARGV[$i] eq "-bv") {
+       $ARGV[$i] = "--buchung-visible";
     } elsif ($ARGV[$i] eq "-lc") {
        $ARGV[$i] = "--list-categories";
     }
@@ -303,6 +353,11 @@ while ($i <= $#ARGV) {
            } else {
                &sales_list();
            }
+       } elsif ($ARGV[$i] eq "visible") {
+           if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
+               $i++;
+               &toggle_visibility($ARGV[$i]);
+           }
        } elsif ($ARGV[$i] eq "input") {
            &buchung_input();
        } elsif ($ARGV[$i] eq "unpaid") {
@@ -350,6 +405,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;
     }