X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FOrder.pm;h=29ff0ac49e7f7d52a1383f27d8e81a751a7274a1;hb=cf96c5880f1ba0694298d7541fedce47c8c99051;hp=00efeca766cf39eb4537ac1624bdb70a06ebc764;hpb=486d0a3bc8e6ff90823a1b5e0461d0b61c11b771;p=kivitendo-erp.git diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 00efeca76..29ff0ac49 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -4,7 +4,7 @@ use strict; use parent qw(SL::Controller::Base); use SL::Helper::Flash qw(flash_later); -use SL::Presenter::Tag qw(select_tag hidden_tag); +use SL::Presenter::Tag qw(select_tag hidden_tag div_tag); use SL::Locale::String qw(t8); use SL::SessionFile::Random; use SL::PriceSource; @@ -21,6 +21,8 @@ use SL::DB::PartsGroup; use SL::DB::Printer; use SL::DB::Language; use SL::DB::RecordLink; +use SL::DB::Shipto; +use SL::DB::Translation; use SL::Helper::CreatePDF qw(:all); use SL::Helper::PrintOptions; @@ -30,7 +32,7 @@ use SL::Helper::UserPreferences::UpdatePositions; use SL::Controller::Helper::GetModels; -use List::Util qw(first); +use List::Util qw(first sum0); use List::UtilsBy qw(sort_by uniq_by); use List::MoreUtils qw(any none pairwise first_index); use English qw(-no_match_vars); @@ -40,7 +42,7 @@ use Sort::Naturally; use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(item_ids_to_delete) ], + scalar => [ qw(item_ids_to_delete is_custom_shipto_to_delete) ], 'scalar --get_set_init' => [ qw(order valid_types type cv p multi_items_models all_price_factors search_cvpartnumber show_update_button) ], ); @@ -92,11 +94,10 @@ sub action_edit { foreach my $item (@{$self->order->items_sorted}) { $item->{new_fake_id} = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); } - # trigger rendering values for second row/longdescription as hidden, - # because they are loaded only on demand. So we need to keep the values - # from the source. - $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; - $_->{render_longdescription} = 1 for @{ $self->order->items_sorted }; + # trigger rendering values for second row as hidden, because they + # are loaded only on demand. So we need to keep the values from + # the source. + $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; } $self->recalc(); @@ -242,11 +243,8 @@ sub action_save_as_new { # print the order # # This is called if "print" is pressed in the print dialog. -# If PDF creation was requested and succeeded, the pdf is stored in a session -# file and the filename is stored as session value with an unique key. A -# javascript function with this key is then called. This function calls the -# download action below (action_download_pdf), which offers the file for -# download. +# If PDF creation was requested and succeeded, the pdf is offered for download +# via send_file (which uses ajax in this case). sub action_print { my ($self) = @_; @@ -264,6 +262,7 @@ sub action_print { my $formname = $::form->{print_options}->{formname}; my $copies = $::form->{print_options}->{copies}; my $groupitems = $::form->{print_options}->{groupitems}; + my $printer_id = $::form->{print_options}->{printer_id}; # only pdf and opendocument by now if (none { $format eq $_ } qw(pdf opendocument opendocument_pdf)) { @@ -275,22 +274,20 @@ sub action_print { return $self->js->flash('error', t8('Media \'#1\' is not supported yet/anymore.', $media))->render; } - my $language; - $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id}; - # create a form for generate_attachment_filename my $form = Form->new; $form->{$self->nr_key()} = $self->order->number; $form->{type} = $self->type; $form->{format} = $format; $form->{formname} = $formname; - $form->{language} = '_' . $language->template_code if $language; + $form->{language} = '_' . $self->order->language->template_code if $self->order->language; my $pdf_filename = $form->generate_attachment_filename(); my $pdf; my @errors = generate_pdf($self->order, \$pdf, { format => $format, formname => $formname, - language => $language, + language => $self->order->language, + printer_id => $printer_id, groupitems => $groupitems }); if (scalar @errors) { return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render; @@ -298,16 +295,13 @@ sub action_print { if ($media eq 'screen') { # screen/download - my $sfile = SL::SessionFile::Random->new(mode => "w"); - $sfile->fh->print($pdf); - $sfile->fh->close; - - my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000); - $::auth->set_session_value("Order::print-${key}" => $sfile->file_name); - - $self->js - ->run('kivi.Order.download_pdf', $pdf_filename, $key) - ->flash('info', t8('The PDF has been created')); + $self->js->flash('info', t8('The PDF has been created')); + $self->send_file( + \$pdf, + type => SL::MIME->mime_type_from_ext($pdf_filename), + name => $pdf_filename, + js_no_render => 1, + ); } elsif ($media eq 'printer') { # printer @@ -354,24 +348,16 @@ sub action_print { $self->js->render; } -# offer pdf for download -# -# It needs to get the key for the session value to get the pdf file. -sub action_download_pdf { +# open the email dialog +sub action_save_and_show_email_dialog { my ($self) = @_; - my $key = $::form->{key}; - my $tmp_filename = $::auth->get_session_value("Order::print-${key}"); - return $self->send_file( - $tmp_filename, - type => SL::MIME->mime_type_from_ext($::form->{pdf_filename}), - name => $::form->{pdf_filename}, - ); -} + my $errors = $self->save(); -# open the email dialog -sub action_show_email_dialog { - my ($self) = @_; + if (scalar @{ $errors }) { + $self->js->flash('error', $_) foreach @{ $errors }; + return $self->js->render(); + } my $cv_method = $self->cv; @@ -389,9 +375,11 @@ sub action_show_email_dialog { my $form = Form->new; $form->{$self->nr_key()} = $self->order->number; + $form->{cusordnumber} = $self->order->cusordnumber; $form->{formname} = $self->type; $form->{type} = $self->type; - $form->{language} = 'de'; + $form->{language} = '_' . $self->order->language->template_code if $self->order->language; + $form->{language_id} = $self->order->language->id if $self->order->language; $form->{format} = 'pdf'; $email_form->{subject} = $form->generate_email_subject(); @@ -442,14 +430,12 @@ sub action_send_email { $::form->{media} = 'email'; if (($::form->{attachment_policy} // '') !~ m{^(?:old_file|no_file)$}) { - my $language; - $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id}; - my $pdf; my @errors = generate_pdf($self->order, \$pdf, {media => $::form->{media}, format => $::form->{print_options}->{format}, formname => $::form->{print_options}->{formname}, - language => $language, + language => $self->order->language, + printer_id => $::form->{print_options}->{printer_id}, groupitems => $::form->{print_options}->{groupitems}}); if (scalar @errors) { return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self); @@ -463,6 +449,7 @@ sub action_send_email { $::form->{tmpdir} = $sfile->get_path; # for Form::cleanup which may be called in Form::send_email } + $::form->{id} = $self->order->id; # this is used in SL::Mailer to create a linked record to the mail $::form->send_email(\%::myconfig, 'pdf'); # internal notes @@ -478,11 +465,15 @@ sub action_send_email { $self->order->update_attributes(intnotes => $intnotes); - $self->js - ->val('#order_intnotes', $intnotes) - ->run('kivi.Order.close_email_dialog') - ->flash('info', t8('The email has been sent.')) - ->render($self); + flash_later('info', t8('The email has been sent.')); + + my @redirect_params = ( + action => 'edit', + type => $self->type, + id => $self->order->id, + ); + + $self->redirect_to(@redirect_params); } # open the periodic invoices config dialog @@ -696,9 +687,9 @@ sub action_customer_vendor_changed { } if ($self->order->$cv_method->shipto && scalar @{ $self->order->$cv_method->shipto } > 0) { - $self->js->show('#shipto_row'); + $self->js->show('#shipto_selection'); } else { - $self->js->hide('#shipto_row'); + $self->js->hide('#shipto_selection'); } $self->js->val( '#order_salesman_id', $self->order->salesman_id) if $self->order->is_sales; @@ -706,14 +697,17 @@ sub action_customer_vendor_changed { $self->js ->replaceWith('#order_cp_id', $self->build_contact_select) ->replaceWith('#order_shipto_id', $self->build_shipto_select) + ->replaceWith('#shipto_inputs ', $self->build_shipto_inputs) ->replaceWith('#business_info_row', $self->build_business_info_row) ->val( '#order_taxzone_id', $self->order->taxzone_id) ->val( '#order_taxincluded', $self->order->taxincluded) + ->val( '#order_currency_id', $self->order->currency_id) ->val( '#order_payment_id', $self->order->payment_id) ->val( '#order_delivery_term_id', $self->order->delivery_term_id) ->val( '#order_intnotes', $self->order->intnotes) - ->val( '#language_id', $self->order->$cv_method->language_id) - ->focus( '#order_' . $self->cv . '_id'); + ->val( '#order_language_id', $self->order->$cv_method->language_id) + ->focus( '#order_' . $self->cv . '_id') + ->run('kivi.Order.update_exchangerate'); $self->js_redisplay_amounts_and_taxes; $self->js_redisplay_cvpartnumbers; @@ -791,16 +785,18 @@ sub action_add_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, - TYPE => $self->type, - ALL_PRICE_FACTORS => $self->all_price_factors, - SEARCH_CVPARTNUMBER => $self->search_cvpartnumber, - SHOW_UPDATE_BUTTON => $self->show_update_button, + ITEM => $item, + ID => $item_id, + SELF => $self, ); - $self->js - ->append('#row_table_id', $row_as_html); + if ($::form->{insert_before_item_id}) { + $self->js + ->before ('.row_entry:has(#item_' . $::form->{insert_before_item_id} . ')', $row_as_html); + } else { + $self->js + ->append('#row_table_id', $row_as_html); + } if ( $item->part->is_assortment ) { $form_attr->{qty_as_number} = 1 unless $form_attr->{qty_as_number}; @@ -820,25 +816,28 @@ sub action_add_item { $self->get_item_cvpartnumber($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, - TYPE => $self->type, - ALL_PRICE_FACTORS => $self->all_price_factors, - SEARCH_CVPARTNUMBER => $self->search_cvpartnumber, - SHOW_UPDATE_BUTTON => $self->show_update_button, + ITEM => $item, + ID => $item_id, + SELF => $self, ); - $self->js - ->append('#row_table_id', $row_as_html); + if ($::form->{insert_before_item_id}) { + $self->js + ->before ('.row_entry:has(#item_' . $::form->{insert_before_item_id} . ')', $row_as_html); + } else { + $self->js + ->append('#row_table_id', $row_as_html); + } }; }; $self->js ->val('.add_item_input', '') ->run('kivi.Order.init_row_handlers') - ->run('kivi.Order.row_table_scroll_down') ->run('kivi.Order.renumber_positions') ->focus('#add_item_parts_id_name'); + $self->js->run('kivi.Order.row_table_scroll_down') if !$::form->{insert_before_item_id}; + $self->js_redisplay_amounts_and_taxes; $self->js->render(); } @@ -904,24 +903,28 @@ sub action_add_multi_items { $self->get_item_cvpartnumber($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, - TYPE => $self->type, - ALL_PRICE_FACTORS => $self->all_price_factors, - SEARCH_CVPARTNUMBER => $self->search_cvpartnumber, - SHOW_UPDATE_BUTTON => $self->show_update_button, + ITEM => $item, + ID => $item_id, + SELF => $self, ); - $self->js->append('#row_table_id', $row_as_html); + if ($::form->{insert_before_item_id}) { + $self->js + ->before ('.row_entry:has(#item_' . $::form->{insert_before_item_id} . ')', $row_as_html); + } else { + $self->js + ->append('#row_table_id', $row_as_html); + } } $self->js ->run('kivi.Order.close_multi_items_dialog') ->run('kivi.Order.init_row_handlers') - ->run('kivi.Order.row_table_scroll_down') ->run('kivi.Order.renumber_positions') ->focus('#add_item_parts_id_name'); + $self->js->run('kivi.Order.row_table_scroll_down') if !$::form->{insert_before_item_id}; + $self->js_redisplay_amounts_and_taxes; $self->js->render(); } @@ -937,6 +940,18 @@ sub action_recalc_amounts_and_taxes { $self->js->render(); } +sub action_update_exchangerate { + my ($self) = @_; + + my $data = { + is_standard => $self->order->currency_id == $::instance_conf->get_currency_id, + currency_name => $self->order->currency->name, + exchangerate => $self->order->daily_exchangerate_as_null_number, + }; + + $self->render(\SL::JSON::to_json($data), { type => 'json', process => 0 }); +} + # redisplay item rows if they are sorted by an attribute sub action_reorder_items { my ($self) = @_; @@ -982,22 +997,6 @@ sub action_price_popup { $self->render_price_dialog($item); } -# get the longdescription for an item if the dialog to enter/change the -# longdescription was opened and the longdescription is empty -# -# If this item is new, get the longdescription from Part. -# Otherwise get it from OrderItem. -sub action_get_item_longdescription { - my $longdescription; - - if ($::form->{item_id}) { - $longdescription = SL::DB::OrderItem->new(id => $::form->{item_id})->load->longdescription; - } elsif ($::form->{parts_id}) { - $longdescription = SL::DB::Part->new(id => $::form->{parts_id})->load->notes; - } - $_[0]->render(\ $longdescription, { type => 'text' }); -} - # load the second row for one or more items # # This action gets the html code for all items second rows by rendering a template for @@ -1024,11 +1023,12 @@ sub action_update_row_from_master_data { my ($self) = @_; foreach my $item_id (@{ $::form->{item_ids} }) { - my $idx = first_index { $_ eq $item_id } @{ $::form->{orderitem_ids} }; - my $item = $self->order->items_sorted->[$idx]; + my $idx = first_index { $_ eq $item_id } @{ $::form->{orderitem_ids} }; + my $item = $self->order->items_sorted->[$idx]; + my $texts = get_part_texts($item->part, $self->order->language_id); - $item->description($item->part->description); - $item->longdescription($item->part->notes); + $item->description($texts->{description}); + $item->longdescription($texts->{longdescription}); my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order); @@ -1041,16 +1041,24 @@ sub action_update_row_from_master_data { $price_src = $price_source->best_price ? $price_source->best_price : $price_source->price_from_source(""); + $price_src->price($::form->round_amount($price_src->price / $self->order->exchangerate, 5)) if $self->order->exchangerate; $price_src->price(0) if !$price_source->best_price; } + $item->sellprice($price_src->price); $item->active_price_source($price_src); $self->js ->run('kivi.Order.update_sellprice', $item_id, $item->sellprice_as_number) - ->val('.row_entry:has(#item_' . $item_id . ') [name = "order.orderitems[].description"]', $item->description) - ->val('.row_entry:has(#item_' . $item_id . ') [name = "order.orderitems[].longdescription"]', $item->longdescription); + ->html('.row_entry:has(#item_' . $item_id . ') [name = "partnumber"] a', $item->part->partnumber) + ->val ('.row_entry:has(#item_' . $item_id . ') [name = "order.orderitems[].description"]', $item->description) + ->val ('.row_entry:has(#item_' . $item_id . ') [name = "order.orderitems[].longdescription"]', $item->longdescription); + + if ($self->search_cvpartnumber) { + $self->get_item_cvpartnumber($item); + $self->js->html('.row_entry:has(#item_' . $item_id . ') [name = "cvpartnumber"]', $item->{cvpartnumber}); + } } $self->recalc(); @@ -1280,15 +1288,31 @@ sub build_contact_select { sub build_shipto_select { my ($self) = @_; - select_tag('order.shipto_id', [ $self->order->{$self->cv}->shipto ], - value_key => 'shipto_id', - title_key => 'displayable_id', - default => $self->order->shipto_id, - with_empty => 1, - style => 'width: 300px', + select_tag('order.shipto_id', + [ {displayable_id => t8("No/individual shipping address"), shipto_id => ''}, $self->order->{$self->cv}->shipto ], + value_key => 'shipto_id', + title_key => 'displayable_id', + default => $self->order->shipto_id, + with_empty => 0, + style => 'width: 300px', ); } +# build the inputs for the cusom shipto dialog +# +# Needed, if customer/vendor changed. +sub build_shipto_inputs { + my ($self) = @_; + + my $content = $self->p->render('common/_ship_to_dialog', + vc_obj => $self->order->customervendor, + cs_obj => $self->order->custom_shipto, + cvars => $self->order->custom_shipto->cvars_by_config, + id_selector => '#order_shipto_id'); + + div_tag($content, id => 'shipto_inputs'); +} + # render the info line for business # # Needed, if customer/vendor changed. @@ -1338,6 +1362,12 @@ sub load_order { return if !$::form->{id}; $self->order(SL::DB::Order->new(id => $::form->{id})->load); + + # Add an empty custom shipto to the order, so that the dialog can render the cvar inputs. + # You need a custom shipto object to call cvars_by_config to get the cvars. + $self->order->custom_shipto(SL::DB::Shipto->new(module => 'OE', custom_variables => [])) if !$self->order->custom_shipto; + + return $self->order; } # load or create a new order object @@ -1354,8 +1384,9 @@ sub make_order { # order here solves this problem. my $order; $order = SL::DB::Order->new(id => $::form->{id})->load(with => [ 'orderitems', 'orderitems.part' ]) if $::form->{id}; - $order ||= SL::DB::Order->new(orderitems => [], - quotation => (any { $self->type eq $_ } (sales_quotation_type(), request_quotation_type()))); + $order ||= SL::DB::Order->new(orderitems => [], + quotation => (any { $self->type eq $_ } (sales_quotation_type(), request_quotation_type())), + currency_id => $::instance_conf->get_currency_id(),); my $cv_id_method = $self->cv . '_id'; if (!$::form->{id} && $::form->{$cv_id_method}) { @@ -1363,11 +1394,13 @@ sub make_order { setup_order_from_cv($order); } - my $form_orderitems = delete $::form->{order}->{orderitems}; - my $form_periodic_invoices_config = delete $::form->{order}->{periodic_invoices_config}; + my $form_orderitems = delete $::form->{order}->{orderitems}; + my $form_periodic_invoices_config = delete $::form->{order}->{periodic_invoices_config}; $order->assign_attributes(%{$::form->{order}}); + $self->setup_custom_shipto_from_form($order, $::form); + if (my $periodic_invoices_config_attrs = $form_periodic_invoices_config ? SL::YAML::Load($form_periodic_invoices_config) : undef) { my $periodic_invoices_config = $order->periodic_invoices_config || $order->periodic_invoices_config(SL::DB::PeriodicInvoicesConfig->new); $periodic_invoices_config->assign_attributes(%$periodic_invoices_config_attrs); @@ -1414,9 +1447,13 @@ sub make_item { $item ||= SL::DB::OrderItem->new(custom_variables => []); $item->assign_attributes(%$attr); - $item->longdescription($item->part->notes) if $is_new && !defined $attr->{longdescription}; - $item->project_id($record->globalproject_id) if $is_new && !defined $attr->{project_id}; - $item->lastcost($record->is_sales ? $item->part->lastcost : 0) if $is_new && !defined $attr->{lastcost_as_number}; + + if ($is_new) { + my $texts = get_part_texts($item->part, $record->language_id); + $item->longdescription($texts->{longdescription}) if !defined $attr->{longdescription}; + $item->project_id($record->globalproject_id) if !defined $attr->{project_id}; + $item->lastcost($record->is_sales ? $item->part->lastcost : 0) if !defined $attr->{lastcost_as_number}; + } return $item; } @@ -1453,8 +1490,9 @@ sub new_item { $price_src->price($item->sellprice); } else { $price_src = $price_source->best_price - ? $price_source->best_price - : $price_source->price_from_source(""); + ? $price_source->best_price + : $price_source->price_from_source(""); + $price_src->price($::form->round_amount($price_src->price / $record->exchangerate, 5)) if $record->exchangerate; $price_src->price(0) if !$price_source->best_price; } @@ -1487,7 +1525,9 @@ sub new_item { # saved. Adding empty custom_variables to new orderitem here solves this problem. $new_attr{custom_variables} = []; - $item->assign_attributes(%new_attr); + my $texts = get_part_texts($part, $record->language_id, description => $new_attr{description}, longdescription => $new_attr{longdescription}); + + $item->assign_attributes(%new_attr, %{ $texts }); return $item; } @@ -1495,7 +1535,7 @@ sub new_item { sub setup_order_from_cv { my ($order) = @_; - $order->$_($order->customervendor->$_) for (qw(taxzone_id payment_id delivery_term_id)); + $order->$_($order->customervendor->$_) for (qw(taxzone_id payment_id delivery_term_id currency_id)); $order->intnotes($order->customervendor->notes); @@ -1508,26 +1548,46 @@ sub setup_order_from_cv { } +# setup custom shipto from form +# +# The dialog returns form variables starting with 'shipto' and cvars starting +# with 'shiptocvar_'. +# Mark it to be deleted if a shipto from master data is selected +# (i.e. order has a shipto). +# Else, update or create a new custom shipto. If the fields are empty, it +# will not be saved on save. +sub setup_custom_shipto_from_form { + my ($self, $order, $form) = @_; + + if ($order->shipto) { + $self->is_custom_shipto_to_delete(1); + } else { + my $custom_shipto = $order->custom_shipto || $order->custom_shipto(SL::DB::Shipto->new(module => 'OE', custom_variables => [])); + + my $shipto_cvars = {map { my ($key) = m{^shiptocvar_(.+)}; $key => delete $form->{$_}} grep { m{^shiptocvar_} } keys %$form}; + my $shipto_attrs = {map { $_ => delete $form->{$_}} grep { m{^shipto} } keys %$form}; + + $custom_shipto->assign_attributes(%$shipto_attrs); + $custom_shipto->cvar_by_name($_)->value($shipto_cvars->{$_}) for keys %$shipto_cvars; + } +} + # recalculate prices and taxes # # Using the PriceTaxCalculator. Store linetotals in the item objects. sub recalc { my ($self) = @_; - # bb: todo: currency later - $self->order->currency_id($::instance_conf->get_currency_id()); - my %pat = $self->order->calculate_prices_and_taxes(); + $self->{taxes} = []; - foreach my $tax_chart_id (keys %{ $pat{taxes} }) { - my $tax = SL::DB::Manager::Tax->find_by(chart_id => $tax_chart_id); + foreach my $tax_id (keys %{ $pat{taxes_by_tax_id} }) { + my $netamount = sum0 map { $pat{amounts}->{$_}->{amount} } grep { $pat{amounts}->{$_}->{tax_id} == $tax_id } keys %{ $pat{amounts} }; - my @amount_keys = grep { $pat{amounts}->{$_}->{tax_id} == $tax->id } keys %{ $pat{amounts} }; - push(@{ $self->{taxes} }, { amount => $pat{taxes}->{$tax_chart_id}, - netamount => $pat{amounts}->{$amount_keys[0]}->{amount}, - tax => $tax }); + push(@{ $self->{taxes} }, { amount => $pat{taxes_by_tax_id}->{$tax_id}, + netamount => $netamount, + tax => SL::DB::Tax->new(id => $tax_id)->load }); } - pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items_sorted}, @{$pat{items}}; } @@ -1579,6 +1639,12 @@ sub save { my $db = $self->order->db; $db->with_transaction(sub { + # delete custom shipto if it is to be deleted or if it is empty + if ($self->order->custom_shipto && ($self->is_custom_shipto_to_delete || $self->order->custom_shipto->is_empty)) { + $self->order->custom_shipto->delete if $self->order->custom_shipto->shipto_id; + $self->order->custom_shipto(undef); + } + SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete || []}; $self->order->save(cascade => 1); @@ -1627,6 +1693,14 @@ sub workflow_sales_or_purchase_order { : $::form->{type} eq sales_order_type() ? purchase_order_type() : ''; + # check for direct delivery + # copy shipto in custom shipto (custom shipto will be copied by new_from() in case) + my $custom_shipto; + if ( $::form->{type} eq sales_order_type() && $destination_type eq purchase_order_type() + && $::form->{use_shipto} && $self->order->shipto) { + $custom_shipto = $self->order->shipto->clone('SL::DB::Order'); + } + $self->order(SL::DB::Order->new_from($self->order, destination_type => $destination_type)); $self->{converted_from_oe_id} = delete $::form->{id}; @@ -1635,6 +1709,15 @@ sub workflow_sales_or_purchase_order { $item->{new_fake_id} = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); } + if ($::form->{type} eq sales_order_type() && $destination_type eq purchase_order_type()) { + if ($::form->{use_shipto}) { + $self->order->custom_shipto($custom_shipto) if $custom_shipto; + } else { + # remove any custom shipto if not wanted + $self->order->custom_shipto(SL::DB::Shipto->new(module => 'OE', custom_variables => [])); + } + } + # change form type $::form->{type} = $destination_type; $self->type($self->init_type); @@ -1645,11 +1728,10 @@ sub workflow_sales_or_purchase_order { $self->get_unalterable_data(); $self->pre_render(); - # trigger rendering values for second row/longdescription as hidden, - # because they are loaded only on demand. So we need to keep the values - # from the source. - $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; - $_->{render_longdescription} = 1 for @{ $self->order->items_sorted }; + # trigger rendering values for second row as hidden, because they + # are loaded only on demand. So we need to keep the values from the + # source. + $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; $self->render( 'order/form', @@ -1663,7 +1745,9 @@ sub pre_render { my ($self) = @_; $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted(); + $self->{all_currencies} = SL::DB::Manager::Currency->get_all_sorted(); $self->{all_departments} = SL::DB::Manager::Department->get_all_sorted(); + $self->{all_languages} = SL::DB::Manager::Language->get_all_sorted(); $self->{all_employees} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->employee_id, deleted => 0 ] ], sort_by => 'name'); @@ -1679,10 +1763,9 @@ sub pre_render { $self->{positions_scrollbar_height} = SL::Helper::UserPreferences::PositionsScrollbar->new()->get_height(); my $print_form = Form->new(''); - $print_form->{type} = $self->type; - $print_form->{printers} = SL::DB::Manager::Printer->get_all_sorted; - $print_form->{languages} = SL::DB::Manager::Language->get_all_sorted; - $self->{print_options} = SL::Helper::PrintOptions->get_print_options( + $print_form->{type} = $self->type; + $print_form->{printers} = SL::DB::Manager::Printer->get_all_sorted; + $self->{print_options} = SL::Helper::PrintOptions->get_print_options( form => $print_form, options => {dialog_name_prefix => 'print_options.', show_headers => 1, @@ -1717,7 +1800,8 @@ sub pre_render { $self->get_item_cvpartnumber($_) for @{$self->order->items_sorted}; - $::request->{layout}->use_javascript("${_}.js") for qw(kivi.SalesPurchase kivi.Order kivi.File ckeditor/ckeditor ckeditor/adapters/jquery edit_periodic_invoices_config calculate_qty); + $::request->{layout}->use_javascript("${_}.js") for qw(kivi.SalesPurchase kivi.Order kivi.File ckeditor/ckeditor ckeditor/adapters/jquery + edit_periodic_invoices_config calculate_qty kivi.Validator follow_up); $self->setup_edit_action_bar; } @@ -1736,7 +1820,7 @@ sub setup_edit_action_bar { call => [ 'kivi.Order.save', 'save', $::instance_conf->get_order_warn_duplicate_parts, $::instance_conf->get_order_warn_no_deliverydate, ], - checks => [ 'kivi.Order.check_save_active_periodic_invoices' ], + checks => [ 'kivi.Order.check_save_active_periodic_invoices', ['kivi.validate_form','#order_form'] ], ], action => [ t8('Save as new'), @@ -1754,13 +1838,11 @@ sub setup_edit_action_bar { t8('Save and Sales Order'), submit => [ '#order_form', { action => "Order/sales_order" } ], only_if => (any { $self->type eq $_ } (sales_quotation_type(), purchase_order_type())), - disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, ], action => [ t8('Save and Purchase Order'), - submit => [ '#order_form', { action => "Order/purchase_order" } ], - only_if => (any { $self->type eq $_ } (sales_order_type(), request_quotation_type())), - disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, + call => [ 'kivi.Order.purchase_order_check_for_direct_delivery' ], + only_if => (any { $self->type eq $_ } (sales_order_type(), request_quotation_type())), ], action => [ t8('Save and Delivery Order'), @@ -1793,7 +1875,8 @@ sub setup_edit_action_bar { ], action => [ t8('Save and E-mail'), - call => [ 'kivi.Order.email', $::instance_conf->get_order_warn_duplicate_parts ], + call => [ 'kivi.Order.save', 'save_and_show_email_dialog', $::instance_conf->get_order_warn_duplicate_parts ], + disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, ], action => [ t8('Download attachments of all parts'), @@ -1810,6 +1893,18 @@ sub setup_edit_action_bar { disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, only_if => $deletion_allowed, ], + + combobox => [ + action => [ + t8('more') + ], + action => [ + t8('Follow-Up'), + call => [ 'kivi.Order.follow_up_window' ], + disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, + only_if => $::auth->assert('productivity', 1), + ], + ], # end of combobox "more" ); } } @@ -1825,6 +1920,7 @@ sub generate_pdf { $print_form->{format} = $params->{format} || 'pdf'; $print_form->{media} = $params->{media} || 'file'; $print_form->{groupitems} = $params->{groupitems}; + $print_form->{printer_id} = $params->{printer_id}; $print_form->{media} = 'file' if $print_form->{media} eq 'screen'; $order->language($params->{language}); @@ -1843,7 +1939,7 @@ sub generate_pdf { extension => $template_ext, email => $print_form->{media} eq 'email', language => $params->{language}, - printer_id => $print_form->{printer_id}, # todo + printer_id => $print_form->{printer_id}, ); if (!defined $template_file) { @@ -1955,6 +2051,9 @@ sub get_title_for { sub get_item_cvpartnumber { my ($self, $item) = @_; + return if !$self->search_cvpartnumber; + return if !$self->order->customervendor; + if ($self->cv eq 'vendor') { my @mms = grep { $_->make eq $self->order->customervendor->id } @{$item->part->makemodels}; $item->{cvpartnumber} = $mms[0]->model if scalar @mms; @@ -1964,6 +2063,30 @@ sub get_item_cvpartnumber { } } +sub get_part_texts { + my ($part_or_id, $language_or_id, %defaults) = @_; + + my $part = ref($part_or_id) ? $part_or_id : SL::DB::Part->load_cached($part_or_id); + my $language_id = ref($language_or_id) ? $language_or_id->id : $language_or_id; + my $texts = { + description => $defaults{description} // $part->description, + longdescription => $defaults{longdescription} // $part->notes, + }; + + return $texts unless $language_id; + + my $translation = SL::DB::Manager::Translation->get_first( + where => [ + parts_id => $part->id, + language_id => $language_id, + ]); + + $texts->{description} = $translation->translation if $translation && $translation->translation; + $texts->{longdescription} = $translation->longdescription if $translation && $translation->longdescription; + + return $texts; +} + sub sales_order_type { 'sales_order'; } @@ -2109,8 +2232,6 @@ java script functions =item * testing -=item * currency - =item * credit limit =item * more workflows (quotation, rfq) @@ -2119,12 +2240,8 @@ java script functions =item * select units in input row? -=item * custom shipto address - =item * check for direct delivery (workflow sales order -> purchase order) -=item * language / part translations - =item * access rights =item * display weights @@ -2175,10 +2292,6 @@ should be implemented. C does not use the currently inserted string for filtering. -=item * - -The language selected in print or email dialog is not saved when the order is saved. - =back =head1 To discuss / Nice to have @@ -2192,10 +2305,6 @@ How to expand/collapse second row. Now it can be done clicking the icon or =item * -Possibility to change longdescription in input row? - -=item * - Possibility to select PriceSources in input row? =item *