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