use SL::DB::Default;
use SL::DB::Unit;
use SL::DB::Price;
+use SL::DB::Part;
use SL::Helper::DateTime;
use SL::Helper::CreatePDF qw(:all);
+use SL::Controller::Helper::GetModels;
+
use List::Util qw(max first);
use List::MoreUtils qw(none pairwise first_index);
use English qw(-no_match_vars);
use Rose::Object::MakeMethods::Generic
(
- 'scalar --get_set_init' => [ qw(order valid_types type cv p) ],
+ scalar => [ qw(item_ids_to_delete) ],
+ 'scalar --get_set_init' => [ qw(order valid_types type cv p multi_items_models) ],
);
__PACKAGE__->run_before('_check_auth');
__PACKAGE__->run_before('_recalc',
- only => [ qw(edit update save save_and_delivery_order create_pdf send_email) ]);
+ only => [ qw(update save save_and_delivery_order create_pdf send_email) ]);
__PACKAGE__->run_before('_get_unalterable_data',
only => [ qw(save save_and_delivery_order create_pdf send_email) ]);
sub action_edit {
my ($self) = @_;
+ $self->_load_order;
+ $self->_recalc();
$self->_pre_render();
$self->render(
'order/form',
$self->js->render();
}
+sub action_unit_changed {
+ my ($self) = @_;
+
+ my $idx = first_index { $_ eq $::form->{item_id} } @{ $::form->{orderitem_ids} };
+ my $item = $self->order->items_sorted->[$idx];
+
+ my $old_unit_obj = SL::DB::Unit->new(name => $::form->{old_unit})->load;
+ $item->sellprice($item->unit_obj->convert_to($item->sellprice, $old_unit_obj));
+
+ $self->_recalc();
+
+ $self->js
+ ->run('update_sellprice', $::form->{item_id}, $item->sellprice_as_number);
+ $self->_js_redisplay_linetotals;
+ $self->_js_redisplay_amounts_and_taxes;
+ $self->js->render();
+}
+
sub action_add_item {
my ($self) = @_;
return unless $form_attr->{parts_id};
- my $item = SL::DB::OrderItem->new;
- $item->assign_attributes(%$form_attr);
+ my $item = _new_item($self->order, $form_attr);
+ $self->order->add_items($item);
- my $part = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
+ $self->_recalc();
- my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
+ my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
+ my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id);
- my $price_src;
- if ($item->sellprice) {
- $price_src = $price_source->price_from_source("");
- $price_src->price($item->sellprice);
- } else {
- $price_src = $price_source->best_price
- ? $price_source->best_price
- : $price_source->price_from_source("");
- $price_src->price(0) if !$price_source->best_price;
- }
+ $self->js
+ ->append('#row_table_id', $row_as_html)
+ ->val('.add_item_input', '')
+ ->run('row_table_scroll_down')
+ ->run('row_set_keyboard_events_by_id', $item_id)
+ ->run('set_unit_change_with_oldval_by_id', $item_id)
+ ->run('renumber_positions')
+ ->on('.recalc', 'change', 'recalc_amounts_and_taxes')
+ ->on('.reformat_number', 'change', 'reformat_number')
+ ->focus('#add_item_parts_id_name');
- my $discount_src;
- if ($item->discount) {
- $discount_src = $price_source->discount_from_source("");
- $discount_src->discount($item->discount);
+ $self->_js_redisplay_amounts_and_taxes;
+ $self->js->render();
+}
+
+sub action_show_multi_items_dialog {
+ require SL::DB::PartsGroup;
+ $_[0]->render('order/tabs/_multi_items_dialog', { layout => 0 },
+ all_partsgroups => SL::DB::Manager::PartsGroup->get_all);
+}
+
+sub action_multi_items_update_result {
+ my $max_count = 100;
+ my $count = $_[0]->multi_items_models->count;
+
+ if ($count == 0) {
+ my $text = SL::Presenter::EscapedText->new(text => $::locale->text('No results.'));
+ $_[0]->render($text, { layout => 0 });
+ } elsif ($count > $max_count) {
+ my $text = SL::Presenter::EscapedText->new(text => $::locale->text('Too many results (#1 from #2).', $count, $max_count));
+ $_[0]->render($text, { layout => 0 });
} else {
- $discount_src = $price_source->best_discount
- ? $price_source->best_discount
- : $price_source->discount_from_source("");
- $discount_src->discount(0) if !$price_source->best_discount;
+ my $multi_items = $_[0]->multi_items_models->get;
+ $_[0]->render('order/tabs/_multi_items_result', { layout => 0 },
+ multi_items => $multi_items);
}
+}
- my %new_attr;
- $new_attr{part} = $part;
- $new_attr{description} = $part->description if ! $item->description;
- $new_attr{qty} = 1.0 if ! $item->qty;
- $new_attr{sellprice} = $price_src->price;
- $new_attr{discount} = $discount_src->discount;
- $new_attr{active_price_source} = $price_src;
- $new_attr{active_discount_source} = $discount_src;
-
- # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
- # they cannot be retrieved via custom_variables until the order/orderitem is
- # saved. Adding empty custom_variables to new orderitem here solves this problem.
- $new_attr{custom_variables} = [];
+sub action_add_multi_items {
+ my ($self) = @_;
- $item->assign_attributes(%new_attr);
+ my @form_attr = grep { $_->{qty_as_number} } @{ $::form->{add_multi_items} };
+ return $self->js->render() unless scalar @form_attr;
- $self->order->add_items($item);
+ my @items;
+ foreach my $attr (@form_attr) {
+ push @items, _new_item($self->order, $attr);
+ }
+ $self->order->add_items(@items);
$self->_recalc();
- my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
- my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id);
+ foreach my $item (@items) {
+ my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
+ my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id);
+
+ $self->js
+ ->append('#row_table_id', $row_as_html)
+ ->run('row_set_keyboard_events_by_id', $item_id)
+ ->run('set_unit_change_with_oldval_by_id', $item_id);
+ }
$self->js
- ->append('#row_table_id', $row_as_html)
- ->val('.add_item_input', '')
+ ->run('close_multi_items_dialog')
->run('row_table_scroll_down')
- ->run('row_set_keyboard_events_by_id', $item_id)
+ ->run('renumber_positions')
->on('.recalc', 'change', 'recalc_amounts_and_taxes')
->on('.reformat_number', 'change', 'reformat_number')
->focus('#add_item_parts_id_name');
$self->js->render();
}
+sub action_reorder_items {
+ my ($self) = @_;
+
+ my %sort_keys = (
+ partnumber => sub { $_[0]->part->partnumber },
+ description => sub { $_[0]->description },
+ qty => sub { $_[0]->qty },
+ sellprice => sub { $_[0]->sellprice },
+ discount => sub { $_[0]->discount },
+ );
+
+ my $method = $sort_keys{$::form->{order_by}};
+ my @to_sort = map { { old_pos => $_->position, order_by => $method->($_) } } @{ $self->order->items_sorted };
+ if ($::form->{sort_dir}) {
+ @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort;
+ } else {
+ @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort;
+ }
+ $self->js
+ ->run('redisplay_items', \@to_sort)
+ ->render;
+}
+
sub action_price_popup {
my ($self) = @_;
my $idx = first_index { $_ eq $::form->{item_id} } @{ $::form->{orderitem_ids} };
- my $item = $self->order->items->[$idx];
+ my $item = $self->order->items_sorted->[$idx];
$self->render_price_dialog($item);
}
sub _js_redisplay_linetotals {
my ($self) = @_;
- my @data = map {$::form->format_amount(\%::myconfig, $_->{linetotal}, 2, 0)} @{ $self->order->items };
+ my @data = map {$::form->format_amount(\%::myconfig, $_->{linetotal}, 2, 0)} @{ $self->order->items_sorted };
$self->js
->run('redisplay_linetotals', \@data);
}
}
sub init_order {
- _make_order();
+ $_[0]->_make_order;
+}
+
+sub init_multi_items_models {
+ SL::Controller::Helper::GetModels->new(
+ controller => $_[0],
+ model => 'Part',
+ with_objects => [ qw(unit_obj) ],
+ disable_plugin => 'paginated',
+ source => $::form->{multi_items},
+ sorted => {
+ _default => {
+ by => 'partnumber',
+ dir => 1,
+ },
+ partnumber => t8('Partnumber'),
+ description => t8('Description')}
+ );
}
sub _check_auth {
$self->js->render;
}
+sub _load_order {
+ my ($self) = @_;
+
+ return if !$::form->{id};
+
+ $self->order(SL::DB::Manager::Order->find_by(id => $::form->{id}));
+}
+
sub _make_order {
my ($self) = @_;
$order = SL::DB::Manager::Order->find_by(id => $::form->{id}) if $::form->{id};
$order ||= SL::DB::Order->new(orderitems => []);
+ my $form_orderitems = delete $::form->{order}->{orderitems};
$order->assign_attributes(%{$::form->{order}});
+ # remove deleted items
+ $self->item_ids_to_delete([]);
+ foreach my $idx (reverse 0..$#{$order->orderitems}) {
+ my $item = $order->orderitems->[$idx];
+ if (none { $item->id == $_->{id} } @{$form_orderitems}) {
+ splice @{$order->orderitems}, $idx, 1;
+ push @{$self->item_ids_to_delete}, $item->id;
+ }
+ }
+
+ my @items;
+ my $pos = 1;
+ foreach my $form_attr (@{$form_orderitems}) {
+ my $item = _make_item($order, $form_attr);
+ $item->position($pos);
+ push @items, $item;
+ $pos++;
+ }
+ $order->add_items(grep {!$_->id} @items);
+
return $order;
}
+
+# Make item objects from form values. For items already existing read from db.
+# Create a new item else. And assign attributes.
+sub _make_item {
+ my ($record, $attr) = @_;
+
+ my $item;
+ $item = first { $_->id == $attr->{id} } @{$record->items} if $attr->{id};
+
+ # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
+ # they cannot be retrieved via custom_variables until the order/orderitem is
+ # saved. Adding empty custom_variables to new orderitem here solves this problem.
+ $item ||= SL::DB::OrderItem->new(custom_variables => []);
+ $item->assign_attributes(%$attr);
+
+ return $item;
+}
+
+sub _new_item {
+ my ($record, $attr) = @_;
+
+ my $item = SL::DB::OrderItem->new;
+ $item->assign_attributes(%$attr);
+
+ my $part = SL::DB::Part->new(id => $attr->{parts_id})->load;
+ my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+
+ $item->unit($part->unit) if !$item->unit;
+
+ my $price_src;
+ if ($item->sellprice) {
+ $price_src = $price_source->price_from_source("");
+ $price_src->price($item->sellprice);
+ } else {
+ $price_src = $price_source->best_price
+ ? $price_source->best_price
+ : $price_source->price_from_source("");
+ $price_src->price(0) if !$price_source->best_price;
+ }
+
+ my $discount_src;
+ if ($item->discount) {
+ $discount_src = $price_source->discount_from_source("");
+ $discount_src->discount($item->discount);
+ } else {
+ $discount_src = $price_source->best_discount
+ ? $price_source->best_discount
+ : $price_source->discount_from_source("");
+ $discount_src->discount(0) if !$price_source->best_discount;
+ }
+
+ my %new_attr;
+ $new_attr{part} = $part;
+ $new_attr{description} = $part->description if ! $item->description;
+ $new_attr{qty} = 1.0 if ! $item->qty;
+ $new_attr{sellprice} = $price_src->price;
+ $new_attr{discount} = $discount_src->discount;
+ $new_attr{active_price_source} = $price_src;
+ $new_attr{active_discount_source} = $discount_src;
+
+ # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
+ # they cannot be retrieved via custom_variables until the order/orderitem is
+ # saved. Adding empty custom_variables to new orderitem here solves this problem.
+ $new_attr{custom_variables} = [];
+
+ $item->assign_attributes(%new_attr);
+
+ return $item;
+}
+
sub _recalc {
my ($self) = @_;
$db->do_transaction(
sub {
- $self->order->save();
+ SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete};
+ $self->order->save(cascade => 1);
}) || push(@{$errors}, $db->error);
return $errors;
$self->{current_employee_id} = SL::DB::Manager::Employee->current->id;
- foreach my $item (@{$self->order->items}) {
+ foreach my $item (@{$self->order->orderitems}) {
my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
$item->active_price_source( $price_source->price_from_source( $item->active_price_source ));
$item->active_discount_source($price_source->discount_from_source($item->active_discount_source));
-
}
if ($self->order->ordnumber && $::instance_conf->get_webdav) {