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 print "Buchungseingabe\n\n";
246 while ($weiter =~ /[JjYy]/) {
247 $i=0;while ($i <= $#fieldname) {
248 $ans = &read_input($fieldname[$i],$input[$i]);
249 if ($fieldname[$i] eq "Category" && $ans eq "?") {
250 @categories = &get_categories() if ($#categories);
251 printf " %s\n", join (", ",@categories);
252 } elsif ($fieldname[$i] eq "Datum") {
253 if ($ans =~ /^\d+\.\d+.\d+$/) {
256 } elsif ($ans =~ /^\d+\.\d+.$/) {
257 $ans .= $date_year + 1900;
260 } elsif ($ans =~ /^\d+\.$/) {
261 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
270 if (!$input[5]) { # USt selbst berechnen
271 if ($input[4] != 0) {
272 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
276 if ($input[3] =~ /[EeIi\+]/) {
277 $input[5] *= -1 if ($input[5] < 0);
278 $input[6] *= -1 if ($input[6] < 0);
280 $input[5] *= -1 if ($input[5] > 0);
281 $input[6] *= -1 if ($input[6] > 0);
284 $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d,%d)",
285 &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
286 $input[5], $input[6], 1, $input[7]);
287 $sth = $dbh->do($query);
288 $weiter = &read_input("Weiter",'j');
295 print "infocon [options] [-h|--help] commands\n";
296 print " --buchung-category|-bc [category]\n";
297 print " --buchung-input|-bi\n";
298 print " --buchung-unpaid\n";
299 print " --buchung-hidden\n";
300 print " --pay <nr> | --unpay <nr>\n";
301 print " --hide <nr> | --unhide <nr>\n";
302 print " --list-categories|-lc\n";
305 print " --verbose|-v\n";
306 print " --year|-y year\n";
307 print " --direction|--dir|-d in|out\n";
312 $term = new Term::ReadLine '';
317 &usage() if ($#ARGV == -1);
318 while ($i <= $#ARGV) {
320 if ($ARGV[$i] eq "-bc") {
321 $ARGV[$i] = "--buchung-category";
322 } elsif ($ARGV[$i] eq "-bi") {
323 $ARGV[$i] = "--buchung-input";
324 } elsif ($ARGV[$i] eq "-bh") {
325 $ARGV[$i] = "--buchung-hidden";
326 } elsif ($ARGV[$i] eq "-bu") {
327 $ARGV[$i] = "--buchung-unpaid";
328 } elsif ($ARGV[$i] eq "-lc") {
329 $ARGV[$i] = "--list-categories";
332 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
334 } elsif ($ARGV[$i] =~ /^--list-/) {
335 $ARGV[$i] =~ s/^--list-//;
336 if ($ARGV[$i] eq "categories") {
341 } elsif ($ARGV[$i] =~ /^--buchung-/) {
342 $ARGV[$i] =~ s/^--buchung-//;
343 if ($ARGV[$i] eq "category") {
344 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
346 &sales_list("category = '$ARGV[$i]'");
350 } elsif ($ARGV[$i] eq "hidden") {
351 $saved_table = $table;
353 &sales_list("visible = 0");
355 &sales_list("visible = 0");
356 $table = $table_saved;
357 } elsif ($ARGV[$i] eq "input") {
359 } elsif ($ARGV[$i] eq "unpaid") {
360 $saved_table = $table;
362 &sales_list("paid = 0");
364 &sales_list("paid = 0");
365 $table = $table_saved;
369 } elsif ($ARGV[$i] eq "--pay") {
370 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
371 && ($ARGV[$i+1] =~ /^\d+$/)) {
373 &pay_invoice ($ARGV[$i], 1);
375 } elsif ($ARGV[$i] eq "--unpay") {
376 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
377 && ($ARGV[$i+1] =~ /^\d+$/)) {
379 &pay_invoice ($ARGV[$i], 0);
381 } elsif ($ARGV[$i] eq "--hide") {
382 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
383 && ($ARGV[$i+1] =~ /^\d+$/)) {
385 &hide_invoice ($ARGV[$i], 1);
387 } elsif ($ARGV[$i] eq "--unhide") {
388 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
389 && ($ARGV[$i+1] =~ /^\d+$/)) {
391 &hide_invoice ($ARGV[$i], 0);
393 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
394 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
397 if ($opt_year < 70) {
399 } elsif ($opt_year < 100) {
402 if ($opt_year < 2002) {
404 } elsif ($opt_year > 2001) {
408 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
409 || $ARGV[$i] eq "-d") {
410 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
414 } elsif ($ARGV[$i] eq "--dm") {
416 } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
418 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {