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 #======================================================================
43 $main::lxdebug->enter_sub();
45 my ($self, $myconfig, $form) = @_;
48 my $dbh = $form->dbconnect($myconfig);
50 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;
87 $main::lxdebug->leave_sub();
90 sub payment_accounts {
91 $main::lxdebug->enter_sub();
93 my ($self, $myconfig, $form) = @_;
96 my $dbh = SL::DB->client->dbh;
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;
112 $main::lxdebug->leave_sub();
116 $main::lxdebug->enter_sub();
118 my ($self, $myconfig, $form) = @_;
120 my $dbh = SL::DB->client->dbh;
122 my ($query, $arap, @values);
123 my $invnumber = "invnumber";
125 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
127 if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
129 $arap = ($form->{type} eq 'check') ? "ap" : "ar";
130 my ($accno) = split /--/, $form->{account};
133 qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
134 qq| a.invoice, '$arap' AS module | .
135 qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
136 qq|WHERE s.formname = ? | .
137 qq| AND s.chart_id = c.id | .
138 qq| AND c.accno = ? | .
139 qq| AND s.trans_id = a.id | .
140 qq| AND a.${vc}_id = vc.id | .
141 qq| AND ac.trans_id = s.trans_id | .
142 qq| AND ac.chart_id = c.id | .
143 qq| AND NOT ac.fx_transaction|;
144 @values = ($form->{type}, $accno);
148 my $invoice = "a.invoice";
149 my $quonumber = "a.quonumber";
151 if ($form->{type} =~ /_(order|quotation)$/) {
152 $invnumber = "ordnumber";
157 if ($form->{type} eq 'packing_list') {
158 $invnumber = "donumber";
159 $arap = "delivery_orders";
165 qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | .
166 qq| a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
168 qq|FROM $arap a, ${vc} vc, status s | .
169 qq|WHERE s.trans_id = a.id | .
170 qq| AND s.spoolfile IS NOT NULL | .
171 ($form->{type} eq 'packing_list'
172 ? qq| AND s.formname IN (?, ?) |
173 : qq| AND s.formname = ? |) .
174 qq| AND a.${vc}_id = vc.id|;
175 @values = ($form->{type});
177 if ($form->{type} eq 'packing_list') {
178 @values = qw(sales_delivery_order purchase_delivery_order);
182 if ($form->{"${vc}_id"}) {
183 $query .= qq| AND a.${vc}_id = ?|;
184 push(@values, conv_i($form->{"${vc}_id"}));
185 } elsif ($form->{ $vc }) {
186 $query .= " AND vc.name ILIKE ?";
187 push(@values, like($form->{ $vc }));
189 foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
190 if ($form->{$column}) {
191 $query .= " AND a.$column ILIKE ?";
192 push(@values, like($form->{$column}));
196 if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) {
197 if ($form->{transdatefrom}) {
198 $query .= " AND a.transdate >= ?";
199 push(@values, $form->{transdatefrom});
201 if ($form->{transdateto}) {
202 $query .= " AND a.transdate <= ?";
203 push(@values, $form->{transdateto});
207 my @a = ("transdate", $invnumber, "name");
208 my $sortorder = join ', ', $form->sort_columns(@a);
210 if (grep({ $_ eq $form->{sort} }
211 qw(transdate invnumber ordnumber quonumber donumber name))) {
212 $sortorder = $form->{sort};
215 $query .= " ORDER BY $sortorder";
217 my $sth = $dbh->prepare($query);
218 $sth->execute(@values) ||
219 $form->dberror($query . " (" . join(", ", @values) . ")");
222 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
223 push @{ $form->{SPOOL} }, $ref;
228 $main::lxdebug->leave_sub();
232 $main::lxdebug->enter_sub();
234 my ($self, $myconfig, $form) = @_;
236 my $spool = $::lx_office_conf{paths}->{spool};
238 SL::DB->client->with_transaction(sub {
239 my $dbh = SL::DB->client->dbh;
243 if ($form->{type} =~ /(check|receipt)/) {
244 $query = qq|DELETE FROM status WHERE spoolfile = ?|;
247 qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
248 qq|WHERE spoolfile = ?|;
250 my $sth = $dbh->prepare($query) || $form->dberror($query);
252 foreach my $i (1 .. $form->{rowcount}) {
253 if ($form->{"checked_$i"}) {
254 $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
259 foreach my $i (1 .. $form->{rowcount}) {
260 if ($form->{"checked_$i"}) {
261 unlink(qq|$spool/$form->{"spoolfile_$i"}|);
265 }) or do { die SL::DB->client->error };
267 $main::lxdebug->leave_sub();
272 $main::lxdebug->enter_sub();
274 my ($self, $myconfig, $form, $output) = @_;
276 my $spool = $::lx_office_conf{paths}->{spool};
278 # connect to database
279 my $dbh = SL::DB->client->dbh;
282 qq|UPDATE status SET printed = '1' | .
283 qq|WHERE formname = ? AND spoolfile = ?|;
284 my $sth = $dbh->prepare($query) || $form->dberror($query);
286 foreach my $i (1 .. $form->{rowcount}) {
287 if ($form->{"checked_$i"}) {
288 # $output is safe ( = does not come directly from the browser).
289 open(OUT, $output) or $form->error("$output : $!");
291 $form->{"spoolfile_$i"} =~ s|.*/||;
292 my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
294 # send file to printer
295 open(IN, $spoolfile) or $form->error("$spoolfile : $!");
303 $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
304 $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
310 $main::lxdebug->leave_sub();