Verknüpfung von Einzelpositionen auch bei periodisch erzeugten Rechnungen (Auftrag...
authorJan Büren <jan@kivitendo-premium.de>
Tue, 24 Feb 2015 13:12:34 +0000 (14:12 +0100)
committerJan Büren <jan@kivitendo-premium.de>
Tue, 24 Feb 2015 13:12:34 +0000 (14:12 +0100)
Für InvoiceItem in Invoice ein converted_from_*_id hinzugefügt wie bei der Verknüpfung von Auftrag zu
Rechnung, bzw. Lieferschein zu Rechnung. Letzterer Weg wird aktuell bei Objekten noch nicht
benutzt. Ferner einen Rückgabewert von create_links (RecordLinks) etwas verbessert (undef bei nicht
 erfolgtem Anlegen).
Offen: Implementierung der Verknüpfung von DeliveryOrderItem-Objekten mit InvoiceItem-Objekten.
Diese werden aktuell nicht benutzt und betrifft auch nicht periodische Rechnungen.

SL/BackgroundJob/CreatePeriodicInvoices.pm
SL/DB/Helper/LinkedRecords.pm
SL/DB/Invoice.pm
SL/RecordLinks.pm

index ce89577..5055083 100644 (file)
@@ -143,6 +143,8 @@ sub _replace_vars {
 }
 
 sub _create_periodic_invoice {
+  $main::lxdebug->enter_sub();
+
   my $self              = shift;
   my $config            = shift;
   my $period_start_date = shift;
@@ -188,6 +190,21 @@ sub _create_periodic_invoice {
 
     $order->link_to_record($invoice);
 
+    foreach my $item (@{ $invoice->items }) {
+      foreach (qw(orderitems)) {    # expand if needed (delivery_order_items)
+          if ($item->{"converted_from_${_}_id"}) {
+            die unless $item->{id};
+            RecordLinks->create_links('mode'       => 'ids',
+                                      'from_table' => $_,
+                                      'from_ids'   => $item->{"converted_from_${_}_id"},
+                                      'to_table'   => 'invoice',
+                                      'to_id'      => $item->{id},
+            ) || die;
+            delete $item->{"converted_from_${_}_id"};
+         }
+      }
+    }
+
     SL::DB::PeriodicInvoice->new(config_id         => $config->id,
                                  ar_id             => $invoice->id,
                                  period_start_date => $period_start_date)
@@ -198,7 +215,7 @@ sub _create_periodic_invoice {
     $::lxdebug->message(LXDebug->WARN(), "_create_invoice failed: " . join("\n", (split(/\n/, $self->{db_obj}->db->error))[0..2]));
     return undef;
   }
-
+  $main::lxdebug->leave_sub();
   return $invoice;
 }
 
index bb21ecf..1151981 100644 (file)
@@ -384,7 +384,7 @@ array reference.
 Examples:
 
 If you only need invoices created directly from an order C<$order> (no
-delivery orders inbetween) then the call could look like this:
+delivery orders in between) then the call could look like this:
 
   my $invoices = $order->linked_records(
     direction => 'to',
index e8b49c4..a64ef2f 100644 (file)
@@ -193,17 +193,20 @@ sub new_from {
 
     $item_parents{$source_item_id} ||= $source_item->$item_parent_column;
     my $item_parent                  = $item_parents{$source_item_id};
-
-    SL::DB::InvoiceItem->new(map({ ( $_ => $source_item->$_ ) }
-                                 qw(parts_id description qty sellprice discount project_id serialnumber pricegroup_id transdate cusordnumber unit
-                                    base_qty longdescription lastcost price_factor_id active_discount_source active_price_source), @item_columns),
-                             deliverydate     => $source_item->reqdate,
-                             fxsellprice      => $source_item->sellprice,
-                             custom_variables => \@custom_variables,
-                             ordnumber        => ref($item_parent) eq 'SL::DB::Order'         ? $item_parent->ordnumber : $source_item->ordnumber,
-                             donumber         => ref($item_parent) eq 'SL::DB::DeliveryOrder' ? $item_parent->donumber  : $source_item->can('donumber') ? $source_item->donumber : '',
-                           );
-
+    my $current_invoice_item =
+      SL::DB::InvoiceItem->new(map({ ( $_ => $source_item->$_ ) }
+                                   qw(parts_id description qty sellprice discount project_id serialnumber pricegroup_id transdate cusordnumber unit
+                                      base_qty longdescription lastcost price_factor_id active_discount_source active_price_source), @item_columns),
+                               deliverydate     => $source_item->reqdate,
+                               fxsellprice      => $source_item->sellprice,
+                               custom_variables => \@custom_variables,
+                               ordnumber        => ref($item_parent) eq 'SL::DB::Order'         ? $item_parent->ordnumber : $source_item->ordnumber,
+                               donumber         => ref($item_parent) eq 'SL::DB::DeliveryOrder' ? $item_parent->donumber  : $source_item->can('donumber') ? $source_item->donumber : '',
+                             );
+
+    $current_invoice_item->{"converted_from_orderitems_id"}           = $_->{id} if ref($item_parent) eq 'SL::DB::Order';
+    $current_invoice_item->{"converted_from_delivery_order_items_id"} = $_->{id} if ref($item_parent) eq 'SL::DB::DeliveryOrder';
+    $current_invoice_item;
   } @{ $items };
 
   @items = grep { $_->qty * 1 } @items if $params{skip_items_zero_qty};
index 77086ba..0bd62fa 100644 (file)
@@ -48,7 +48,7 @@ sub create_links {
 
   if (!scalar @links) {
     $main::lxdebug->leave_sub();
-    return;
+    return undef;
   }
 
   my $myconfig = \%main::myconfig;