1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
31 # Batch printing module backend routines
33 #======================================================================
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form) = @_;
45 my $dbh = $form->dbconnect($myconfig);
47 my %arap = (invoice => 'ar',
50 purchase_order => 'oe',
51 sales_quotation => 'oe',
52 request_quotation => 'oe',
56 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
57 my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
60 qq|SELECT count(*) | .
61 qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | .
62 qq| WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
63 qq| AND s.spoolfile IS NOT NULL) AS total|;
65 my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
67 # build selection list
68 if ($count < $myconfig->{vclimit}) {
70 qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name | .
71 qq|FROM $vc vc, $arap_type a, status s | .
72 qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
73 qq| AND s.spoolfile IS NOT NULL|;
75 $sth = $dbh->prepare($query);
76 $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
78 $form->{"all_${vc}"} = [];
79 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
80 push @{ $form->{"all_${vc}"} }, $ref;
87 $main::lxdebug->leave_sub();
90 sub payment_accounts {
91 $main::lxdebug->enter_sub();
93 my ($self, $myconfig, $form) = @_;
96 my $dbh = $form->dbconnect($myconfig);
99 qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
100 qq|FROM status s, chart c | .
101 qq|WHERE s.chart_id = c.id AND s.formname = ?|;
102 my $sth = $dbh->prepare($query);
103 $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
105 $form->{accounts} = [];
106 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
107 push @{ $form->{accounts} }, $ref;
113 $main::lxdebug->leave_sub();
117 $main::lxdebug->enter_sub();
119 my ($self, $myconfig, $form) = @_;
121 # connect to database
122 my $dbh = $form->dbconnect($myconfig);
124 my ($query, $arap, @values);
125 my $invnumber = "invnumber";
127 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
129 if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
131 $arap = ($form->{type} eq 'check') ? "ap" : "ar";
132 my ($accno) = split /--/, $form->{account};
135 qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
136 qq| a.invoice, '$arap' AS module | .
137 qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
138 qq|WHERE s.formname = ? | .
139 qq| AND s.chart_id = c.id | .
140 qq| AND c.accno = ? | .
141 qq| AND s.trans_id = a.id | .
142 qq| AND a.${vc}_id = vc.id | .
143 qq| AND ac.trans_id = s.trans_id | .
144 qq| AND ac.chart_id = c.id | .
145 qq| AND NOT ac.fx_transaction|;
146 @values = ($form->{type}, $accno);
150 my $invoice = "a.invoice";
152 if ($form->{type} =~ /_(order|quotation)$/) {
153 $invnumber = "ordnumber";
159 qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | .
160 qq| a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
162 qq|FROM $arap a, ${vc} vc, status s | .
163 qq|WHERE s.trans_id = a.id | .
164 qq| AND s.spoolfile IS NOT NULL | .
165 qq| AND s.formname = ? | .
166 qq| AND a.${vc}_id = vc.id|;
167 @values = ($form->{type});
170 if ($form->{"${vc}_id"}) {
171 $query .= qq| AND a.${vc}_id = ?|;
172 push(@values, conv_i($form->{"${vc}_id"}));
173 } elsif ($form->{ $vc }) {
174 $query .= " AND vc.name ILIKE ?";
175 push(@values, $form->like($form->{ $vc }));
177 foreach my $column (qw(invnumber ordnumber quonumber)) {
178 if ($form->{$column}) {
179 $query .= " AND a.$column ILIKE ?";
180 push(@values, $form->like($form->{$column}));
184 if ($form->{type} =~ /(invoice|sales_order|sales_quotation|packing_list|puchase_order|request_quotation)$/) {
185 if ($form->{transdatefrom}) {
186 $query .= " AND a.transdate >= ?";
187 push(@values, $form->{transdatefrom});
189 if ($form->{transdateto}) {
190 $query .= " AND a.transdate <= ?";
191 push(@values, $form->{transdateto});
195 my @a = (transdate, $invnumber, name);
196 my $sortorder = join ', ', $form->sort_columns(@a);
198 if (grep({ $_ eq $form->{sort} }
199 qw(transdate invnumber ordnumber quonumber name))) {
200 $sortorder = $form->{sort};
203 $query .= " ORDER BY $sortorder";
205 my $sth = $dbh->prepare($query);
206 $sth->execute(@values) ||
207 $form->dberror($query . " (" . join(", ", @values) . ")");
210 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
211 push @{ $form->{SPOOL} }, $ref;
217 $main::lxdebug->leave_sub();
221 $main::lxdebug->enter_sub();
223 my ($self, $myconfig, $form, $spool) = @_;
225 # connect to database, turn AutoCommit off
226 my $dbh = $form->dbconnect_noauto($myconfig);
230 if ($form->{type} =~ /(check|receipt)/) {
231 $query = qq|DELETE FROM status WHERE spoolfile = ?|;
234 qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
235 qq|WHERE spoolfile = ?|;
237 my $sth = $dbh->prepare($query) || $form->dberror($query);
239 foreach my $i (1 .. $form->{rowcount}) {
240 if ($form->{"checked_$i"}) {
241 $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
247 my $rc = $dbh->commit;
251 foreach my $i (1 .. $form->{rowcount}) {
252 if ($form->{"checked_$i"}) {
253 unlink(qq|$spool/$form->{"spoolfile_$i"}|);
258 $main::lxdebug->leave_sub();
264 $main::lxdebug->enter_sub();
266 my ($self, $myconfig, $form, $spool, $output) = @_;
268 # connect to database
269 my $dbh = $form->dbconnect($myconfig);
272 qq|UPDATE status SET printed = '1' | .
273 qq|WHERE formname = ? AND spoolfile = ?|;
274 my $sth = $dbh->prepare($query) || $form->dberror($query);
276 foreach my $i (1 .. $form->{rowcount}) {
277 if ($form->{"checked_$i"}) {
278 # $output is safe ( = does not come directly from the browser).
279 open(OUT, $output) or $form->error("$output : $!");
281 $form->{"spoolfile_$i"} =~ s|.*/||;
282 $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
284 # send file to printer
285 open(IN, $spoolfile) or $form->error("$spoolfile : $!");
293 $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
294 $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
302 $main::lxdebug->leave_sub();