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.
26 $engine = "dbi:Pg:dbname=infocon";
27 $dbh = DBI->connect($engine);
29 print "Access to database denied!\n";
38 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
39 return sprintf ("%d.%02d.%02d", $3,$2,$1);
42 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
43 # Datenbank gespeichert werden kann.
47 return "" if (!$_[0]);
49 ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
52 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
56 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
57 $day = $date_mday-1 if ($date_mday);
60 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
65 ($day,$mon,$year) = split(/\./, $_[0]);
73 } elsif ($year < 100) {
76 return sprintf("%4d%02d%02d", $year,$mon,$day);
83 my $value = $pay==1?1:0;
87 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
88 $sth = $dbh->do($query);
95 my $value = $hide==1?0:1;
99 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
100 $sth = $dbh->do($query);
114 if ($where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
116 $where .= " AND visible = 1";
118 $where .= "visible = 1";
124 $where .= " AND date ~* '$opt_year'";
126 $where .= "date ~* '$opt_year'";
130 if ($opt_direction) {
131 if ($opt_direction eq "in") {
133 } elsif ($opt_direction eq "out") {
144 $query = "SELECT nr,date,description,price FROM $table";
145 $query .= " WHERE $where" if ($where);
146 $query .= " ORDER by date,nr";
147 $sth = $dbh->prepare($query);
148 if ($sth && ($rc = $sth->execute) > 0) {
149 print " Nr. Datum Bezeichnung Betrag\n";
150 print "------------------------------------------------------------------\n";
151 while (@row = $sth->fetchrow_array) {
152 $descr = substr($row[2],0,40);
153 printf "%4d %8s %-40s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
160 print "-----------------------------------------------------------------\n"
161 if ($sum_neg > 0 || $sum_pos > 0) ;
162 printf " Zahlungseingänge %9.2f\n", $sum_pos
164 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
166 print "==================================================================\n";
167 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
177 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
180 $query = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ORDER by description",
181 $table, $date_year+1900);
182 $sth = $dbh->prepare($query);
183 if ($sth && ($rc = $sth->execute) > 0) {
184 while (@row = $sth->fetchrow_array) {
185 push(@arr, $row[0]) if ($row[0]);
198 $query = "SELECT DISTINCT category FROM $table ORDER by category";
199 $sth = $dbh->prepare($query);
200 if ($sth && ($rc = $sth->execute) > 0) {
201 while (@row = $sth->fetchrow_array) {
202 push(@arr, $row[0]) if ($row[0]);
210 @categories = get_categories unless @categories;
212 printf "%s\n", join (", ",@categories);
222 $ans = $term->readline ($prompt . " [" . $default . "]: ");
224 $ans = $term->readline ($prompt . ": ");
226 if (length ($ans) == 0) {
228 } elsif ($ans eq ".") {
235 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
236 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
237 # Nr. anschliessend bereits wieder vergeben ist.
246 $query = "SELECT nr FROM $table ORDER BY nr DESC";
247 $sth = $dbh->prepare($query);
250 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
259 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
264 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
267 @categories = get_categories unless @categories;
268 my @descriptions = get_descriptions;
270 $term = new Term::ReadLine '' unless $term;
272 $term->addhistory($_) foreach (@categories);
273 $term->addhistory($_) foreach (@descriptions);
275 my $attribs = $term->Attribs;
277 $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
279 print "Buchungseingabe\n\n";
280 while ($weiter =~ /[JjYy1]/) {
281 $i=0;while ($i <= $#fieldname) {
282 if ($fieldname[$i] eq "Category") {
283 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
284 $attribs->{completion_word} = \@categories;
285 } elsif ($fieldname[$i] eq "Description") {
286 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
287 $attribs->{completion_word} = \@descriptions;
288 } elsif ($fieldname[$i] eq "Tax assigned") {
289 if ($input[$i-1] == 0) {
294 $attribs->{completion_word} = undef;
296 $ans = read_input($fieldname[$i],$input[$i]);
297 if ($fieldname[$i] eq "Category" && $ans eq "?") {
298 printf " %s\n", join (", ",@categories);
299 } elsif ($fieldname[$i] eq "Datum") {
300 if ($ans =~ /^\d+\.\d+.\d+$/) {
303 } elsif ($ans =~ /^\d+\.\d+.$/) {
304 $ans .= $date_year + 1900;
307 } elsif ($ans =~ /^\d+\.$/) {
308 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
312 } elsif ($fieldname[$i] eq "Paid") {
313 if ($ans =~ /[1jJyY]/) {
324 if (!$input[5]) { # USt selbst berechnen
325 if ($input[4] != 0) {
326 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
330 if ($input[3] =~ /[EeIi\+]/) {
331 $input[5] *= -1 if ($input[5] < 0);
332 $input[6] *= -1 if ($input[6] < 0);
334 $input[5] *= -1 if ($input[5] > 0);
335 $input[6] *= -1 if ($input[6] > 0);
338 $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
339 $input[5], $input[6], 1, $input[7]);
340 $weiter = read_input("Weiter",'j');
347 print "infocon [options] [-h|--help] commands\n";
348 print " --buchung-category|-bc [category]\n";
349 print " --buchung-input|-bi\n";
350 print " --buchung-unpaid\n";
351 print " --buchung-hidden\n";
352 print " --pay <nr> | --unpay <nr>\n";
353 print " --hide <nr> | --unhide <nr>\n";
354 print " --list-categories|-lc\n";
357 print " --verbose|-v\n";
358 print " --year|-y year\n";
359 print " --direction|--dir|-d in|out\n";
369 while ($i <= $#ARGV) {
371 if ($ARGV[$i] eq "-bc") {
372 $ARGV[$i] = "--buchung-category";
373 } elsif ($ARGV[$i] eq "-bi") {
374 $ARGV[$i] = "--buchung-input";
375 } elsif ($ARGV[$i] eq "-bh") {
376 $ARGV[$i] = "--buchung-hidden";
377 } elsif ($ARGV[$i] eq "-bu") {
378 $ARGV[$i] = "--buchung-unpaid";
379 } elsif ($ARGV[$i] eq "-lc") {
380 $ARGV[$i] = "--list-categories";
383 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
385 } elsif ($ARGV[$i] =~ /^--list-/) {
386 $ARGV[$i] =~ s/^--list-//;
387 if ($ARGV[$i] eq "categories") {
392 } elsif ($ARGV[$i] =~ /^--buchung-/) {
393 $ARGV[$i] =~ s/^--buchung-//;
394 if ($ARGV[$i] eq "category") {
395 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
397 sales_list("category = '$ARGV[$i]'");
401 } elsif ($ARGV[$i] eq "hidden") {
402 $saved_table = $table;
404 sales_list("visible = 0");
406 sales_list("visible = 0");
407 $table = $table_saved;
408 } elsif ($ARGV[$i] eq "input") {
410 } elsif ($ARGV[$i] eq "unpaid") {
411 $saved_table = $table;
413 sales_list("paid = 0");
415 sales_list("paid = 0");
416 $table = $table_saved;
420 } elsif ($ARGV[$i] eq "--pay") {
421 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
422 && ($ARGV[$i+1] =~ /^\d+$/)) {
424 pay_invoice ($ARGV[$i], 1);
426 } elsif ($ARGV[$i] eq "--unpay") {
427 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
428 && ($ARGV[$i+1] =~ /^\d+$/)) {
430 pay_invoice ($ARGV[$i], 0);
432 } elsif ($ARGV[$i] eq "--hide") {
433 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
434 && ($ARGV[$i+1] =~ /^\d+$/)) {
436 hide_invoice ($ARGV[$i], 1);
438 } elsif ($ARGV[$i] eq "--unhide") {
439 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
440 && ($ARGV[$i+1] =~ /^\d+$/)) {
442 hide_invoice ($ARGV[$i], 0);
444 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
445 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
448 if ($opt_year < 70) {
450 } elsif ($opt_year < 100) {
453 if ($opt_year < 2002) {
455 } elsif ($opt_year > 2001) {
459 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
460 || $ARGV[$i] eq "-d") {
461 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
465 } elsif ($ARGV[$i] eq "--dm") {
467 } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
469 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {