X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/4cd645132b98802d2c21288ed557a67224a72cf9..491a3c27055cabce59b4d93a6e389a59125eeef7:/SL/Controller/Order.pm 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) = @_;