X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/88a78448cd46a676f4b9342f2991a58ae9393074..0ea99d65cafaa827d3bccbaf6e9ccb8f7458abfd:/SL/Controller/Order.pm diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 79e4ee834..6f422134a 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -51,6 +51,7 @@ sub action_add { my ($self) = @_; $self->order->transdate(DateTime->now_local()); + $self->order->reqdate(DateTime->today_local->next_workday) if !$self->order->reqdate; $self->_pre_render(); $self->render( @@ -124,9 +125,14 @@ sub action_create_pdf { my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000); $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name); - $::form->{formname} = $self->type; - $::form->{language} = 'de'; - my $pdf_filename = $::form->get_formname_translation . '_' . $self->order->ordnumber . '.pdf'; + my $form = Form->new; + $form->{ordnumber} = $self->order->ordnumber; + $form->{formname} = $self->type; + $form->{type} = $self->type; + $form->{language} = 'de'; + $form->{format} = 'pdf'; + + my $pdf_filename = $form->generate_attachment_filename(); $self->js ->run('download_pdf', $pdf_filename, $key) @@ -287,53 +293,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; - } - - # bb: not sure but: maybe there should be a $price_source->discount_from_source - # which can also return an empty_discout if source is "". - my $discount; - my $discount_src; - if ($item->discount) { - $discount = $item->discount; - } else { - $discount = $price_source->best_discount - ? $price_source->best_discount->discount - : 0; - $discount_src = $price_source->best_discount->source 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; - $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(); @@ -352,6 +312,53 @@ 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; + $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; @@ -525,6 +532,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) = @_; @@ -588,6 +647,7 @@ sub _pre_render { my ($self) = @_; $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted(); + $self->{all_departments} = SL::DB::Manager::Department->get_all_sorted(); $self->{all_employees} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->employee_id, deleted => 0 ] ], sort_by => 'name'); @@ -604,13 +664,9 @@ sub _pre_render { 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)); - my $price_src = $price_source->price_from_source($item->active_price_source); - $item->active_price_source($price_src); - - my $discount_src; - $discount_src = $price_source->price_from_source($item->active_discount_source)->source if $item->active_discount_source; - $item->active_discount_source($discount_src); } $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);