From: Sven Schöling Date: Mon, 8 Nov 2021 03:20:20 +0000 (+0100) Subject: Inventory::new_from aus DeliveryOrderItemsStock Objekten X-Git-Tag: kivitendo-mebil_0.1-0~10^2~2^2~216^2~63 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=b24583b6dba89646151a3cee600b076fc58daf30;p=kivitendo-erp.git Inventory::new_from aus DeliveryOrderItemsStock Objekten --- 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) = @_;