convert_to_delivery_order um record_links auf item-Ebene erweitert
authorJan Büren <jan@kivitendo-premium.de>
Thu, 3 Dec 2015 20:01:10 +0000 (21:01 +0100)
committerJan Büren <jan@kivitendo-premium.de>
Thu, 3 Dec 2015 20:01:10 +0000 (21:01 +0100)
Bisher wurden nur die Belege verknüpft und nicht die einzelnen
Items. Analoge Implementierung wie bei convert_to_invoice.
Sinnvoll wäre ein Auslagern, dieser "zu ähnlichen" Verfahren in beiden
Fällen

SL/DB/DeliveryOrder.pm
SL/DB/Order.pm

index d15b3fa..284c15f 100644 (file)
@@ -164,14 +164,15 @@ sub new_from {
     $item_parents{$source_item_id} ||= $source_item->$item_parent_column;
     my $item_parent                  = $item_parents{$source_item_id};
 
-    SL::DB::DeliveryOrderItem->new(map({ ( $_ => $source_item->$_ ) }
+    my $current_do_item = SL::DB::DeliveryOrderItem->new(map({ ( $_ => $source_item->$_ ) }
                                          qw(base_qty cusordnumber description discount lastcost longdescription marge_price_factor parts_id price_factor price_factor_id
                                             project_id qty reqdate sellprice serialnumber transdate unit active_discount_source active_price_source
                                          )),
                                    custom_variables => \@custom_variables,
                                    ordnumber        => ref($item_parent) eq 'SL::DB::Order' ? $item_parent->ordnumber : $source_item->ordnumber,
                                  );
-
+    $current_do_item->{"converted_from_orderitems_id"} = $_->{id} if ref($item_parent) eq 'SL::DB::Order';
+    $current_do_item;
   } @{ $items };
 
   @items = grep { $params{item_filter}->($_) } @items if $params{item_filter};
@@ -197,8 +198,9 @@ sub convert_to_invoice {
     require SL::DB::Invoice;
     $invoice = SL::DB::Invoice->new_from($self, %params)->post || 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(delivery_order_items)) {    # expand if needed (delivery_order_items)
+      foreach (qw(delivery_order_items)) {    # expand if needed (orderitems)
         if ($item->{"converted_from_${_}_id"}) {
           die unless $item->{id};
           RecordLinks->create_links('mode'       => 'ids',
index d61ec31..4d0a2fa 100644 (file)
@@ -164,6 +164,22 @@ sub convert_to_delivery_order {
     $delivery_order->save;
     $custom_shipto->save if $custom_shipto;
     $self->link_to_record($delivery_order);
+    # TODO extend link_to_record for items, otherwise long-term no d.r.y.
+    foreach my $item (@{ $delivery_order->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'   => 'delivery_order_items',
+                                    'to_id'      => $item->{id},
+          ) || die;
+          delete $item->{"converted_from_${_}_id"};
+        }
+      }
+    }
+
     $self->update_attributes(delivered => 1);
     1;
   })) {