Auslagern in VK-Rechnung: Beim Buchen einer VK-Rechnung wird ausgelagert.
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Thu, 26 Feb 2015 14:35:13 +0000 (15:35 +0100)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Fri, 6 Mar 2015 13:06:31 +0000 (14:06 +0100)
Todo: Es werden noch nicht alle Einstellungen berücksichtigt, die bestimmen,
wann von welchem Lager/Lagerplatz ausgelagert wird.

SL/IS.pm
SL/WH.pm
bin/mozilla/is.pl

index 989330c..6a8ef46 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -1291,6 +1291,63 @@ SQL
   return $rc;
 }
 
+sub transfer_out {
+  $::lxdebug->enter_sub;
+
+  my ($self, $form) = @_; 
+
+  my @transfers;
+
+  foreach my $i (1 .. $form->{rowcount}) {
+    next if !$form->{"id_$i"};
+    my ($wh_id, $bin_id) = _determine_wh_and_bin($::instance_conf, $form->{"id_$i"});
+
+    if ($wh_id && $bin_id) {
+      push @transfers, {
+        'parts_id'         => $form->{"id_$i"},
+        'qty'              => $form->{"qty_$i"},
+        'unit'             => $form->{"unit_$i"},
+        'transfer_type'    => 'shipped',
+        'src_warehouse_id' => $wh_id,
+        'src_bin_id'       => $bin_id,
+        'project_id'       => $form->{"project_id_$i"},
+        'invoice_id'       => $form->{"invoice_id_$i"},
+      };
+    }
+  }
+
+  require SL::WH;
+  WH->transfer(@transfers);
+
+  $::lxdebug->leave_sub;
+  return 1;
+}
+
+sub _determine_wh_and_bin {
+  $::lxdebug->enter_sub(2);
+
+  my ($conf, $part_id) = @_;
+
+  my $part = SL::DB::Part->new(id => $part_id)->load;
+
+  if ($part->is_service && !$conf->get_transfer_default_services) {
+    $::lxdebug->leave_sub(2);
+    return;
+  }
+    
+
+  my $wh_id  = $part->warehouse_id;
+  my $bin_id = $part->bin_id;
+
+  if (!$wh_id && !$bin_id && $conf->get_transfer_default_ignore_onhand) {
+    $wh_id  = $conf->get_warehouse_id_ignore_onhand;
+    $bin_id = $conf->get_bin_id_ignore_onhand;
+  }
+
+  $::lxdebug->leave_sub(2);
+  return ($wh_id, $bin_id);
+}
+
 sub _delete_payments {
   $main::lxdebug->enter_sub();
 
index 5b24cfd..a8c1ae3 100644 (file)
--- a/SL/WH.pm
+++ b/SL/WH.pm
@@ -110,7 +110,7 @@ sub transfer {
           trans_id         => $trans_id,
           shippingdate     => !$transfer->{shippingdate} || $transfer->{shippingdate} eq 'current_date'
                               ? $now : $transfer->{shippingdate},
-          map { $_ => $transfer->{$_} } qw( chargenumber bestbefore oe_id delivery_order_items_stock_id comment),
+          map { $_ => $transfer->{$_} } qw(chargenumber bestbefore oe_id delivery_order_items_stock_id invoice_id comment),
       );
 
       if ($unit) {
index 9518551..0bcd3c3 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,34 @@ 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) {
+    require SL::DB::Inventory;
+    my $rose_db = SL::DB::Inventory->new->db;
+    my $error;
+
+    if (!$rose_db->with_transaction(sub {
+      if (!eval {
+        IS->post_invoice(\%myconfig, \%$form, $rose_db->dbh);
+        IS->transfer_out(\%$form);
+        1;
+      }) {
+        $error = $EVAL_ERROR;
+        return;
+      }
+
+      1;
+    })) {
+      $form->error($locale->text("Cannot post invoice and/or transfer out!\nError was:\n") . $locale->text($error));
+    }
+  } else {
+    if (!IS->post_invoice(\%myconfig, \%$form)) {
+      $form->error($locale->text('Cannot post invoice!'));
+    }
+  }
+
   remove_draft() if $form->{remove_draft};
 
   if(!exists $form->{addition}) {