From b24583b6dba89646151a3cee600b076fc58daf30 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 8 Nov 2021 04:20:20 +0100 Subject: [PATCH] Inventory::new_from aus DeliveryOrderItemsStock Objekten --- SL/DB/DeliveryOrderItem.pm | 6 ++++++ SL/DB/Inventory.pm | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/SL/DB/DeliveryOrderItem.pm b/SL/DB/DeliveryOrderItem.pm index 045e2e6c7..a42dad510 100644 --- a/SL/DB/DeliveryOrderItem.pm +++ b/SL/DB/DeliveryOrderItem.pm @@ -48,6 +48,12 @@ sub displayable_delivery_order_info { . " (" . $self->delivery_order->transdate->to_kivitendo . ")"; }; +sub effective_project { + my ($self) = @_; + + $self->project // $self->delivery_order->project; +} + __END__ =pod diff --git a/SL/DB/Inventory.pm b/SL/DB/Inventory.pm index a2b571ce6..df78623cf 100644 --- a/SL/DB/Inventory.pm +++ b/SL/DB/Inventory.pm @@ -4,7 +4,10 @@ package SL::DB::Inventory; use strict; +use Carp; +use DateTime; +use SL::DBUtils qw(selectrow_query); use SL::DB::MetaSetup::Inventory; use SL::DB::Manager::Inventory; @@ -19,6 +22,35 @@ sub part { goto &parts; } +sub new_from { + my ($class, $obj) = @_; + + if ('SL::DB::DeliveryOrderItemsStock' eq ref $obj) { + return $class->new_from_delivery_order_stock($obj); + } + + croak "unknown obj type (@{[ ref $obj ]}) for SL::DB::Inventory::new_from"; +} + +sub new_from_delivery_order_stock { + my ($stock) = @_; + + my $project = $stock->delivery_order_item->effective_project; + + return __PACKAGE__->new( + delivery_order_items_stock_id => $stock->id, + parts_id => $stock->delivery_order_item->parts_id, + qty => $stock->unit->convert_to($stock->qty => $stock->delivery_order_item->part->unit_obj), + unit => $stock->delivery_order_item->part->unit_obj, + warehouse_id => $stock->warehouse_id, + bin_id => $stock->bin_id, + chargenumber => $stock->chargenumber, + bestbefore => $stock->bestbefore, + project_id => $project ? $project->id : undef, + # trans_type - not set here, set in controller + ); +} + sub _before_save_create_trans_id { my ($self, %params) = @_; -- 2.20.1