X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/1e1ecfe611e6c07ed4cf908f62bb36afde37f4b7..d59c544786c8f76756e2ec10dc3b8f803355efdc:/SL/Controller/Order.pm diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index f4d567dee..836351e4b 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -30,7 +30,7 @@ use Rose::Object::MakeMethods::Generic # safety __PACKAGE__->run_before('_check_auth'); -__PACKAGE__->run_before('_setup', +__PACKAGE__->run_before('_recalc', only => [ qw(edit update save) ]); @@ -82,8 +82,14 @@ sub action_update { sub action_save { my ($self) = @_; - $self->_save(); + my $errors = $self->_save(); + if (scalar @{ $errors }) { + $self->js->flash('error', $_) foreach @{ $errors }; + return $self->js->render($self); + } + + flash_later('info', $::locale->text('The order has been saved')); my @redirect_params = ( action => 'edit', type => $self->type, @@ -119,7 +125,7 @@ sub action_customer_vendor_changed { ->replaceWith('#order_cp_id', $self->build_contact_select) ->replaceWith('#order_shipto_id', $self->build_shipto_select) ->val('#order_taxzone_id', $self->order->{$self->cv}->taxzone_id) - ->focus('#order_' . $self->cv . ' _id') + ->focus('#order_' . $self->cv . '_id') ->render($self); } @@ -127,82 +133,83 @@ sub action_add_item { my ($self) = @_; my $form_attr = $::form->{add_item}; - my $item = SL::DB::OrderItem->new; + + 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 $cv_class = "SL::DB::" . ucfirst($self->cv); - my $cv_discount = $::form->{$self->cv . '_id'}? $cv_class->new(id => $::form->{$self->cv . '_id'})->load->discount :0.0; + my $cv_method = $self->cv; + my $cv_discount = $self->order->$cv_method? $self->order->$cv_method->discount : 0.0; my %new_attr; - $new_attr{id} = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); - $new_attr{part} = $part; - $new_attr{qty} = 1.0 if ! $item->{qty}; - $new_attr{unit} = $part->unit; - $new_attr{sellprice} = $part->sellprice if ! $item->{sellprice}; - $new_attr{discount} = $cv_discount if ! $item->{discount}; + $new_attr{part} = $part; + $new_attr{description} = $part->description if ! $item->description; + $new_attr{qty} = 1.0 if ! $item->qty; + $new_attr{unit} = $part->unit; + $new_attr{sellprice} = $part->sellprice if ! $item->sellprice; + $new_attr{discount} = $cv_discount if ! $item->discount; + + # 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); $self->order->add_items($item); - $self->_setup(); + $self->_recalc(); - my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item); + 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 tbody', $row_as_html) - ->focus('#row_table_id tr:last [id$="parts_id_name"]') - ->off('[id^="order_orderitems"][id$="parts_id"]', 'change', 'set_item_values') - ->on('[id^="order_orderitems"][id$="parts_id"]', 'change', 'set_item_values') - ->html('#netamount_id', $::form->format_amount(\%::myconfig, $self->order->netamount, -2)) - ->html('#amount_id', $::form->format_amount(\%::myconfig, $self->order->amount, -2)) - ->remove('.tax_row') - ->insertBefore($self->build_tax_rows, '#amount_row_id') - ->render($self); + ->append('#row_table_id', $row_as_html) + ->val('#add_item_parts_id', '') + ->val('#add_item_parts_id_name', '') + ->val('#add_item_description', '') + ->val('#add_item_qty_as_number', '') + ->val('#add_item_sellprice_as_number', '') + ->val('#add_item_discount_as_percent', '') + ->run('row_table_scroll_down') + ->run('row_set_keyboard_events_by_id', $item_id) + ->on('.recalc', 'change', 'recalc_amounts_and_taxes') + ->focus('#add_item_parts_id_name'); + + $self->_js_redisplay_amounts_and_taxes; + $self->js->render($self); } -sub action_set_item_values { +sub action_recalc_amounts_and_taxes { my ($self) = @_; - my $is_new = $::form->{item_id} =~ m{^new_}; - 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; + $self->_recalc(); + $self->_js_redisplay_linetotals; + $self->_js_redisplay_amounts_and_taxes; + $self->js->render($self); +} - 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); +sub _js_redisplay_linetotals { + my ($self) = @_; + my @data = map {$::form->format_amount(\%::myconfig, $_->{linetotal}, 2, 0)} @{ $self->order->items }; + $self->js + ->run('redisplay_linetotals', \@data); +} - $self->_setup(); +sub _js_redisplay_amounts_and_taxes { + my ($self) = @_; $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_percent) - ->run('display_linetotal', $::form->{item_id}, $::form->format_amount(\%::myconfig, $item->{linetotal}, -2)) ->html('#netamount_id', $::form->format_amount(\%::myconfig, $self->order->netamount, -2)) ->html('#amount_id', $::form->format_amount(\%::myconfig, $self->order->amount, -2)) ->remove('.tax_row') - ->insertBefore($self->build_tax_rows, '#amount_row_id') - ->render($self); + ->insertBefore($self->build_tax_rows, '#amount_row_id'); } - # # helpers # @@ -305,7 +312,7 @@ sub _make_order { } -sub _setup { +sub _recalc { my ($self) = @_; # bb: todo: currency later @@ -325,12 +332,20 @@ sub _setup { sub _save { my ($self) = @_; + # autovivify all cvars that are not in the form (cvars_by_config can do it) + foreach my $item (@{ $self->order->items }) { + $item->cvars_by_config; + } + + my $errors = []; my $db = $self->order->db; $db->do_transaction( sub { $self->order->save(); - }) || die($db->error); + }) || push(@{$errors}, $db->error); + + return $errors; }