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 $main::lxdebug->message(0, "kuh1 $query");
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 $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;
86 $main::lxdebug->message(0, "kuh2 $query");
91 $main::lxdebug->leave_sub();
94 sub payment_accounts {
95 $main::lxdebug->enter_sub();
97 my ($self, $myconfig, $form) = @_;
100 my $dbh = $form->dbconnect($myconfig);
103 qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
104 qq|FROM status s, chart c | .
105 qq|WHERE s.chart_id = c.id AND s.formname = ?|;
106 my $sth = $dbh->prepare($query);
107 $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
109 $form->{accounts} = [];
110 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
111 push @{ $form->{accounts} }, $ref;
117 $main::lxdebug->leave_sub();
121 $main::lxdebug->enter_sub();
123 my ($self, $myconfig, $form) = @_;
125 # connect to database
126 my $dbh = $form->dbconnect($myconfig);
128 my ($query, $arap, @values);
129 my $invnumber = "invnumber";
131 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
133 if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
135 $arap = ($form->{type} eq 'check') ? "ap" : "ar";
136 my ($accno) = split /--/, $form->{account};
139 qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
140 qq| a.invoice, '$arap' AS module | .
141 qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
142 qq|WHERE s.formname = ? | .
143 qq| AND s.chart_id = c.id | .
144 qq| AND c.accno = ? | .
145 qq| AND s.trans_id = a.id | .
146 qq| AND a.${vc}_id = vc.id | .
147 qq| AND ac.trans_id = s.trans_id | .
148 qq| AND ac.chart_id = c.id | .
149 qq| AND NOT ac.fx_transaction|;
150 @values = ($form->{type}, $accno);
154 my $invoice = "a.invoice";
156 if ($form->{type} =~ /_(order|quotation)$/) {
157 $invnumber = "ordnumber";
163 qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | .
164 qq| a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
166 qq|FROM $arap a, ${vc} vc, status s | .
167 qq|WHERE s.trans_id = a.id | .
168 qq| AND s.spoolfile IS NOT NULL | .
169 qq| AND s.formname = ? | .
170 qq| AND a.${vc}_id = vc.id|;
171 @values = ($form->{type});
174 if ($form->{"${vc}_id"}) {
175 $query .= qq| AND a.${vc}_id = ?|;
176 push(@values, conv_i($form->{"${vc}_id"}));
177 } elsif ($form->{ $vc }) {
178 $query .= " AND vc.name ILIKE ?";
179 push(@values, $form->like($form->{ $vc }));
181 foreach my $column (qw(invnumber ordnumber quonumber)) {
182 if ($form->{$column}) {
183 $query .= " AND a.$column ILIKE ?";
184 push(@values, $form->like($form->{$column}));
188 if ($form->{type} =~ /(invoice|sales_order|sales_quotation|packing_list|puchase_order|request_quotation)$/) {
189 if ($form->{transdatefrom}) {
190 $query .= " AND a.transdate >= ?";
191 push(@values, $form->{transdatefrom});
193 if ($form->{transdateto}) {
194 $query .= " AND a.transdate <= ?";
195 push(@values, $form->{transdateto});
199 my @a = (transdate, $invnumber, name);
200 my $sortorder = join ', ', $form->sort_columns(@a);
202 if (grep({ $_ eq $form->{sort} }
203 qw(transdate invnumber ordnumber quonumber name))) {
204 $sortorder = $form->{sort};
207 $query .= " ORDER BY $sortorder";
209 my $sth = $dbh->prepare($query);
210 $sth->execute(@values) ||
211 $form->dberror($query . " (" . join(", ", @values) . ")");
214 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
215 push @{ $form->{SPOOL} }, $ref;
221 $main::lxdebug->leave_sub();
225 $main::lxdebug->enter_sub();
227 my ($self, $myconfig, $form, $spool) = @_;
229 # connect to database, turn AutoCommit off
230 my $dbh = $form->dbconnect_noauto($myconfig);
234 if ($form->{type} =~ /(check|receipt)/) {
235 $query = qq|DELETE FROM status WHERE spoolfile = ?|;
238 qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
239 qq|WHERE spoolfile = ?|;
241 my $sth = $dbh->prepare($query) || $form->dberror($query);
243 foreach my $i (1 .. $form->{rowcount}) {
244 if ($form->{"checked_$i"}) {
245 $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
251 my $rc = $dbh->commit;
255 foreach my $i (1 .. $form->{rowcount}) {
256 if ($form->{"checked_$i"}) {
257 unlink(qq|$spool/$form->{"spoolfile_$i"}|);
262 $main::lxdebug->leave_sub();
268 $main::lxdebug->enter_sub();
270 my ($self, $myconfig, $form, $spool) = @_;
272 # connect to database
273 my $dbh = $form->dbconnect($myconfig);
276 qq|UPDATE status SET printed = '1' | .
277 qq|WHERE formname = ? AND spoolfile = ?|;
278 my $sth = $dbh->prepare($query) || $form->dberror($query);
280 foreach my $i (1 .. $form->{rowcount}) {
281 if ($form->{"checked_$i"}) {
282 open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!");
284 $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();