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::LinkedRecords;
12 use SL::DB::Helper::RecordItem;
13 use SL::DB::Helper::CustomVariables (
14 sub_module => 'orderitems',
18 class => 'SL::DB::Part',
24 __PACKAGE__->meta->initialize;
26 __PACKAGE__->configure_acts_as_list(group_by => [qw(trans_id)]);
28 sub is_price_update_available {
30 return $self->origprice > $self->part->sellprice;
36 my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder', with_objects => [ 'orderitems' ]);
37 my @doi = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @$d_orders;
39 return sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @doi);
42 sub linked_delivery_order_items {
45 return $self->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrderItem');
49 # checks for delivery_order_stock_id entries, which have been converted to inventory entries
50 # uses several rose relationships
51 # doesn't differentiate between sales and orders
54 my $delivered_qty = 0;
55 foreach my $doi ( @{$self->linked_delivery_order_items} ) {
56 next unless scalar @{$doi->delivery_order_stock_entries};
57 $delivered_qty += sum map { $_->inventory ? $_->qty : 0 } @{$doi->delivery_order_stock_entries};
59 return $delivered_qty;
62 sub delivered_qty_sql {
63 # checks for delivery_order_stock_id entries, which have been converted to inventory entries
67 SELECT (sum(i.qty) * CASE WHEN oe.customer_id IS NULL THEN 1 ELSE -1 END) AS delivered
69 INNER JOIN record_links rl ON ( oi.id = rl.FROM_id
70 and rl.FROM_table = 'orderitems'
71 and rl.to_table::text = 'delivery_order_items'::text
73 INNER JOIN delivery_order_items doi ON (doi.id =rl.to_id)
74 INNER JOIN delivery_order_items_stock dois ON (dois.delivery_order_item_id = doi.id)
75 INNER JOIN inventory i ON (dois.id = i.delivery_order_items_stock_id)
76 INNER JOIN oe ON (oe.id = oi.trans_id)
80 my ($delivered_qty) = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id);
82 return $delivered_qty;
85 sub delivered_qty_sql_multi {
86 # checks for delivery_order_stock_id entries, which have been converted to inventory entries
90 SELECT sum(dois.qty) from delivery_order_items_stock dois
91 LEFT OUTER JOIN inventory i ON (dois.id = i.delivery_order_items_stock_id)
93 dois.delivery_order_item_id in (
101 from_table = 'orderitems' AND
102 to_table = 'delivery_order_items'
106 my ($delivered_qty) = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id);
108 return $delivered_qty;
111 sub record { goto &order }
121 SL::DB::OrderItems: Rose model for orderitems
131 returns the number of orderitems which are already linked to Delivery Orders.
132 The linked key is parts_id and not orderitems (id) -> delivery_order_items (id).
133 Therefore this function is not safe for identical parts_id.
135 C<$::form-E<gt>format_amount(\%::myconfig, $_[0]-E<gt>shipped_qty);>
137 =item C<delivered_qty>
139 Returns the amount of this orderitem that has been delivered, meaning it
140 appears in a delivery order AND has been transferred. The delivery order items
141 are found by direct record_links from orderitems to delivery order items.
142 Delivery order items that were added manually and not via the record workflow
143 will therefore not be calculated correctly.
145 Orders that were created before the individual items were linked (this feature
146 was added in kivitendo 3.2.0) will therefore return incorrect results.
148 =item C<delivered_qty_sql>
150 Does the same as delivered_qty, but via sql.
154 =item C<delivered_qty_sql>
156 Returns the amount of the orderitem that has actually been shipped, not just where delivery_order_items exist (how delivered_qty works).
158 Doesn't deal with different units yet.
164 Older versions of OrderItem.pm had more functions which where used for calculating the
165 qty for the different states of the Delivery Order.
166 For example to get the qty in already marked as delivered Delivery Orders:
170 return $self-E<gt>_delivered_qty;
174 my @d_orders_delivered = grep { $_-E<gt>delivered } @$d_orders;
175 my @doi_delivered = grep { $_-E<gt>parts_id == $self-E<gt>parts_id } map { $_-E<gt>orderitems } @d_orders_delivered;
178 In general the function C<shipped_qty> and all (project) related functions should be marked deprecate,
179 because of the better linked item to item data in the record_links table.
183 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>