X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/aa36021ace7b4075af6d2d815e0e208b7227d9c4..f275cac9ddfb00d05435e73fc85f0a6c016095b8:/SL/Controller/Order.pm diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 75c636ced..316198eed 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -18,12 +18,13 @@ use SL::DB::Employee; use SL::DB::Project; use SL::DB::Default; use SL::DB::Unit; +use SL::DB::Price; use SL::Helper::DateTime; use SL::Helper::CreatePDF qw(:all); use List::Util qw(max first); -use List::MoreUtils qw(none pairwise); +use List::MoreUtils qw(none pairwise first_index); use English qw(-no_match_vars); use File::Spec; @@ -37,7 +38,7 @@ use Rose::Object::MakeMethods::Generic __PACKAGE__->run_before('_check_auth'); __PACKAGE__->run_before('_recalc', - only => [ qw(edit update save create_pdf send_email) ]); + only => [ qw(edit update save save_and_delivery_order create_pdf send_email) ]); __PACKAGE__->run_before('_get_unalterable_data', only => [ qw(save save_and_delivery_order create_pdf send_email) ]); @@ -241,6 +242,25 @@ sub action_send_email { ->render($self); } +sub action_save_and_delivery_order { + my ($self) = @_; + + my $errors = $self->_save(); + + if (scalar @{ $errors }) { + $self->js->flash('error', $_) foreach @{ $errors }; + return $self->js->render(); + } + flash_later('info', $::locale->text('The order has been saved')); + + my @redirect_params = ( + controller => 'oe.pl', + action => 'oe_delivery_order_from_order', + id => $self->order->id, + ); + + $self->redirect_to(@redirect_params); +} sub action_customer_vendor_changed { my ($self) = @_; @@ -296,17 +316,40 @@ sub action_add_item { 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_method = $self->cv; - my $cv_discount = $self->order->$cv_method? $self->order->$cv_method->discount : 0.0; + 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{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{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 @@ -328,6 +371,7 @@ sub action_add_item { ->run('row_table_scroll_down') ->run('row_set_keyboard_events_by_id', $item_id) ->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; @@ -344,6 +388,15 @@ sub action_recalc_amounts_and_taxes { $self->js->render(); } +sub action_price_popup { + my ($self) = @_; + + my $idx = first_index { $_ eq $::form->{item_id} } @{ $::form->{orderitem_ids} }; + my $item = $self->order->items->[$idx]; + + $self->render_price_dialog($item); +} + sub _js_redisplay_linetotals { my ($self) = @_; @@ -456,6 +509,27 @@ sub build_tax_rows { } +sub render_price_dialog { + my ($self, $record_item) = @_; + + my $price_source = SL::PriceSource->new(record_item => $record_item, record => $self->order); + + $self->js + ->run( + 'kivi.io.price_chooser_dialog', + t8('Available Prices'), + $self->render('order/tabs/_price_sources_dialog', { output => 0 }, price_source => $price_source) + ) + ->reinit_widgets; + +# if (@errors) { +# $self->js->text('#dialog_flash_error_content', join ' ', @errors); +# $self->js->show('#dialog_flash_error'); +# } + + $self->js->render; +} + sub _make_order { my ($self) = @_; @@ -471,7 +545,6 @@ sub _make_order { return $order; } - sub _recalc { my ($self) = @_; @@ -500,12 +573,10 @@ sub _get_unalterable_data { if ($item->id) { # load data from orderitems (db) my $db_item = SL::DB::OrderItem->new(id => $item->id)->load; - $item->$_($db_item->$_) for qw(active_discount_source active_price_source longdescription); + $item->$_($db_item->$_) for qw(longdescription); } else { # set data from part (or other sources) $item->longdescription($item->part->notes); - #$item->active_price_source(''); - #$item->active_discount_source(''); } # autovivify all cvars that are not in the form (cvars_by_config can do it). @@ -571,6 +642,13 @@ sub _pre_render { $self->{current_employee_id} = SL::DB::Manager::Employee->current->id; + foreach my $item (@{$self->order->items}) { + my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order); + $item->active_price_source( $price_source->price_from_source( $item->active_price_source )); + $item->active_discount_source($price_source->discount_from_source($item->active_discount_source)); + + } + $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery); }