3 # infocon - Admin-Tool for InfoCon
4 # Copyright (c) 1998-2003,2005-6 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;
177 $query = "SELECT DISTINCT category FROM $table ORDER by category";
178 $sth = $dbh->prepare($query);
179 if ($sth && ($rc = $sth->execute) > 0) {
180 while (@row = $sth->fetchrow_array) {
181 push(@arr, $row[0]) if ($row[0]);
189 @categories = &get_categories() if ($#categories);
191 printf "%s\n", join (", ",@categories);
201 $ans = $term->readline ($prompt . " [" . $default . "]: ");
203 $ans = $term->readline ($prompt . ": ");
205 if (length ($ans) == 0) {
207 } elsif ($ans eq ".") {
213 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
214 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
215 # Nr. anschliessend bereits wieder vergeben ist.
224 $query = "SELECT nr FROM $table ORDER BY nr DESC";
225 $sth = $dbh->prepare($query);
228 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
237 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
242 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
245 $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
247 print "Buchungseingabe\n\n";
248 while ($weiter =~ /[JjYy1]/) {
249 $i=0;while ($i <= $#fieldname) {
250 $ans = &read_input($fieldname[$i],$input[$i]);
251 if ($fieldname[$i] eq "Category" && $ans eq "?") {
252 @categories = &get_categories() if ($#categories);
253 printf " %s\n", join (", ",@categories);
254 } elsif ($fieldname[$i] eq "Datum") {
255 if ($ans =~ /^\d+\.\d+.\d+$/) {
258 } elsif ($ans =~ /^\d+\.\d+.$/) {
259 $ans .= $date_year + 1900;
262 } elsif ($ans =~ /^\d+\.$/) {
263 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
267 } elsif ($fieldname[$i] eq "Paid") {
268 if ($ans =~ /[1jJyY]/) {
279 if (!$input[5]) { # USt selbst berechnen
280 if ($input[4] != 0) {
281 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
285 if ($input[3] =~ /[EeIi\+]/) {
286 $input[5] *= -1 if ($input[5] < 0);
287 $input[6] *= -1 if ($input[6] < 0);
289 $input[5] *= -1 if ($input[5] > 0);
290 $input[6] *= -1 if ($input[6] > 0);
293 $sth->execute (&get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
294 $input[5], $input[6], 1, $input[7]);
295 $weiter = &read_input("Weiter",'j');
302 print "infocon [options] [-h|--help] commands\n";
303 print " --buchung-category|-bc [category]\n";
304 print " --buchung-input|-bi\n";
305 print " --buchung-unpaid\n";
306 print " --buchung-hidden\n";
307 print " --pay <nr> | --unpay <nr>\n";
308 print " --hide <nr> | --unhide <nr>\n";
309 print " --list-categories|-lc\n";
312 print " --verbose|-v\n";
313 print " --year|-y year\n";
314 print " --direction|--dir|-d in|out\n";
319 $term = new Term::ReadLine '';
324 &usage() if ($#ARGV == -1);
325 while ($i <= $#ARGV) {
327 if ($ARGV[$i] eq "-bc") {
328 $ARGV[$i] = "--buchung-category";
329 } elsif ($ARGV[$i] eq "-bi") {
330 $ARGV[$i] = "--buchung-input";
331 } elsif ($ARGV[$i] eq "-bh") {
332 $ARGV[$i] = "--buchung-hidden";
333 } elsif ($ARGV[$i] eq "-bu") {
334 $ARGV[$i] = "--buchung-unpaid";
335 } elsif ($ARGV[$i] eq "-lc") {
336 $ARGV[$i] = "--list-categories";
339 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
341 } elsif ($ARGV[$i] =~ /^--list-/) {
342 $ARGV[$i] =~ s/^--list-//;
343 if ($ARGV[$i] eq "categories") {
348 } elsif ($ARGV[$i] =~ /^--buchung-/) {
349 $ARGV[$i] =~ s/^--buchung-//;
350 if ($ARGV[$i] eq "category") {
351 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
353 &sales_list("category = '$ARGV[$i]'");
357 } elsif ($ARGV[$i] eq "hidden") {
358 $saved_table = $table;
360 &sales_list("visible = 0");
362 &sales_list("visible = 0");
363 $table = $table_saved;
364 } elsif ($ARGV[$i] eq "input") {
366 } elsif ($ARGV[$i] eq "unpaid") {
367 $saved_table = $table;
369 &sales_list("paid = 0");
371 &sales_list("paid = 0");
372 $table = $table_saved;
376 } elsif ($ARGV[$i] eq "--pay") {
377 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
378 && ($ARGV[$i+1] =~ /^\d+$/)) {
380 &pay_invoice ($ARGV[$i], 1);
382 } elsif ($ARGV[$i] eq "--unpay") {
383 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
384 && ($ARGV[$i+1] =~ /^\d+$/)) {
386 &pay_invoice ($ARGV[$i], 0);
388 } elsif ($ARGV[$i] eq "--hide") {
389 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
390 && ($ARGV[$i+1] =~ /^\d+$/)) {
392 &hide_invoice ($ARGV[$i], 1);
394 } elsif ($ARGV[$i] eq "--unhide") {
395 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
396 && ($ARGV[$i+1] =~ /^\d+$/)) {
398 &hide_invoice ($ARGV[$i], 0);
400 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
401 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
404 if ($opt_year < 70) {
406 } elsif ($opt_year < 100) {
409 if ($opt_year < 2002) {
411 } elsif ($opt_year > 2001) {
415 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
416 || $ARGV[$i] eq "-d") {
417 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
421 } elsif ($ARGV[$i] eq "--dm") {
423 } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
425 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {