$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(),
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 = (