Presenter Code - gl_transaction aus invoice wieder entfernt
[kivitendo-erp.git] / bin / mozilla / is.pl
index 9518551..cac93da 100644 (file)
@@ -39,6 +39,7 @@ use Data::Dumper;
 use DateTime;
 use List::MoreUtils qw(uniq);
 use List::Util qw(max sum);
+use English qw(-no_match_vars);
 
 use SL::DB::Default;
 use SL::DB::Customer;
@@ -778,8 +779,43 @@ sub post {
   }
 
   relink_accounts();
-  $form->error($locale->text('Cannot post invoice!'))
-    unless IS->post_invoice(\%myconfig, \%$form);
+
+  # If transfer_out is requested, get rose db handle and do post and
+  # transfer out in one transaction. Otherwise just post the invoice.
+  if ($::instance_conf->get_is_transfer_out && $form->{type} ne 'credit_note' && !$form->{storno}) {
+    require SL::DB::Inventory;
+    my $rose_db = SL::DB::Inventory->new->db;
+    my @errors;
+
+    if (!$rose_db->with_transaction(sub {
+      if (!eval {
+        if (!IS->post_invoice(\%myconfig, \%$form, $rose_db->dbh)) {
+          push @errors, $locale->text('Cannot post invoice!');
+          die 'posting error';
+        }
+        my $err = IS->transfer_out(\%$form, $rose_db->dbh);
+        if (@{ $err }) {
+          push @errors, @{ $err };
+          die 'transfer error';
+        }
+
+        1;
+      }) {
+        push @errors, $EVAL_ERROR;
+        die 'transaction error';
+      }
+
+      1;
+    })) {
+      push @errors, $rose_db->error;
+      $form->error($locale->text('Cannot post invoice and/or transfer out! Error message:') . "\n" . join("\n", @errors));
+    }
+  } else {
+    if (!IS->post_invoice(\%myconfig, \%$form)) {
+      $form->error($locale->text('Cannot post invoice!'));
+    }
+  }
+
   remove_draft() if $form->{remove_draft};
 
   if(!exists $form->{addition}) {