3 # infocon - Admin-Tool for InfoCon
4 # Copyright (c) 1998-2003,2005-7 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.
26 $engine = "dbi:Pg:dbname=infocon";
27 $dbh = DBI->connect($engine);
29 print "Access to database denied!\n";
37 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
38 return sprintf ("%d.%02d.%02d", $3,$2,$1);
41 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
42 # Datenbank gespeichert werden kann.
46 return "" if (!$_[0]);
48 ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
51 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
55 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
56 $day = $date_mday-1 if ($date_mday);
59 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
64 ($day,$mon,$year) = split(/\./, $_[0]);
72 } elsif ($year < 100) {
75 return sprintf("%4d%02d%02d", $year,$mon,$day);
82 my $value = $pay==1?1:0;
86 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
87 $sth = $dbh->do($query);
94 my $value = $hide==1?0:1;
98 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
99 $sth = $dbh->do($query);
113 if ($where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
115 $where .= " AND visible = 1";
117 $where .= "visible = 1";
123 $where .= " AND date ~* '$opt_year'";
125 $where .= "date ~* '$opt_year'";
129 if ($opt_direction) {
130 if ($opt_direction eq "in") {
132 } elsif ($opt_direction eq "out") {
143 $query = "SELECT nr,date,description,price FROM $table";
144 $query .= " WHERE $where" if ($where);
145 $query .= " ORDER by date,nr";
146 $sth = $dbh->prepare($query);
147 if ($sth && ($rc = $sth->execute) > 0) {
148 print " Nr. Datum Bezeichnung Betrag\n";
149 print "------------------------------------------------------------------\n";
150 while (@row = $sth->fetchrow_array) {
151 $descr = substr($row[2],0,40);
152 printf "%4d %8s %-40s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
159 print "-----------------------------------------------------------------\n"
160 if ($sum_neg > 0 || $sum_pos > 0) ;
161 printf " Zahlungseingänge %9.2f\n", $sum_pos
163 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
165 print "==================================================================\n";
166 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
176 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
179 $query = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ORDER by description",
180 $table, $date_year+1900);
181 $sth = $dbh->prepare($query);
182 if ($sth && ($rc = $sth->execute) > 0) {
183 while (@row = $sth->fetchrow_array) {
184 push(@arr, $row[0]) if ($row[0]);
197 $query = "SELECT DISTINCT category FROM $table ORDER by category";
198 $sth = $dbh->prepare($query);
199 if ($sth && ($rc = $sth->execute) > 0) {
200 while (@row = $sth->fetchrow_array) {
201 push(@arr, $row[0]) if ($row[0]);
209 @categories = get_categories unless @categories;
211 printf "%s\n", join (", ",@categories);
221 $ans = $term->readline ($prompt . " [" . $default . "]: ");
223 $ans = $term->readline ($prompt . ": ");
225 if (length ($ans) == 0) {
227 } elsif ($ans eq ".") {
234 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
235 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
236 # Nr. anschliessend bereits wieder vergeben ist.
245 $query = "SELECT nr FROM $table ORDER BY nr DESC";
246 $sth = $dbh->prepare($query);
249 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
258 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
263 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
266 @categories = get_categories unless @categories;
267 my @descriptions = get_descriptions;
269 $term->addhistory($_) foreach (@categories);
270 $term->addhistory($_) foreach (@descriptions);
272 my $attribs = $term->Attribs;
274 $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
276 print "Buchungseingabe\n\n";
277 while ($weiter =~ /[JjYy1]/) {
278 $i=0;while ($i <= $#fieldname) {
279 if ($fieldname[$i] eq "Category") {
280 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
281 $attribs->{completion_word} = \@categories;
282 } elsif ($fieldname[$i] eq "Description") {
283 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
284 $attribs->{completion_word} = \@descriptions;
285 } elsif ($fieldname[$i] eq "Tax assigned") {
286 if ($input[$i-1] == 0) {
291 $attribs->{completion_word} = undef;
293 $ans = read_input($fieldname[$i],$input[$i]);
294 if ($fieldname[$i] eq "Category" && $ans eq "?") {
295 printf " %s\n", join (", ",@categories);
296 } elsif ($fieldname[$i] eq "Datum") {
297 if ($ans =~ /^\d+\.\d+.\d+$/) {
300 } elsif ($ans =~ /^\d+\.\d+.$/) {
301 $ans .= $date_year + 1900;
304 } elsif ($ans =~ /^\d+\.$/) {
305 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
309 } elsif ($fieldname[$i] eq "Paid") {
310 if ($ans =~ /[1jJyY]/) {
321 if (!$input[5]) { # USt selbst berechnen
322 if ($input[4] != 0) {
323 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
327 if ($input[3] =~ /[EeIi\+]/) {
328 $input[5] *= -1 if ($input[5] < 0);
329 $input[6] *= -1 if ($input[6] < 0);
331 $input[5] *= -1 if ($input[5] > 0);
332 $input[6] *= -1 if ($input[6] > 0);
335 $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
336 $input[5], $input[6], 1, $input[7]);
337 $weiter = read_input("Weiter",'j');
344 print "infocon [options] [-h|--help] commands\n";
345 print " --buchung-category|-bc [category]\n";
346 print " --buchung-input|-bi\n";
347 print " --buchung-unpaid\n";
348 print " --buchung-hidden\n";
349 print " --pay <nr> | --unpay <nr>\n";
350 print " --hide <nr> | --unhide <nr>\n";
351 print " --list-categories|-lc\n";
354 print " --verbose|-v\n";
355 print " --year|-y year\n";
356 print " --direction|--dir|-d in|out\n";
361 $term = new Term::ReadLine '';
367 while ($i <= $#ARGV) {
369 if ($ARGV[$i] eq "-bc") {
370 $ARGV[$i] = "--buchung-category";
371 } elsif ($ARGV[$i] eq "-bi") {
372 $ARGV[$i] = "--buchung-input";
373 } elsif ($ARGV[$i] eq "-bh") {
374 $ARGV[$i] = "--buchung-hidden";
375 } elsif ($ARGV[$i] eq "-bu") {
376 $ARGV[$i] = "--buchung-unpaid";
377 } elsif ($ARGV[$i] eq "-lc") {
378 $ARGV[$i] = "--list-categories";
381 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
383 } elsif ($ARGV[$i] =~ /^--list-/) {
384 $ARGV[$i] =~ s/^--list-//;
385 if ($ARGV[$i] eq "categories") {
390 } elsif ($ARGV[$i] =~ /^--buchung-/) {
391 $ARGV[$i] =~ s/^--buchung-//;
392 if ($ARGV[$i] eq "category") {
393 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
395 sales_list("category = '$ARGV[$i]'");
399 } elsif ($ARGV[$i] eq "hidden") {
400 $saved_table = $table;
402 sales_list("visible = 0");
404 sales_list("visible = 0");
405 $table = $table_saved;
406 } elsif ($ARGV[$i] eq "input") {
408 } elsif ($ARGV[$i] eq "unpaid") {
409 $saved_table = $table;
411 sales_list("paid = 0");
413 sales_list("paid = 0");
414 $table = $table_saved;
418 } elsif ($ARGV[$i] eq "--pay") {
419 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
420 && ($ARGV[$i+1] =~ /^\d+$/)) {
422 pay_invoice ($ARGV[$i], 1);
424 } elsif ($ARGV[$i] eq "--unpay") {
425 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
426 && ($ARGV[$i+1] =~ /^\d+$/)) {
428 pay_invoice ($ARGV[$i], 0);
430 } elsif ($ARGV[$i] eq "--hide") {
431 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
432 && ($ARGV[$i+1] =~ /^\d+$/)) {
434 hide_invoice ($ARGV[$i], 1);
436 } elsif ($ARGV[$i] eq "--unhide") {
437 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
438 && ($ARGV[$i+1] =~ /^\d+$/)) {
440 hide_invoice ($ARGV[$i], 0);
442 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
443 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
446 if ($opt_year < 70) {
448 } elsif ($opt_year < 100) {
451 if ($opt_year < 2002) {
453 } elsif ($opt_year > 2001) {
457 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
458 || $ARGV[$i] eq "-d") {
459 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
463 } elsif ($ARGV[$i] eq "--dm") {
465 } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
467 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {