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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form) = @_;
47 my $dbh = $form->dbconnect($myconfig);
49 my %arap = (invoice => 'ar',
52 purchase_order => 'oe',
53 sales_quotation => 'oe',
54 request_quotation => 'oe',
58 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
59 my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
62 qq|SELECT count(*) | .
63 qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | .
64 qq| WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
65 qq| AND s.spoolfile IS NOT NULL) AS total|;
67 my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
69 # build selection list
70 if ($count < $myconfig->{vclimit}) {
72 qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name | .
73 qq|FROM $vc vc, $arap_type a, status s | .
74 qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
75 qq| AND s.spoolfile IS NOT NULL|;
77 my $sth = $dbh->prepare($query);
78 $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
80 $form->{"all_${vc}"} = [];
81 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
82 push @{ $form->{"all_${vc}"} }, $ref;
89 $main::lxdebug->leave_sub();
92 sub payment_accounts {
93 $main::lxdebug->enter_sub();
95 my ($self, $myconfig, $form) = @_;
98 my $dbh = $form->dbconnect($myconfig);
101 qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
102 qq|FROM status s, chart c | .
103 qq|WHERE s.chart_id = c.id AND s.formname = ?|;
104 my $sth = $dbh->prepare($query);
105 $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
107 $form->{accounts} = [];
108 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
109 push @{ $form->{accounts} }, $ref;
115 $main::lxdebug->leave_sub();
119 $main::lxdebug->enter_sub();
121 my ($self, $myconfig, $form) = @_;
123 # connect to database
124 my $dbh = $form->dbconnect($myconfig);
126 my ($query, $arap, @values);
127 my $invnumber = "invnumber";
129 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
131 if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
133 $arap = ($form->{type} eq 'check') ? "ap" : "ar";
134 my ($accno) = split /--/, $form->{account};
137 qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
138 qq| a.invoice, '$arap' AS module | .
139 qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
140 qq|WHERE s.formname = ? | .
141 qq| AND s.chart_id = c.id | .
142 qq| AND c.accno = ? | .
143 qq| AND s.trans_id = a.id | .
144 qq| AND a.${vc}_id = vc.id | .
145 qq| AND ac.trans_id = s.trans_id | .
146 qq| AND ac.chart_id = c.id | .
147 qq| AND NOT ac.fx_transaction|;
148 @values = ($form->{type}, $accno);
152 my $invoice = "a.invoice";
154 if ($form->{type} =~ /_(order|quotation)$/) {
155 $invnumber = "ordnumber";
161 qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | .
162 qq| a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
164 qq|FROM $arap a, ${vc} vc, status s | .
165 qq|WHERE s.trans_id = a.id | .
166 qq| AND s.spoolfile IS NOT NULL | .
167 qq| AND s.formname = ? | .
168 qq| AND a.${vc}_id = vc.id|;
169 @values = ($form->{type});
172 if ($form->{"${vc}_id"}) {
173 $query .= qq| AND a.${vc}_id = ?|;
174 push(@values, conv_i($form->{"${vc}_id"}));
175 } elsif ($form->{ $vc }) {
176 $query .= " AND vc.name ILIKE ?";
177 push(@values, $form->like($form->{ $vc }));
179 foreach my $column (qw(invnumber ordnumber quonumber)) {
180 if ($form->{$column}) {
181 $query .= " AND a.$column ILIKE ?";
182 push(@values, $form->like($form->{$column}));
186 if ($form->{type} =~ /(invoice|sales_order|sales_quotation|packing_list|puchase_order|request_quotation)$/) {
187 if ($form->{transdatefrom}) {
188 $query .= " AND a.transdate >= ?";
189 push(@values, $form->{transdatefrom});
191 if ($form->{transdateto}) {
192 $query .= " AND a.transdate <= ?";
193 push(@values, $form->{transdateto});
197 my @a = ("transdate", $invnumber, "name");
198 my $sortorder = join ', ', $form->sort_columns(@a);
200 if (grep({ $_ eq $form->{sort} }
201 qw(transdate invnumber ordnumber quonumber name))) {
202 $sortorder = $form->{sort};
205 $query .= " ORDER BY $sortorder";
207 my $sth = $dbh->prepare($query);
208 $sth->execute(@values) ||
209 $form->dberror($query . " (" . join(", ", @values) . ")");
212 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
213 push @{ $form->{SPOOL} }, $ref;
219 $main::lxdebug->leave_sub();
223 $main::lxdebug->enter_sub();
225 my ($self, $myconfig, $form, $spool) = @_;
227 # connect to database, turn AutoCommit off
228 my $dbh = $form->dbconnect_noauto($myconfig);
232 if ($form->{type} =~ /(check|receipt)/) {
233 $query = qq|DELETE FROM status WHERE spoolfile = ?|;
236 qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
237 qq|WHERE spoolfile = ?|;
239 my $sth = $dbh->prepare($query) || $form->dberror($query);
241 foreach my $i (1 .. $form->{rowcount}) {
242 if ($form->{"checked_$i"}) {
243 $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
249 my $rc = $dbh->commit;
253 foreach my $i (1 .. $form->{rowcount}) {
254 if ($form->{"checked_$i"}) {
255 unlink(qq|$spool/$form->{"spoolfile_$i"}|);
260 $main::lxdebug->leave_sub();
266 $main::lxdebug->enter_sub();
268 my ($self, $myconfig, $form, $spool, $output) = @_;
270 # connect to database
271 my $dbh = $form->dbconnect($myconfig);
274 qq|UPDATE status SET printed = '1' | .
275 qq|WHERE formname = ? AND spoolfile = ?|;
276 my $sth = $dbh->prepare($query) || $form->dberror($query);
278 foreach my $i (1 .. $form->{rowcount}) {
279 if ($form->{"checked_$i"}) {
280 # $output is safe ( = does not come directly from the browser).
281 open(OUT, $output) or $form->error("$output : $!");
283 $form->{"spoolfile_$i"} =~ s|.*/||;
284 my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
286 # send file to printer
287 open(IN, $spoolfile) or $form->error("$spoolfile : $!");
295 $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
296 $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
304 $main::lxdebug->leave_sub();