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 if ($where !~ /visible/) {
156 $where .= " AND " if $where;
157 $where .= "visible = 1";
160 $query = "SELECT nr,date,description,price FROM $table";
161 $query .= " WHERE $where" if ($where);
162 $query .= " ORDER by date,nr";
164 $sth = $dbh->prepare($query);
165 if ($sth && (my $rc = $sth->execute) > 0) {
166 print " Nr. Datum Bezeichnung Betrag\n";
167 print "------------------------------------------------------------------\n";
168 while (@row = $sth->fetchrow_array) {
169 $descr = substr($row[2],0,40);
170 printf "%4d %8s %-40s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
177 print "-----------------------------------------------------------------\n"
178 if ($sum_neg > 0 || $sum_pos > 0) ;
179 printf " Zahlungseingänge %9.2f\n", $sum_pos
181 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
183 print "==================================================================\n";
184 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
191 my $category = shift;
196 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
199 $query = sprintf("SELECT DISTINCT description FROM %s WHERE date LIKE '%d%%' ",
200 $table, $date_year+1900);
201 $query .= sprintf("AND category = '%s' ", $category) if defined $category;
202 $query .= "ORDER by description";
203 $sth = $dbh->prepare($query);
204 if ($sth && (my $rc = $sth->execute) > 0) {
205 while (@row = $sth->fetchrow_array) {
206 push(@arr, $row[0]) if ($row[0]);
219 $query = "SELECT DISTINCT category FROM $table ORDER by category";
220 $sth = $dbh->prepare($query);
221 if ($sth && (my $rc = $sth->execute) > 0) {
222 while (@row = $sth->fetchrow_array) {
223 push(@arr, $row[0]) if ($row[0]);
231 @categories = get_categories unless @categories;
233 printf "%s\n", join (", ",@categories);
245 $ans = $term->readline ($prompt . " [" . $default . "]: ");
247 $ans = $term->readline ($prompt . ": ");
249 if (length ($ans) == 0) {
251 } elsif ($ans eq ".") {
254 $ans =~ s/ *$// if $ans;
258 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
259 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
260 # Nr. anschliessend bereits wieder vergeben ist.
269 $query = "SELECT nr FROM $table ORDER BY nr DESC";
270 $sth = $dbh->prepare($query);
273 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
282 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
289 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
292 @categories = get_categories unless @categories;
293 @descriptions = get_descriptions;
295 $term = new Term::ReadLine '' unless $term;
297 $term->addhistory($_) foreach (@categories);
298 $term->addhistory($_) foreach (@descriptions);
300 my $attribs = $term->Attribs;
302 my $sth = $dbh->prepare ("INSERT INTO $table VALUES (?,?,?,?,?,?,?,?,?)");
304 print "Buchungseingabe\n\n";
305 while ($weiter =~ /[JjYy1]/) {
306 $i=0;while ($i <= $#fieldname) {
307 if ($fieldname[$i] eq "Category") {
308 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
309 $attribs->{completion_word} = \@categories;
310 } elsif ($fieldname[$i] eq "Description") {
311 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
312 @descriptions = get_descriptions $input[1];
313 $attribs->{completion_word} = \@descriptions;
314 } elsif ($fieldname[$i] eq "Tax assigned") {
315 if ($input[$i-1] == 0) {
320 $attribs->{completion_word} = undef;
322 $ans = read_input($fieldname[$i],$input[$i]);
323 if ($fieldname[$i] eq "Category" && $ans eq "?") {
324 printf " %s\n", join (", ",@categories);
325 } elsif ($fieldname[$i] eq "Datum") {
326 if ($ans =~ /^\d+\.\d+.\d+$/) {
329 } elsif ($ans =~ /^\d+\.\d+.$/) {
330 $ans .= $date_year + 1900;
333 } elsif ($ans =~ /^\d+\.$/) {
334 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
338 } elsif ($fieldname[$i] eq "Paid") {
339 if ($ans =~ /[1jJyY]/) {
350 if (!$input[5]) { # USt selbst berechnen
351 if ($input[4] != 0) {
352 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
356 if ($input[3] =~ /[EeIi\+]/) {
357 $input[5] *= -1 if ($input[5] < 0);
358 $input[6] *= -1 if ($input[6] < 0);
360 $input[5] *= -1 if ($input[5] > 0);
361 $input[6] *= -1 if ($input[6] > 0);
364 $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[4],
365 $input[5], $input[6], 1, $input[7]);
366 $weiter = read_input("Weiter",'j');
376 sales_list("visible = 0");
378 sales_list("visible = 0");
385 sales_list("paid = 0");
387 sales_list("paid = 0");
393 print "infocon [options] [-h|--help] commands\n";
394 print " --buchung-category|-bc [category]\n";
395 print " --buchung-input|-bi\n";
396 print " --buchung-unpaid\n";
397 print " --buchung-hidden\n";
398 print " --pay <nr> | --unpay <nr>\n";
399 print " --hide <nr> | --unhide <nr>\n";
400 print " --list-categories|-lc\n";
403 print " --verbose|-v\n";
404 print " --year|-y year\n";
405 print " --direction|--dir|-d in|out\n";
418 'buchung-input' => undef,
419 'buchung-unpaid' => undef,
420 'buchung-hidden' => undef,
421 'list-categories' => undef,
424 'buchung-category|bc:s' => \$data{category},
425 'pay=s' => \$data{pay},
426 'unpay=s' => \$data{unpay},
427 'hide=s' => \$data{hide},
428 'unhide=s' => \$data{unhide},
429 'year=i' => \$opt_year,
430 'direction|d=s' => \$opt_direction,
431 'mailto:s' => \$data{mailto},
433 'verbose' => \$opt_verbose,
435 'dm' => sub {$table = "sales_dm"},
436 'buchung-input|bi' => \$data{'buchung-input'},
437 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
438 'buchung-hidden|bh' => \$data{'buchung-hidden'},
439 'list-categories|lc' => \$data{'list-categories'},
442 my $cmdln = 'infocon ' . join (' ', @ARGV);
443 GetOptions(%options);
445 if ($opt_year != 0 && $opt_year < 2002) {
449 if (defined $opt_direction) {
450 usage unless $opt_direction =~ /^(in|out)$/i;
453 if (defined $data{mailto}) {
454 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
455 print "From: Joey Schulze <joey\@infodrom.org>\n";
456 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
457 printf "Subject: %s\n", $cmdln;
458 print "MIME-Version: 1.0\n";
459 print "Content-type: text/plain; charset=iso-8859-1\n";
460 print "Content-Disposition: inline\n";
461 print "Content-Transfer-Encoding: 8bit\n";
466 if (defined $data{category}) {
467 if (length($data{category})) {
468 sales_list("category = '".$data{category}."'");
473 } elsif (defined $data{'buchung-input'}) {
475 } elsif (defined $data{'buchung-unpaid'}) {
477 } elsif (defined $data{'buchung-hidden'}) {
479 } elsif (defined $data{'list-categories'}) {
481 } elsif (defined $data{pay}) {
482 pay_invoice($data{pay}, 1);
483 } elsif (defined $data{unpay}) {
484 pay_invoice($data{unpay}, 0);
485 } elsif (defined $data{hide}) {
486 hide_invoice($data{hide}, 1);
487 } elsif (defined $data{unhide}) {
488 hide_invoice($data{unhide}, 0);