Inventory::new_from aus DeliveryOrderItemsStock Objekten
authorSven Schöling <s.schoeling@googlemail.com>
Mon, 8 Nov 2021 03:20:20 +0000 (04:20 +0100)
committerJan Büren <jan@kivitendo.de>
Mon, 14 Feb 2022 14:02:19 +0000 (15:02 +0100)
SL/DB/DeliveryOrderItem.pm
SL/DB/Inventory.pm

index 045e2e6..a42dad5 100644 (file)
@@ -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
index a2b571c..df78623 100644 (file)
@@ -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) = @_;