Zeiterfassung: Konvertierung: Position aus zugehörigem Auftrag
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Tue, 19 Jan 2021 12:32:04 +0000 (13:32 +0100)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Wed, 5 May 2021 15:25:03 +0000 (17:25 +0200)
SL/BackgroundJob/ConvertTimeRecordings.pm
SL/DB/DeliveryOrder.pm

index c6223f4..7540d55 100644 (file)
@@ -193,7 +193,26 @@ sub convert_with_linking {
         $do->save;
         $_->update_attributes(booked => 1) for @{$time_recordings_by_order_id->{$related_order_id}};
 
+        $related_order->link_to_record($do);
+
+        # TODO extend link_to_record for items, otherwise long-term no d.r.y.
+        foreach my $item (@{ $do->items }) {
+          foreach (qw(orderitems)) {
+            if ($item->{"converted_from_${_}_id"}) {
+              die unless $item->{id};
+              RecordLinks->create_links('mode'       => 'ids',
+                                        'from_table' => $_,
+                                        'from_ids'   => $item->{"converted_from_${_}_id"},
+                                        'to_table'   => 'delivery_order_items',
+                                        'to_id'      => $item->{id},
+              ) || die;
+              delete $item->{"converted_from_${_}_id"};
+            }
+          }
+        }
+
         # Todo: reduce qty on related order
+
         1;
       })) {
         $::lxdebug->message(LXDebug->WARN(),
index b17fb48..9d00da0 100644 (file)
@@ -262,7 +262,19 @@ sub new_from_time_recordings {
   my $delivery_order;
 
   if ($params{related_order}) {
-    $delivery_order = SL::DB::DeliveryOrder->new_from($params{related_order}, items => \@items, %params);
+    # collect suitable items in related order
+    my @items_to_use;
+    foreach my $item (@items) {
+      my $item_to_use = first {$item->parts_id == $_->parts_id} @{ $params{related_order}->items_sorted };
+
+      die "no suitable item found in related order" if !$item_to_use;
+
+      my %new_attributes;
+      $new_attributes{$_} = $item->$_ for qw(qty unit_obj longdescription);
+      $item_to_use->assign_attributes(%new_attributes);
+      push @items_to_use, $item_to_use;
+    }
+    $delivery_order = SL::DB::DeliveryOrder->new_from($params{related_order}, items => \@items_to_use, %params);
 
   } else {
     my %args = (