AR: single-dbh
[kivitendo-erp.git] / SL / AR.pm
index 8e8cf2f..510cc5d 100644 (file)
--- a/SL/AR.pm
+++ b/SL/AR.pm
@@ -42,12 +42,21 @@ use SL::MoreCommon;
 use SL::DB::Default;
 use SL::TransNumber;
 use SL::Util qw(trim);
+use SL::DB;
 
 use strict;
 
 sub post_transaction {
+  my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_post_transaction, $self, $myconfig, $form, $provided_dbh, $payments_only);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _post_transaction {
   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
 
   my ($query, $sth, $null, $taxrate, $amount, $tax);
@@ -56,7 +65,7 @@ sub post_transaction {
 
   my @values;
 
-  my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+  my $dbh = $provided_dbh || SL::DB->client->dbh;
   $form->{defaultcurrency} = $form->get_default_currency($myconfig);
 
   # set exchangerate
@@ -323,18 +332,11 @@ sub post_transaction {
     $datev->export;
 
     if ($datev->errors) {
-      $dbh->rollback;
       die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors;
     }
   }
 
-  my $rc = 1;
-  if (!$provided_dbh) {
-    $rc = $dbh->commit();
-    $dbh->disconnect();
-  }
-
-  $main::lxdebug->leave_sub() and return $rc;
+  return 1;
 }
 
 sub _delete_payments {
@@ -377,12 +379,19 @@ sub _delete_payments {
 }
 
 sub post_payment {
+  my ($self, $myconfig, $form, $locale) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_post_payment, $self, $myconfig, $form, $locale);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+sub _post_payment {
   my ($self, $myconfig, $form, $locale) = @_;
 
-  # connect to database, turn off autocommit
-  my $dbh = $form->dbconnect_noauto($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my (%payments, $old_form, $row, $item, $query, %keep_vars);
 
@@ -431,12 +440,7 @@ sub post_payment {
 
   restore_form($old_form);
 
-  my $rc = $dbh->commit();
-  $dbh->disconnect();
-
-  $main::lxdebug->leave_sub();
-
-  return $rc;
+  return 1;
 }
 
 sub delete_transaction {
@@ -444,20 +448,15 @@ sub delete_transaction {
 
   my ($self, $myconfig, $form) = @_;
 
-  # connect to database, turn AutoCommit off
-  my $dbh = $form->dbconnect_noauto($myconfig);
-
-  # acc_trans entries are deleted by database triggers.
-  my $query = qq|DELETE FROM ar WHERE id = ?|;
-  do_query($form, $dbh, $query, $form->{id});
-
-  # commit
-  my $rc = $dbh->commit;
-  $dbh->disconnect;
+  SL::DB->client->with_transaction(sub {
+    # acc_trans entries are deleted by database triggers.
+    my $query = qq|DELETE FROM ar WHERE id = ?|;
+    do_query($form, SL::DB->client->dbh, $query, $form->{id});
+  });
 
   $main::lxdebug->leave_sub();
 
-  return $rc;
+  return 1;
 }
 
 sub ar_transactions {
@@ -683,7 +682,7 @@ sub setup_form {
   $form->{forex} = $form->{exchangerate};
   $exchangerate  = $form->{exchangerate} ? $form->{exchangerate} : 1;
 
-  # expected keys: AR, AR_paid, AR_tax, AR_amount 
+  # expected keys: AR, AR_paid, AR_tax, AR_amount
   foreach my $key (keys %{ $form->{AR_links} }) {
     $j = 0;
     $k = 0;
@@ -788,12 +787,21 @@ sub setup_form {
 }
 
 sub storno {
+  my ($self, $form, $myconfig, $id) = @_;
   $main::lxdebug->enter_sub();
 
+  my $rc = SL::DB->client->with_transaction(\&_storno, $self, $form, $myconfig, $id);
+
+  $::lxdebug->leave_sub;
+  return $rc;
+}
+
+
+sub _storno {
   my ($self, $form, $myconfig, $id) = @_;
 
   my ($query, $new_id, $storno_row, $acc_trans_rows);
-  my $dbh = $form->get_standard_dbh($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   $query = qq|SELECT nextval('glid')|;
   ($new_id) = selectrow_query($form, $dbh, $query);
@@ -838,9 +846,7 @@ sub storno {
 
   map { IO->set_datepaid(table => 'ar', id => $_, dbh => $dbh) } ($id, $new_id);
 
-  $dbh->commit;
-
-  $main::lxdebug->leave_sub();
+  return 1;
 }