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., 51 Franklin Street, Fifth Floor, Boston,
 
  30 #======================================================================
 
  32 # Batch printing module backend routines
 
  34 #======================================================================
 
  44   $main::lxdebug->enter_sub();
 
  46   my ($self, $myconfig, $form) = @_;
 
  49   my $dbh = $form->dbconnect($myconfig);
 
  51   my %arap = (invoice           => 'ar',
 
  53               purchase_order    => 'oe',
 
  54               sales_quotation   => 'oe',
 
  55               request_quotation => 'oe',
 
  59   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
  60   my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
 
  63     qq|SELECT count(*) | .
 
  64     qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | .
 
  65     qq|  WHERE a.${vc}_id = vc.id  AND s.trans_id = a.id AND s.formname = ? | .
 
  66     qq|    AND s.spoolfile IS NOT NULL) AS total|;
 
  68   my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
 
  70   # build selection list
 
  71   if ($count < $myconfig->{vclimit}) {
 
  73       qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name | .
 
  74       qq|FROM $vc vc, $arap_type a, status s | .
 
  75       qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
 
  76       qq|  AND s.spoolfile IS NOT NULL|;
 
  78     my $sth = $dbh->prepare($query);
 
  79     $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
 
  81     $form->{"all_${vc}"} = [];
 
  82     while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
  83       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 = SL::DB->client->dbh;
 
 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;
 
 113   $main::lxdebug->leave_sub();
 
 117   $main::lxdebug->enter_sub();
 
 119   my ($self, $myconfig, $form) = @_;
 
 121   my $dbh = SL::DB->client->dbh;
 
 123   my ($query, $arap, @values);
 
 124   my $invnumber = "invnumber";
 
 126   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
 128   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
 
 130     $arap = ($form->{type} eq 'check') ? "ap" : "ar";
 
 131     my ($accno) = split /--/, $form->{account};
 
 134       qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
 
 135       qq|  a.invoice, '$arap' AS module | .
 
 136       qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
 
 137       qq|WHERE s.formname = ? | .
 
 138       qq|  AND s.chart_id = c.id | .
 
 139       qq|  AND c.accno = ? | .
 
 140       qq|  AND s.trans_id = a.id | .
 
 141       qq|  AND a.${vc}_id = vc.id | .
 
 142       qq|  AND ac.trans_id = s.trans_id | .
 
 143       qq|  AND ac.chart_id = c.id | .
 
 144       qq|  AND NOT ac.fx_transaction|;
 
 145     @values = ($form->{type}, $accno);
 
 149     my $invoice = "a.invoice";
 
 150     my $quonumber = "a.quonumber";
 
 152     if ($form->{type} =~ /_(order|quotation)$/) {
 
 153       $invnumber = "ordnumber";
 
 158     if ($form->{type} eq 'packing_list') {
 
 159       $invnumber = "donumber";
 
 160       $arap      = "delivery_orders";
 
 166       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | .
 
 167       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
 
 169       qq|FROM $arap a, ${vc} vc, status s | .
 
 170       qq|WHERE s.trans_id = a.id | .
 
 171       qq|  AND s.spoolfile IS NOT NULL | .
 
 172     ($form->{type} eq 'packing_list'
 
 173     ? qq|  AND s.formname IN (?, ?) |
 
 174     : qq|  AND s.formname = ? |) .
 
 175       qq|  AND a.${vc}_id = vc.id|;
 
 176     @values = ($form->{type});
 
 178     if ($form->{type} eq 'packing_list') {
 
 179       @values = qw(sales_delivery_order purchase_delivery_order);
 
 183   if ($form->{"${vc}_id"}) {
 
 184     $query .= qq| AND a.${vc}_id = ?|;
 
 185     push(@values, conv_i($form->{"${vc}_id"}));
 
 186   } elsif ($form->{ $vc }) {
 
 187     $query .= " AND vc.name ILIKE ?";
 
 188     push(@values, like($form->{ $vc }));
 
 190   foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
 
 191     if ($form->{$column}) {
 
 192       $query .= " AND a.$column ILIKE ?";
 
 193       push(@values, like($form->{$column}));
 
 197   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) {
 
 198     if ($form->{transdatefrom}) {
 
 199       $query .= " AND a.transdate >= ?";
 
 200       push(@values, $form->{transdatefrom});
 
 202     if ($form->{transdateto}) {
 
 203       $query .= " AND a.transdate <= ?";
 
 204       push(@values, $form->{transdateto});
 
 208   my @a = ("transdate", $invnumber, "name");
 
 209   my $sortorder = join ', ', $form->sort_columns(@a);
 
 211   if (grep({ $_ eq $form->{sort} }
 
 212            qw(transdate invnumber ordnumber quonumber donumber name))) {
 
 213     $sortorder = $form->{sort};
 
 216   $query .= " ORDER BY $sortorder";
 
 218   my $sth = $dbh->prepare($query);
 
 219   $sth->execute(@values) ||
 
 220     $form->dberror($query . " (" . join(", ", @values) . ")");
 
 223   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 224     push @{ $form->{SPOOL} }, $ref;
 
 229   $main::lxdebug->leave_sub();
 
 233   $main::lxdebug->enter_sub();
 
 235   my ($self, $myconfig, $form) = @_;
 
 237   my $spool = $::lx_office_conf{paths}->{spool};
 
 239   SL::DB->client->with_transaction(sub {
 
 240     my $dbh = SL::DB->client->dbh;
 
 244     if ($form->{type} =~ /(check|receipt)/) {
 
 245       $query = qq|DELETE FROM status WHERE spoolfile = ?|;
 
 248         qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
 
 249         qq|WHERE spoolfile = ?|;
 
 251     my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 253     foreach my $i (1 .. $form->{rowcount}) {
 
 254       if ($form->{"checked_$i"}) {
 
 255         $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
 
 260     foreach my $i (1 .. $form->{rowcount}) {
 
 261       if ($form->{"checked_$i"}) {
 
 262         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
 
 266   }) or do { die SL::DB->client->error };
 
 268   $main::lxdebug->leave_sub();
 
 273   $main::lxdebug->enter_sub();
 
 275   my ($self, $myconfig, $form, $output) = @_;
 
 277   my $spool = $::lx_office_conf{paths}->{spool};
 
 279   # connect to database
 
 280   my $dbh = SL::DB->client->dbh;
 
 283     qq|UPDATE status SET printed = '1' | .
 
 284     qq|WHERE formname = ? AND spoolfile = ?|;
 
 285   my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 287   foreach my $i (1 .. $form->{rowcount}) {
 
 288     if ($form->{"checked_$i"}) {
 
 289       # $output is safe ( = does not come directly from the browser).
 
 290       open(OUT, $output) or $form->error("$output : $!");
 
 292       $form->{"spoolfile_$i"} =~ s|.*/||;
 
 293       my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
 
 295       # send file to printer
 
 296       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
 
 304       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
 
 305         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
 
 311   $main::lxdebug->leave_sub();