Finanzcontrollingbericht: Stornorechnungen berücksichtigen
authorMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 31 Jan 2013 11:34:02 +0000 (12:34 +0100)
committerSven Schöling <s.schoeling@linet-services.de>
Tue, 17 Dec 2013 13:36:34 +0000 (14:36 +0100)
SL/Controller/FinancialControllingReport.pm

index 58df26b..2e62e0f 100644 (file)
@@ -138,13 +138,18 @@ sub calculate_data {
   my ($self) = @_;
 
   foreach my $order (@{ $self->{orders} }) {
-    my $delivery_orders = $order->linked_records(direction => 'to', to => 'DeliveryOrder', via => 'Order', query => [ '!customer_id' => undef ]);
-    my $invoices        = $order->linked_records(direction => 'to', to => 'Invoice',       via => [ 'Order', 'DeliveryOrder' ]);
-
-    $order->{delivered_amount}  = sum map { $self->sum_relevant_items(order => $order, other => $_, by_order => 1) } @{ $delivery_orders };
-    $order->{billed_amount}     = sum map { $self->sum_relevant_items(order => $order, other => $_)                } @{ $invoices        };
-    $order->{paid_amount}       = sum map { $_->paid                                                               } @{ $invoices        };
-    my $billed_amount           = sum map { $_->netamount                                                          } @{ $invoices        };
+    my @delivery_orders = @{ $order->linked_records(direction => 'to', to => 'DeliveryOrder', via => 'Order', query => [ '!customer_id' => undef ]) };
+    my @invoices        = @{ $order->linked_records(direction => 'to', to => 'Invoice',       via => [ 'Order', 'DeliveryOrder' ])                  };
+
+    @invoices = (
+      grep({ !$_->storno_id } @invoices),
+      map({ @{ $_->storno_invoices } } grep { $_->storno && !$_->storno_id } @invoices),
+    );
+
+    $order->{delivered_amount}  = sum map { $self->sum_relevant_items(order => $order, other => $_, by_order => 1) } @delivery_orders;
+    $order->{billed_amount}     = sum map { $self->sum_relevant_items(order => $order, other => $_)                } @invoices;
+    $order->{paid_amount}       = sum map { $_->paid                                                               } @invoices;
+    my $billed_amount           = sum map { $_->netamount                                                          } @invoices;
     $order->{other_amount}      = $billed_amount             - $order->{billed_amount};
     $order->{billable_amount}   = $order->{delivered_amount} - $order->{billed_amount};