Revision 10c6ffbe
Von Bernd Bleßmann vor etwa 1 Jahr hinzugefügt
SL/Controller/Order.pm | ||
---|---|---|
19 | 19 |
use SL::DB::AuthUser; |
20 | 20 |
use SL::DB::History; |
21 | 21 |
use SL::DB::Order; |
22 |
use SL::DB::OrderItem; |
|
22 | 23 |
use SL::DB::Default; |
23 | 24 |
use SL::DB::Unit; |
24 | 25 |
use SL::DB::Part; |
... | ... | |
229 | 230 |
sub action_save { |
230 | 231 |
my ($self) = @_; |
231 | 232 |
|
232 |
my $errors = $self->save(); |
|
233 |
|
|
234 |
if (scalar @{ $errors }) { |
|
235 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
236 |
return $self->js->render(); |
|
237 |
} |
|
233 |
$self->save(); |
|
238 | 234 |
|
239 | 235 |
my $text = $self->type eq sales_order_intake_type() ? $::locale->text('The order intake has been saved') |
240 | 236 |
: $self->type eq sales_order_type() ? $::locale->text('The order confirmation has been saved') |
... | ... | |
269 | 265 |
SL::DB->client->with_transaction( |
270 | 266 |
sub { |
271 | 267 |
SL::Model::Record->increment_subversion($self->order); |
272 |
|
|
273 |
# Todo: Call SL::Model::Record->save when implemented |
|
274 |
# SL::Model::Record->save($self->order); |
|
275 |
my $errors = $self->save(); |
|
276 |
die join "\n", @{ $errors } if scalar @{ $errors }; |
|
277 |
|
|
268 |
$self->save(); |
|
278 | 269 |
1; |
279 | 270 |
} |
280 | 271 |
); |
... | ... | |
357 | 348 |
sub action_print { |
358 | 349 |
my ($self) = @_; |
359 | 350 |
|
360 |
my $errors = $self->save(); |
|
361 |
|
|
362 |
if (scalar @{ $errors }) { |
|
363 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
364 |
return $self->js->render(); |
|
365 |
} |
|
351 |
$self->save(); |
|
366 | 352 |
|
367 | 353 |
$self->js_reset_order_and_item_ids_after_save; |
368 | 354 |
|
... | ... | |
446 | 432 |
sub action_preview_pdf { |
447 | 433 |
my ($self) = @_; |
448 | 434 |
|
449 |
my $errors = $self->save(); |
|
450 |
if (scalar @{ $errors }) { |
|
451 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
452 |
return $self->js->render(); |
|
453 |
} |
|
435 |
$self->save(); |
|
454 | 436 |
|
455 | 437 |
$self->js_reset_order_and_item_ids_after_save; |
456 | 438 |
|
... | ... | |
505 | 487 |
my ($self) = @_; |
506 | 488 |
|
507 | 489 |
if (!$self->is_final_version) { |
508 |
my $errors = $self->save(); |
|
509 |
|
|
510 |
if (scalar @{ $errors }) { |
|
511 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
512 |
return $self->js->render(); |
|
513 |
} |
|
490 |
$self->save(); |
|
514 | 491 |
|
515 | 492 |
$self->js_reset_order_and_item_ids_after_save; |
516 | 493 |
} |
... | ... | |
575 | 552 |
my ($self) = @_; |
576 | 553 |
|
577 | 554 |
if (!$self->is_final_version) { |
578 |
my $errors = $self->save(); |
|
579 |
|
|
580 |
if (scalar @{ $errors }) { |
|
555 |
eval { |
|
556 |
$self->save(); |
|
557 |
1; |
|
558 |
} or do { |
|
581 | 559 |
$self->js->run('kivi.Order.close_email_dialog'); |
582 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
583 |
return $self->js->render(); |
|
584 |
} |
|
560 |
die $EVAL_ERROR; |
|
561 |
}; |
|
585 | 562 |
|
586 | 563 |
$self->js_reset_order_and_item_ids_after_save; |
587 | 564 |
} |
... | ... | |
863 | 840 |
sub action_save_and_reclamation { |
864 | 841 |
my ($self) = @_; |
865 | 842 |
|
866 |
# cann't use save_and_redirect_to, because id is set! |
|
867 |
my $errors = $self->save(); |
|
868 |
if (scalar @{ $errors }) { |
|
869 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
870 |
return $self->js->render(); |
|
871 |
} |
|
843 |
# can't use save_and_redirect_to, because id is set! |
|
844 |
$self->save(); |
|
872 | 845 |
|
873 | 846 |
my $to_type = $self->order->is_sales ? 'sales_reclamation' |
874 | 847 |
: 'purchase_reclamation'; |
... | ... | |
2162 | 2135 |
sub save { |
2163 | 2136 |
my ($self) = @_; |
2164 | 2137 |
|
2165 |
$self->recalc(); |
|
2166 |
$self->get_unalterable_data(); |
|
2167 |
|
|
2168 |
my $errors = []; |
|
2169 |
my $db = $self->order->db; |
|
2170 |
|
|
2171 | 2138 |
if (scalar @{$self->order->items} == 0 && !grep { $self->type eq $_ } @{$::instance_conf->get_allowed_documents_with_no_positions() || []}) { |
2172 | 2139 |
return [t8('The action you\'ve chosen has not been executed because the document does not contain any item yet.')]; |
2173 | 2140 |
} |
... | ... | |
2195 | 2162 |
$self->order->add_phone_notes($phone_note) if $is_new; |
2196 | 2163 |
} |
2197 | 2164 |
|
2165 |
# create first version if none exists |
|
2166 |
$self->order->add_order_version(SL::DB::OrderVersion->new(version => 1)) if !$self->order->order_version; |
|
2167 |
|
|
2198 | 2168 |
my @converted_from_oe_ids; |
2199 | 2169 |
if ($::form->{converted_from_oe_id}) { |
2200 | 2170 |
@converted_from_oe_ids = split ' ', $::form->{converted_from_oe_id}; |
... | ... | |
2214 | 2184 |
} |
2215 | 2185 |
|
2216 | 2186 |
my $is_new = !$self->order->id; |
2217 |
$db->with_transaction(sub { |
|
2218 |
my $validity_token; |
|
2219 |
if (!$self->order->id) { |
|
2220 |
$validity_token = SL::DB::Manager::ValidityToken->fetch_valid_token( |
|
2221 |
scope => SL::DB::ValidityToken::SCOPE_ORDER_SAVE(), |
|
2222 |
token => $::form->{form_validity_token}, |
|
2223 |
); |
|
2224 |
|
|
2225 |
die $::locale->text('The form is not valid anymore.') if !$validity_token; |
|
2226 |
} |
|
2227 |
|
|
2228 |
# delete custom shipto if it is to be deleted or if it is empty |
|
2229 |
if ($self->order->custom_shipto && ($self->is_custom_shipto_to_delete || $self->order->custom_shipto->is_empty)) { |
|
2230 |
$self->order->custom_shipto->delete if $self->order->custom_shipto->shipto_id; |
|
2231 |
$self->order->custom_shipto(undef); |
|
2232 |
} |
|
2233 |
|
|
2234 |
SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete || []}; |
|
2235 |
$self->order->save(cascade => 1); |
|
2236 |
# create first version if none exists |
|
2237 |
SL::DB::OrderVersion->new(oe_id => $self->order->id, version => 1)->save unless scalar @{ $self->order->order_version }; |
|
2238 |
|
|
2239 |
# link records |
|
2240 |
if (@converted_from_oe_ids) { |
|
2241 |
$self->link_requirement_specs_linking_to_created_from_objects(@converted_from_oe_ids); |
|
2242 |
} |
|
2243 | 2187 |
|
2244 |
$self->set_project_in_linked_requirement_specs if $self->order->globalproject_id; |
|
2188 |
my $items_to_delete = scalar @{ $self->item_ids_to_delete || [] } |
|
2189 |
? SL::DB::Manager::OrderItem->get_all(where => [id => $self->item_ids_to_delete]) |
|
2190 |
: undef; |
|
2245 | 2191 |
|
2246 |
# Close reachable sales order intakes in the from-workflow if this is a sales order |
|
2247 |
if (sales_order_type() eq $self->type) { |
|
2248 |
my $lr = $self->order->linked_records(direction => 'from', recursive => 1); |
|
2249 |
$lr = [grep { 'SL::DB::Order' eq ref $_ && !$_->closed && $_->is_type('sales_order_intake')} @$lr]; |
|
2250 |
if (@$lr) { |
|
2251 |
SL::DB::Manager::Order->update_all(set => {closed => 1}, |
|
2252 |
where => [id => [map {$_->id} @$lr]]); |
|
2253 |
} |
|
2254 |
} |
|
2255 |
|
|
2256 |
$self->save_history('SAVED'); |
|
2257 |
|
|
2258 |
$validity_token->delete if $validity_token; |
|
2259 |
delete $::form->{form_validity_token}; |
|
2260 |
|
|
2261 |
1; |
|
2262 |
}) || push(@{$errors}, $db->error); |
|
2192 |
SL::Model::Record->save($self->order, |
|
2193 |
with_validity_token => { scope => SL::DB::ValidityToken::SCOPE_ORDER_SAVE(), token => $::form->{form_validity_token} }, |
|
2194 |
delete_custom_shipto => $self->is_custom_shipto_to_delete || $self->order->custom_shipto->is_empty, |
|
2195 |
items_to_delete => $items_to_delete, |
|
2196 |
history => { snumbers => $self->get_history_snumbers() }, |
|
2197 |
); |
|
2263 | 2198 |
|
2264 | 2199 |
if ($is_new && $self->order->is_sales) { |
2265 | 2200 |
my $imap_client = SL::IMAPClient->new(); |
... | ... | |
2268 | 2203 |
} |
2269 | 2204 |
} |
2270 | 2205 |
|
2271 |
|
|
2272 |
return $errors; |
|
2206 |
delete $::form->{form_validity_token}; |
|
2273 | 2207 |
} |
2274 | 2208 |
|
2275 | 2209 |
sub pre_render { |
... | ... | |
2897 | 2831 |
sub save_and_redirect_to { |
2898 | 2832 |
my ($self, %params) = @_; |
2899 | 2833 |
|
2900 |
my $errors = $self->save(); |
|
2901 |
|
|
2902 |
if (scalar @{ $errors }) { |
|
2903 |
$self->js->flash('error', $_) foreach @{ $errors }; |
|
2904 |
return $self->js->render(); |
|
2905 |
} |
|
2834 |
$self->save(); |
|
2906 | 2835 |
|
2907 | 2836 |
my $text = $self->type eq sales_order_intake_type() ? $::locale->text('The order intake has been saved') |
2908 | 2837 |
: $self->type eq sales_order_type() ? $::locale->text('The order confirmation has been saved') |
Auch abrufbar als: Unified diff
Order-Controller: Model::Record->save verwenden