commit ecb95993cd0a1635dc759e0733cac883be3fdc0c
Author: Sven Schöling <s.schoeling@googlemail.com>
Date:   Mon Jul 24 17:14:06 2017 +0200

    DeliveryPlan: calc_qtys mit SL::Helper::ShippedQty berechnen

diff --git a/SL/Controller/DeliveryPlan.pm b/SL/Controller/DeliveryPlan.pm
index 16991cf3c..2332462ac 100644
--- a/SL/Controller/DeliveryPlan.pm
+++ b/SL/Controller/DeliveryPlan.pm
@@ -9,6 +9,7 @@ use SL::DB::Business;
 use SL::Controller::Helper::GetModels;
 use SL::Controller::Helper::ReportGenerator;
 use SL::Locale::String;
+use SL::Helper::ShippedQty;
 use SL::AM;
 use SL::DBUtils ();
 use Carp;
@@ -26,7 +27,6 @@ my %sort_columns = (
   partnumber        => t8('Part Number'),
   qty               => t8('Qty'),
   shipped_qty       => t8('shipped'),
-  delivered_qty     => t8('transferred in / out'),
   not_shipped_qty   => t8('not shipped'),
   ordnumber         => t8('Order'),
   customer          => t8('Customer'),
@@ -54,7 +54,7 @@ sub prepare_report {
   my $report      = SL::ReportGenerator->new(\%::myconfig, $::form);
   $self->{report} = $report;
 
-  my @columns     = qw(reqdate customer vendor ordnumber partnumber description qty shipped_qty not_shipped_qty delivered_qty);
+  my @columns     = qw(reqdate customer vendor ordnumber partnumber description qty shipped_qty not_shipped_qty);
 
   my @sortable    = qw(reqdate customer vendor ordnumber partnumber description);
 
@@ -67,7 +67,6 @@ sub prepare_report {
     qty               => {      sub => sub { $_[0]->qty_as_number . ' ' . $_[0]->unit                                        } },
     shipped_qty       => {      sub => sub { $::form->format_amount(\%::myconfig, $_[0]{shipped_qty}, 2) . ' ' . $_[0]->unit } },
     not_shipped_qty   => {      sub => sub { $::form->format_amount(\%::myconfig, $_[0]->qty - $_[0]{shipped_qty}, 2) . ' ' . $_[0]->unit } },
-    delivered_qty     => {      sub => sub { $::form->format_amount(\%::myconfig, $_[0]{delivered_qty}, 2) .' ' . $_[0]->unit } },
     ordnumber         => {      sub => sub { $_[0]->order->ordnumber                                                         },
                            obj_link => sub { $self->link_to($_[0]->order)                                                    } },
     vendor            => {      sub => sub { $_[0]->order->vendor->name                                                      },
@@ -105,59 +104,13 @@ sub prepare_report {
 
 sub calc_qtys {
   my ($self, $orderitems) = @_;
-  # using $orderitem->shipped_qty 40 times is far too slow. need to do it manually
-  #
 
   return unless scalar @$orderitems;
 
-  my %orderitems_by_id = map { $_->id => $_ } @$orderitems;
-
-  my $query = $self->use_linked_items ? _calc_qtys_query_linked_items(scalar @$orderitems)
-            :                           _calc_qtys_query_match_parts(scalar @$orderitems);
-
-  my $result = SL::DBUtils::selectall_hashref_query($::form, $::form->get_standard_dbh, $query, map { $_->id } @$orderitems);
-
-  for my $row (@$result) {
-    my $item = $orderitems_by_id{ $row->{id} };
-    $item->{shipped_qty}   ||= 0;
-    $item->{delivered_qty} ||= 0;
-    $item->{shipped_qty}    += AM->convert_unit($row->{unit} => $item->unit) * $row->{qty};
-    $item->{delivered_qty}  += AM->convert_unit($row->{unit} => $item->unit) * $row->{qty} if $row->{delivered};
-  }
-}
-
-sub _calc_qtys_query_match_parts {
-  my ($num_items) = @_;
-
-  my $query = <<SQL;
-    SELECT oi.id, doi.qty, doi.unit, doe.delivered
-    FROM record_links rl
-    INNER JOIN delivery_order_items doi ON (doi.delivery_order_id = rl.to_id)
-    INNER JOIN delivery_orders doe ON (doe.id = rl.to_id)
-    INNER JOIN orderitems oi ON (oi.trans_id = rl.from_id)
-    WHERE rl.from_table = 'oe'
-      AND rl.to_table   = 'delivery_orders'
-      AND oi.parts_id   = doi.parts_id
-      AND oi.id IN (@{[ join ', ', ("?")x $num_items ]})
-SQL
-
-  return $query;
-}
-
-sub _calc_qtys_query_linked_items {
-  my ($num_items) = @_;
-
-  my $query = <<SQL;
-    SELECT rl.from_id as id, doi.qty, doi.unit, doe.delivered
-    FROM record_links rl
-    INNER JOIN delivery_order_items doi ON (doi.id = rl.to_id)
-    INNER JOIN delivery_orders doe ON (doe.id = doi.delivery_order_id)
-    WHERE rl.from_table LIKE 'orderitems'
-      AND rl.to_table   LIKE 'delivery_order_items'
-      AND rl.from_id IN (@{[ join ', ', ("?")x $num_items ]})
-SQL
-
-  return $query;
+  SL::Helper::ShippedQty
+    ->new(fill_up => !$self->use_linked_items)
+    ->calculate($orderitems)
+    ->write_to_objects;
 }
 
 sub make_filter_summary {
