#! /usr/bin/perl # infocon - Admin-Tool for InfoCon # Copyright (c) 1998-2003,2005-6 Martin Schulze # # 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. # $Id$ use DBI; use Term::ReadLine; $table = "sales"; $engine = "dbi:Pg:dbname=infocon"; $dbh = DBI->connect($engine); if (!$dbh) { print "Access to database denied!\n"; return 1; } @categories = (); sub sdate { $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/; return sprintf ("%d.%02d.%02d", $3,$2,$1); } # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der # Datenbank gespeichert werden kann. # sub date_to_string { return "" if (!$_[0]); ($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") { $day = $date_mday; $mon = $date_mon+1; $year = $date_year; } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") { $day = $date_mday-1 if ($date_mday); $mon = $date_mon+1; $year = $date_year; } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") { $day = $date_mday+1; $mon = $date_mon+1; $year = $date_year; } else { ($day,$mon,$year) = split(/\./, $_[0]); if (!$year) { $year = $date_year; } } if ($year < 70) { $year += 2000; } elsif ($year < 100) { $year += 1900; } return sprintf("%4d%02d%02d", $year,$mon,$day); } sub pay_invoice { my $nr = shift; my $pay = shift; my $value = $pay==1?1:0; my $query; 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; my $descr; my $sum_pos=0; my $sum_neg=0; my $query; my @row; 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'"; } else { $where .= "date ~* '$opt_year'"; } } if ($opt_direction) { if ($opt_direction eq "in") { $d = "price >= 0" } elsif ($opt_direction eq "out") { $d = "price <= 0" } if ($where) { $where .= " AND $d"; } else { $where .= "$d"; } } $query = "SELECT nr,date,description,price FROM $table"; $query .= " WHERE $where" if ($where); $query .= " ORDER by date,nr"; $sth = $dbh->prepare($query); if ($sth && ($rc = $sth->execute) > 0) { print " Nr. Datum Bezeichnung Betrag\n"; 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]; if ($row[3] < 0.0) { $sum_neg -= $row[3]; } else { $sum_pos += $row[3]; } } print "-----------------------------------------------------------------\n" if ($sum_neg > 0 || $sum_pos > 0) ; printf " Zahlungseingänge %9.2f\n", $sum_pos if ($sum_pos > 0); printf " Zahlungsausgänge %9.2f\n", -$sum_neg if ($sum_neg > 0); print "==================================================================\n"; printf " Summe %9.2f\n\n", $sum_pos - $sum_neg; } } sub get_categories { my $query; my @row; my $sth; my @arr = (); $query = "SELECT DISTINCT category FROM $table ORDER by category"; $sth = $dbh->prepare($query); if ($sth && ($rc = $sth->execute) > 0) { while (@row = $sth->fetchrow_array) { push(@arr, $row[0]) if ($row[0]); } } return @arr; } sub list_categories { @categories = &get_categories() if ($#categories); printf "%s\n", join (", ",@categories); } sub read_input { my $prompt = shift; my $default = shift; my $ans; if ($default) { $ans = $term->readline ($prompt . " [" . $default . "]: "); } else { $ans = $term->readline ($prompt . ": "); } if (length ($ans) == 0) { $ans = $default; } elsif ($ans eq ".") { $ans = ''; } return $ans; } # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der # INSERT nicht schnell darauf folgt, kann es passieren, dass die # Nr. anschliessend bereits wieder vergeben ist. # sub get_next_nr { my $query; my $sth; my $rc; my @row; $query = "SELECT nr FROM $table ORDER BY nr DESC"; $sth = $dbh->prepare($query); if ($sth) { $rc = $sth->execute; if ($rc > 0 && (@row = $sth->fetchrow_array)) { return $row[0] + 1; } } return 1; } sub buchung_input { my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid'); my @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(); $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)"); print "Buchungseingabe\n\n"; while ($weiter =~ /[JjYy1]/) { $i=0;while ($i <= $#fieldname) { $ans = &read_input($fieldname[$i],$input[$i]); 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++; } } elsif ($fieldname[$i] eq "Paid") { if ($ans =~ /[1jJyY]/) { $input[$i] = 1; } else { $input[$i] = 0; } $i++; } else { $input[$i] = $ans; $i++; } } if (!$input[5]) { # USt selbst berechnen if ($input[4] != 0) { $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100)); } } if ($input[3] =~ /[EeIi\+]/) { $input[5] *= -1 if ($input[5] < 0); $input[6] *= -1 if ($input[6] < 0); } else { $input[5] *= -1 if ($input[5] > 0); $input[6] *= -1 if ($input[6] > 0); } $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; } } sub usage { print "infocon [options] [-h|--help] commands\n"; print " --buchung-category|-bc [category]\n"; print " --buchung-input|-bi\n"; print " --buchung-unpaid\n"; print " --buchung-hidden\n"; print " --pay | --unpay \n"; print " --hide | --unhide \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"; print " --dm\n"; exit 0; } $term = new Term::ReadLine ''; $i = 0; $opt_all = 0; $opt_verbose = 0; $opt_year = 0; &usage() if ($#ARGV == -1); 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(); } 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; } $i++; }