Added support for Readline, which should simply adding receipes
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index 4af5f9d..5d24747 100755 (executable)
@@ -1,18 +1,18 @@
 #! /usr/bin/perl
 
 #  infocon - Admin-Tool for InfoCon
-#  Copyright (c) 1998-2001  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,7 +20,9 @@
 # $Id$
 
 use DBI;
+use Term::ReadLine;
 
+$table = "sales";
 $engine  = "dbi:Pg:dbname=infocon";
 $dbh = DBI->connect($engine);
 if (!$dbh) {
@@ -119,7 +121,7 @@ sub sales_list
        }
     }
 
-    $query  = "SELECT nr,date,description,price FROM sales";
+    $query  = "SELECT nr,date,description,price FROM $table";
     $query .= " WHERE $where" if ($where);
     $query .= " ORDER by date,nr";
     $sth = $dbh->prepare($query);
@@ -142,7 +144,7 @@ sub sales_list
        printf " Zahlungsausgänge                                       %9.2f\n", -$sum_neg
            if ($sum_neg > 0);
        print "==================================================================\n";
-       printf " Summe                                                  %9.2f\n", $sum_pos - $sum_neg;
+       printf " Summe                                                  %9.2f\n\n", $sum_pos - $sum_neg;
     }
 }
 
@@ -153,7 +155,7 @@ sub get_categories
     my $sth;
     my @arr = ();
 
-    $query  = "SELECT DISTINCT category FROM sales ORDER by category";
+    $query  = "SELECT DISTINCT category FROM $table ORDER by category";
     $sth = $dbh->prepare($query);
     if ($sth && ($rc = $sth->execute) > 0) {
        while (@row = $sth->fetchrow_array) {
@@ -176,11 +178,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 ".") {
@@ -200,7 +202,7 @@ sub get_next_nr
     my $rc;
     my @row;
 
-    $query = "SELECT nr FROM sales ORDER BY nr DESC";
+    $query = "SELECT nr FROM $table ORDER BY nr DESC";
     $sth = $dbh->prepare($query);
     if ($sth) {
        $rc = $sth->execute;
@@ -218,6 +220,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]/) {
@@ -226,6 +230,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++;
@@ -245,7 +262,7 @@ sub buchung_input
            $input[6] *= -1 if ($input[6] > 0);
        }
 
-       $query = sprintf ("INSERT INTO sales VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d)",
+       $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);
@@ -266,9 +283,11 @@ sub usage
     print "    --verbose|-v\n";
     print "    --year|-y year\n";
     print "    --direction|--dir|-d in|out\n";
+    print "    --dm\n";
     exit 0;
 }
 
+$term = new Term::ReadLine '';
 $i = 0;
 $opt_verbose = 0;
 $opt_year = 0;
@@ -304,7 +323,12 @@ while ($i <= $#ARGV) {
        } elsif ($ARGV[$i] eq "input") {
            &buchung_input();
        } elsif ($ARGV[$i] eq "unpaid") {
-               &sales_list("paid = 0");
+           $saved_table = $table;
+           $table = "sales_dm";
+           &sales_list("paid = 0");
+           $table = "sales";
+           &sales_list("paid = 0");
+           $table = $table_saved;
        } else {
            &usage();
        }
@@ -329,6 +353,11 @@ while ($i <= $#ARGV) {
            } 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") {
@@ -336,6 +365,8 @@ while ($i <= $#ARGV) {
            $i++;
            $opt_direction = $1;
        }
+    } elsif ($ARGV[$i] eq "--dm") {
+       $table = "sales_dm";
     } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {
        $opt_verbose = 1;
     }