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 #======================================================================
 
  43 sub payment_accounts {
 
  44   $main::lxdebug->enter_sub();
 
  46   my ($self, $myconfig, $form) = @_;
 
  49   my $dbh = SL::DB->client->dbh;
 
  52     qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
 
  53     qq|FROM status s, chart c | .
 
  54     qq|WHERE s.chart_id = c.id AND s.formname = ?|;
 
  55   my $sth = $dbh->prepare($query);
 
  56   $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
 
  58   $form->{accounts} = [];
 
  59   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
  60     push @{ $form->{accounts} }, $ref;
 
  65   $main::lxdebug->leave_sub();
 
  69   $main::lxdebug->enter_sub();
 
  71   my ($self, $myconfig, $form) = @_;
 
  73   my $dbh = SL::DB->client->dbh;
 
  75   my ($query, $arap, @values);
 
  76   my $invnumber = "invnumber";
 
  78   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
  80   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
 
  82     $arap = ($form->{type} eq 'check') ? "ap" : "ar";
 
  83     my ($accno) = split /--/, $form->{account};
 
  86       qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
 
  87       qq|  a.invoice, '$arap' AS module | .
 
  88       qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
 
  89       qq|WHERE s.formname = ? | .
 
  90       qq|  AND s.chart_id = c.id | .
 
  91       qq|  AND c.accno = ? | .
 
  92       qq|  AND s.trans_id = a.id | .
 
  93       qq|  AND a.${vc}_id = vc.id | .
 
  94       qq|  AND ac.trans_id = s.trans_id | .
 
  95       qq|  AND ac.chart_id = c.id | .
 
  96       qq|  AND NOT ac.fx_transaction|;
 
  97     @values = ($form->{type}, $accno);
 
 101     my $invoice = "a.invoice";
 
 102     my $quonumber = "a.quonumber";
 
 104     if ($form->{type} =~ /_(order|quotation)$/) {
 
 105       $invnumber = "ordnumber";
 
 110     if ($form->{type} eq 'packing_list') {
 
 111       $invnumber = "donumber";
 
 112       $arap      = "delivery_orders";
 
 118       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | .
 
 119       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
 
 121       qq|FROM $arap a, ${vc} vc, status s | .
 
 122       qq|WHERE s.trans_id = a.id | .
 
 123       qq|  AND s.spoolfile IS NOT NULL | .
 
 124     ($form->{type} eq 'packing_list'
 
 125     ? qq|  AND s.formname IN (?, ?) |
 
 126     : qq|  AND s.formname = ? |) .
 
 127       qq|  AND a.${vc}_id = vc.id|;
 
 128     @values = ($form->{type});
 
 130     if ($form->{type} eq 'packing_list') {
 
 131       @values = qw(sales_delivery_order purchase_delivery_order);
 
 135   if ($form->{"${vc}_id"}) {
 
 136     $query .= qq| AND a.${vc}_id = ?|;
 
 137     push(@values, conv_i($form->{"${vc}_id"}));
 
 138   } elsif ($form->{ $vc }) {
 
 139     $query .= " AND vc.name ILIKE ?";
 
 140     push(@values, like($form->{ $vc }));
 
 142   foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
 
 143     if ($form->{$column}) {
 
 144       $query .= " AND a.$column ILIKE ?";
 
 145       push(@values, like($form->{$column}));
 
 149   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) {
 
 150     if ($form->{transdatefrom}) {
 
 151       $query .= " AND a.transdate >= ?";
 
 152       push(@values, $form->{transdatefrom});
 
 154     if ($form->{transdateto}) {
 
 155       $query .= " AND a.transdate <= ?";
 
 156       push(@values, $form->{transdateto});
 
 160   my @a = ("transdate", $invnumber, "name");
 
 161   my $sortorder = join ', ', $form->sort_columns(@a);
 
 163   if (grep({ $_ eq $form->{sort} }
 
 164            qw(transdate invnumber ordnumber quonumber donumber name))) {
 
 165     $sortorder = $form->{sort};
 
 168   $query .= " ORDER BY $sortorder";
 
 170   my $sth = $dbh->prepare($query);
 
 171   $sth->execute(@values) ||
 
 172     $form->dberror($query . " (" . join(", ", @values) . ")");
 
 175   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 176     push @{ $form->{SPOOL} }, $ref;
 
 181   $main::lxdebug->leave_sub();
 
 185   $main::lxdebug->enter_sub();
 
 187   my ($self, $myconfig, $form) = @_;
 
 189   my $spool = $::lx_office_conf{paths}->{spool};
 
 191   SL::DB->client->with_transaction(sub {
 
 192     my $dbh = SL::DB->client->dbh;
 
 196     if ($form->{type} =~ /(check|receipt)/) {
 
 197       $query = qq|DELETE FROM status WHERE spoolfile = ?|;
 
 200         qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
 
 201         qq|WHERE spoolfile = ?|;
 
 203     my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 205     foreach my $i (1 .. $form->{rowcount}) {
 
 206       if ($form->{"checked_$i"}) {
 
 207         $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
 
 212     foreach my $i (1 .. $form->{rowcount}) {
 
 213       if ($form->{"checked_$i"}) {
 
 214         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
 
 218   }) or do { die SL::DB->client->error };
 
 220   $main::lxdebug->leave_sub();
 
 225   $main::lxdebug->enter_sub();
 
 227   my ($self, $myconfig, $form, $output) = @_;
 
 229   my $spool = $::lx_office_conf{paths}->{spool};
 
 231   # connect to database
 
 232   my $dbh = SL::DB->client->dbh;
 
 235     qq|UPDATE status SET printed = '1' | .
 
 236     qq|WHERE formname = ? AND spoolfile = ?|;
 
 237   my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 239   foreach my $i (1 .. $form->{rowcount}) {
 
 240     if ($form->{"checked_$i"}) {
 
 241       # $output is safe ( = does not come directly from the browser).
 
 242       open(OUT, $output) or $form->error("$output : $!");
 
 244       $form->{"spoolfile_$i"} =~ s|.*/||;
 
 245       my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
 
 247       # send file to printer
 
 248       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
 
 256       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
 
 257         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
 
 263   $main::lxdebug->leave_sub();