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