so Search.tsf and Search.rdf work
[freeside.git] / sql-ledger / SL / BP.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2003
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # Batch printing module backend routines
26 #
27 #======================================================================
28
29 package BP;
30
31
32 sub get_vc {
33   my ($self, $myconfig, $form) = @_;
34
35   # connect to database
36   my $dbh = $form->dbconnect($myconfig);
37   
38   my %arap = ( invoice => ['ar'],
39                packing_list => ['oe', 'ar'],
40                sales_order => ['oe'],
41                work_order => ['oe'],
42                pick_list => ['oe', 'ar'],
43                purchase_order => ['oe'],
44                bin_list => ['oe'],
45                sales_quotation => ['oe'],
46                request_quotation => ['oe'],
47                check => ['ap'],
48                receipt => ['ar']
49              );
50   
51   my $query = "";
52   my $sth;
53   my $n;
54   my $count;
55   my $item;
56   
57   foreach $item (@{ $arap{$form->{type}} }) {
58     $query = qq|
59               SELECT count(*)
60               FROM (SELECT DISTINCT vc.id
61                     FROM $form->{vc} vc, $item a, status s
62                     WHERE a.$form->{vc}_id = vc.id
63                     AND s.trans_id = a.id
64                     AND s.formname = '$form->{type}'
65                     AND s.spoolfile IS NOT NULL) AS total|;
66     ($n) = $dbh->selectrow_array($query);
67     $count += $n;
68   }
69
70
71   # build selection list
72   my $union = "";
73   $query = "";
74   if ($count < $myconfig->{vclimit}) {
75     foreach $item (@{ $arap{$form->{type}} }) {
76       $query .= qq|
77                   $union
78                   SELECT DISTINCT vc.id, vc.name
79                   FROM $form->{vc} vc, $item a, status s
80                   WHERE a.$form->{vc}_id = vc.id
81                   AND s.trans_id = a.id
82                   AND s.formname = '$form->{type}'
83                   AND s.spoolfile IS NOT NULL|;
84       $union = "UNION";
85     }
86     
87     $sth = $dbh->prepare($query);
88     $sth->execute || $form->dberror($query);
89
90     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
91       push @{ $form->{"all_$form->{vc}"} }, $ref;
92     }
93     $sth->finish;
94   }
95
96   $form->all_years($dbh, $myconfig);
97
98   $dbh->disconnect;
99  
100 }
101                  
102   
103
104 sub payment_accounts {
105   my ($self, $myconfig, $form) = @_;
106   
107   # connect to database
108   my $dbh = $form->dbconnect($myconfig);
109
110   my $query = qq|SELECT DISTINCT c.accno, c.description
111                  FROM status s, chart c
112                  WHERE s.chart_id = c.id
113                  AND s.formname = '$form->{type}'|;
114   my $sth = $dbh->prepare($query);
115   $sth->execute || $form->dberror($query);
116
117   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
118     push @{ $form->{accounts} }, $ref;
119   }
120   
121   $sth->finish;
122   $dbh->disconnect;
123   
124 }
125
126  
127 sub get_spoolfiles {
128   my ($self, $myconfig, $form) = @_;
129
130   # connect to database
131   my $dbh = $form->dbconnect($myconfig);
132
133   my $query;
134   my $invnumber = "invnumber";
135   my $item;
136   
137   my %arap = ( invoice => ['ar'],
138                packing_list => ['oe', 'ar'],
139                sales_order => ['oe'],
140                work_order => ['oe'],
141                pick_list => ['oe', 'ar'],
142                purchase_order => ['oe'],
143                bin_list => ['oe'],
144                sales_quotation => ['oe'],
145                request_quotation => ['oe'],
146                check => ['ap'],
147                receipt => ['ar']
148              );
149   
150
151   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
152     
153     my ($accno) = split /--/, $form->{account};
154     
155     $query = qq|SELECT a.id, vc.name, a.invnumber, ac.transdate, s.spoolfile,
156                 a.invoice, '$arap{$form->{type}}[0]' AS module
157                 FROM acc_trans ac
158                 JOIN chart c ON (c.id = ac.chart_id)
159                 JOIN $arap{$form->{type}}[0] a ON (a.id = ac.trans_id)
160                 JOIN status s ON (s.trans_id = a.id)
161                 JOIN $form->{vc} vc ON (vc.id = a.$form->{vc}_id)
162                 WHERE s.formname = '$form->{type}'
163                 AND c.accno = '$accno'
164                 AND NOT ac.fx_transaction|;
165
166       if ($form->{"$form->{vc}_id"}) {
167         $query .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
168       } else {
169         if ($form->{$form->{vc}}) {
170           $item = $form->like(lc $form->{$form->{vc}});
171           $query .= " AND lower(vc.name) LIKE '$item'";
172         }
173       }
174       if ($form->{invnumber}) {
175         $item = $form->like(lc $form->{invnumber});
176         $query .= " AND lower(a.invnumber) LIKE '$item'";
177       }
178       if ($form->{ordnumber}) {
179         $item = $form->like(lc $form->{ordnumber});
180         $query .= " AND lower(a.ordnumber) LIKE '$item'";
181       }
182       if ($form->{quonumber}) {
183         $item = $form->like(lc $form->{quonumber});
184         $query .= " AND lower(a.quonumber) LIKE '$item'";
185       }
186
187       $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
188       $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
189
190
191   } else {
192     
193     foreach $item (@{ $arap{$form->{type}} }) {
194       
195       $invoice = "a.invoice";
196       $invnumber = "invnumber";
197       
198       if ($item eq 'oe') {
199         $invnumber = "ordnumber";
200         $invoice = "'0'"; 
201       }
202       
203       $query .= qq|
204                 $union
205                 SELECT a.id, vc.name, a.$invnumber AS invnumber, a.transdate,
206                 a.ordnumber, a.quonumber, $invoice AS invoice,
207                 '$item' AS module, s.spoolfile
208                 FROM $item a, $form->{vc} vc, status s
209                 WHERE s.trans_id = a.id
210                 AND s.spoolfile IS NOT NULL
211                 AND s.formname = '$form->{type}'
212                 AND a.$form->{vc}_id = vc.id|;
213
214       if ($form->{"$form->{vc}_id"}) {
215         $query .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
216       } else {
217         if ($form->{$form->{vc}}) {
218           $item = $form->like(lc $form->{$form->{vc}});
219           $query .= " AND lower(vc.name) LIKE '$item'";
220         }
221       }
222       if ($form->{invnumber}) {
223         $item = $form->like(lc $form->{invnumber});
224         $query .= " AND lower(a.invnumber) LIKE '$item'";
225       }
226       if ($form->{ordnumber}) {
227         $item = $form->like(lc $form->{ordnumber});
228         $query .= " AND lower(a.ordnumber) LIKE '$item'";
229       }
230       if ($form->{quonumber}) {
231         $item = $form->like(lc $form->{quonumber});
232         $query .= " AND lower(a.quonumber) LIKE '$item'";
233       }
234
235       $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
236       $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
237
238       $union = "UNION";
239
240     }
241   }
242
243   my %ordinal = ( 'name' => 2,
244                   'invnumber' => 3,
245                   'transdate' => 4,
246                   'ordnumber' => 5,
247                   'quonumber' => 6
248                 );
249   my @a = (transdate, $invnumber, name);
250   my $sortorder = $form->sort_order(\@a, \%ordinal);
251  
252   $query .= " ORDER by $sortorder";
253
254   my $sth = $dbh->prepare($query);
255   $sth->execute || $form->dberror($query);
256
257   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
258     push @{ $form->{SPOOL} }, $ref;
259   }
260   
261   $sth->finish;
262   $dbh->disconnect;
263
264 }
265
266
267 sub delete_spool {
268   my ($self, $myconfig, $form, $spool) = @_;
269
270   # connect to database, turn AutoCommit off
271   my $dbh = $form->dbconnect_noauto($myconfig);
272
273   my $query;
274   my %audittrail;
275   
276   if ($form->{type} =~ /(check|receipt)/) {
277     $query = qq|DELETE FROM status
278                 WHERE spoolfile = ?|;
279   } else {
280     $query = qq|UPDATE status SET
281                  spoolfile = NULL,
282                  printed = '1'
283                  WHERE spoolfile = ?|;
284   }
285   my $sth = $dbh->prepare($query) || $form->dberror($query);
286   
287   
288   foreach my $i (1 .. $form->{rowcount}) {
289     if ($form->{"checked_$i"}) {
290       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
291       $sth->finish;
292       
293       %audittrail = ( tablename  => $form->{module},
294                       reference  => $form->{"reference_$i"},
295                       formname   => $form->{type},
296                       action     => 'dequeued',
297                       id         => $form->{"id_$i"} );
298  
299       $form->audittrail($dbh, "", \%audittrail);
300     }
301   }
302     
303   # commit
304   my $rc = $dbh->commit;
305   $dbh->disconnect;
306
307   if ($rc) {
308     foreach my $i (1 .. $form->{rowcount}) {
309       $_ = qq|$spool/$form->{"spoolfile_$i"}|;
310       if ($form->{"checked_$i"}) {
311         unlink;
312       }
313     }
314   }
315
316   $rc;
317   
318 }
319
320
321 sub print_spool {
322   my ($self, $myconfig, $form, $spool) = @_;
323
324   # connect to database
325   my $dbh = $form->dbconnect_noauto($myconfig);
326
327   my %audittrail;
328   
329   my $query = qq|UPDATE status SET
330                  printed = '1'
331                  WHERE formname = '$form->{type}'
332                  AND spoolfile = ?|;
333   my $sth = $dbh->prepare($query) || $form->dberror($query);
334   
335   foreach my $i (1 .. $form->{rowcount}) {
336     if ($form->{"checked_$i"}) {
337       open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!");
338       
339       $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
340       
341       # send file to printer
342       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
343
344       while (<IN>) {
345         print OUT $_;
346       }
347       close(IN);
348       close(OUT);
349
350       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
351       $sth->finish;
352       
353       %audittrail = ( tablename  => $form->{module},
354                       reference  => $form->{"reference_$i"},
355                       formname   => $form->{type},
356                       action     => 'printed',
357                       id         => $form->{"id_$i"} );
358  
359       $form->audittrail($dbh, "", \%audittrail);
360       
361       $dbh->commit;
362     }
363   }
364
365   $dbh->disconnect;
366
367 }
368
369
370 1;
371