From 9c2d09b80c7fe80982c4e63b5fb051002a152411 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Tue, 19 Jan 2021 13:32:04 +0100 Subject: [PATCH] =?utf8?q?Zeiterfassung:=20Konvertierung:=20Position=20aus?= =?utf8?q?=20zugeh=C3=B6rigem=20Auftrag?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/BackgroundJob/ConvertTimeRecordings.pm | 19 +++++++++++++++++++ SL/DB/DeliveryOrder.pm | 14 +++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/SL/BackgroundJob/ConvertTimeRecordings.pm b/SL/BackgroundJob/ConvertTimeRecordings.pm index c6223f4f8..7540d55cd 100644 --- a/SL/BackgroundJob/ConvertTimeRecordings.pm +++ b/SL/BackgroundJob/ConvertTimeRecordings.pm @@ -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(), diff --git a/SL/DB/DeliveryOrder.pm b/SL/DB/DeliveryOrder.pm index b17fb48b4..9d00da021 100644 --- a/SL/DB/DeliveryOrder.pm +++ b/SL/DB/DeliveryOrder.pm @@ -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 = ( -- 2.20.1