From 89a2d4f109e72abfd5d9423e2cb4d61e5193ad08 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Tue, 3 Apr 2018 14:05:46 +0200 Subject: [PATCH] Auftrags-Controller: bei als neu speichern auch ein neues Objekt erzeugen Behebt #351 (redmine). --- SL/Controller/Order.pm | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index b3477b2e3..36896ee11 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -137,32 +137,44 @@ sub action_save { sub action_save_as_new { my ($self) = @_; - if (!$self->order->id) { + my $order = $self->order; + + if (!$order->id) { $self->js->flash('error', t8('This object has not been saved yet.')); return $self->js->render(); } - delete $::form->{$_} for qw(closed delivered converted_from_oe_id converted_from_orderitems_ids); - - my $src_order = SL::DB::Order->new(id => $self->order->id)->load; + # load order from db to check if values changed + my $saved_order = SL::DB::Order->new(id => $order->id)->load; + my %new_attrs; # Lets assign a new number if the user hasn't changed the previous one. # If it has been changed manually then use it as-is. - if (trim($self->order->number) eq $src_order->number) { - $self->order->number(''); - } + $new_attrs{number} = (trim($order->number) eq $saved_order->number) + ? '' + : trim($order->number); - # Clear reqdate and transdate unless changed - if ($self->order->transdate == $src_order->transdate) { - $self->order->transdate(DateTime->today_local) - } - if ($self->order->reqdate == $src_order->reqdate) { + # Clear transdate unless changed + $new_attrs{transdate} = ($order->transdate == $saved_order->transdate) + ? DateTime->today_local + : $order->transdate; + + # Set new reqdate unless changed + if ($order->reqdate == $saved_order->reqdate) { my $extra_days = $self->type eq _sales_quotation_type() ? $::instance_conf->get_reqdate_interval : 1; - $self->order->reqdate(DateTime->today_local->next_workday(extra_days => $extra_days)); + $new_attrs{reqdate} = DateTime->today_local->next_workday(extra_days => $extra_days); + } else { + $new_attrs{reqdate} = $order->reqdate; } # Update employee - $self->order->employee(SL::DB::Manager::Employee->current); + $new_attrs{employee} = SL::DB::Manager::Employee->current; + + # Create new record from current one + $self->order(SL::DB::Order->new_from($order, destination_type => $order->type, attributes => \%new_attrs)); + + # no linked records on save as new + delete $::form->{$_} for qw(converted_from_oe_id converted_from_orderitems_ids); # save $self->action_save(); -- 2.20.1