Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / BP.pm
index f7eaf2b..4489b8d 100644 (file)
--- a/SL/BP.pm
+++ b/SL/BP.pm
@@ -25,7 +25,8 @@
 # GNU General Public License for more details.
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
 #======================================================================
 #
 # Batch printing module backend routines
 package BP;
 
 use SL::DBUtils;
+use SL::DB;
 
 use strict;
 
-sub get_vc {
-  $main::lxdebug->enter_sub();
-
-  my ($self, $myconfig, $form) = @_;
-
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
-
-  my %arap = (invoice           => 'ar',
-              sales_order       => 'oe',
-              purchase_order    => 'oe',
-              sales_quotation   => 'oe',
-              request_quotation => 'oe',
-              check             => 'ap',
-              receipt           => 'ar');
-
-  my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
-  my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar';
-
-  my $query =
-    qq|SELECT count(*) | .
-    qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | .
-    qq|  WHERE a.${vc}_id = vc.id  AND s.trans_id = a.id AND s.formname = ? | .
-    qq|    AND s.spoolfile IS NOT NULL) AS total|;
-
-  my ($count) = selectrow_query($form, $dbh, $query, $form->{type});
-
-  # build selection list
-  if ($count < $myconfig->{vclimit}) {
-    $query =
-      qq|SELECT DISTINCT ON (vc.id) vc.id, vc.name | .
-      qq|FROM $vc vc, $arap_type a, status s | .
-      qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | .
-      qq|  AND s.spoolfile IS NOT NULL|;
-
-    my $sth = $dbh->prepare($query);
-    $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
-
-    $form->{"all_${vc}"} = [];
-    while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
-      push @{ $form->{"all_${vc}"} }, $ref;
-    }
-    $sth->finish;
-  }
-
-  $dbh->disconnect;
-
-  $main::lxdebug->leave_sub();
-}
-
 sub payment_accounts {
   $main::lxdebug->enter_sub();
 
   my ($self, $myconfig, $form) = @_;
 
   # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my $query =
     qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
@@ -109,7 +61,6 @@ sub payment_accounts {
   }
 
   $sth->finish;
-  $dbh->disconnect;
 
   $main::lxdebug->leave_sub();
 }
@@ -119,8 +70,7 @@ sub get_spoolfiles {
 
   my ($self, $myconfig, $form) = @_;
 
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my ($query, $arap, @values);
   my $invnumber = "invnumber";
@@ -187,12 +137,12 @@ sub get_spoolfiles {
     push(@values, conv_i($form->{"${vc}_id"}));
   } elsif ($form->{ $vc }) {
     $query .= " AND vc.name ILIKE ?";
-    push(@values, $form->like($form->{ $vc }));
+    push(@values, like($form->{ $vc }));
   }
   foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
     if ($form->{$column}) {
       $query .= " AND a.$column ILIKE ?";
-      push(@values, $form->like($form->{$column}));
+      push(@values, like($form->{$column}));
     }
   }
 
@@ -227,7 +177,6 @@ sub get_spoolfiles {
   }
 
   $sth->finish;
-  $dbh->disconnect;
 
   $main::lxdebug->leave_sub();
 }
@@ -239,42 +188,37 @@ sub delete_spool {
 
   my $spool = $::lx_office_conf{paths}->{spool};
 
-  # connect to database, turn AutoCommit off
-  my $dbh = $form->dbconnect_noauto($myconfig);
+  SL::DB->client->with_transaction(sub {
+    my $dbh = SL::DB->client->dbh;
 
-  my $query;
-
-  if ($form->{type} =~ /(check|receipt)/) {
-    $query = qq|DELETE FROM status WHERE spoolfile = ?|;
-  } else {
-    $query =
-      qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
-      qq|WHERE spoolfile = ?|;
-  }
-  my $sth = $dbh->prepare($query) || $form->dberror($query);
+    my $query;
 
-  foreach my $i (1 .. $form->{rowcount}) {
-    if ($form->{"checked_$i"}) {
-      $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
-      $sth->finish;
+    if ($form->{type} =~ /(check|receipt)/) {
+      $query = qq|DELETE FROM status WHERE spoolfile = ?|;
+    } else {
+      $query =
+        qq|UPDATE status SET spoolfile = NULL, printed = '1' | .
+        qq|WHERE spoolfile = ?|;
     }
-  }
+    my $sth = $dbh->prepare($query) || $form->dberror($query);
 
-  # commit
-  my $rc = $dbh->commit;
-  $dbh->disconnect;
+    foreach my $i (1 .. $form->{rowcount}) {
+      if ($form->{"checked_$i"}) {
+        $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
+        $sth->finish;
+      }
+    }
 
-  if ($rc) {
     foreach my $i (1 .. $form->{rowcount}) {
       if ($form->{"checked_$i"}) {
         unlink(qq|$spool/$form->{"spoolfile_$i"}|);
       }
     }
-  }
+    1;
+  }) or do { die SL::DB->client->error };
 
   $main::lxdebug->leave_sub();
-
-  return $rc;
+  return 1;
 }
 
 sub print_spool {
@@ -285,7 +229,7 @@ sub print_spool {
   my $spool = $::lx_office_conf{paths}->{spool};
 
   # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my $query =
     qq|UPDATE status SET printed = '1' | .
@@ -316,10 +260,7 @@ sub print_spool {
     }
   }
 
-  $dbh->disconnect;
-
   $main::lxdebug->leave_sub();
 }
 
 1;
-