From 491a3c27055cabce59b4d93a6e389a59125eeef7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Mon, 2 Nov 2015 15:48:11 +0100 Subject: [PATCH] Auftrags-Controller: Mehrfach-Artikelauswahl mit Mengeneingabe. Erste, sehr einfache Version als Diskussionsgrundlage. --- SL/Controller/Order.pm | 144 ++++++++++++------ .../order/tabs/_multi_items_dialog.html | 35 +++++ templates/webpages/order/tabs/basic_data.html | 40 +++++ 3 files changed, 174 insertions(+), 45 deletions(-) create mode 100644 templates/webpages/order/tabs/_multi_items_dialog.html diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 38f511cb5..523334f3c 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -287,51 +287,7 @@ sub action_add_item { return unless $form_attr->{parts_id}; - my $item = SL::DB::OrderItem->new; - $item->assign_attributes(%$form_attr); - - my $part = SL::DB::Part->new(id => $form_attr->{parts_id})->load; - - my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order); - - 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); - + my $item = _make_item($self->order, $form_attr); $self->order->add_items($item); $self->_recalc(); @@ -357,6 +313,52 @@ sub action_add_item { $self->js->render(); } +sub action_show_multi_items_dialog { + my ($self) = @_; + + $self->{multi_items}->{parts} = SL::DB::Manager::Part->get_all_sorted(where => [ obsolete => 0 ]); + my $dialog_html = $self->render('order/tabs/_multi_items_dialog', { output => 0 }); + + $self->js + ->run('show_multi_items_dialog', $dialog_html, t8('Add multiple parts')) + ->reinit_widgets + ->render(); +} + +sub action_add_multi_items { + my ($self) = @_; + + my @form_attr = grep { $_->{qty} } @{ $::form->{add_multi_items} }; + return unless scalar @form_attr; + + my @items; + foreach my $attr (@form_attr) { + push @items, _make_item($self->order, $attr); + } + $self->order->add_items(@items); + + $self->_recalc(); + + 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); + } + + $self->js + ->run('close_multi_items_dialog') + ->run('row_table_scroll_down') + ->on('.recalc', 'change', 'recalc_amounts_and_taxes') + ->on('.reformat_number', 'change', 'reformat_number') + ->focus('#add_item_parts_id_name'); + + $self->_js_redisplay_amounts_and_taxes; + $self->js->render(); +} + sub action_recalc_amounts_and_taxes { my ($self) = @_; @@ -524,6 +526,58 @@ sub _make_order { return $order; } +sub _make_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) = @_; diff --git a/templates/webpages/order/tabs/_multi_items_dialog.html b/templates/webpages/order/tabs/_multi_items_dialog.html new file mode 100644 index 000000000..45c5b92f8 --- /dev/null +++ b/templates/webpages/order/tabs/_multi_items_dialog.html @@ -0,0 +1,35 @@ +[%- USE T8 %][%- USE HTML %][%- USE L %][%- USE LxERP %] + +
+

[%- 'Add multiple parts' | $T8 %] [%- SELF.type | $T8 %]

+ + + [%- FOREACH part = SELF.multi_items.parts %] + + + + + + + [%- END %] +
+ [% L.hidden_tag("add_multi_items[+].parts_id", part.id) %] + [% L.input_tag("add_multi_items[].qty", '', size => 5) %] + [% HTML.escape(part.unit) %][% HTML.escape(part.partnumber) %][% HTML.escape(part.description) %]
+ +
+[% L.hidden_tag('action', 'Order/dispatch') %] +[% L.button_tag('add_multi_items()', LxERP.t8('Continue')) %] +[%- LxERP.t8("Cancel") %] + + + +
diff --git a/templates/webpages/order/tabs/basic_data.html b/templates/webpages/order/tabs/basic_data.html index e7ab1380d..508db12a5 100644 --- a/templates/webpages/order/tabs/basic_data.html +++ b/templates/webpages/order/tabs/basic_data.html @@ -124,6 +124,8 @@ [%- PROCESS order/tabs/_item_input.html %] + [% L.button_tag('setup_multi_items_dialog()', LxERP.t8('Add multiple parts')) %] +
@@ -262,6 +264,44 @@ function add_item() { $.post("controller.pl", data, kivi.eval_json_result); } +function setup_multi_items_dialog() { + if (!check_cv()) return; + var data = $('#order_form').serialize(); + data += '&action=Order/show_multi_items_dialog'; + + $.post("controller.pl", data, kivi.eval_json_result); +} + +var multi_items_dialog; + +function show_multi_items_dialog(html, title) { + var id = 'jq_multi_items_dialog'; + var dialog_params = { + id: id, + width: 800, + height: 500, + modal: true, + close: function(event, ui) { + multi_items_dialog.remove(); + }, + }; + + $('#' + id).remove(); + + multi_items_dialog = $('').appendTo('body'); + multi_items_dialog.attr('title', title); + multi_items_dialog.html(html); + multi_items_dialog.dialog(dialog_params); + + $('.cancel').click(close_multi_items_dialog); + + return true; +} + +var close_multi_items_dialog = function() { + multi_items_dialog.dialog('close'); +} + function delete_order_item_row(clicked) { var row = $(clicked).parents("tbody").first(); $(row).remove(); -- 2.20.1