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',
51 purchase_order => 'oe',
52 sales_quotation => 'oe',
53 request_quotation => 'oe',
57 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
58 my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
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|;
66 my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
68 # build selection list
69 if ($count < $myconfig->{vclimit}) {
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|;
76 my $sth = $dbh->prepare($query);
77 $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
79 $form->{"all_${vc}"} = [];
80 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
81 push @{ $form->{"all_${vc}"} }, $ref;
88 $main::lxdebug->leave_sub();
91 sub payment_accounts {
92 $main::lxdebug->enter_sub();
94 my ($self, $myconfig, $form) = @_;
97 my $dbh = $form->dbconnect($myconfig);
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})");
106 $form->{accounts} = [];
107 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
108 push @{ $form->{accounts} }, $ref;
114 $main::lxdebug->leave_sub();
118 $main::lxdebug->enter_sub();
120 my ($self, $myconfig, $form) = @_;
122 # connect to database
123 my $dbh = $form->dbconnect($myconfig);
125 my ($query, $arap, @values);
126 my $invnumber = "invnumber";
128 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
130 if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
132 $arap = ($form->{type} eq 'check') ? "ap" : "ar";
133 my ($accno) = split /--/, $form->{account};
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);
151 my $invoice = "a.invoice";
153 if ($form->{type} =~ /_(order|quotation)$/) {
154 $invnumber = "ordnumber";
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, | .
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});
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 }));
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}));
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});
190 if ($form->{transdateto}) {
191 $query .= " AND a.transdate <= ?";
192 push(@values, $form->{transdateto});
196 my @a = ("transdate", $invnumber, "name");
197 my $sortorder = join ', ', $form->sort_columns(@a);
199 if (grep({ $_ eq $form->{sort} }
200 qw(transdate invnumber ordnumber quonumber name))) {
201 $sortorder = $form->{sort};
204 $query .= " ORDER BY $sortorder";
206 my $sth = $dbh->prepare($query);
207 $sth->execute(@values) ||
208 $form->dberror($query . " (" . join(", ", @values) . ")");
211 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
212 push @{ $form->{SPOOL} }, $ref;
218 $main::lxdebug->leave_sub();
222 $main::lxdebug->enter_sub();
224 my ($self, $myconfig, $form) = @_;
226 my $spool = $::lx_office_conf{paths}->{spool};
228 # connect to database, turn AutoCommit off
229 my $dbh = $form->dbconnect_noauto($myconfig);
233 if ($form->{type} =~ /(check|receipt)/) {
234 $query = qq|DELETE FROM status WHERE spoolfile = ?|;
237 qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
238 qq|WHERE spoolfile = ?|;
240 my $sth = $dbh->prepare($query) || $form->dberror($query);
242 foreach my $i (1 .. $form->{rowcount}) {
243 if ($form->{"checked_$i"}) {
244 $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
250 my $rc = $dbh->commit;
254 foreach my $i (1 .. $form->{rowcount}) {
255 if ($form->{"checked_$i"}) {
256 unlink(qq|$spool/$form->{"spoolfile_$i"}|);
261 $main::lxdebug->leave_sub();
267 $main::lxdebug->enter_sub();
269 my ($self, $myconfig, $form, $output) = @_;
271 my $spool = $::lx_office_conf{paths}->{spool};
273 # connect to database
274 my $dbh = $form->dbconnect($myconfig);
277 qq|UPDATE status SET printed = '1' | .
278 qq|WHERE formname = ? AND spoolfile = ?|;
279 my $sth = $dbh->prepare($query) || $form->dberror($query);
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 : $!");
286 $form->{"spoolfile_$i"} =~ s|.*/||;
287 my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
289 # send file to printer
290 open(IN, $spoolfile) or $form->error("$spoolfile : $!");
298 $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
299 $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
307 $main::lxdebug->leave_sub();