SL:: single-dbh und disconnects
[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 use SL::DB;
39
40 use strict;
41
42 sub get_vc {
43   $main::lxdebug->enter_sub();
44
45   my ($self, $myconfig, $form) = @_;
46
47   # connect to database
48   my $dbh = $form->dbconnect($myconfig);
49
50   my %arap = (invoice           => 'ar',
51               sales_order       => 'oe',
52               purchase_order    => 'oe',
53               sales_quotation   => 'oe',
54               request_quotation => 'oe',
55               check             => 'ap',
56               receipt           => 'ar');
57
58   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
59   my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
60
61   my $query =
62     qq|SELECT count(*) | .
63     qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | .
64     qq|  WHERE a.${vc}_id = vc.id  AND s.trans_id = a.id AND s.formname = ? | .
65     qq|    AND s.spoolfile IS NOT NULL) AS total|;
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     my $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
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 = SL::DB->client->dbh;
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
112   $main::lxdebug->leave_sub();
113 }
114
115 sub get_spoolfiles {
116   $main::lxdebug->enter_sub();
117
118   my ($self, $myconfig, $form) = @_;
119
120   my $dbh = SL::DB->client->dbh;
121
122   my ($query, $arap, @values);
123   my $invnumber = "invnumber";
124
125   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
126
127   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
128
129     $arap = ($form->{type} eq 'check') ? "ap" : "ar";
130     my ($accno) = split /--/, $form->{account};
131
132     $query =
133       qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
134       qq|  a.invoice, '$arap' AS module | .
135       qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
136       qq|WHERE s.formname = ? | .
137       qq|  AND s.chart_id = c.id | .
138       qq|  AND c.accno = ? | .
139       qq|  AND s.trans_id = a.id | .
140       qq|  AND a.${vc}_id = vc.id | .
141       qq|  AND ac.trans_id = s.trans_id | .
142       qq|  AND ac.chart_id = c.id | .
143       qq|  AND NOT ac.fx_transaction|;
144     @values = ($form->{type}, $accno);
145
146   } else {
147     $arap = "ar";
148     my $invoice = "a.invoice";
149     my $quonumber = "a.quonumber";
150
151     if ($form->{type} =~ /_(order|quotation)$/) {
152       $invnumber = "ordnumber";
153       $arap      = "oe";
154       $invoice   = '0';
155     }
156
157     if ($form->{type} eq 'packing_list') {
158       $invnumber = "donumber";
159       $arap      = "delivery_orders";
160       $invoice   = '0';
161       $quonumber = '0';
162     }
163
164     $query =
165       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | .
166       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
167       qq|  s.spoolfile | .
168       qq|FROM $arap a, ${vc} vc, status s | .
169       qq|WHERE s.trans_id = a.id | .
170       qq|  AND s.spoolfile IS NOT NULL | .
171     ($form->{type} eq 'packing_list'
172     ? qq|  AND s.formname IN (?, ?) |
173     : qq|  AND s.formname = ? |) .
174       qq|  AND a.${vc}_id = vc.id|;
175     @values = ($form->{type});
176
177     if ($form->{type} eq 'packing_list') {
178       @values = qw(sales_delivery_order purchase_delivery_order);
179     }
180   }
181
182   if ($form->{"${vc}_id"}) {
183     $query .= qq| AND a.${vc}_id = ?|;
184     push(@values, conv_i($form->{"${vc}_id"}));
185   } elsif ($form->{ $vc }) {
186     $query .= " AND vc.name ILIKE ?";
187     push(@values, like($form->{ $vc }));
188   }
189   foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
190     if ($form->{$column}) {
191       $query .= " AND a.$column ILIKE ?";
192       push(@values, like($form->{$column}));
193     }
194   }
195
196   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) {
197     if ($form->{transdatefrom}) {
198       $query .= " AND a.transdate >= ?";
199       push(@values, $form->{transdatefrom});
200     }
201     if ($form->{transdateto}) {
202       $query .= " AND a.transdate <= ?";
203       push(@values, $form->{transdateto});
204     }
205   }
206
207   my @a = ("transdate", $invnumber, "name");
208   my $sortorder = join ', ', $form->sort_columns(@a);
209
210   if (grep({ $_ eq $form->{sort} }
211            qw(transdate invnumber ordnumber quonumber donumber name))) {
212     $sortorder = $form->{sort};
213   }
214
215   $query .= " ORDER BY $sortorder";
216
217   my $sth = $dbh->prepare($query);
218   $sth->execute(@values) ||
219     $form->dberror($query . " (" . join(", ", @values) . ")");
220
221   $form->{SPOOL} = [];
222   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
223     push @{ $form->{SPOOL} }, $ref;
224   }
225
226   $sth->finish;
227
228   $main::lxdebug->leave_sub();
229 }
230
231 sub delete_spool {
232   $main::lxdebug->enter_sub();
233
234   my ($self, $myconfig, $form) = @_;
235
236   my $spool = $::lx_office_conf{paths}->{spool};
237
238   SL::DB->client->with_transaction(sub {
239     my $dbh = SL::DB->client->dbh;
240
241     my $query;
242
243     if ($form->{type} =~ /(check|receipt)/) {
244       $query = qq|DELETE FROM status WHERE spoolfile = ?|;
245     } else {
246       $query =
247         qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
248         qq|WHERE spoolfile = ?|;
249     }
250     my $sth = $dbh->prepare($query) || $form->dberror($query);
251
252     foreach my $i (1 .. $form->{rowcount}) {
253       if ($form->{"checked_$i"}) {
254         $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
255         $sth->finish;
256       }
257     }
258
259     foreach my $i (1 .. $form->{rowcount}) {
260       if ($form->{"checked_$i"}) {
261         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
262       }
263     }
264   });
265
266   $main::lxdebug->leave_sub();
267   return 1;
268 }
269
270 sub print_spool {
271   $main::lxdebug->enter_sub();
272
273   my ($self, $myconfig, $form, $output) = @_;
274
275   my $spool = $::lx_office_conf{paths}->{spool};
276
277   # connect to database
278   my $dbh = SL::DB->client->dbh;
279
280   my $query =
281     qq|UPDATE status SET printed = '1' | .
282     qq|WHERE formname = ? AND spoolfile = ?|;
283   my $sth = $dbh->prepare($query) || $form->dberror($query);
284
285   foreach my $i (1 .. $form->{rowcount}) {
286     if ($form->{"checked_$i"}) {
287       # $output is safe ( = does not come directly from the browser).
288       open(OUT, $output) or $form->error("$output : $!");
289
290       $form->{"spoolfile_$i"} =~ s|.*/||;
291       my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
292
293       # send file to printer
294       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
295
296       while (<IN>) {
297         print OUT $_;
298       }
299       close(IN);
300       close(OUT);
301
302       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
303         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
304       $sth->finish;
305
306     }
307   }
308
309   $main::lxdebug->leave_sub();
310 }
311
312 1;
313