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";
 
 152     my $quonumber = "a.quonumber";
 
 154     if ($form->{type} =~ /_(order|quotation)$/) {
 
 155       $invnumber = "ordnumber";
 
 160     if ($form->{type} eq 'packing_list') {
 
 161       $invnumber = "donumber";
 
 162       $arap      = "delivery_orders";
 
 168       qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | .
 
 169       qq|  a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | .
 
 171       qq|FROM $arap a, ${vc} vc, status s | .
 
 172       qq|WHERE s.trans_id = a.id | .
 
 173       qq|  AND s.spoolfile IS NOT NULL | .
 
 174     ($form->{type} eq 'packing_list'
 
 175     ? qq|  AND s.formname IN (?, ?) |
 
 176     : qq|  AND s.formname = ? |) .
 
 177       qq|  AND a.${vc}_id = vc.id|;
 
 178     @values = ($form->{type});
 
 180     if ($form->{type} eq 'packing_list') {
 
 181       @values = qw(sales_delivery_order purchase_delivery_order);
 
 185   if ($form->{"${vc}_id"}) {
 
 186     $query .= qq| AND a.${vc}_id = ?|;
 
 187     push(@values, conv_i($form->{"${vc}_id"}));
 
 188   } elsif ($form->{ $vc }) {
 
 189     $query .= " AND vc.name ILIKE ?";
 
 190     push(@values, $form->like($form->{ $vc }));
 
 192   foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
 
 193     if ($form->{$column}) {
 
 194       $query .= " AND a.$column ILIKE ?";
 
 195       push(@values, $form->like($form->{$column}));
 
 199   if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) {
 
 200     if ($form->{transdatefrom}) {
 
 201       $query .= " AND a.transdate >= ?";
 
 202       push(@values, $form->{transdatefrom});
 
 204     if ($form->{transdateto}) {
 
 205       $query .= " AND a.transdate <= ?";
 
 206       push(@values, $form->{transdateto});
 
 210   my @a = ("transdate", $invnumber, "name");
 
 211   my $sortorder = join ', ', $form->sort_columns(@a);
 
 213   if (grep({ $_ eq $form->{sort} }
 
 214            qw(transdate invnumber ordnumber quonumber donumber name))) {
 
 215     $sortorder = $form->{sort};
 
 218   $query .= " ORDER BY $sortorder";
 
 220   my $sth = $dbh->prepare($query);
 
 221   $sth->execute(@values) ||
 
 222     $form->dberror($query . " (" . join(", ", @values) . ")");
 
 225   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 226     push @{ $form->{SPOOL} }, $ref;
 
 232   $main::lxdebug->leave_sub();
 
 236   $main::lxdebug->enter_sub();
 
 238   my ($self, $myconfig, $form) = @_;
 
 240   my $spool = $::lx_office_conf{paths}->{spool};
 
 242   # connect to database, turn AutoCommit off
 
 243   my $dbh = $form->dbconnect_noauto($myconfig);
 
 247   if ($form->{type} =~ /(check|receipt)/) {
 
 248     $query = qq|DELETE FROM status WHERE spoolfile = ?|;
 
 251       qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
 
 252       qq|WHERE spoolfile = ?|;
 
 254   my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 256   foreach my $i (1 .. $form->{rowcount}) {
 
 257     if ($form->{"checked_$i"}) {
 
 258       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
 
 264   my $rc = $dbh->commit;
 
 268     foreach my $i (1 .. $form->{rowcount}) {
 
 269       if ($form->{"checked_$i"}) {
 
 270         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
 
 275   $main::lxdebug->leave_sub();
 
 281   $main::lxdebug->enter_sub();
 
 283   my ($self, $myconfig, $form, $output) = @_;
 
 285   my $spool = $::lx_office_conf{paths}->{spool};
 
 287   # connect to database
 
 288   my $dbh = $form->dbconnect($myconfig);
 
 291     qq|UPDATE status SET printed = '1' | .
 
 292     qq|WHERE formname = ? AND spoolfile = ?|;
 
 293   my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 295   foreach my $i (1 .. $form->{rowcount}) {
 
 296     if ($form->{"checked_$i"}) {
 
 297       # $output is safe ( = does not come directly from the browser).
 
 298       open(OUT, $output) or $form->error("$output : $!");
 
 300       $form->{"spoolfile_$i"} =~ s|.*/||;
 
 301       my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
 
 303       # send file to printer
 
 304       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
 
 312       $sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
 
 313         $form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
 
 321   $main::lxdebug->leave_sub();