From 4ce66c79400b7019ddcfd4ac7d4f5ae6ff3dadf4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Mon, 10 Aug 2015 13:52:17 +0200 Subject: [PATCH] =?utf8?q?Auftrags-Controller:=20=C3=84nderung=20der=20?= =?utf8?q?=C3=9Cbergabe=20aus=20der=20Form=20und=20Objekt-Erzeugung?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit - Form wird bei Ajax-Call serialisiert - item_id ist order.orderitems.id - Orderitems durch assign_attributes erzeugen lassen - linetotal wird direkt über PriceTaxCalculator berechnet --- SL/Controller/Order.pm | 104 ++++++------------ templates/webpages/order/form.html | 2 +- templates/webpages/order/tabs/_row.html | 4 +- templates/webpages/order/tabs/basic_data.html | 25 ++--- 4 files changed, 50 insertions(+), 85 deletions(-) diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 9aaa1f15f..781babd5f 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -18,22 +18,18 @@ use SL::DB::Unit; use SL::Helper::DateTime; -use List::Util qw(max); -use List::MoreUtils qw(none); +use List::Util qw(max first); +use List::MoreUtils qw(none pairwise); use Rose::Object::MakeMethods::Generic ( - 'scalar' => [ qw(order) ], - 'scalar --get_set_init' => [ qw(valid_types type cv js p) ], + 'scalar --get_set_init' => [ qw(order valid_types type cv js p) ], ); # safety __PACKAGE__->run_before('_check_auth'); -__PACKAGE__->run_before('_load_or_new_order', - only => [ qw(add edit update save customer_vendor_changed set_item_values) ]); - __PACKAGE__->run_before('_setup', only => [ qw(edit update save) ]); @@ -100,10 +96,10 @@ sub action_save { sub action_customer_vendor_changed { my ($self) = @_; - if ($self->type eq _sales_order_type()) { + if ($self->cv eq 'customer') { $self->order->customer(SL::DB::Manager::Customer->find_by_or_create(id => $::form->{cv_id})); - } elsif ($self->type eq _purchase_order_type()) { + } elsif ($self->cv eq 'vendor') { $self->order->vendor(SL::DB::Manager::Vendor->find_by_or_create(id => $::form->{cv_id})); } @@ -144,33 +140,37 @@ sub action_add_item_row { sub action_set_item_values { my ($self) = @_; - my $part = SL::DB::Part->new(id => $::form->{parts_id})->load; - my $is_new = $::form->{item_id} =~ m{^new_}; - my $item_id = $is_new ? undef : $::form->{item_id}; - my $item = SL::DB::Manager::OrderItem->find_by_or_create(id => $item_id); + my $item_id = $::form->{item_id}; + + my $item = first {$_->id eq $::form->{item_id}} @{$self->order->items}; + my $form_attr = first {$_->{id} eq $::form->{item_id}} @{ $::form->{order}->{orderitems} }; + + delete $form_attr->{id}; + + my $part = SL::DB::Part->new(id => $form_attr->{parts_id})->load; my $cv_class = "SL::DB::" . ucfirst($self->cv); my $cv_discount = $::form->{cv_id}? $cv_class->new(id => $::form->{$self->cv . '_id'})->load->discount :0.0; - $item->assign_attributes( - parts_id => $part->id, - qty => $::form->{qty} ? $::form->parse_amount(\%::myconfig, $::form->{qty}) : 1.0, - unit => $part->unit, - discount => $::form->{discount} ? $::form->parse_amount(\%::myconfig, $::form->{discount}) : $cv_discount, - sellprice => $::form->{sellprice} ? $::form->parse_amount(\%::myconfig, $::form->{sellprice}) : $part->sellprice, - ); - $self->order->add_items([$item]); + my %new_attr; + $new_attr{sellprice} = $part->sellprice if ! $form_attr->{sellprice_as_number}; + $new_attr{discount} = $cv_discount if ! $form_attr->{discount_as_percent}; + $new_attr{unit} = $part->unit if ! $form_attr->{unit}; + $new_attr{qty} = 1.0 if ! $form_attr->{qty_as_number}; + + $item->assign_attributes(%new_attr); + + $self->_setup(); - my $linetotal = _linetotal($self->order, $item); $self->js ->val( '#' . $::form->{qty_dom_id}, $item->qty_as_number) ->val( '#' . $::form->{unit_dom_id}, $item->unit) ->val( '#' . $::form->{sellprice_dom_id}, $item->sellprice_as_number) - ->val( '#' . $::form->{discount_dom_id}, $item->discount_as_number) - ->run('recalc_linetotal', $::form->{item_id}, $::form->format_amount(\%::myconfig, $linetotal, -2)) + ->val( '#' . $::form->{discount_dom_id}, $item->discount_as_percent) + ->run('recalc_linetotal', $::form->{item_id}, $::form->format_amount(\%::myconfig, $item->{linetotal}, -2)) ->render($self); } @@ -211,6 +211,10 @@ sub init_p { SL::Presenter->get; } +sub init_order { + _make_order(); +} + sub _check_auth { my ($self) = @_; @@ -246,17 +250,20 @@ sub build_shipto_select { ); } -sub _load_or_new_order { +sub _make_order { my ($self) = @_; - return $self->order(SL::DB::Manager::Order->find_by_or_create(id => $::form->{id})); + my $order = SL::DB::Manager::Order->find_by_or_create(id => $::form->{id}); + + $order->assign_attributes(%{$::form->{order}}); + + return $order; } + sub _setup { my ($self) = @_; - $self->order->assign_attributes(%{$::form->{order}}); - # bb: todo: currency later $self->order->currency_id($::instance_conf->get_currency_id()); @@ -268,9 +275,7 @@ sub _setup { tax => $tax }); } - foreach my $item ($self->order->items) { - $item->{linetotal} = _linetotal($self->order, $item); - } + pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items}, @{$pat{items}}; } sub _save { @@ -299,43 +304,6 @@ sub _pre_render { $self->{current_employee_id} = SL::DB::Manager::Employee->current->id; } -# The following subs are more or less copied/pasted from SL::DB::Helper::PriceTaxCalculator. -sub _linetotal { - my ($order, $item) = @_; - - # bb: todo: currencies are not handled by now - my $exchangerate = _get_exchangerate($order); - - my $num_dec = max 2, _num_decimal_places($item->sellprice); - my $discount = _round($item->sellprice * ($item->discount || 0), $num_dec); - my $sellprice = _round($item->sellprice - $discount, $num_dec); - my $linetotal = _round($sellprice * $item->qty / $item->price_factor, 2 ) * $exchangerate; - $linetotal = _round($linetotal, 2 ); - - return $linetotal; -} - -sub _get_exchangerate { - my ($order) = @_; - require SL::DB::Default; - - my $exchangerate = 1; - my $currency = $order->currency_id ? $order->currency->name || '' : ''; - if ($currency ne SL::DB::Default->get_default_currency) { - $exchangerate = $::form->check_exchangerate(\%::myconfig, $currency, $order->transdate, $order->is_sales ? 'buy' : 'sell'); - } - - return $exchangerate; -} - -sub _num_decimal_places { - return length( (split(/\./, '' . ($_[0] * 1), 2))[1] || '' ); -} - -sub _round { - return $::form->round_amount(@_); -} - sub _sales_order_type { 'sales_order'; } diff --git a/templates/webpages/order/form.html b/templates/webpages/order/form.html index 37cd0e7d2..b2e68a518 100644 --- a/templates/webpages/order/form.html +++ b/templates/webpages/order/form.html @@ -2,7 +2,7 @@ [%- USE LxERP %] [%- USE L %] -
+
[% FORM.title %]
[% L.hidden_tag('callback', FORM.callback) %] diff --git a/templates/webpages/order/tabs/_row.html b/templates/webpages/order/tabs/_row.html index 14ba18d9c..2201666c2 100644 --- a/templates/webpages/order/tabs/_row.html +++ b/templates/webpages/order/tabs/_row.html @@ -5,7 +5,7 @@ - [% L.hidden_tag("item_id", ITEM.id, id='item_' _ ITEM.id, ) %] + [% L.hidden_tag("order.orderitems[+].id", ITEM.id, id='item_' _ ITEM.id, ) %] [%- LxERP.t8('reorder item') %] @@ -15,7 +15,7 @@ LxERP.t8("X"), confirm=LxERP.t8("Are you sure?")) %] - [% L.part_picker("order.orderitems[+].parts_id", + [% L.part_picker("order.orderitems[].parts_id", ITEM.part, style='width: 300px') %] diff --git a/templates/webpages/order/tabs/basic_data.html b/templates/webpages/order/tabs/basic_data.html index 93f954a7b..67ce0e2fa 100644 --- a/templates/webpages/order/tabs/basic_data.html +++ b/templates/webpages/order/tabs/basic_data.html @@ -199,25 +199,22 @@ function set_item_values(event) { var parts_id = $(event.target).val(); var row = $(event.target).parents("tr").first(); - var item_id_dom = $(row).find('[name="item_id"]'); + var item_id_dom = $(row).find('[name="order.orderitems[+].id"]'); var qty_dom = $(row).find('[id^="order_orderitems"][id$="qty_as_number"]'); var unit_dom = $(row).find('[id^="order_orderitems"][id$="unit"]'); var sellprice_dom = $(row).find('[id^="order_orderitems"][id$="sellprice_as_number"]'); var discount_dom = $(row).find('[id^="order_orderitems"][id$="discount_as_percent"]'); - $.post("controller.pl", { 'action': 'Order/set_item_values', - 'item_id': item_id_dom.val(), - 'cv_id': cv_id, - 'parts_id': parts_id, - 'qty': qty_dom.val(), - 'discount': discount_dom.val(), - 'sellprice': sellprice_dom.val(), - 'qty_dom_id': qty_dom.attr("id"), - 'unit_dom_id': unit_dom.attr("id"), - 'sellprice_dom_id': sellprice_dom.attr("id"), - 'discount_dom_id': discount_dom.attr("id"), - 'type': function(){ return $('#type').val() } - }, kivi.eval_json_result); + var data = $('#order_form').serialize(); + data += '&action=Order/set_item_values'; + data += '&type=' + $('#type').val(); + data += '&item_id=' + item_id_dom.val(); + data += '&qty_dom_id=' + qty_dom.attr("id"); + data += '&unit_dom_id=' + unit_dom.attr("id"); + data += '&sellprice_dom_id=' + sellprice_dom.attr("id"); + data += '&discount_dom_id=' + discount_dom.attr("id"); + + $.post("controller.pl", data, kivi.eval_json_result); } function recalc_linetotal(item_id, amount) { -- 2.20.1