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();
$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) = @_;
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) = @_;
--- /dev/null
+[%- USE T8 %][%- USE HTML %][%- USE L %][%- USE LxERP %]
+
+<form method="post" id="multi_items_form" method="POST">
+<h2>[%- 'Add multiple parts' | $T8 %] [%- SELF.type | $T8 %]</h2>
+
+<table width="100%">
+ [%- FOREACH part = SELF.multi_items.parts %]
+ <tr>
+ <td>
+ [% L.hidden_tag("add_multi_items[+].parts_id", part.id) %]
+ [% L.input_tag("add_multi_items[].qty", '', size => 5) %]
+ </td>
+ <td>[% HTML.escape(part.unit) %]</td>
+ <td>[% HTML.escape(part.partnumber) %]</td>
+ <td>[% HTML.escape(part.description) %]</td>
+ </tr>
+ [%- END %]
+</table>
+
+<br>
+[% L.hidden_tag('action', 'Order/dispatch') %]
+[% L.button_tag('add_multi_items()', LxERP.t8('Continue')) %]
+<a href="#" onclick="close_multi_items_dialog();">[%- LxERP.t8("Cancel") %]</a>
+
+<script type='text/javascript'>
+function add_multi_items() {
+ var data = $('#order_form').serialize();
+ data += '&';
+ data += $('#multi_items_form').serialize();
+ data += '&action=Order/add_multi_items';
+ $.post("controller.pl", data, kivi.eval_json_result);
+}
+</script>
+
+</form>
[%- PROCESS order/tabs/_item_input.html %]
+ [% L.button_tag('setup_multi_items_dialog()', LxERP.t8('Add multiple parts')) %]</td>
+
<table width="100%">
<tr>
<td>
$.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 = $('<div style="display:none" id="' + id + '"></div>').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();