Fixt #266 Kontenabgleich mit Bank transaktionssicher
authorJan Büren <jan@kivitendo.de>
Sat, 14 Mar 2020 07:17:27 +0000 (08:17 +0100)
committerJan Büren <jan@kivitendo.de>
Sat, 14 Mar 2020 07:17:27 +0000 (08:17 +0100)
SL/Controller/Reconciliation.pm

index 6f8ebfe..6e2af69 100644 (file)
@@ -179,23 +179,26 @@ sub action_reconcile_proposals {
 
   my $counter = 0;
 
-  foreach my $bt_id ( @{ $::form->{bt_ids} }) {
-    my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
-    my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
-    $bank_transaction->cleared('1');
-    $bank_transaction->save;
-    foreach my $acc_trans_id (@{ $::form->{proposal_list}->{$bt_id}->{BB} }) {
-      SL::DB::ReconciliationLink->new(
-        rec_group => $rec_group,
-        bank_transaction_id => $bt_id,
-        acc_trans_id => $acc_trans_id
-      )->save;
-      my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $acc_trans_id);
-      $acc_trans->cleared('1');
-      $acc_trans->save;
+  # reconcile transaction safe
+  SL::DB->client->with_transaction(sub {
+    foreach my $bt_id ( @{ $::form->{bt_ids} }) {
+      my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
+      my $bank_transaction = SL::DB::Manager::BankTransaction->find_by(id => $bt_id);
+      $bank_transaction->cleared('1');
+      $bank_transaction->save;
+      foreach my $acc_trans_id (@{ $::form->{proposal_list}->{$bt_id}->{BB} }) {
+        SL::DB::ReconciliationLink->new(
+          rec_group => $rec_group,
+          bank_transaction_id => $bt_id,
+          acc_trans_id => $acc_trans_id
+        )->save;
+        my $acc_trans = SL::DB::Manager::AccTransaction->find_by(acc_trans_id => $acc_trans_id);
+        $acc_trans->cleared('1');
+        $acc_trans->save;
+      }
+      $counter++;
     }
-    $counter++;
-  }
+  }) or die t8('Unable to reconcile, database transaction failure');
 
   flash('ok', t8('#1 proposal(s) saved.', $counter));
 
@@ -354,35 +357,36 @@ sub _get_elements_and_validate {
 sub _reconcile {
   my ($self) = @_;
 
-  # 1. step: set AccTrans and BankTransactions to 'cleared'
-  foreach my $element (@{ $self->{ELEMENTS} }) {
-    $element->cleared('1');
-    # veto either invoice_amount is fully assigned or not! No state tricks in later workflow!
-    # invoice_amount should be a distinct sign, that some bookings were really made from a bank transaction
-    # $element->invoice_amount($element->amount) if $element->isa('SL::DB::BankTransaction');
-    $element->save;
-  }
+  # reconcile transaction safe
+  SL::DB->client->with_transaction(sub {
 
-  # 2. step: insert entry in reconciliation_links
-  my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
-  #There is either a 1:n relation or a n:1 relation
-  if (scalar @{ $::form->{bt_ids} } == 1) {
-    my $bt_id = @{ $::form->{bt_ids} }[0];
-    foreach my $bb_id (@{ $::form->{bb_ids} }) {
-      my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
-                                                     acc_trans_id        => $bb_id,
-                                                     rec_group           => $rec_group);
-      $rec_link->save;
+    # 1. step: set AccTrans and BankTransactions to 'cleared'
+    foreach my $element (@{ $self->{ELEMENTS} }) {
+      $element->cleared('1');
+      # veto either invoice_amount is fully assigned or not! No state tricks in later workflow!
+      $element->save;
     }
-  } else {
-    my $bb_id = @{ $::form->{bb_ids} }[0];
-    foreach my $bt_id (@{ $::form->{bt_ids} }) {
-      my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
-                                                     acc_trans_id        => $bb_id,
-                                                     rec_group           => $rec_group);
-      $rec_link->save;
+    # 2. step: insert entry in reconciliation_links
+    my $rec_group = SL::DB::Manager::ReconciliationLink->get_new_rec_group();
+    #There is either a 1:n relation or a n:1 relation
+    if (scalar @{ $::form->{bt_ids} } == 1) {
+      my $bt_id = @{ $::form->{bt_ids} }[0];
+      foreach my $bb_id (@{ $::form->{bb_ids} }) {
+        my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
+                                                       acc_trans_id        => $bb_id,
+                                                       rec_group           => $rec_group);
+        $rec_link->save;
+      }
+    } else {
+      my $bb_id = @{ $::form->{bb_ids} }[0];
+      foreach my $bt_id (@{ $::form->{bt_ids} }) {
+        my $rec_link = SL::DB::ReconciliationLink->new(bank_transaction_id => $bt_id,
+                                                       acc_trans_id        => $bb_id,
+                                                       rec_group           => $rec_group);
+        $rec_link->save;
+      }
     }
-  }
+  }) or die t8('Unable to reconcile, database transaction failure');
 }
 
 sub _filter_to_where {