3 # infocon - Administration tool for InfoCon
4 # Copyright (c) 1998-2003,2005-8,10 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";
134 $where .= " AND " if $where;
135 $where .= sprintf("year = %d", $opt_year);
138 if ($opt_direction) {
139 if ($opt_direction eq "in") {
141 } elsif ($opt_direction eq "out") {
152 if ($where !~ /visible/) {
153 $where .= " AND " if $where;
154 $where .= "visible = 1";
157 $query = "SELECT nr,date,description,price FROM $table";
158 $query .= " WHERE $where" if ($where);
159 $query .= " ORDER by date,nr";
161 $sth = $dbh->prepare($query);
162 if ($sth && (my $rc = $sth->execute) > 0) {
163 print " Nr. Datum Bezeichnung Betrag\n";
164 print "------------------------------------------------------------------\n";
165 while (@row = $sth->fetchrow_array) {
166 $descr = substr($row[2],0,40);
167 printf "%4d %8s %-40s %9.2f\n", $row[0], sdate($row[1]), $descr, $row[3];
174 print "-----------------------------------------------------------------\n"
175 if ($sum_neg > 0 || $sum_pos > 0) ;
176 printf " Zahlungseingänge %9.2f\n", $sum_pos
178 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
180 print "==================================================================\n";
181 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
188 my $category = shift;
193 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
196 $query = sprintf("SELECT DISTINCT description FROM %s WHERE year = %d ",
197 $table, $date_year+1900);
198 $query .= sprintf("AND category = '%s' ", $category) if defined $category;
199 $query .= "ORDER by description";
200 $sth = $dbh->prepare($query);
201 if ($sth && (my $rc = $sth->execute) > 0) {
202 while (@row = $sth->fetchrow_array) {
203 push(@arr, $row[0]) if ($row[0]);
216 $query = "SELECT DISTINCT category FROM $table ORDER by category";
217 $sth = $dbh->prepare($query);
218 if ($sth && (my $rc = $sth->execute) > 0) {
219 while (@row = $sth->fetchrow_array) {
220 push(@arr, $row[0]) if ($row[0]);
228 @categories = get_categories unless @categories;
230 printf "%s\n", join (", ",@categories);
242 $ans = $term->readline ($prompt . " [" . $default . "]: ");
244 $ans = $term->readline ($prompt . ": ");
246 if (length ($ans) == 0) {
248 } elsif ($ans eq ".") {
251 $ans =~ s/ *$// if $ans;
255 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
256 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
257 # Nr. anschliessend bereits wieder vergeben ist.
266 $query = "SELECT nr FROM $table ORDER BY nr DESC";
267 $sth = $dbh->prepare($query);
270 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
279 my @fieldname = ('Datum','PDF','Year','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
286 my ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
289 @categories = get_categories unless @categories;
290 @descriptions = get_descriptions;
292 $term = new Term::ReadLine '' unless $term;
294 $term->addhistory($_) foreach (@categories);
295 $term->addhistory($_) foreach (@descriptions);
297 my $attribs = $term->Attribs;
299 my $sth = $dbh->prepare ("INSERT INTO $table (nr,date,pdf,year,category,description,tax_percent,tax_assigned,price,paid) VALUES (?,?,?,?,?,?,?,?,?,?)");
301 print "Buchungseingabe\n\n";
302 while ($weiter =~ /[JjYy1]/) {
303 $i=0;while ($i <= $#fieldname) {
304 if ($fieldname[$i] eq "Category") {
305 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
306 $attribs->{completion_word} = \@categories;
307 } elsif ($fieldname[$i] eq "Year") {
308 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
309 $input[$i] = substr($input[0],-4);
310 $attribs->{completion_word} = $input[$i];
311 } elsif ($fieldname[$i] eq "Description") {
312 $attribs->{completion_entry_function} = $attribs->{list_completion_function};
313 @descriptions = get_descriptions $input[1];
314 $attribs->{completion_word} = \@descriptions;
315 } elsif ($fieldname[$i] eq "Tax assigned") {
316 if ($input[$i-1] == 0) {
321 $attribs->{completion_word} = undef;
323 $ans = read_input($fieldname[$i],$input[$i]);
324 if ($fieldname[$i] eq "Category" && $ans eq "?") {
325 printf " %s\n", join (", ",@categories);
326 } elsif ($fieldname[$i] eq "Datum") {
327 if ($ans =~ /^\d+\.\d+.\d+$/) {
330 } elsif ($ans =~ /^\d+\.\d+.$/) {
331 $ans .= $date_year + 1900;
334 } elsif ($ans =~ /^\d+\.$/) {
335 $ans .= sprintf ("%d.%d", $date_mon + 1, $date_year + 1900);
339 } elsif ($fieldname[$i] eq "Paid" || $fieldname[$i] eq "PDF") {
340 if ($ans =~ /[1jJyY]/) {
351 if (!$input[6]) { # USt selbst berechnen
352 if ($input[5] != 0) {
353 $input[6] = $input[7] - ($input[7] / ((100+$input[5])/100));
357 if ($input[4] =~ /[EeIi\+]/) {
358 $input[6] *= -1 if ($input[6] < 0);
359 $input[7] *= -1 if ($input[7] < 0);
361 $input[6] *= -1 if ($input[6] > 0);
362 $input[7] *= -1 if ($input[7] > 0);
365 $sth->execute (get_next_nr(), date_to_string($input[0]), $input[1], $input[2], $input[3], $input[4],
366 $input[6], $input[7], $input[8], $input[9]);
367 $weiter = read_input("Weiter",'j');
377 sales_list("visible = 0");
379 sales_list("visible = 0");
386 sales_list("paid = 0");
388 sales_list("paid = 0");
394 print "infocon [options] [-h|--help] commands\n";
395 print " --buchung-category|-bc [category]\n";
396 print " --buchung-input|-bi\n";
397 print " --buchung-unpaid\n";
398 print " --buchung-hidden\n";
399 print " --pay <nr> | --unpay <nr>\n";
400 print " --hide <nr> | --unhide <nr>\n";
401 print " --list-categories|-lc\n";
404 print " --verbose|-v\n";
405 print " --year|-y year\n";
406 print " --direction|--dir|-d in|out\n";
419 'buchung-input' => undef,
420 'buchung-unpaid' => undef,
421 'buchung-hidden' => undef,
422 'list-categories' => undef,
425 'buchung-category|bc:s' => \$data{category},
426 'pay=s' => \$data{pay},
427 'unpay=s' => \$data{unpay},
428 'hide=s' => \$data{hide},
429 'unhide=s' => \$data{unhide},
430 'year=i' => \$opt_year,
431 'direction|d=s' => \$opt_direction,
432 'mailto:s' => \$data{mailto},
434 'verbose' => \$opt_verbose,
436 'dm' => sub {$table = "sales_dm"},
437 'buchung-input|bi' => \$data{'buchung-input'},
438 'buchung-unpaid|bu' => \$data{'buchung-unpaid'},
439 'buchung-hidden|bh' => \$data{'buchung-hidden'},
440 'list-categories|lc' => \$data{'list-categories'},
443 my $cmdln = 'infocon ' . join (' ', @ARGV);
444 GetOptions(%options);
446 if ($opt_year != 0 && $opt_year < 2002) {
450 if (defined $opt_direction) {
451 usage unless $opt_direction =~ /^(in|out)$/i;
454 if (defined $data{mailto}) {
455 if (open(STDOUT, "| /usr/sbin/sendmail -t")) {
456 print "From: Joey Schulze <joey\@infodrom.org>\n";
457 printf "To: %s\n", length($data{mailto})?$data{mailto}:'Joey Schulze <joey@infodrom.org>';
458 printf "Subject: %s\n", $cmdln;
459 print "MIME-Version: 1.0\n";
460 print "Content-type: text/plain; charset=iso-8859-1\n";
461 print "Content-Disposition: inline\n";
462 print "Content-Transfer-Encoding: 8bit\n";
467 if (defined $data{category}) {
468 if (length($data{category})) {
469 sales_list("category = '".$data{category}."'");
474 } elsif (defined $data{'buchung-input'}) {
476 } elsif (defined $data{'buchung-unpaid'}) {
478 } elsif (defined $data{'buchung-hidden'}) {
480 } elsif (defined $data{'list-categories'}) {
482 } elsif (defined $data{pay}) {
483 pay_invoice($data{pay}, 1);
484 } elsif (defined $data{unpay}) {
485 pay_invoice($data{unpay}, 0);
486 } elsif (defined $data{hide}) {
487 hide_invoice($data{hide}, 1);
488 } elsif (defined $data{unhide}) {
489 hide_invoice($data{unhide}, 0);