From 3f0ed511a589aca9642391108a960ef4e8a6286d Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 6 Apr 2016 13:58:04 +0200 Subject: [PATCH] Shipto: Methode zum Clonen in SL::DB::Shipto und nicht in DeliveryOrder->new_from --- SL/DB/DeliveryOrder.pm | 11 +----- SL/DB/Shipto.pm | 79 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/SL/DB/DeliveryOrder.pm b/SL/DB/DeliveryOrder.pm index 284c15fc8..bc518018e 100644 --- a/SL/DB/DeliveryOrder.pm +++ b/SL/DB/DeliveryOrder.pm @@ -136,16 +136,7 @@ sub new_from { # save it, too. my $custom_shipto; if (!$source->shipto_id && $source->id) { - my $old = $source->custom_shipto; - if ($old) { - $custom_shipto = SL::DB::Shipto->new( - map { +($_ => $old->$_) } - grep { !m{^ (?: itime | mtime | shipto_id | trans_id ) $}x } - map { $_->name } - @{ $old->meta->columns } - ); - $custom_shipto->module('DO'); - } + $custom_shipto = $source->custom_shipto->clone($class) if $source->can('custom_shipto') && $source->custom_shipto; } else { $args{shipto_id} = $source->shipto_id; diff --git a/SL/DB/Shipto.pm b/SL/DB/Shipto.pm index 5e2813d2f..a0580e37f 100644 --- a/SL/DB/Shipto.pm +++ b/SL/DB/Shipto.pm @@ -2,6 +2,8 @@ package SL::DB::Shipto; use strict; +use Carp; + use SL::DB::MetaSetup::Shipto; use SL::DB::Manager::Shipto; use SL::DB::Helper::CustomVariables ( @@ -43,4 +45,81 @@ sub detach { $_[0]; } +sub clone { + my ($self, $target) = @_; + + my $type = ref($target) || $target; + my $module = $type =~ m{::Order$} ? 'OE' + : $type =~ m{::DeliveryOrder$} ? 'DO' + : $type =~ m{::Invoice$} ? 'AR' + : $type =~ m{::(?:Customer|Vendor)$} ? 'CT' + : croak "Unsupported target class '$type'"; + + my $new_shipto = SL::DB::Shipto->new( + (map { +($_ => $self->$_) } + grep { !m{^ (?: itime | mtime | shipto_id | trans_id ) $}x } + map { $_->name } + @{ $self->meta->columns }), + module => $module, + ); + + return $new_shipto; +} + 1; + +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Shipto - Database model for shipping addresses + +=head1 SYNOPSIS + + my $order = SL::DB::Order->new(id => …)->load; + if ($order->custom_shipto) { + my $cloned_shipto = $order->custom_shipto->clone('SL::DB::Invoice'); + } + +=head1 FUNCTIONS + +=over 4 + +=item C + +Creates and returns a clone of the current object. The mandatory +parameter C<$target> must be either an instance of a Rose DB class or +the name of one. It's used for setting the new instance's C +attribute to the correct value. + +Currently the following classes are supported: + +=over 2 + +=item C + +=item C + +=item C + +=item C + +=item C + +=back + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut -- 2.20.1