use strict and warnings
[infodrom.org/service.infodrom.org] / src / InfoCon / buch / infocon
index 45e64f5..26f6faa 100755 (executable)
@@ -1,7 +1,7 @@
 #! /usr/bin/perl
 
 #  infocon - Admin-Tool for InfoCon
-#  Copyright (c) 1998-2003,2005-6,7  Martin Schulze <joey@infodrom.org>
+#  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;
 
-$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 @categories = ();
+my $term = undef;
+my $opt_all = 0;
+my $opt_verbose = 0;
+my $opt_year = 0;
+my $opt_direction = undef;
 
 sub sdate
 {
@@ -43,9 +51,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") {
@@ -144,7 +154,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) {
@@ -167,6 +177,26 @@ sub sales_list
     }
 }
 
+sub get_descriptions
+{
+    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);
+    $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;
+}
+
 sub get_categories
 {
     my $query;
@@ -176,7 +206,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]);
        }
@@ -207,6 +237,7 @@ sub read_input
     } elsif ($ans eq ".") {
        $ans = '';
     }
+    $ans =~ s/ *$//;
     return $ans;
 }
 
@@ -238,18 +269,42 @@ sub buchung_input
     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;
 
-    $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
+    @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 (?,?,?,?,?,?,?,?,?)");
 
     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 "?") {
-               @categories = get_categories unless @categories;
                printf "  %s\n", join (", ",@categories);
            } elsif ($fieldname[$i] eq "Datum") {
                if ($ans =~ /^\d+\.\d+.\d+$/) {
@@ -316,11 +371,7 @@ sub usage
     exit 0;
 }
 
-$term = new Term::ReadLine '';
-$i = 0;
-$opt_all = 0;
-$opt_verbose = 0;
-$opt_year = 0;
+my $i = 0;
 usage unless @ARGV;
 while ($i <= $#ARGV) {
     # Some aliases
@@ -355,21 +406,21 @@ while ($i <= $#ARGV) {
                sales_list;
            }
        } elsif ($ARGV[$i] eq "hidden") {
-           $saved_table = $table;
+           my $saved_table = $table;
            $table = "sales_dm";
            sales_list("visible = 0");
            $table = "sales";
            sales_list("visible = 0");
-           $table = $table_saved;
+           $table = $saved_table;
        } elsif ($ARGV[$i] eq "input") {
            buchung_input;
        } elsif ($ARGV[$i] eq "unpaid") {
-           $saved_table = $table;
+           my $saved_table = $table;
            $table = "sales_dm";
            sales_list("paid = 0");
            $table = "sales";
            sales_list("paid = 0");
-           $table = $table_saved;
+           $table = $saved_table;
        } else {
            usage;
        }