3 # infocon - Admin-Tool for InfoCon
4 # Copyright (c) 1998-2002 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.
25 $engine = "dbi:Pg:dbname=infocon";
26 $dbh = DBI->connect($engine);
28 print "Access to database denied!\n";
36 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
37 return sprintf ("%d.%02d.%02d", $3,$2,$1);
40 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
41 # Datenbank gespeichert werden kann.
45 return "" if (!$_[0]);
47 ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
50 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
54 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
55 $day = $date_mday-1 if ($date_mday);
58 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
63 ($day,$mon,$year) = split(/\./, $_[0]);
71 } elsif ($year < 100) {
74 return sprintf("%4d%02d%02d", $year,$mon,$day);
81 my $value = $pay==1?1:0;
86 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
87 $sth = $dbh->do($query);
103 $where .= " AND date ~* '$opt_year'";
105 $where .= "date ~* '$opt_year'";
109 if ($opt_direction) {
110 if ($opt_direction eq "in") {
112 } elsif ($opt_direction eq "out") {
123 $query = "SELECT nr,date,description,price FROM $table";
124 $query .= " WHERE $where" if ($where);
125 $query .= " ORDER by date,nr";
126 $sth = $dbh->prepare($query);
127 if ($sth && ($rc = $sth->execute) > 0) {
128 print " Nr. Datum Bezeichnung Betrag\n";
129 print "------------------------------------------------------------------\n";
130 while (@row = $sth->fetchrow_array) {
131 $descr = substr($row[2],0,40);
132 printf "%4d %8s %-40s %9.2f\n", $row[0], &sdate($row[1]), $descr, $row[3];
139 print "-----------------------------------------------------------------\n"
140 if ($sum_neg > 0 || $sum_pos > 0) ;
141 printf " Zahlungseingänge %9.2f\n", $sum_pos
143 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
145 print "==================================================================\n";
146 printf " Summe %9.2f\n\n", $sum_pos - $sum_neg;
157 $query = "SELECT DISTINCT category FROM $table ORDER by category";
158 $sth = $dbh->prepare($query);
159 if ($sth && ($rc = $sth->execute) > 0) {
160 while (@row = $sth->fetchrow_array) {
161 push(@arr, $row[0]) if ($row[0]);
169 @categories = &get_categories() if ($#categories);
171 printf "%s\n", join (", ",@categories);
181 printf " [%s]", $default if ($default);
184 chop ($ans) if ($ans);
185 if (length ($ans) == 0) {
187 } elsif ($ans eq ".") {
193 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
194 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
195 # Nr. anschliessend bereits wieder vergeben ist.
204 $query = "SELECT nr FROM $table ORDER BY nr DESC";
205 $sth = $dbh->prepare($query);
208 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
217 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
223 print "Buchungseingabe\n\n";
224 while ($weiter =~ /[JjYy]/) {
225 $i=0;while ($i <= $#fieldname) {
226 $ans = &read_input($fieldname[$i],$input[$i]);
227 if ($fieldname[$i] eq "Category" && $ans eq "?") {
228 @categories = &get_categories() if ($#categories);
229 printf " %s\n", join (", ",@categories);
235 if (!$input[5]) { # USt selbst berechnen
236 if ($input[4] != 0) {
237 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
241 if ($input[3] =~ /[EeIi\+]/) {
242 $input[5] *= -1 if ($input[5] < 0);
243 $input[6] *= -1 if ($input[6] < 0);
245 $input[5] *= -1 if ($input[5] > 0);
246 $input[6] *= -1 if ($input[6] > 0);
249 $query = sprintf ("INSERT INTO $table VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d)",
250 &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
251 $input[5], $input[6], $input[7]);
252 $sth = $dbh->do($query);
253 $weiter = &read_input("Weiter",'j');
260 print "infocon [options] [-h|--help] commands\n";
261 print " --buchung-category|-bc [category]\n";
262 print " --buchung-input|-bi\n";
263 print " --buchung-unpaid\n";
264 print " --pay <nr> | --unpay <nr>\n";
265 print " --list-categories|-lc\n";
267 print " --verbose|-v\n";
268 print " --year|-y year\n";
269 print " --direction|--dir|-d in|out\n";
277 &usage() if ($#ARGV == -1);
278 while ($i <= $#ARGV) {
280 if ($ARGV[$i] eq "-bc") {
281 $ARGV[$i] = "--buchung-category";
282 } elsif ($ARGV[$i] eq "-bi") {
283 $ARGV[$i] = "--buchung-input";
284 } elsif ($ARGV[$i] eq "-lc") {
285 $ARGV[$i] = "--list-categories";
288 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
290 } elsif ($ARGV[$i] =~ /^--list-/) {
291 $ARGV[$i] =~ s/^--list-//;
292 if ($ARGV[$i] eq "categories") {
297 } elsif ($ARGV[$i] =~ /^--buchung-/) {
298 $ARGV[$i] =~ s/^--buchung-//;
299 if ($ARGV[$i] eq "category") {
300 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
302 &sales_list("category = '$ARGV[$i]'");
306 } elsif ($ARGV[$i] eq "input") {
308 } elsif ($ARGV[$i] eq "unpaid") {
309 $saved_table = $table;
311 &sales_list("paid = 0");
313 &sales_list("paid = 0");
314 $table = $table_saved;
318 } elsif ($ARGV[$i] eq "--pay") {
319 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
320 && ($ARGV[$i+1] =~ /^\d+$/)) {
322 &pay_invoice ($ARGV[$i], 1);
324 } elsif ($ARGV[$i] eq "--unpay") {
325 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
326 && ($ARGV[$i+1] =~ /^\d+$/)) {
328 &pay_invoice ($ARGV[$i], 0);
330 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
331 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
334 if ($opt_year < 70) {
336 } elsif ($opt_year < 100) {
339 if ($opt_year < 2002) {
341 } elsif ($opt_year > 2001) {
345 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
346 || $ARGV[$i] eq "-d") {
347 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
351 } elsif ($ARGV[$i] eq "--dm") {
353 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {