3 # infocon - Administration 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.
30 my $engine = "dbi:Pg:dbname=infocon";
31 my $dbh = DBI->connect($engine);
33 print "Access to database denied!\n";
43 my $opt_direction = undef;
47 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
48 return sprintf ("%d.%02d.%02d", $3,$2,$1);
51 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
52 # Datenbank gespeichert werden kann.
58 return "" if (!$_[0]);
60 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
63 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
67 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
68 $day = $date_mday-1 if ($date_mday);
71 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
76 ($day,$mon,$year) = split(/\./, $_[0]);
84 } elsif ($year < 100) {
87 return sprintf("%4d%02d%02d", $year,$mon,$day);
94 my $value = $pay==1?1:0;
98 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
99 $sth = $dbh->do($query);
106 my $value = $hide==1?0:1;
110 $query = "UPDATE sales SET visible=$value WHERE nr = $nr";
111 $sth = $dbh->do($query);
125 if ($where && $where !~ /visible/ && (!$opt_all || $opt_all == 0)) {
127 $where .= " AND visible = 1";
129 $where .= "visible = 1";
135 $where .= " AND date ~* '$opt_year'";
137 $where .= "date ~* '$opt_year'";
141 if ($opt_direction) {
142 if ($opt_direction eq "in") {
144 } elsif ($opt_direction eq "out") {
155 $query = "SELECT nr,date,description,price FROM $table";
156 $query .= " WHERE $where" if ($where);
157 $query .= " ORDER by date,nr";
158 $sth = $dbh->prepare($query);
159 if ($sth && (my $rc = $sth->execute) > 0) {
160 print " Nr. Datum Bezeichnung Betrag\n";
161 print "------------------------------------------------------------------\n";
162 while (@row = $sth->fetchrow_array) {
163 $descr = substr($row[2],0,40);
164 printf "%4d %8s %-40s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
171 print "-----------------------------------------------------------------\n"
172 if ($sum_neg > 0 || $sum_pos > 0) ;
173 printf " Zahlungseingänge %9.2f\n", $sum_pos
175 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
177 print "==================================================================\n";
178 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
185 my $category = shift;
190 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
193 $query = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ",
194 $table, $date_year+1900);
195 $query .= sprintf("AND category = '%s' ", $category) if defined $category;
196 $query .= "ORDER by description";
197 $sth = $dbh->prepare($query);
198 if ($sth && (my $rc = $sth->execute) > 0) {
199 while (@row = $sth->fetchrow_array) {
200 push(@arr, $row[0]) if ($row[0]);
213 $query = "SELECT DISTINCT category FROM $table ORDER by category";
214 $sth = $dbh->prepare($query);
215 if ($sth && (my $rc = $sth->execute) > 0) {
216 while (@row = $sth->fetchrow_array) {
217 push(@arr, $row[0]) if ($row[0]);
225 @categories = get_categories unless @categories;
227 printf "%s\n", join (", ",@categories);
239 $ans = $term->readline ($prompt . " [" . $default . "]: ");
241 $ans = $term->readline ($prompt . ": ");
243 if (length ($ans) == 0) {
245 } elsif ($ans eq ".") {
248 $ans =~ s/ *$// if $ans;
252 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
253 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
254 # Nr. anschliessend bereits wieder vergeben ist.
263 $query = "SELECT nr FROM $table ORDER BY nr DESC";
264 $sth = $dbh->prepare($query);
267 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
276 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
283 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
286 @categories = get_categories unless @categories;
287 @descriptions = get_descriptions;
289 $term = new Term::ReadLine '' unless $term;
291 $term->addhistory($_) foreach (@categories);
292 $term->addhistory($_) foreach (@descriptions);
294 my $attribs = $term->Attribs;
296 my $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
298 print "Buchungseingabe\n\n";
299 while ($weiter =~ /[JjYy1]/) {
300 $i=0;while ($i <= $#fieldname) {
301 if ($fieldname[$i] eq "Category") {
302 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
303 $attribs->{completion_word} = \@categories;
304 } elsif ($fieldname[$i] eq "Description") {
305 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
306 @descriptions = get_descriptions $input[1];
307 $attribs->{completion_word} = \@descriptions;
308 } elsif ($fieldname[$i] eq "Tax assigned") {
309 if ($input[$i-1] == 0) {
314 $attribs->{completion_word} = undef;
316 $ans = read_input($fieldname[$i],$input[$i]);
317 if ($fieldname[$i] eq "Category" && $ans eq "?") {
318 printf " %s\n", join (", ",@categories);
319 } elsif ($fieldname[$i] eq "Datum") {
320 if ($ans =~ /^\d+\.\d+.\d+$/) {
323 } elsif ($ans =~ /^\d+\.\d+.$/) {
324 $ans .= $date_year + 1900;
327 } elsif ($ans =~ /^\d+\.$/) {
328 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
332 } elsif ($fieldname[$i] eq "Paid") {
333 if ($ans =~ /[1jJyY]/) {
344 if (!$input[5]) { # USt selbst berechnen
345 if ($input[4] != 0) {
346 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
350 if ($input[3] =~ /[EeIi\+]/) {
351 $input[5] *= -1 if ($input[5] < 0);
352 $input[6] *= -1 if ($input[6] < 0);
354 $input[5] *= -1 if ($input[5] > 0);
355 $input[6] *= -1 if ($input[6] > 0);
358 $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
359 $input[5], $input[6], 1, $input[7]);
360 $weiter = read_input("Weiter",'j');
370 sales_list("visible = 0");
372 sales_list("visible = 0");
379 sales_list("paid = 0");
381 sales_list("paid = 0");
387 print "infocon [options] [-h|--help] commands\n";
388 print " --buchung-category|-bc [category]\n";
389 print " --buchung-input|-bi\n";
390 print " --buchung-unpaid\n";
391 print " --buchung-hidden\n";
392 print " --pay <nr> | --unpay <nr>\n";
393 print " --hide <nr> | --unhide <nr>\n";
394 print " --list-categories|-lc\n";
397 print " --verbose|-v\n";
398 print " --year|-y year\n";
399 print " --direction|--dir|-d in|out\n";
411 'buchung-input' => undef,
412 'buchung-unpaid' => undef,
413 'buchung-hidden' => undef,
414 'list-categories' => undef,
417 'buchung-category|bc:s' => \$data{category},
418 'pay=s' => \$data{pay},
419 'unpay=s' => \$data{unpay},
420 'hide=s' => \$data{hide},
421 'unhide=s' => \$data{unhide},
422 'year=i' => \$opt_year,
423 'direction=s' => \$opt_direction,
425 'verbose' => \$opt_verbose,
427 'dm' => sub {$table = "sales_dm"},
428 'buchung-input|bi' => \$data{'buchung-input'},
429 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
430 'buchung-hidden|bh' => \$data{'buchung-hidden'},
431 'list-categories|lc' => \$data{'list-categories'},
434 GetOptions(%options);
436 if ($opt_year != 0 && $opt_year < 2002) {
440 if (defined $opt_direction) {
441 usage unless $opt_direction =~ /^(in|out)$/i;
444 if (defined $data{category}) {
445 if (length($data{category})) {
446 sales_list("category = '".$data{category}."'");
451 } elsif (defined $data{'buchung-input'}) {
453 } elsif (defined $data{'buchung-unpaid'}) {
455 } elsif (defined $data{'buchung-hidden'}) {
457 } elsif (defined $data{'list-categories'}) {
459 } elsif (defined $data{pay}) {
460 pay_invoce($data{pay}, 1);
461 } elsif (defined $data{unpay}) {
462 pay_invoce($data{unpay}, 0);
463 } elsif (defined $data{hide}) {
464 hide_invoce($data{hide}, 1);
465 } elsif (defined $data{unhide}) {
466 hide_invoce($data{unhide}, 0);