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