3 # infocon - Admin-Tool for InfoCon
4 # Copyright (c) 1998-2003,2005-8 Martin Schulze <joey@infodrom.org>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 my $engine = "dbi:Pg:dbname=infocon";
30 my $dbh = DBI->connect($engine);
32 print "Access to database denied!\n";
41 my $opt_direction = undef;
45 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
46 return sprintf ("%d.%02d.%02d", $3,$2,$1);
49 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
50 # Datenbank gespeichert werden kann.
56 return "" if (!$_[0]);
58 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
61 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
65 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
66 $day = $date_mday-1 if ($date_mday);
69 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
74 ($day,$mon,$year) = split(/\./, $_[0]);
82 } elsif ($year < 100) {
85 return sprintf("%4d%02d%02d", $year,$mon,$day);
92 my $value = $pay==1?1:0;
96 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
97 $sth = $dbh->do($query);
104 my $value = $hide==1?0:1;
108 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
109 $sth = $dbh->do($query);
123 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
125 $where .= " AND visible = 1";
127 $where .= "visible = 1";
133 $where .= " AND date ~* '$opt_year'";
135 $where .= "date ~* '$opt_year'";
139 if ($opt_direction) {
140 if ($opt_direction eq "in") {
142 } elsif ($opt_direction eq "out") {
153 $query = "SELECT nr,date,description,price FROM $table";
154 $query .= " WHERE $where" if ($where);
155 $query .= " ORDER by date,nr";
156 $sth = $dbh->prepare($query);
157 if ($sth && (my $rc = $sth->execute) > 0) {
158 print " Nr. Datum Bezeichnung Betrag\n";
159 print "------------------------------------------------------------------\n";
160 while (@row = $sth->fetchrow_array) {
161 $descr = substr($row[2],0,40);
162 printf "%4d %8s %-40s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
169 print "-----------------------------------------------------------------\n"
170 if ($sum_neg > 0 || $sum_pos > 0) ;
171 printf " Zahlungseingänge %9.2f\n", $sum_pos
173 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
175 print "==================================================================\n";
176 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
186 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
189 $query = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ORDER by description",
190 $table, $date_year+1900);
191 $sth = $dbh->prepare($query);
192 if ($sth && (my $rc = $sth->execute) > 0) {
193 while (@row = $sth->fetchrow_array) {
194 push(@arr, $row[0]) if ($row[0]);
207 $query = "SELECT DISTINCT category FROM $table ORDER by category";
208 $sth = $dbh->prepare($query);
209 if ($sth && (my $rc = $sth->execute) > 0) {
210 while (@row = $sth->fetchrow_array) {
211 push(@arr, $row[0]) if ($row[0]);
219 @categories = get_categories unless @categories;
221 printf "%s\n", join (", ",@categories);
231 $ans = $term->readline ($prompt . " [" . $default . "]: ");
233 $ans = $term->readline ($prompt . ": ");
235 if (length ($ans) == 0) {
237 } elsif ($ans eq ".") {
244 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
245 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
246 # Nr. anschliessend bereits wieder vergeben ist.
255 $query = "SELECT nr FROM $table ORDER BY nr DESC";
256 $sth = $dbh->prepare($query);
259 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
268 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
274 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
277 @categories = get_categories unless @categories;
278 my @descriptions = get_descriptions;
280 $term = new Term::ReadLine '' unless $term;
282 $term->addhistory($_) foreach (@categories);
283 $term->addhistory($_) foreach (@descriptions);
285 my $attribs = $term->Attribs;
287 my $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
289 print "Buchungseingabe\n\n";
290 while ($weiter =~ /[JjYy1]/) {
291 $i=0;while ($i <= $#fieldname) {
292 if ($fieldname[$i] eq "Category") {
293 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
294 $attribs->{completion_word} = \@categories;
295 } elsif ($fieldname[$i] eq "Description") {
296 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
297 $attribs->{completion_word} = \@descriptions;
298 } elsif ($fieldname[$i] eq "Tax assigned") {
299 if ($input[$i-1] == 0) {
304 $attribs->{completion_word} = undef;
306 $ans = read_input($fieldname[$i],$input[$i]);
307 if ($fieldname[$i] eq "Category" && $ans eq "?") {
308 printf " %s\n", join (", ",@categories);
309 } elsif ($fieldname[$i] eq "Datum") {
310 if ($ans =~ /^\d+\.\d+.\d+$/) {
313 } elsif ($ans =~ /^\d+\.\d+.$/) {
314 $ans .= $date_year + 1900;
317 } elsif ($ans =~ /^\d+\.$/) {
318 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
322 } elsif ($fieldname[$i] eq "Paid") {
323 if ($ans =~ /[1jJyY]/) {
334 if (!$input[5]) { # USt selbst berechnen
335 if ($input[4] != 0) {
336 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
340 if ($input[3] =~ /[EeIi\+]/) {
341 $input[5] *= -1 if ($input[5] < 0);
342 $input[6] *= -1 if ($input[6] < 0);
344 $input[5] *= -1 if ($input[5] > 0);
345 $input[6] *= -1 if ($input[6] > 0);
348 $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
349 $input[5], $input[6], 1, $input[7]);
350 $weiter = read_input("Weiter",'j');
357 print "infocon [options] [-h|--help] commands\n";
358 print " --buchung-category|-bc [category]\n";
359 print " --buchung-input|-bi\n";
360 print " --buchung-unpaid\n";
361 print " --buchung-hidden\n";
362 print " --pay <nr> | --unpay <nr>\n";
363 print " --hide <nr> | --unhide <nr>\n";
364 print " --list-categories|-lc\n";
367 print " --verbose|-v\n";
368 print " --year|-y year\n";
369 print " --direction|--dir|-d in|out\n";
376 while ($i <= $#ARGV) {
378 if ($ARGV[$i] eq "-bc") {
379 $ARGV[$i] = "--buchung-category";
380 } elsif ($ARGV[$i] eq "-bi") {
381 $ARGV[$i] = "--buchung-input";
382 } elsif ($ARGV[$i] eq "-bh") {
383 $ARGV[$i] = "--buchung-hidden";
384 } elsif ($ARGV[$i] eq "-bu") {
385 $ARGV[$i] = "--buchung-unpaid";
386 } elsif ($ARGV[$i] eq "-lc") {
387 $ARGV[$i] = "--list-categories";
390 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
392 } elsif ($ARGV[$i] =~ /^--list-/) {
393 $ARGV[$i] =~ s/^--list-//;
394 if ($ARGV[$i] eq "categories") {
399 } elsif ($ARGV[$i] =~ /^--buchung-/) {
400 $ARGV[$i] =~ s/^--buchung-//;
401 if ($ARGV[$i] eq "category") {
402 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
404 sales_list("category = '$ARGV[$i]'");
408 } elsif ($ARGV[$i] eq "hidden") {
409 my $saved_table = $table;
411 sales_list("visible = 0");
413 sales_list("visible = 0");
414 $table = $saved_table;
415 } elsif ($ARGV[$i] eq "input") {
417 } elsif ($ARGV[$i] eq "unpaid") {
418 my $saved_table = $table;
420 sales_list("paid = 0");
422 sales_list("paid = 0");
423 $table = $saved_table;
427 } elsif ($ARGV[$i] eq "--pay") {
428 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
429 && ($ARGV[$i+1] =~ /^\d+$/)) {
431 pay_invoice ($ARGV[$i], 1);
433 } elsif ($ARGV[$i] eq "--unpay") {
434 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
435 && ($ARGV[$i+1] =~ /^\d+$/)) {
437 pay_invoice ($ARGV[$i], 0);
439 } elsif ($ARGV[$i] eq "--hide") {
440 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
441 && ($ARGV[$i+1] =~ /^\d+$/)) {
443 hide_invoice ($ARGV[$i], 1);
445 } elsif ($ARGV[$i] eq "--unhide") {
446 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
447 && ($ARGV[$i+1] =~ /^\d+$/)) {
449 hide_invoice ($ARGV[$i], 0);
451 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
452 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
455 if ($opt_year < 70) {
457 } elsif ($opt_year < 100) {
460 if ($opt_year < 2002) {
462 } elsif ($opt_year > 2001) {
466 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
467 || $ARGV[$i] eq "-d") {
468 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
472 } elsif ($ARGV[$i] eq "--dm") {
474 } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
476 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {