1 package SL::DB::OrderItem;
5 use List::Util qw(sum);
7 use SL::DB::MetaSetup::OrderItem;
8 use SL::DB::Manager::OrderItem;
9 use SL::DB::DeliveryOrderItemsStock;
10 use SL::DB::Helper::ActsAsList;
11 use SL::DB::Helper::CustomVariables (
12 sub_module => 'orderitems',
16 class => 'SL::DB::Part',
22 __PACKAGE__->meta->initialize;
24 __PACKAGE__->configure_acts_as_list(group_by => [qw(trans_id)]);
26 sub is_price_update_available {
28 return $self->origprice > $self->part->sellprice;
34 my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder');
35 my @doi = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @$d_orders;
38 return sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @doi);
44 return $self->_delivered_qty;
50 return $self->{delivered_qty} if $self->{delivered_qty};
52 my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder');
54 my @d_orders_delivered = grep { $_->delivered } @$d_orders;
56 my @doi_delivered = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @d_orders_delivered;
59 $self->{delivered_qty} = sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @doi_delivered);
61 return $self->{delivered_qty};
67 my $price_factor = $self->price_factor || 1;
69 $self->_delivered_qty;
70 return ($self->_delivered_qty * $self->sellprice * (1 - $self->discount ) / $price_factor);
76 my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder');
77 my @doi = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @$d_orders;
81 my $doi_stocks = SL::DB::Manager::DeliveryOrderItemsStock->get_all(query => [delivery_order_item_id => [ map {$_->id} @doi ]]);
84 return sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @{ $doi_stocks });
90 return SL::DB::Manager::Order->find_by(id => $self->trans_id)->taxincluded ? $::locale->text('WARN: Tax included value!') : '';