3 # infocon - Admin-Tool for InfoCon
4 # Copyright (c) 1998-2003,2005 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);
88 $query = "SELECT nr,visible FROM sales WHERE nr = $nr";
89 $sth = $dbh->prepare($query);
90 if ($sth && ($rc = $sth->execute) > 0) {
91 @row = $sth->fetchrow_array;
92 $value = $row[1]==1?0:1;
94 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
95 $sth = $dbh->do($query);
103 my $value = $pay==1?1:0;
107 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
108 $sth = $dbh->do($query);
122 if (!$opt_all || $opt_all == 0) {
124 $where .= " AND visible = 1";
126 $where .= "visible = 1";
132 $where .= " AND date ~* '$opt_year'";
134 $where .= "date ~* '$opt_year'";
138 if ($opt_direction) {
139 if ($opt_direction eq "in") {
141 } elsif ($opt_direction eq "out") {
152 $query = "SELECT nr,date,description,price FROM $table";
153 $query .= " WHERE $where" if ($where);
154 $query .= " ORDER by date,nr";
155 $sth = $dbh->prepare($query);
156 if ($sth && ($rc = $sth->execute) > 0) {
157 print " Nr. Datum Bezeichnung Betrag\n";
158 print "------------------------------------------------------------------\n";
159 while (@row = $sth->fetchrow_array) {
160 $descr = substr($row[2],0,40);
161 printf "%4d %8s %-40s %9.2f\n", $row[0], &sdate($row[1]), $descr, $row[3];
168 print "-----------------------------------------------------------------\n"
169 if ($sum_neg > 0 || $sum_pos > 0) ;
170 printf " Zahlungseingänge %9.2f\n", $sum_pos
172 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
174 print "==================================================================\n";
175 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
186 $query = "SELECT DISTINCT category FROM $table ORDER by category";
187 $sth = $dbh->prepare($query);
188 if ($sth && ($rc = $sth->execute) > 0) {
189 while (@row = $sth->fetchrow_array) {
190 push(@arr, $row[0]) if ($row[0]);
198 @categories = &get_categories() if ($#categories);
200 printf "%s\n", join (", ",@categories);
210 $ans = $term->readline ($prompt . " [" . $default . "]: ");
212 $ans = $term->readline ($prompt . ": ");
214 if (length ($ans) == 0) {
216 } elsif ($ans eq ".") {
222 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
223 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
224 # Nr. anschliessend bereits wieder vergeben ist.
233 $query = "SELECT nr FROM $table ORDER BY nr DESC";
234 $sth = $dbh->prepare($query);
237 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
246 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
251 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
254 print "Buchungseingabe\n\n";
255 while ($weiter =~ /[JjYy]/) {
256 $i=0;while ($i <= $#fieldname) {
257 $ans = &read_input($fieldname[$i],$input[$i]);
258 if ($fieldname[$i] eq "Category" && $ans eq "?") {
259 @categories = &get_categories() if ($#categories);
260 printf " %s\n", join (", ",@categories);
261 } elsif ($fieldname[$i] eq "Datum") {
262 if ($ans =~ /^\d+\.\d+.\d+$/) {
265 } elsif ($ans =~ /^\d+\.\d+.$/) {
266 $ans .= $date_year + 1900;
269 } elsif ($ans =~ /^\d+\.$/) {
270 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
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 $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d,%d)",
294 &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
295 $input[5], $input[6], 1, $input[7]);
296 $sth = $dbh->do($query);
297 $weiter = &read_input("Weiter",'j');
304 print "infocon [options] [-h|--help] commands\n";
305 print " --buchung-category|-bc [category]\n";
306 print " --buchung-input|-bi\n";
307 print " --buchung-unpaid\n";
308 print " --buchung-visible|-bv <nr>\n";
309 print " --pay <nr> | --unpay <nr>\n";
310 print " --list-categories|-lc\n";
313 print " --verbose|-v\n";
314 print " --year|-y year\n";
315 print " --direction|--dir|-d in|out\n";
320 $term = new Term::ReadLine '';
325 &usage() if ($#ARGV == -1);
326 while ($i <= $#ARGV) {
328 if ($ARGV[$i] eq "-bc") {
329 $ARGV[$i] = "--buchung-category";
330 } elsif ($ARGV[$i] eq "-bi") {
331 $ARGV[$i] = "--buchung-input";
332 } elsif ($ARGV[$i] eq "-bv") {
333 $ARGV[$i] = "--buchung-visible";
334 } elsif ($ARGV[$i] eq "-lc") {
335 $ARGV[$i] = "--list-categories";
338 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
340 } elsif ($ARGV[$i] =~ /^--list-/) {
341 $ARGV[$i] =~ s/^--list-//;
342 if ($ARGV[$i] eq "categories") {
347 } elsif ($ARGV[$i] =~ /^--buchung-/) {
348 $ARGV[$i] =~ s/^--buchung-//;
349 if ($ARGV[$i] eq "category") {
350 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
352 &sales_list("category = '$ARGV[$i]'");
356 } elsif ($ARGV[$i] eq "visible") {
357 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
359 &toggle_visibility($ARGV[$i]);
361 } elsif ($ARGV[$i] eq "input") {
363 } elsif ($ARGV[$i] eq "unpaid") {
364 $saved_table = $table;
366 &sales_list("paid = 0");
368 &sales_list("paid = 0");
369 $table = $table_saved;
373 } elsif ($ARGV[$i] eq "--pay") {
374 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
375 && ($ARGV[$i+1] =~ /^\d+$/)) {
377 &pay_invoice ($ARGV[$i], 1);
379 } elsif ($ARGV[$i] eq "--unpay") {
380 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
381 && ($ARGV[$i+1] =~ /^\d+$/)) {
383 &pay_invoice ($ARGV[$i], 0);
385 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
386 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
389 if ($opt_year < 70) {
391 } elsif ($opt_year < 100) {
394 if ($opt_year < 2002) {
396 } elsif ($opt_year > 2001) {
400 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
401 || $ARGV[$i] eq "-d") {
402 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
406 } elsif ($ARGV[$i] eq "--dm") {
408 } elsif ($ARGV[$i] eq "-a" || $ARGV[$i] eq "--all") {
410 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {