3 # infocon - Admin-Tool for InfoCon
4 # Copyright (c) 1998-2001 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.
24 $engine = "dbi:Pg:dbname=infocon";
25 $dbh = DBI->connect($engine);
27 print "Access to database denied!\n";
35 $_[0] =~ /\d{2}(\d{2})(\d{2})(\d{2})/;
36 return sprintf ("%d.%02d.%02d", $3,$2,$1);
39 # Wandelt einen lesbaren Datumsstring in die Form um, in der er in der
40 # Datenbank gespeichert werden kann.
44 return "" if (!$_[0]);
46 ($date_sec,$date_min,$date_hour,$date_mday,$date_mon,$date_year,$date_wday,$date_isdst)
49 if ($_[0] eq "heute" || $_[0] eq "sofort" || $_[0] eq "pronto" || $_[0] eq "today" || $_[0] eq "now") {
53 } elsif ($_[0] eq "gestern" || $_[0] eq "yesterday") {
54 $day = $date_mday-1 if ($date_mday);
57 } elsif ($_[0] eq "morgen" || $_[0] eq "tomorrow") {
62 ($day,$mon,$year) = split(/\./, $_[0]);
70 } elsif ($year < 100) {
73 return sprintf("%4d%02d%02d", $year,$mon,$day);
80 my $value = $pay==1?1:0;
85 $query = "UPDATE sales SET paid=$value WHERE nr = $nr";
86 $sth = $dbh->do($query);
102 $where .= " AND date ~* '$opt_year'";
104 $where .= "date ~* '$opt_year'";
108 if ($opt_direction) {
109 if ($opt_direction eq "in") {
111 } elsif ($opt_direction eq "out") {
122 $query = "SELECT nr,date,description,price FROM sales";
123 $query .= " WHERE $where" if ($where);
124 $query .= " ORDER by date,nr";
125 $sth = $dbh->prepare($query);
126 if ($sth && ($rc = $sth->execute) > 0) {
127 print " Nr. Datum Bezeichnung Betrag\n";
128 print "------------------------------------------------------------------\n";
129 while (@row = $sth->fetchrow_array) {
130 $descr = substr($row[2],0,40);
131 printf "%4d %8s %-40s %9.2f\n", $row[0], &sdate($row[1]), $descr, $row[3];
138 print "-----------------------------------------------------------------\n"
139 if ($sum_neg > 0 || $sum_pos > 0) ;
140 printf " Zahlungseingänge %9.2f\n", $sum_pos
142 printf " Zahlungsausgänge %9.2f\n", -$sum_neg
144 print "==================================================================\n";
145 printf " Summe %9.2f\n", $sum_pos - $sum_neg;
156 $query = "SELECT DISTINCT category FROM sales ORDER by category";
157 $sth = $dbh->prepare($query);
158 if ($sth && ($rc = $sth->execute) > 0) {
159 while (@row = $sth->fetchrow_array) {
160 push(@arr, $row[0]) if ($row[0]);
168 @categories = &get_categories() if ($#categories);
170 printf "%s\n", join (", ",@categories);
180 printf " [%s]", $default if ($default);
183 chop ($ans) if ($ans);
184 if (length ($ans) == 0) {
186 } elsif ($ans eq ".") {
192 # Gibt die naechste freie Nr. in der Datenbank zurueck. Wenn der
193 # INSERT nicht schnell darauf folgt, kann es passieren, dass die
194 # Nr. anschliessend bereits wieder vergeben ist.
203 $query = "SELECT nr FROM sales ORDER BY nr DESC";
204 $sth = $dbh->prepare($query);
207 if ($rc > 0 && (@row = $sth->fetchrow_array)) {
216 my @fieldname = ('Datum','Category','Description','Ein/Aus','Tax percent','Tax assigned','Price','Paid');
222 print "Buchungseingabe\n\n";
223 while ($weiter =~ /[JjYy]/) {
224 $i=0;while ($i <= $#fieldname) {
225 $ans = &read_input($fieldname[$i],$input[$i]);
226 if ($fieldname[$i] eq "Category" && $ans eq "?") {
227 @categories = &get_categories() if ($#categories);
228 printf " %s\n", join (", ",@categories);
234 if (!$input[5]) { # USt selbst berechnen
235 if ($input[4] != 0) {
236 $input[5] = $input[6] - ($input[6] / ((100+$input[4])/100));
240 if ($input[3] =~ /[EeIi\+]/) {
241 $input[5] *= -1 if ($input[5] < 0);
242 $input[6] *= -1 if ($input[6] < 0);
244 $input[5] *= -1 if ($input[5] > 0);
245 $input[6] *= -1 if ($input[6] > 0);
248 $query = sprintf ("INSERT INTO sales VALUES (%d,'%s','%s','%s',%8.2f,%8.2f,%8.2f,%d)",
249 &get_next_nr(), &date_to_string($input[0]), $input[1], $input[2], $input[4],
250 $input[5], $input[6], $input[7]);
251 $sth = $dbh->do($query);
252 $weiter = &read_input("Weiter",'j');
259 print "infocon [options] [-h|--help] commands\n";
260 print " --buchung-category|-bc [category]\n";
261 print " --buchung-input|-bi\n";
262 print " --buchung-unpaid\n";
263 print " --pay <nr> | --unpay <nr>\n";
264 print " --list-categories|-lc\n";
266 print " --verbose|-v\n";
267 print " --year|-y year\n";
268 print " --direction|--dir|-d in|out\n";
275 &usage() if ($#ARGV == -1);
276 while ($i <= $#ARGV) {
278 if ($ARGV[$i] eq "-bc") {
279 $ARGV[$i] = "--buchung-category";
280 } elsif ($ARGV[$i] eq "-bi") {
281 $ARGV[$i] = "--buchung-input";
282 } elsif ($ARGV[$i] eq "-lc") {
283 $ARGV[$i] = "--list-categories";
286 if ($ARGV[$i] eq "-h" || $ARGV[$i] eq "--help") {
288 } elsif ($ARGV[$i] =~ /^--list-/) {
289 $ARGV[$i] =~ s/^--list-//;
290 if ($ARGV[$i] eq "categories") {
295 } elsif ($ARGV[$i] =~ /^--buchung-/) {
296 $ARGV[$i] =~ s/^--buchung-//;
297 if ($ARGV[$i] eq "category") {
298 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)) {
300 &sales_list("category = '$ARGV[$i]'");
304 } elsif ($ARGV[$i] eq "input") {
306 } elsif ($ARGV[$i] eq "unpaid") {
307 &sales_list("paid = 0");
311 } elsif ($ARGV[$i] eq "--pay") {
312 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
313 && ($ARGV[$i+1] =~ /^\d+$/)) {
315 &pay_invoice ($ARGV[$i], 1);
317 } elsif ($ARGV[$i] eq "--unpay") {
318 if ($i+1 <= $#ARGV && ($ARGV[$i+1] !~ /^-/)
319 && ($ARGV[$i+1] =~ /^\d+$/)) {
321 &pay_invoice ($ARGV[$i], 0);
323 } elsif ($ARGV[$i] eq "--year" || $ARGV[$i] eq "-y") {
324 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(\d+)$/)) {
327 if ($opt_year < 70) {
329 } elsif ($opt_year < 100) {
333 } elsif ($ARGV[$i] eq "--direction" || $ARGV[$i] eq "--dir"
334 || $ARGV[$i] eq "-d") {
335 if ($i+1 <= $#ARGV && ($ARGV[$i+1] =~ /^(in|out)$/i)) {
339 } elsif ($ARGV[$i] eq "-v" || $ARGV[$i] eq "--verbose") {