Revision 91abaf6c
Von Bernd Bleßmann vor etwa 8 Jahren hinzugefügt
SL/Controller/Order.pm | ||
---|---|---|
20 | 20 |
use SL::DB::Default; |
21 | 21 |
use SL::DB::Unit; |
22 | 22 |
use SL::DB::Price; |
23 |
use SL::DB::Part; |
|
23 | 24 |
|
24 | 25 |
use SL::Helper::DateTime; |
25 | 26 |
use SL::Helper::CreatePDF qw(:all); |
26 | 27 |
|
28 |
use SL::Controller::Helper::GetModels; |
|
29 |
|
|
27 | 30 |
use List::Util qw(max first); |
28 | 31 |
use List::MoreUtils qw(none pairwise first_index); |
29 | 32 |
use English qw(-no_match_vars); |
... | ... | |
31 | 34 |
|
32 | 35 |
use Rose::Object::MakeMethods::Generic |
33 | 36 |
( |
34 |
'scalar --get_set_init' => [ qw(order valid_types type cv p) ], |
|
37 |
'scalar --get_set_init' => [ qw(order valid_types type cv p multi_items_models) ],
|
|
35 | 38 |
); |
36 | 39 |
|
37 | 40 |
|
... | ... | |
333 | 336 |
|
334 | 337 |
return unless $form_attr->{parts_id}; |
335 | 338 |
|
336 |
my $item = SL::DB::OrderItem->new;
|
|
337 |
$item->assign_attributes(%$form_attr);
|
|
339 |
my $item = _make_item($self->order, $form_attr);
|
|
340 |
$self->order->add_items($item);
|
|
338 | 341 |
|
339 |
my $part = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
|
|
342 |
$self->_recalc();
|
|
340 | 343 |
|
341 |
my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order); |
|
344 |
my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); |
|
345 |
my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id); |
|
342 | 346 |
|
343 |
my $price_src; |
|
344 |
if ($item->sellprice) { |
|
345 |
$price_src = $price_source->price_from_source(""); |
|
346 |
$price_src->price($item->sellprice); |
|
347 |
} else { |
|
348 |
$price_src = $price_source->best_price |
|
349 |
? $price_source->best_price |
|
350 |
: $price_source->price_from_source(""); |
|
351 |
$price_src->price(0) if !$price_source->best_price; |
|
352 |
} |
|
347 |
$self->js |
|
348 |
->append('#row_table_id', $row_as_html) |
|
349 |
->val('.add_item_input', '') |
|
350 |
->run('row_table_scroll_down') |
|
351 |
->run('row_set_keyboard_events_by_id', $item_id) |
|
352 |
->on('.recalc', 'change', 'recalc_amounts_and_taxes') |
|
353 |
->on('.reformat_number', 'change', 'reformat_number') |
|
354 |
->focus('#add_item_parts_id_name'); |
|
353 | 355 |
|
354 |
my $discount_src; |
|
355 |
if ($item->discount) { |
|
356 |
$discount_src = $price_source->discount_from_source(""); |
|
357 |
$discount_src->discount($item->discount); |
|
356 |
$self->_js_redisplay_amounts_and_taxes; |
|
357 |
$self->js->render(); |
|
358 |
} |
|
359 |
|
|
360 |
sub action_show_multi_items_dialog { |
|
361 |
require SL::DB::PartsGroup; |
|
362 |
$_[0]->render('order/tabs/_multi_items_dialog', { layout => 0 }, |
|
363 |
all_partsgroups => SL::DB::Manager::PartsGroup->get_all); |
|
364 |
} |
|
365 |
|
|
366 |
sub action_multi_items_update_result { |
|
367 |
my $max_count = 100; |
|
368 |
my $count = $_[0]->multi_items_models->count; |
|
369 |
|
|
370 |
if ($count == 0) { |
|
371 |
my $text = SL::Presenter::EscapedText->new(text => $::locale->text('No results.')); |
|
372 |
$_[0]->render($text, { layout => 0 }); |
|
373 |
} elsif ($count > $max_count) { |
|
374 |
my $text = SL::Presenter::EscapedText->new(text => $::locale->text('Too many results (#1 from #2).', $count, $max_count)); |
|
375 |
$_[0]->render($text, { layout => 0 }); |
|
358 | 376 |
} else { |
359 |
$discount_src = $price_source->best_discount |
|
360 |
? $price_source->best_discount |
|
361 |
: $price_source->discount_from_source(""); |
|
362 |
$discount_src->discount(0) if !$price_source->best_discount; |
|
377 |
my $multi_items = $_[0]->multi_items_models->get; |
|
378 |
$_[0]->render('order/tabs/_multi_items_result', { layout => 0 }, |
|
379 |
multi_items => $multi_items); |
|
363 | 380 |
} |
381 |
} |
|
364 | 382 |
|
365 |
my %new_attr; |
|
366 |
$new_attr{part} = $part; |
|
367 |
$new_attr{description} = $part->description if ! $item->description; |
|
368 |
$new_attr{qty} = 1.0 if ! $item->qty; |
|
369 |
$new_attr{sellprice} = $price_src->price; |
|
370 |
$new_attr{discount} = $discount_src->discount; |
|
371 |
$new_attr{active_price_source} = $price_src; |
|
372 |
$new_attr{active_discount_source} = $discount_src; |
|
373 |
|
|
374 |
# add_custom_variables adds cvars to an orderitem with no cvars for saving, but |
|
375 |
# they cannot be retrieved via custom_variables until the order/orderitem is |
|
376 |
# saved. Adding empty custom_variables to new orderitem here solves this problem. |
|
377 |
$new_attr{custom_variables} = []; |
|
383 |
sub action_add_multi_items { |
|
384 |
my ($self) = @_; |
|
378 | 385 |
|
379 |
$item->assign_attributes(%new_attr); |
|
386 |
my @form_attr = grep { $_->{qty_as_number} } @{ $::form->{add_multi_items} }; |
|
387 |
return $self->js->render() unless scalar @form_attr; |
|
380 | 388 |
|
381 |
$self->order->add_items($item); |
|
389 |
my @items; |
|
390 |
foreach my $attr (@form_attr) { |
|
391 |
push @items, _make_item($self->order, $attr); |
|
392 |
} |
|
393 |
$self->order->add_items(@items); |
|
382 | 394 |
|
383 | 395 |
$self->_recalc(); |
384 | 396 |
|
385 |
my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); |
|
386 |
my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id); |
|
397 |
foreach my $item (@items) { |
|
398 |
my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); |
|
399 |
my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id); |
|
400 |
|
|
401 |
$self->js |
|
402 |
->append('#row_table_id', $row_as_html) |
|
403 |
->run('row_set_keyboard_events_by_id', $item_id); |
|
404 |
} |
|
387 | 405 |
|
388 | 406 |
$self->js |
389 |
->append('#row_table_id', $row_as_html) |
|
390 |
->val('.add_item_input', '') |
|
407 |
->run('close_multi_items_dialog') |
|
391 | 408 |
->run('row_table_scroll_down') |
392 |
->run('row_set_keyboard_events_by_id', $item_id) |
|
393 | 409 |
->on('.recalc', 'change', 'recalc_amounts_and_taxes') |
394 | 410 |
->on('.reformat_number', 'change', 'reformat_number') |
395 | 411 |
->focus('#add_item_parts_id_name'); |
... | ... | |
483 | 499 |
_make_order(); |
484 | 500 |
} |
485 | 501 |
|
502 |
sub init_multi_items_models { |
|
503 |
SL::Controller::Helper::GetModels->new( |
|
504 |
controller => $_[0], |
|
505 |
model => 'Part', |
|
506 |
with_objects => [ qw(unit_obj) ], |
|
507 |
disable_plugin => 'paginated', |
|
508 |
source => $::form->{multi_items}, |
|
509 |
sorted => { |
|
510 |
_default => { |
|
511 |
by => 'partnumber', |
|
512 |
dir => 1, |
|
513 |
}, |
|
514 |
partnumber => t8('Partnumber'), |
|
515 |
description => t8('Description')} |
|
516 |
); |
|
517 |
} |
|
518 |
|
|
486 | 519 |
sub _check_auth { |
487 | 520 |
my ($self) = @_; |
488 | 521 |
|
... | ... | |
565 | 598 |
return $order; |
566 | 599 |
} |
567 | 600 |
|
601 |
sub _make_item { |
|
602 |
my ($record, $attr) = @_; |
|
603 |
|
|
604 |
my $item = SL::DB::OrderItem->new; |
|
605 |
$item->assign_attributes(%$attr); |
|
606 |
|
|
607 |
my $part = SL::DB::Part->new(id => $attr->{parts_id})->load; |
|
608 |
my $price_source = SL::PriceSource->new(record_item => $item, record => $record); |
|
609 |
|
|
610 |
$item->unit($part->unit) if !$item->unit; |
|
611 |
|
|
612 |
my $price_src; |
|
613 |
if ($item->sellprice) { |
|
614 |
$price_src = $price_source->price_from_source(""); |
|
615 |
$price_src->price($item->sellprice); |
|
616 |
} else { |
|
617 |
$price_src = $price_source->best_price |
|
618 |
? $price_source->best_price |
|
619 |
: $price_source->price_from_source(""); |
|
620 |
$price_src->price(0) if !$price_source->best_price; |
|
621 |
} |
|
622 |
|
|
623 |
my $discount_src; |
|
624 |
if ($item->discount) { |
|
625 |
$discount_src = $price_source->discount_from_source(""); |
|
626 |
$discount_src->discount($item->discount); |
|
627 |
} else { |
|
628 |
$discount_src = $price_source->best_discount |
|
629 |
? $price_source->best_discount |
|
630 |
: $price_source->discount_from_source(""); |
|
631 |
$discount_src->discount(0) if !$price_source->best_discount; |
|
632 |
} |
|
633 |
|
|
634 |
my %new_attr; |
|
635 |
$new_attr{part} = $part; |
|
636 |
$new_attr{description} = $part->description if ! $item->description; |
|
637 |
$new_attr{qty} = 1.0 if ! $item->qty; |
|
638 |
$new_attr{sellprice} = $price_src->price; |
|
639 |
$new_attr{discount} = $discount_src->discount; |
|
640 |
$new_attr{active_price_source} = $price_src; |
|
641 |
$new_attr{active_discount_source} = $discount_src; |
|
642 |
|
|
643 |
# add_custom_variables adds cvars to an orderitem with no cvars for saving, but |
|
644 |
# they cannot be retrieved via custom_variables until the order/orderitem is |
|
645 |
# saved. Adding empty custom_variables to new orderitem here solves this problem. |
|
646 |
$new_attr{custom_variables} = []; |
|
647 |
|
|
648 |
$item->assign_attributes(%new_attr); |
|
649 |
|
|
650 |
return $item; |
|
651 |
} |
|
652 |
|
|
568 | 653 |
sub _recalc { |
569 | 654 |
my ($self) = @_; |
570 | 655 |
|
Auch abrufbar als: Unified diff
Auftrags-Controller: Mehrfach-Artikelauswahl mit Mengeneingabe.