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 #======================================================================
 
  38   $main::lxdebug->enter_sub();
 
  40   my ($self, $myconfig, $form) = @_;
 
  43   my $dbh = $form->dbconnect($myconfig);
 
  45   my %arap = (invoice           => 'ar',
 
  48               purchase_order    => 'oe',
 
  49               sales_quotation   => 'oe',
 
  50               request_quotation => 'oe',
 
  54   $query = qq|SELECT count(*)
 
  55               FROM (SELECT DISTINCT ON (vc.id) vc.id
 
  56                     FROM $form->{vc} vc, $arap{$form->{type}} a, status s
 
  57                     WHERE a.$form->{vc}_id = vc.id
 
  59                     AND s.formname = '$form->{type}'
 
  60                     AND s.spoolfile IS NOT NULL) AS total|;
 
  62   my $sth = $dbh->prepare($query);
 
  63   $sth->execute || $form->dberror($query);
 
  64   my ($count) = $sth->fetchrow_array;
 
  67   # build selection list
 
  68   if ($count < $myconfig->{vclimit}) {
 
  69     $query = qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name
 
  70                 FROM $form->{vc} vc, $arap{$form->{type}} a, status s
 
  71                 WHERE a.$form->{vc}_id = vc.id
 
  73                 AND s.formname = '$form->{type}'
 
  74                 AND s.spoolfile IS NOT NULL|;
 
  76   $sth = $dbh->prepare($query);
 
  77   $sth->execute || $form->dberror($query);
 
  79   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  80     push @{ $form->{"all_$form->{vc}"} }, $ref;
 
  86   $main::lxdebug->leave_sub();
 
  89 sub payment_accounts {
 
  90   $main::lxdebug->enter_sub();
 
  92   my ($self, $myconfig, $form) = @_;
 
  95   my $dbh = $form->dbconnect($myconfig);
 
  97   my $query = qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description
 
  98                  FROM status s, chart c
 
  99                  WHERE s.chart_id = c.id
 
 100                  AND s.formname = '$form->{type}'|;
 
 101   my $sth = $dbh->prepare($query);
 
 102   $sth->execute || $form->dberror($query);
 
 104   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 105     push @{ $form->{accounts} }, $ref;
 
 111   $main::lxdebug->leave_sub();
 
 115   $main::lxdebug->enter_sub();
 
 117   my ($self, $myconfig, $form) = @_;
 
 119   # connect to database
 
 120   my $dbh = $form->dbconnect($myconfig);
 
 123   my $invnumber = "invnumber";
 
 125   if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
 
 127     $arap = ($form->{type} eq 'check') ? "ap" : "ar";
 
 128     my ($accno) = split /--/, $form->{account};
 
 130     $query = qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber,
 
 131                 a.invoice, '$arap' AS module
 
 132                 FROM status s, chart c, $form->{vc} vc, $arap a, acc_trans ac
 
 133                 WHERE s.formname = '$form->{type}'
 
 134                 AND s.chart_id = c.id
 
 135                 AND c.accno = '$accno'
 
 136                 AND s.trans_id = a.id
 
 137                 AND a.$form->{vc}_id = vc.id
 
 138                 AND ac.trans_id = s.trans_id
 
 139                 AND ac.chart_id = c.id
 
 140                 AND NOT ac.fx_transaction|;
 
 144     my $invoice = "a.invoice";
 
 146     if ($form->{type} =~ /_(order|quotation)$/) {
 
 147       $invnumber = "ordnumber";
 
 152     $query = qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber,
 
 153                 a.quonumber, a.transdate, $invoice AS invoice,
 
 154                 '$arap' AS module, vc.name, s.spoolfile
 
 155                 FROM $arap a, $form->{vc} vc, status s
 
 156                 WHERE s.trans_id = a.id
 
 157                 AND s.spoolfile IS NOT NULL
 
 158                 AND s.formname = '$form->{type}'
 
 159                 AND a.$form->{vc}_id = vc.id|;
 
 162   if ($form->{"$form->{vc}_id"}) {
 
 163     $query .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
 
 165     if ($form->{ $form->{vc} }) {
 
 166       my $name = $form->like(lc $form->{ $form->{vc} });
 
 167       $query .= " AND lower(vc.name) LIKE '$name'";
 
 170   if ($form->{invnumber}) {
 
 171     my $number = $form->like(lc $form->{invnumber});
 
 172     $query .= " AND lower(a.invnumber) LIKE '$number'";
 
 174   if ($form->{ordnumber}) {
 
 175     my $ordnumber = $form->like(lc $form->{ordnumber});
 
 176     $query .= " AND lower(a.ordnumber) LIKE '$ordnumber'";
 
 178   if ($form->{quonumber}) {
 
 179     my $quonumber = $form->like(lc $form->{quonumber});
 
 180     $query .= " AND lower(a.quonumber) LIKE '$quonumber'";
 
 183   #  $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
 
 184   #  $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
 
 186   my @a = (transdate, $invnumber, name);
 
 187   my $sortorder = join ', ', $form->sort_columns(@a);
 
 188   $sortorder = $form->{sort} if $form->{sort};
 
 190   $query .= " ORDER by $sortorder";
 
 192   my $sth = $dbh->prepare($query);
 
 193   $sth->execute || $form->dberror($query);
 
 195   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 196     push @{ $form->{SPOOL} }, $ref;
 
 202   $main::lxdebug->leave_sub();
 
 206   $main::lxdebug->enter_sub();
 
 208   my ($self, $myconfig, $form, $spool) = @_;
 
 210   # connect to database, turn AutoCommit off
 
 211   my $dbh = $form->dbconnect_noauto($myconfig);
 
 215   if ($form->{type} =~ /(check|receipt)/) {
 
 216     $query = qq|DELETE FROM status
 
 217                 WHERE spoolfile = ?|;
 
 219     $query = qq|UPDATE status SET
 
 222                  WHERE spoolfile = ?|;
 
 224   my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 226   foreach my $i (1 .. $form->{rowcount}) {
 
 227     if ($form->{"checked_$i"}) {
 
 228       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
 
 234   my $rc = $dbh->commit;
 
 238     foreach my $i (1 .. $form->{rowcount}) {
 
 239       $_ = qq|$spool/$form->{"spoolfile_$i"}|;
 
 240       if ($form->{"checked_$i"}) {
 
 246   $main::lxdebug->leave_sub();
 
 252   $main::lxdebug->enter_sub();
 
 254   my ($self, $myconfig, $form, $spool) = @_;
 
 256   # connect to database
 
 257   my $dbh = $form->dbconnect($myconfig);
 
 259   my $query = qq|UPDATE status SET
 
 261                  WHERE formname = '$form->{type}'
 
 263   my $sth = $dbh->prepare($query) || $form->dberror($query);
 
 265   foreach my $i (1 .. $form->{rowcount}) {
 
 266     if ($form->{"checked_$i"}) {
 
 267       open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!");
 
 269       $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
 
 271       # send file to printer
 
 272       open(IN, $spoolfile) or $form->error("$spoolfile : $!");
 
 280       $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
 
 288   $main::lxdebug->leave_sub();