Vermeidung von SQL injection durch Verwendung parametrisierter Abfragen.
[kivitendo-erp.git] / SL / BP.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # Batch printing module backend routines
32 #
33 #======================================================================
34
35 package BP;
36
37 use SL::DBUtils;
38
39 sub get_vc {
40   $main::lxdebug->enter_sub();
41
42   my ($self, $myconfig, $form) = @_;
43
44   # connect to database
45   my $dbh = $form->dbconnect($myconfig);
46
47   my %arap = (invoice           => 'ar',
48               packing_list      => 'ar',
49               sales_order       => 'oe',
50               purchase_order    => 'oe',
51               sales_quotation   => 'oe',
52               request_quotation => 'oe',
53               check             => 'ap',
54               receipt           => 'ar');
55
56   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
57   my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
58
59   $query =
60     qq|SELECT count(*) | .
61     qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | .
62     qq|  WHERE a.${vc}_id = vc.id  AND s.trans_id = a.id AND s.formname = ? | .
63     qq|    AND s.spoolfile IS NOT NULL) AS total|;
64
65   $main::lxdebug->message(0, "kuh1 $query");
66
67   my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
68
69   # build selection list
70   if ($count < $myconfig->{vclimit}) {
71     $query =
72       qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name | .
73       qq|FROM $vc vc, $arap_type a, status s | .
74       qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
75       qq|  AND s.spoolfile IS NOT NULL|;
76
77     $sth = $dbh->prepare($query);
78     $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
79
80     $form->{"all_${vc}"} = [];
81     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
82       push @{ $form->{"all_${vc}"} }, $ref;
83     }
84     $sth->finish;
85
86     $main::lxdebug->message(0, "kuh2 $query");
87   }
88
89   $dbh->disconnect;
90
91   $main::lxdebug->leave_sub();
92 }
93
94 sub payment_accounts {
95   $main::lxdebug->enter_sub();
96
97   my ($self, $myconfig, $form) = @_;
98
99   # connect to database
100   my $dbh = $form->dbconnect($myconfig);
101
102   my $query =
103     qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
104     qq|FROM status s, chart c | .
105     qq|WHERE s.chart_id = c.id AND s.formname = ?|;
106   my $sth = $dbh->prepare($query);
107   $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
108
109   $form->{accounts} = [];
110   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
111     push @{ $form->{accounts} }, $ref;
112   }
113
114   $sth->finish;
115   $dbh->disconnect;
116
117   $main::lxdebug->leave_sub();
118 }
119
120 sub get_spoolfiles {
121   $main::lxdebug->enter_sub();
122
123   my ($self, $myconfig, $form) = @_;
124
125   # connect to database
126   my $dbh = $form->dbconnect($myconfig);
127
128   my ($query, $arap, @values);
129   my $invnumber = "invnumber";
130
131   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
132
133   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
134
135     $arap = ($form->{type} eq 'check') ? "ap" : "ar";
136     my ($accno) = split /--/, $form->{account};
137
138     $query =
139       qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
140       qq|  a.invoice, '$arap' AS module | .
141       qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
142       qq|WHERE s.formname = ? | .
143       qq|  AND s.chart_id = c.id | .
144       qq|  AND c.accno = ? | .
145       qq|  AND s.trans_id = a.id | .
146       qq|  AND a.${vc}_id = vc.id | .
147       qq|  AND ac.trans_id = s.trans_id | .
148       qq|  AND ac.chart_id = c.id | .
149       qq|  AND NOT ac.fx_transaction|;
150     @values = ($form->{type}, $accno);
151
152   } else {
153     $arap = "ar";
154     my $invoice = "a.invoice";
155
156     if ($form->{type} =~ /_(order|quotation)$/) {
157       $invnumber = "ordnumber";
158       $arap      = "oe";
159       $invoice   = '0';
160     }
161
162     $query =
163       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | .
164       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
165       qq|  s.spoolfile | .
166       qq|FROM $arap a, ${vc} vc, status s | .
167       qq|WHERE s.trans_id = a.id | .
168       qq|  AND s.spoolfile IS NOT NULL | .
169       qq|  AND s.formname = ? | .
170       qq|  AND a.${vc}_id = vc.id|;
171     @values = ($form->{type});
172   }
173
174   if ($form->{"${vc}_id"}) {
175     $query .= qq| AND a.${vc}_id = ?|;
176     push(@values, conv_i($form->{"${vc}_id"}));
177   } elsif ($form->{ $vc }) {
178     $query .= " AND vc.name ILIKE ?";
179     push(@values, $form->like($form->{ $vc }));
180   }
181   foreach my $column (qw(invnumber ordnumber quonumber)) {
182     if ($form->{$column}) {
183       $query .= " AND a.$column ILIKE ?";
184       push(@values, $form->like($form->{$column}));
185     }
186   }
187
188   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|packing_list|puchase_order|request_quotation)$/) {
189     if ($form->{transdatefrom}) {
190       $query .= " AND a.transdate >= ?";
191       push(@values, $form->{transdatefrom});
192     }
193     if ($form->{transdateto}) {
194       $query .= " AND a.transdate <= ?";
195       push(@values, $form->{transdateto});
196     }
197   }
198
199   my @a = (transdate, $invnumber, name);
200   my $sortorder = join ', ', $form->sort_columns(@a);
201
202   if (grep({ $_ eq $form->{sort} }
203            qw(transdate invnumber ordnumber quonumber name))) {
204     $sortorder = $form->{sort};
205   }
206
207   $query .= " ORDER BY $sortorder";
208
209   my $sth = $dbh->prepare($query);
210   $sth->execute(@values) ||
211     $form->dberror($query . " (" . join(", ", @values) . ")");
212
213   $form->{SPOOL} = [];
214   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
215     push @{ $form->{SPOOL} }, $ref;
216   }
217
218   $sth->finish;
219   $dbh->disconnect;
220
221   $main::lxdebug->leave_sub();
222 }
223
224 sub delete_spool {
225   $main::lxdebug->enter_sub();
226
227   my ($self, $myconfig, $form, $spool) = @_;
228
229   # connect to database, turn AutoCommit off
230   my $dbh = $form->dbconnect_noauto($myconfig);
231
232   my $query;
233
234   if ($form->{type} =~ /(check|receipt)/) {
235     $query = qq|DELETE FROM status WHERE spoolfile = ?|;
236   } else {
237     $query =
238       qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
239       qq|WHERE spoolfile = ?|;
240   }
241   my $sth = $dbh->prepare($query) || $form->dberror($query);
242
243   foreach my $i (1 .. $form->{rowcount}) {
244     if ($form->{"checked_$i"}) {
245       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
246       $sth->finish;
247     }
248   }
249
250   # commit
251   my $rc = $dbh->commit;
252   $dbh->disconnect;
253
254   if ($rc) {
255     foreach my $i (1 .. $form->{rowcount}) {
256       if ($form->{"checked_$i"}) {
257         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
258       }
259     }
260   }
261
262   $main::lxdebug->leave_sub();
263
264   return $rc;
265 }
266
267 sub print_spool {
268   $main::lxdebug->enter_sub();
269
270   my ($self, $myconfig, $form, $spool) = @_;
271
272   # connect to database
273   my $dbh = $form->dbconnect($myconfig);
274
275   my $query =
276     qq|UPDATE status SET printed = '1' | .
277     qq|WHERE formname = ? AND spoolfile = ?|;
278   my $sth = $dbh->prepare($query) || $form->dberror($query);
279
280   foreach my $i (1 .. $form->{rowcount}) {
281     if ($form->{"checked_$i"}) {
282       open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!");
283
284       $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
285
286       # send file to printer
287       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
288
289       while (<IN>) {
290         print OUT $_;
291       }
292       close(IN);
293       close(OUT);
294
295       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
296         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
297       $sth->finish;
298
299     }
300   }
301
302   $dbh->disconnect;
303
304   $main::lxdebug->leave_sub();
305 }
306
307 1;
308