Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / DB / OrderItem.pm
index 1eb5247..cc0a92f 100644 (file)
@@ -2,11 +2,8 @@ package SL::DB::OrderItem;
 
 use strict;
 
-use List::Util qw(sum);
-
 use SL::DB::MetaSetup::OrderItem;
 use SL::DB::Manager::OrderItem;
-use SL::DB::DeliveryOrderItemsStock;
 use SL::DB::Helper::ActsAsList;
 use SL::DB::Helper::LinkedRecords;
 use SL::DB::Helper::RecordItem;
@@ -20,6 +17,7 @@ use SL::DB::Helper::CustomVariables (
     }
   },
 );
+use SL::Helper::ShippedQty;
 
 __PACKAGE__->meta->initialize;
 
@@ -31,16 +29,64 @@ sub is_price_update_available {
 }
 
 sub shipped_qty {
-  my ($self) = @_;
+  my ($self, %params) = @_;
+
+  my $force = delete $params{force};
+
+  SL::Helper::ShippedQty->new(%params)->calculate($self)->write_to_objects if $force || !defined $self->{shipped_qty};
 
-  my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder');
-  my @doi      = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @$d_orders;
+  $self->{shipped_qty};
+}
+
+sub linked_delivery_order_items {
+  my ($self) = @_;
 
-  require SL::AM;
-  return sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @doi);
+  return $self->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrderItem');
 }
 
+sub delivered_qty { goto &shipped_qty }
+
 sub record { goto &order }
 
 1;
 
+__END__
+
+=pod
+
+=head1 NAME
+
+SL::DB::OrderItems: Rose model for orderitems
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item C<shipped_qty PARAMS>
+
+Calculates the shipped qty for this orderitem (measured in the current unit)
+and returns it.
+
+Note that the shipped qty is expected not to change within the request and is
+cached in C<shipped_qty> once calculated. If C<< force => 1 >> is passed, the
+existibng cache is ignored.
+
+Given parameters will be passed to L<SL::Helper::ShippedQty>, so you can force
+the shipped/delivered distinction like this:
+
+  $_->shipped_qty(require_stock_out => 0);
+
+Note however that calculating shipped_qty on individual Orderitems is generally
+a bad idea. See L<SL::Helper::ShippedQty> for way to compute these all at once.
+
+=item C<delivered_qty>
+
+Alias for L</shipped_qty>.
+
+=back
+
+=head1 AUTHORS
+
+G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
+
+=cut