Keine direkt vom Browser stammenden Strings bei open() verwenden.
[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   my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
66
67   # build selection list
68   if ($count < $myconfig->{vclimit}) {
69     $query =
70       qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name | .
71       qq|FROM $vc vc, $arap_type a, status s | .
72       qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
73       qq|  AND s.spoolfile IS NOT NULL|;
74
75     $sth = $dbh->prepare($query);
76     $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
77
78     $form->{"all_${vc}"} = [];
79     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
80       push @{ $form->{"all_${vc}"} }, $ref;
81     }
82     $sth->finish;
83   }
84
85   $dbh->disconnect;
86
87   $main::lxdebug->leave_sub();
88 }
89
90 sub payment_accounts {
91   $main::lxdebug->enter_sub();
92
93   my ($self, $myconfig, $form) = @_;
94
95   # connect to database
96   my $dbh = $form->dbconnect($myconfig);
97
98   my $query =
99     qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
100     qq|FROM status s, chart c | .
101     qq|WHERE s.chart_id = c.id AND s.formname = ?|;
102   my $sth = $dbh->prepare($query);
103   $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
104
105   $form->{accounts} = [];
106   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
107     push @{ $form->{accounts} }, $ref;
108   }
109
110   $sth->finish;
111   $dbh->disconnect;
112
113   $main::lxdebug->leave_sub();
114 }
115
116 sub get_spoolfiles {
117   $main::lxdebug->enter_sub();
118
119   my ($self, $myconfig, $form) = @_;
120
121   # connect to database
122   my $dbh = $form->dbconnect($myconfig);
123
124   my ($query, $arap, @values);
125   my $invnumber = "invnumber";
126
127   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
128
129   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
130
131     $arap = ($form->{type} eq 'check') ? "ap" : "ar";
132     my ($accno) = split /--/, $form->{account};
133
134     $query =
135       qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
136       qq|  a.invoice, '$arap' AS module | .
137       qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
138       qq|WHERE s.formname = ? | .
139       qq|  AND s.chart_id = c.id | .
140       qq|  AND c.accno = ? | .
141       qq|  AND s.trans_id = a.id | .
142       qq|  AND a.${vc}_id = vc.id | .
143       qq|  AND ac.trans_id = s.trans_id | .
144       qq|  AND ac.chart_id = c.id | .
145       qq|  AND NOT ac.fx_transaction|;
146     @values = ($form->{type}, $accno);
147
148   } else {
149     $arap = "ar";
150     my $invoice = "a.invoice";
151
152     if ($form->{type} =~ /_(order|quotation)$/) {
153       $invnumber = "ordnumber";
154       $arap      = "oe";
155       $invoice   = '0';
156     }
157
158     $query =
159       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | .
160       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
161       qq|  s.spoolfile | .
162       qq|FROM $arap a, ${vc} vc, status s | .
163       qq|WHERE s.trans_id = a.id | .
164       qq|  AND s.spoolfile IS NOT NULL | .
165       qq|  AND s.formname = ? | .
166       qq|  AND a.${vc}_id = vc.id|;
167     @values = ($form->{type});
168   }
169
170   if ($form->{"${vc}_id"}) {
171     $query .= qq| AND a.${vc}_id = ?|;
172     push(@values, conv_i($form->{"${vc}_id"}));
173   } elsif ($form->{ $vc }) {
174     $query .= " AND vc.name ILIKE ?";
175     push(@values, $form->like($form->{ $vc }));
176   }
177   foreach my $column (qw(invnumber ordnumber quonumber)) {
178     if ($form->{$column}) {
179       $query .= " AND a.$column ILIKE ?";
180       push(@values, $form->like($form->{$column}));
181     }
182   }
183
184   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|packing_list|puchase_order|request_quotation)$/) {
185     if ($form->{transdatefrom}) {
186       $query .= " AND a.transdate >= ?";
187       push(@values, $form->{transdatefrom});
188     }
189     if ($form->{transdateto}) {
190       $query .= " AND a.transdate <= ?";
191       push(@values, $form->{transdateto});
192     }
193   }
194
195   my @a = (transdate, $invnumber, name);
196   my $sortorder = join ', ', $form->sort_columns(@a);
197
198   if (grep({ $_ eq $form->{sort} }
199            qw(transdate invnumber ordnumber quonumber name))) {
200     $sortorder = $form->{sort};
201   }
202
203   $query .= " ORDER BY $sortorder";
204
205   my $sth = $dbh->prepare($query);
206   $sth->execute(@values) ||
207     $form->dberror($query . " (" . join(", ", @values) . ")");
208
209   $form->{SPOOL} = [];
210   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
211     push @{ $form->{SPOOL} }, $ref;
212   }
213
214   $sth->finish;
215   $dbh->disconnect;
216
217   $main::lxdebug->leave_sub();
218 }
219
220 sub delete_spool {
221   $main::lxdebug->enter_sub();
222
223   my ($self, $myconfig, $form, $spool) = @_;
224
225   # connect to database, turn AutoCommit off
226   my $dbh = $form->dbconnect_noauto($myconfig);
227
228   my $query;
229
230   if ($form->{type} =~ /(check|receipt)/) {
231     $query = qq|DELETE FROM status WHERE spoolfile = ?|;
232   } else {
233     $query =
234       qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
235       qq|WHERE spoolfile = ?|;
236   }
237   my $sth = $dbh->prepare($query) || $form->dberror($query);
238
239   foreach my $i (1 .. $form->{rowcount}) {
240     if ($form->{"checked_$i"}) {
241       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
242       $sth->finish;
243     }
244   }
245
246   # commit
247   my $rc = $dbh->commit;
248   $dbh->disconnect;
249
250   if ($rc) {
251     foreach my $i (1 .. $form->{rowcount}) {
252       if ($form->{"checked_$i"}) {
253         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
254       }
255     }
256   }
257
258   $main::lxdebug->leave_sub();
259
260   return $rc;
261 }
262
263 sub print_spool {
264   $main::lxdebug->enter_sub();
265
266   my ($self, $myconfig, $form, $spool, $output) = @_;
267
268   # connect to database
269   my $dbh = $form->dbconnect($myconfig);
270
271   my $query =
272     qq|UPDATE status SET printed = '1' | .
273     qq|WHERE formname = ? AND spoolfile = ?|;
274   my $sth = $dbh->prepare($query) || $form->dberror($query);
275
276   foreach my $i (1 .. $form->{rowcount}) {
277     if ($form->{"checked_$i"}) {
278       # $output is safe ( = does not come directly from the browser).
279       open(OUT, $output) or $form->error("$output : $!");
280
281       $form->{"spoolfile_$i"} =~ s|.*/||;
282       $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
283
284       # send file to printer
285       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
286
287       while (<IN>) {
288         print OUT $_;
289       }
290       close(IN);
291       close(OUT);
292
293       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
294         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
295       $sth->finish;
296
297     }
298   }
299
300   $dbh->disconnect;
301
302   $main::lxdebug->leave_sub();
303 }
304
305 1;
306