Merge branch 'master' of git@lx-office.linet-services.de:lx-office-erp
[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
153     if ($form->{type} =~ /_(order|quotation)$/) {
154       $invnumber = "ordnumber";
155       $arap      = "oe";
156       $invoice   = '0';
157     }
158
159     $query =
160       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | .
161       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
162       qq|  s.spoolfile | .
163       qq|FROM $arap a, ${vc} vc, status s | .
164       qq|WHERE s.trans_id = a.id | .
165       qq|  AND s.spoolfile IS NOT NULL | .
166       qq|  AND s.formname = ? | .
167       qq|  AND a.${vc}_id = vc.id|;
168     @values = ($form->{type});
169   }
170
171   if ($form->{"${vc}_id"}) {
172     $query .= qq| AND a.${vc}_id = ?|;
173     push(@values, conv_i($form->{"${vc}_id"}));
174   } elsif ($form->{ $vc }) {
175     $query .= " AND vc.name ILIKE ?";
176     push(@values, $form->like($form->{ $vc }));
177   }
178   foreach my $column (qw(invnumber ordnumber quonumber)) {
179     if ($form->{$column}) {
180       $query .= " AND a.$column ILIKE ?";
181       push(@values, $form->like($form->{$column}));
182     }
183   }
184
185   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|puchase_order|request_quotation)$/) {
186     if ($form->{transdatefrom}) {
187       $query .= " AND a.transdate >= ?";
188       push(@values, $form->{transdatefrom});
189     }
190     if ($form->{transdateto}) {
191       $query .= " AND a.transdate <= ?";
192       push(@values, $form->{transdateto});
193     }
194   }
195
196   my @a = ("transdate", $invnumber, "name");
197   my $sortorder = join ', ', $form->sort_columns(@a);
198
199   if (grep({ $_ eq $form->{sort} }
200            qw(transdate invnumber ordnumber quonumber name))) {
201     $sortorder = $form->{sort};
202   }
203
204   $query .= " ORDER BY $sortorder";
205
206   my $sth = $dbh->prepare($query);
207   $sth->execute(@values) ||
208     $form->dberror($query . " (" . join(", ", @values) . ")");
209
210   $form->{SPOOL} = [];
211   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
212     push @{ $form->{SPOOL} }, $ref;
213   }
214
215   $sth->finish;
216   $dbh->disconnect;
217
218   $main::lxdebug->leave_sub();
219 }
220
221 sub delete_spool {
222   $main::lxdebug->enter_sub();
223
224   my ($self, $myconfig, $form) = @_;
225
226   my $spool = $::lx_office_conf{paths}->{spool};
227
228   # connect to database, turn AutoCommit off
229   my $dbh = $form->dbconnect_noauto($myconfig);
230
231   my $query;
232
233   if ($form->{type} =~ /(check|receipt)/) {
234     $query = qq|DELETE FROM status WHERE spoolfile = ?|;
235   } else {
236     $query =
237       qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
238       qq|WHERE spoolfile = ?|;
239   }
240   my $sth = $dbh->prepare($query) || $form->dberror($query);
241
242   foreach my $i (1 .. $form->{rowcount}) {
243     if ($form->{"checked_$i"}) {
244       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
245       $sth->finish;
246     }
247   }
248
249   # commit
250   my $rc = $dbh->commit;
251   $dbh->disconnect;
252
253   if ($rc) {
254     foreach my $i (1 .. $form->{rowcount}) {
255       if ($form->{"checked_$i"}) {
256         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
257       }
258     }
259   }
260
261   $main::lxdebug->leave_sub();
262
263   return $rc;
264 }
265
266 sub print_spool {
267   $main::lxdebug->enter_sub();
268
269   my ($self, $myconfig, $form, $output) = @_;
270
271   my $spool = $::lx_office_conf{paths}->{spool};
272
273   # connect to database
274   my $dbh = $form->dbconnect($myconfig);
275
276   my $query =
277     qq|UPDATE status SET printed = '1' | .
278     qq|WHERE formname = ? AND spoolfile = ?|;
279   my $sth = $dbh->prepare($query) || $form->dberror($query);
280
281   foreach my $i (1 .. $form->{rowcount}) {
282     if ($form->{"checked_$i"}) {
283       # $output is safe ( = does not come directly from the browser).
284       open(OUT, $output) or $form->error("$output : $!");
285
286       $form->{"spoolfile_$i"} =~ s|.*/||;
287       my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
288
289       # send file to printer
290       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
291
292       while (<IN>) {
293         print OUT $_;
294       }
295       close(IN);
296       close(OUT);
297
298       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
299         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
300       $sth->finish;
301
302     }
303   }
304
305   $dbh->disconnect;
306
307   $main::lxdebug->leave_sub();
308 }
309
310 1;
311