S:D:Order: convert_to_invoice: items verlinken
authorBernd Bleßmann <bernd@kivitendo-premium.de>
Tue, 28 Sep 2021 07:13:10 +0000 (09:13 +0200)
committerBernd Bleßmann <bernd@kivitendo-premium.de>
Tue, 28 Sep 2021 07:14:13 +0000 (09:14 +0200)
SL/DB/Order.pm

index f1cfba7..cfe8af4 100644 (file)
@@ -240,6 +240,21 @@ sub convert_to_invoice {
     require SL::DB::Invoice;
     $invoice = SL::DB::Invoice->new_from($self)->post(%params) || die;
     $self->link_to_record($invoice);
+    # TODO extend link_to_record for items, otherwise long-term no d.r.y.
+    foreach my $item (@{ $invoice->items }) {
+      foreach (qw(orderitems)) {
+        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"};
+        }
+      }
+    }
     $self->update_attributes(closed => 1);
     1;
   })) {