X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FDeliveryOrder.pm;h=9789710bcb89394199338722d145a6fe8e18a669;hb=14672a1d69298562d8aac987ff2c4eaab3492088;hp=0953811416eefd257bb7603341af9123097a15ae;hpb=c26b9ddd1e6110ed8a2b2d062ec506dd4c4dc140;p=kivitendo-erp.git diff --git a/SL/Controller/DeliveryOrder.pm b/SL/Controller/DeliveryOrder.pm index 095381141..9789710bc 100644 --- a/SL/Controller/DeliveryOrder.pm +++ b/SL/Controller/DeliveryOrder.pm @@ -1,4 +1,4 @@ -package SL::Controller::Order; +package SL::Controller::DeliveryOrder; use strict; use parent qw(SL::Controller::Base); @@ -33,6 +33,7 @@ use SL::Helper::UserPreferences::PositionsScrollbar; use SL::Helper::UserPreferences::UpdatePositions; use SL::Controller::Helper::GetModels; +use SL::Controller::DeliveryOrder::TypeData; use List::Util qw(first sum0); use List::UtilsBy qw(sort_by uniq_by); @@ -45,17 +46,13 @@ use Sort::Naturally; use Rose::Object::MakeMethods::Generic ( scalar => [ qw(item_ids_to_delete is_custom_shipto_to_delete) ], - 'scalar --get_set_init' => [ qw(order valid_types type cv p all_price_factors search_cvpartnumber show_update_button part_picker_classification_ids) ], + 'scalar --get_set_init' => [ qw(order valid_types type cv p all_price_factors search_cvpartnumber show_update_button part_picker_classification_ids type_data) ], ); # safety __PACKAGE__->run_before('check_auth'); -__PACKAGE__->run_before('recalc', - only => [ qw(save save_as_new save_and_delivery_order save_and_invoice save_and_ap_transaction - print send_email) ]); - __PACKAGE__->run_before('get_unalterable_data', only => [ qw(save save_as_new save_and_delivery_order save_and_invoice save_and_ap_transaction print send_email) ]); @@ -69,19 +66,12 @@ sub action_add { my ($self) = @_; $self->order->transdate(DateTime->now_local()); - my $extra_days = $self->type eq sales_quotation_type() ? $::instance_conf->get_reqdate_interval : - $self->type eq sales_order_type() ? $::instance_conf->get_delivery_date_interval : 1; - - if ( ($self->type eq sales_order_type() && $::instance_conf->get_deliverydate_on) - || ($self->type eq sales_quotation_type() && $::instance_conf->get_reqdate_on) - && (!$self->order->reqdate)) { - $self->order->reqdate(DateTime->today_local->next_workday(extra_days => $extra_days)); - } + $self->type_data->set_reqdate_by_type; $self->pre_render(); $self->render( - 'order/form', + 'delivery_order/form', title => $self->get_title_for('add'), %{$self->{template_args}} ); @@ -107,10 +97,9 @@ sub action_edit { $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; } - $self->recalc(); $self->pre_render(); $self->render( - 'order/form', + 'delivery_order/form', title => $self->get_title_for('edit'), %{$self->{template_args}} ); @@ -133,15 +122,15 @@ sub action_edit_collective { # fall back to save as new if only one id is given if (scalar @multi_ids == 1) { - $self->order(SL::DB::Order->new(id => $multi_ids[0])->load); + $self->order(SL::DB::DeliveryOrder->new(id => $multi_ids[0])->load); $self->action_save_as_new(); return; } # make new order from given orders - my @multi_orders = map { SL::DB::Order->new(id => $_)->load } @multi_ids; + my @multi_orders = map { SL::DB::DeliveryOrder->new(id => $_)->load } @multi_ids; $self->{converted_from_oe_id} = join ' ', map { $_->id } @multi_orders; - $self->order(SL::DB::Order->new_from_multi(\@multi_orders, sort_sources_by => 'transdate')); + $self->order(SL::DB::DeliveryOrder->new_from_multi(\@multi_orders, sort_sources_by => 'transdate')); $self->action_edit(); } @@ -157,12 +146,7 @@ sub action_delete { return $self->js->render(); } - my $text = $self->type eq sales_order_type() ? $::locale->text('The order has been deleted') - : $self->type eq purchase_order_type() ? $::locale->text('The order has been deleted') - : $self->type eq sales_quotation_type() ? $::locale->text('The quotation has been deleted') - : $self->type eq request_quotation_type() ? $::locale->text('The rfq has been deleted') - : ''; - flash_later('info', $text); + flash_later('info', $self->type_data->text("delete")); my @redirect_params = ( action => 'add', @@ -183,12 +167,7 @@ sub action_save { return $self->js->render(); } - my $text = $self->type eq sales_order_type() ? $::locale->text('The order has been saved') - : $self->type eq purchase_order_type() ? $::locale->text('The order has been saved') - : $self->type eq sales_quotation_type() ? $::locale->text('The quotation has been saved') - : $self->type eq request_quotation_type() ? $::locale->text('The rfq has been saved') - : ''; - flash_later('info', $text); + flash_later('info', $self->type_data->text("saved")); my @redirect_params = ( action => 'edit', @@ -211,7 +190,7 @@ sub action_save_as_new { } # load order from db to check if values changed - my $saved_order = SL::DB::Order->new(id => $order->id)->load; + my $saved_order = SL::DB::DeliveryOrder->new(id => $order->id)->load; my %new_attrs; # Lets assign a new number if the user hasn't changed the previous one. @@ -226,25 +205,13 @@ sub action_save_as_new { : $order->transdate; # Set new reqdate unless changed if it is enabled in client config - if ($order->reqdate == $saved_order->reqdate) { - my $extra_days = $self->type eq sales_quotation_type() ? $::instance_conf->get_reqdate_interval : - $self->type eq sales_order_type() ? $::instance_conf->get_delivery_date_interval : 1; - - if ( ($self->type eq sales_order_type() && !$::instance_conf->get_deliverydate_on) - || ($self->type eq sales_quotation_type() && !$::instance_conf->get_reqdate_on)) { - $new_attrs{reqdate} = ''; - } else { - $new_attrs{reqdate} = DateTime->today_local->next_workday(extra_days => $extra_days); - } - } else { - $new_attrs{reqdate} = $order->reqdate; - } + $new_attrs{reqdate} = $self->type_data->get_reqdate_by_type($order->reqdate, $saved_order->reqdate); # Update employee $new_attrs{employee} = SL::DB::Manager::Employee->current; # Create new record from current one - $self->order(SL::DB::Order->new_from($order, destination_type => $order->type, attributes => \%new_attrs)); + $self->order(SL::DB::DeliveryOrder->new_from($order, destination_type => $order->type, attributes => \%new_attrs)); # no linked records on save as new delete $::form->{$_} for qw(converted_from_oe_id converted_from_orderitems_ids); @@ -520,109 +487,6 @@ sub action_send_email { $self->redirect_to(@redirect_params); } -# open the periodic invoices config dialog -# -# If there are values in the form (i.e. dialog was opened before), -# then use this values. Create new ones, else. -sub action_show_periodic_invoices_config_dialog { - my ($self) = @_; - - my $config = make_periodic_invoices_config_from_yaml(delete $::form->{config}); - $config ||= SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $::form->{id}) if $::form->{id}; - $config ||= SL::DB::PeriodicInvoicesConfig->new(periodicity => 'm', - order_value_periodicity => 'p', # = same as periodicity - start_date_as_date => $::form->{transdate_as_date} || $::form->current_date, - extend_automatically_by => 12, - active => 1, - email_subject => GenericTranslations->get( - language_id => $::form->{language_id}, - translation_type =>"preset_text_periodic_invoices_email_subject"), - email_body => GenericTranslations->get( - language_id => $::form->{language_id}, - translation_type =>"preset_text_periodic_invoices_email_body"), - ); - $config->periodicity('m') if none { $_ eq $config->periodicity } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES; - $config->order_value_periodicity('p') if none { $_ eq $config->order_value_periodicity } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES); - - $::form->get_lists(printers => "ALL_PRINTERS", - charts => { key => 'ALL_CHARTS', - transdate => 'current_date' }); - - $::form->{AR} = [ grep { $_->{link} =~ m/(?:^|:)AR(?::|$)/ } @{ $::form->{ALL_CHARTS} } ]; - - if ($::form->{customer_id}) { - $::form->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(where => [ cp_cv_id => $::form->{customer_id} ]); - my $customer_object = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id}); - $::form->{postal_invoice} = $customer_object->postal_invoice; - $::form->{email_recipient_invoice_address} = $::form->{postal_invoice} ? '' : $customer_object->invoice_mail; - $config->send_email(0) if $::form->{postal_invoice}; - } - - $self->render('oe/edit_periodic_invoices_config', { layout => 0 }, - popup_dialog => 1, - popup_js_close_function => 'kivi.Order.close_periodic_invoices_config_dialog()', - popup_js_assign_function => 'kivi.Order.assign_periodic_invoices_config()', - config => $config, - %$::form); -} - -# assign the values of the periodic invoices config dialog -# as yaml in the hidden tag and set the status. -sub action_assign_periodic_invoices_config { - my ($self) = @_; - - $::form->isblank('start_date_as_date', $::locale->text('The start date is missing.')); - - my $config = { active => $::form->{active} ? 1 : 0, - terminated => $::form->{terminated} ? 1 : 0, - direct_debit => $::form->{direct_debit} ? 1 : 0, - periodicity => (any { $_ eq $::form->{periodicity} } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES) ? $::form->{periodicity} : 'm', - order_value_periodicity => (any { $_ eq $::form->{order_value_periodicity} } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES)) ? $::form->{order_value_periodicity} : 'p', - start_date_as_date => $::form->{start_date_as_date}, - end_date_as_date => $::form->{end_date_as_date}, - first_billing_date_as_date => $::form->{first_billing_date_as_date}, - print => $::form->{print} ? 1 : 0, - printer_id => $::form->{print} ? $::form->{printer_id} * 1 : undef, - copies => $::form->{copies} * 1 ? $::form->{copies} : 1, - extend_automatically_by => $::form->{extend_automatically_by} * 1 || undef, - ar_chart_id => $::form->{ar_chart_id} * 1, - send_email => $::form->{send_email} ? 1 : 0, - email_recipient_contact_id => $::form->{email_recipient_contact_id} * 1 || undef, - email_recipient_address => $::form->{email_recipient_address}, - email_sender => $::form->{email_sender}, - email_subject => $::form->{email_subject}, - email_body => $::form->{email_body}, - }; - - my $periodic_invoices_config = SL::YAML::Dump($config); - - my $status = $self->get_periodic_invoices_status($config); - - $self->js - ->remove('#order_periodic_invoices_config') - ->insertAfter(hidden_tag('order.periodic_invoices_config', $periodic_invoices_config), '#periodic_invoices_status') - ->run('kivi.Order.close_periodic_invoices_config_dialog') - ->html('#periodic_invoices_status', $status) - ->flash('info', t8('The periodic invoices config has been assigned.')) - ->render($self); -} - -sub action_get_has_active_periodic_invoices { - my ($self) = @_; - - my $config = make_periodic_invoices_config_from_yaml(delete $::form->{config}); - $config ||= SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $::form->{id}) if $::form->{id}; - - my $has_active_periodic_invoices = - $self->type eq sales_order_type() - && $config - && $config->active - && (!$config->end_date || ($config->end_date > DateTime->today_local)) - && $config->get_previous_billed_period_start_date; - - $_[0]->render(\ !!$has_active_periodic_invoices, { type => 'text' }); -} - # save the order and redirect to the frontend subroutine for a new # delivery order sub action_save_and_delivery_order { @@ -682,7 +546,6 @@ sub action_customer_vendor_changed { my ($self) = @_; setup_order_from_cv($self->order); - $self->recalc(); my $cv_method = $self->cv; @@ -715,7 +578,6 @@ sub action_customer_vendor_changed { ->focus( '#order_' . $self->cv . '_id') ->run('kivi.Order.update_exchangerate'); - $self->js_redisplay_amounts_and_taxes; $self->js_redisplay_cvpartnumbers; $self->js->render(); } @@ -764,12 +626,9 @@ sub action_unit_changed { my $old_unit_obj = SL::DB::Unit->new(name => $::form->{old_unit})->load; $item->sellprice($item->unit_obj->convert_to($item->sellprice, $old_unit_obj)); - $self->recalc(); - $self->js ->run('kivi.Order.update_sellprice', $::form->{item_id}, $item->sellprice_as_number); $self->js_redisplay_line_values; - $self->js_redisplay_amounts_and_taxes; $self->js->render(); } @@ -787,12 +646,10 @@ sub action_add_item { $self->order->add_items($item); - $self->recalc(); - $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', + my $row_as_html = $self->p->render('delivery_order/tabs/_row', ITEM => $item, ID => $item_id, SELF => $self, @@ -820,10 +677,9 @@ sub action_add_item { $item->discount(1) unless $assortment_item->charge; $self->order->add_items( $item ); - $self->recalc(); $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', + my $row_as_html = $self->p->render('delivery_order/tabs/_row', ITEM => $item, ID => $item_id, SELF => $self, @@ -846,7 +702,6 @@ sub action_add_item { $self->js->run('kivi.Order.row_table_scroll_down') if !$::form->{insert_before_item_id}; - $self->js_redisplay_amounts_and_taxes; $self->js->render(); } @@ -878,12 +733,10 @@ sub action_add_multi_items { } $self->order->add_items(@items); - $self->recalc(); - foreach my $item (@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', + my $row_as_html = $self->p->render('delivery_order/tabs/_row', ITEM => $item, ID => $item_id, SELF => $self, @@ -906,18 +759,6 @@ sub action_add_multi_items { $self->js->run('kivi.Order.row_table_scroll_down') if !$::form->{insert_before_item_id}; - $self->js_redisplay_amounts_and_taxes; - $self->js->render(); -} - -# recalculate all linetotals, amounts and taxes and redisplay them -sub action_recalc_amounts_and_taxes { - my ($self) = @_; - - $self->recalc(); - - $self->js_redisplay_line_values; - $self->js_redisplay_amounts_and_taxes; $self->js->render(); } @@ -927,7 +768,6 @@ sub action_update_exchangerate { 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 }); @@ -1015,7 +855,6 @@ sub action_return_from_create_part { $item->{new_fake_id} = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000); } - $self->recalc(); $self->get_unalterable_data(); $self->pre_render(); @@ -1026,7 +865,7 @@ sub action_return_from_create_part { $_->{render_longdescription} = 1 for @{ $self->order->items_sorted }; $self->render( - 'order/form', + 'delivery_order/form', title => $self->get_title_for('edit'), %{$self->{template_args}} ); @@ -1040,8 +879,6 @@ sub action_return_from_create_part { sub action_load_second_rows { my ($self) = @_; - $self->recalc() if $self->order->is_sales; # for margin calculation - foreach my $item_id (@{ $::form->{item_ids} }) { my $idx = first_index { $_ eq $item_id } @{ $::form->{orderitem_ids} }; my $item = $self->order->items_sorted->[$idx]; @@ -1097,9 +934,7 @@ sub action_update_row_from_master_data { } } - $self->recalc(); $self->js_redisplay_line_values; - $self->js_redisplay_amounts_and_taxes; $self->js->render(); } @@ -1117,7 +952,7 @@ sub js_load_second_row { $item->parse_custom_variable_values; } - my $row_as_html = $self->p->render('order/tabs/_second_row', ITEM => $item, TYPE => $self->type); + my $row_as_html = $self->p->render('delivery_order/tabs/_second_row', ITEM => $item, TYPE => $self->type); $self->js ->html('#second_row_' . $item_id, $row_as_html) @@ -1149,41 +984,6 @@ sub js_redisplay_line_values { ->run('kivi.Order.redisplay_line_values', $is_sales, \@data); } -sub js_redisplay_amounts_and_taxes { - my ($self) = @_; - - if (scalar @{ $self->{taxes} }) { - $self->js->show('#taxincluded_row_id'); - } else { - $self->js->hide('#taxincluded_row_id'); - } - - if ($self->order->taxincluded) { - $self->js->hide('#subtotal_row_id'); - } else { - $self->js->show('#subtotal_row_id'); - } - - if ($self->order->is_sales) { - my $is_neg = $self->order->marge_total < 0; - $self->js - ->html('#marge_total_id', $::form->format_amount(\%::myconfig, $self->order->marge_total, 2)) - ->html('#marge_percent_id', $::form->format_amount(\%::myconfig, $self->order->marge_percent, 2)) - ->action_if( $is_neg, 'addClass', '#marge_total_id', 'plus0') - ->action_if( $is_neg, 'addClass', '#marge_percent_id', 'plus0') - ->action_if( $is_neg, 'addClass', '#marge_percent_sign_id', 'plus0') - ->action_if(!$is_neg, 'removeClass', '#marge_total_id', 'plus0') - ->action_if(!$is_neg, 'removeClass', '#marge_percent_id', 'plus0') - ->action_if(!$is_neg, 'removeClass', '#marge_percent_sign_id', 'plus0'); - } - - $self->js - ->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'); -} - sub js_redisplay_cvpartnumbers { my ($self) = @_; @@ -1221,15 +1021,11 @@ sub js_reset_order_and_item_ids_after_save { # helpers # -sub init_valid_types { - [ sales_order_type(), purchase_order_type(), sales_quotation_type(), request_quotation_type() ]; -} - sub init_type { my ($self) = @_; if (none { $::form->{type} eq $_ } @{$self->valid_types}) { - die "Not a valid type for order"; + die "Not a valid type for delivery order"; } $self->type($::form->{type}); @@ -1238,11 +1034,7 @@ sub init_type { sub init_cv { my ($self) = @_; - my $cv = (any { $self->type eq $_ } (sales_order_type(), sales_quotation_type())) ? 'customer' - : (any { $self->type eq $_ } (purchase_order_type(), request_quotation_type())) ? 'vendor' - : die "Not a valid type for order"; - - return $cv; + return $self->type_data->customervendor; } sub init_search_cvpartnumber { @@ -1276,20 +1068,14 @@ sub init_all_price_factors { sub init_part_picker_classification_ids { my ($self) = @_; - my $attribute = 'used_for_' . ($self->type =~ m{sales} ? 'sale' : 'purchase'); - return [ map { $_->id } @{ SL::DB::Manager::PartClassification->get_all(where => [ $attribute => 1 ]) } ]; + return [ map { $_->id } @{ SL::DB::Manager::PartClassification->get_all(where => $self->type_data->part_classification_query) } ]; } sub check_auth { my ($self) = @_; - my $right_for = { map { $_ => $_.'_edit' } @{$self->valid_types} }; - - my $right = $right_for->{ $self->type }; - $right ||= 'DOES_NOT_EXIST'; - - $::auth->assert($right); + $::auth->assert($self->type_data->access || 'DOES_NOT_EXIST'); } # build the selection box for contacts @@ -1343,50 +1129,16 @@ sub build_shipto_inputs { # Needed, if customer/vendor changed. sub build_business_info_row { - $_[0]->p->render('order/tabs/_business_info_row', SELF => $_[0]); + $_[0]->p->render('delivery_order/tabs/_business_info_row', SELF => $_[0]); } -# build the rows for displaying taxes -# -# Called if amounts where recalculated and redisplayed. -sub build_tax_rows { - my ($self) = @_; - - my $rows_as_html; - foreach my $tax (sort { $a->{tax}->rate cmp $b->{tax}->rate } @{ $self->{taxes} }) { - $rows_as_html .= $self->p->render('order/tabs/_tax_row', TAX => $tax, TAXINCLUDED => $self->order->taxincluded); - } - return $rows_as_html; -} - - -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 load_order { my ($self) = @_; return if !$::form->{id}; - $self->order(SL::DB::Order->new(id => $::form->{id})->load); + $self->order(SL::DB::DeliveryOrder->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. @@ -1408,10 +1160,8 @@ sub make_order { # be retrieved via items until the order is saved. Adding empty items to new # 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())), - currency_id => $::instance_conf->get_currency_id(),); + $order = SL::DB::DeliveryOrder->new(id => $::form->{id})->load(with => [ 'orderitems', 'orderitems.part' ]) if $::form->{id}; + $order ||= SL::DB::DeliveryOrder->new(orderitems => [], currency_id => $::instance_conf->get_currency_id(), order_type => $self->type_data->validate($::form->{type})); my $cv_id_method = $self->cv . '_id'; if (!$::form->{id} && $::form->{$cv_id_method}) { @@ -1420,17 +1170,11 @@ sub make_order { } 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); - } - # remove deleted items $self->item_ids_to_delete([]); foreach my $idx (reverse 0..$#{$order->orderitems}) { @@ -1469,7 +1213,7 @@ sub make_item { # 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. - $item ||= SL::DB::OrderItem->new(custom_variables => []); + $item ||= SL::DB::DeliveryOrderItem->new(custom_variables => []); $item->assign_attributes(%$attr); @@ -1489,7 +1233,7 @@ sub make_item { sub new_item { my ($record, $attr) = @_; - my $item = SL::DB::OrderItem->new; + my $item = SL::DB::DeliveryOrderItem->new; # Remove attributes where the user left or set the inputs empty. # So these attributes will be undefined and we can distinguish them @@ -1517,7 +1261,6 @@ sub new_item { $price_src = $price_source->best_price ? $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; } @@ -1597,25 +1340,6 @@ sub setup_custom_shipto_from_form { } } -# recalculate prices and taxes -# -# Using the PriceTaxCalculator. Store linetotals in the item objects. -sub recalc { - my ($self) = @_; - - my %pat = $self->order->calculate_prices_and_taxes(); - - $self->{taxes} = []; - 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} }; - - 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}}; -} - # get data for saving, printing, ..., that is not changed in the form # # Only cvars for now. @@ -1672,14 +1396,14 @@ sub save { $self->order->custom_shipto(undef); } - SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete || []}; + SL::DB::DeliveryOrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete || []}; $self->order->save(cascade => 1); # link records if ($::form->{converted_from_oe_id}) { my @converted_from_oe_ids = split ' ', $::form->{converted_from_oe_id}; foreach my $converted_from_oe_id (@converted_from_oe_ids) { - my $src = SL::DB::Order->new(id => $converted_from_oe_id)->load; + my $src = SL::DB::DeliveryOrder->new(id => $converted_from_oe_id)->load; $src->update_attributes(closed => 1) if $src->type =~ /_quotation$/; $src->link_to_record($self->order); } @@ -1717,9 +1441,9 @@ sub workflow_sales_or_request_for_quotation { return $self->js->render(); } - my $destination_type = $::form->{type} eq sales_order_type() ? sales_quotation_type() : request_quotation_type(); + my $destination_type = $self->type_data->workflow("to_quotation_type"); - $self->order(SL::DB::Order->new_from($self->order, destination_type => $destination_type)); + $self->order(SL::DB::DeliveryOrder->new_from($self->order, destination_type => $destination_type)); $self->{converted_from_oe_id} = delete $::form->{id}; # set item ids to new fake id, to identify them as new items @@ -1733,7 +1457,6 @@ sub workflow_sales_or_request_for_quotation { $self->cv ($self->init_cv); $self->check_auth; - $self->recalc(); $self->get_unalterable_data(); $self->pre_render(); @@ -1743,7 +1466,7 @@ sub workflow_sales_or_request_for_quotation { $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; $self->render( - 'order/form', + 'delivery_order/form', title => $self->get_title_for('edit'), %{$self->{template_args}} ); @@ -1760,21 +1483,16 @@ sub workflow_sales_or_purchase_order { return $self->js->render(); } - my $destination_type = $::form->{type} eq sales_quotation_type() ? sales_order_type() - : $::form->{type} eq request_quotation_type() ? purchase_order_type() - : $::form->{type} eq purchase_order_type() ? sales_order_type() - : $::form->{type} eq sales_order_type() ? purchase_order_type() - : ''; + my $destination_type = $self->type_data->workflow("to_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'); + if ($self->type_data->workflow("to_order_copy_shipto") && $::form->{use_shipto} && $self->order->shipto) { + $custom_shipto = $self->order->shipto->clone('SL::DB::DeliveryOrder'); } - $self->order(SL::DB::Order->new_from($self->order, destination_type => $destination_type)); + $self->order(SL::DB::DeliveryOrder->new_from($self->order, destination_type => $destination_type)); $self->{converted_from_oe_id} = delete $::form->{id}; # set item ids to new fake id, to identify them as new items @@ -1782,7 +1500,7 @@ 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 ($self->type_data->workflow("to_order_copy_shipto")) { if ($::form->{use_shipto}) { $self->order->custom_shipto($custom_shipto) if $custom_shipto; } else { @@ -1797,7 +1515,6 @@ sub workflow_sales_or_purchase_order { $self->cv ($self->init_cv); $self->check_auth; - $self->recalc(); $self->get_unalterable_data(); $self->pre_render(); @@ -1807,7 +1524,7 @@ sub workflow_sales_or_purchase_order { $_->{render_second_row} = 1 for @{ $self->order->items_sorted }; $self->render( - 'order/form', + 'delivery_order/form', title => $self->get_title_for('edit'), %{$self->{template_args}} ); @@ -1831,7 +1548,6 @@ sub pre_render { obsolete => 0 ] ]); $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all_sorted(); $self->{current_employee_id} = SL::DB::Manager::Employee->current->id; - $self->{periodic_invoices_status} = $self->get_periodic_invoices_status($self->order->periodic_invoices_config); $self->{order_probabilities} = [ map { { title => ($_ * 10) . '%', id => $_ * 10 } } (0..10) ]; $self->{positions_scrollbar_height} = SL::Helper::UserPreferences::PositionsScrollbar->new()->get_height(); @@ -1854,14 +1570,7 @@ sub pre_render { $item->active_discount_source($price_source->discount_from_source($item->active_discount_source)); } - if (any { $self->type eq $_ } (sales_order_type(), purchase_order_type())) { - # Calculate shipped qtys here to prevent calling calculate for every item via the items method. - # Do not use write_to_objects to prevent order->delivered to be set, because this should be - # the value from db, which can be set manually or is set when linked delivery orders are saved. - SL::Helper::ShippedQty->new->calculate($self->order)->write_to(\@{$self->order->items}); - } - - if ($self->order->number && $::instance_conf->get_webdav) { + if ($self->order->${\ $self->type_data->nr_key } && $::instance_conf->get_webdav) { my $webdav = SL::Webdav->new( type => $self->type, number => $self->order->number, @@ -1873,34 +1582,32 @@ sub pre_render { } } @all_objects; } + $self->{template_args}{inout} = $self->type_data->properties('transfer'); + $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 kivi.Validator follow_up show_history); + $::request->{layout}->use_javascript("${_}.js") for qw(kivi.SalesPurchase kivi.DeliveryOrder kivi.File ckeditor/ckeditor ckeditor/adapters/jquery + calculate_qty kivi.Validator follow_up show_history); $self->setup_edit_action_bar; } sub setup_edit_action_bar { my ($self, %params) = @_; - my $deletion_allowed = (any { $self->type eq $_ } (sales_quotation_type(), request_quotation_type())) - || (($self->type eq sales_order_type()) && $::instance_conf->get_sales_order_show_delete) - || (($self->type eq purchase_order_type()) && $::instance_conf->get_purchase_order_show_delete); + my $deletion_allowed = $self->type_data->show_menu("delete"); for my $bar ($::request->layout->get('actionbar')) { $bar->add( combobox => [ action => [ t8('Save'), - call => [ 'kivi.Order.save', 'save', $::instance_conf->get_order_warn_duplicate_parts, + call => [ 'kivi.DeliveryOrder.save', 'save', $::instance_conf->get_order_warn_duplicate_parts, $::instance_conf->get_order_warn_no_deliverydate, ], - checks => [ 'kivi.Order.check_save_active_periodic_invoices', ['kivi.validate_form','#order_form'] ], ], action => [ t8('Save as new'), - call => [ 'kivi.Order.save', 'save_as_new', $::instance_conf->get_order_warn_duplicate_parts ], - checks => [ 'kivi.Order.check_save_active_periodic_invoices' ], + call => [ 'kivi.DeliveryOrder.save', 'save_as_new', $::instance_conf->get_order_warn_duplicate_parts ], disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, ], ], # end of combobox "Save" @@ -1911,41 +1618,40 @@ sub setup_edit_action_bar { ], action => [ t8('Save and Quotation'), - submit => [ '#order_form', { action => "Order/sales_quotation" } ], - only_if => (any { $self->type eq $_ } (sales_order_type())), + submit => [ '#order_form', { action => "DeliveryOrder/sales_quotation" } ], + only_if => $self->type_data->show_menu("save_and_quotation"), ], action => [ t8('Save and RFQ'), - submit => [ '#order_form', { action => "Order/request_for_quotation" } ], - only_if => (any { $self->type eq $_ } (purchase_order_type())), + submit => [ '#order_form', { action => "DeliveryOrder/request_for_quotation" } ], + only_if => $self->type_data->show_menu("save_and_rfq"), ], action => [ t8('Save and Sales Order'), - submit => [ '#order_form', { action => "Order/sales_order" } ], - only_if => (any { $self->type eq $_ } (sales_quotation_type(), purchase_order_type())), + submit => [ '#order_form', { action => "DeliveryOrder/sales_order" } ], + only_if => $self->type_data->show_menu("save_and_sales_order"), ], action => [ t8('Save and Purchase Order'), - call => [ 'kivi.Order.purchase_order_check_for_direct_delivery' ], - only_if => (any { $self->type eq $_ } (sales_order_type(), request_quotation_type())), + call => [ 'kivi.DeliveryOrder.purchase_order_check_for_direct_delivery' ], + only_if => $self->type_data->show_menu("save_and_purchase_order"), ], action => [ t8('Save and Delivery Order'), - call => [ 'kivi.Order.save', 'save_and_delivery_order', $::instance_conf->get_order_warn_duplicate_parts, + call => [ 'kivi.DeliveryOrder.save', 'save_and_delivery_order', $::instance_conf->get_order_warn_duplicate_parts, $::instance_conf->get_order_warn_no_deliverydate, ], - checks => [ 'kivi.Order.check_save_active_periodic_invoices' ], - only_if => (any { $self->type eq $_ } (sales_order_type(), purchase_order_type())) + only_if => $self->type_data->show_menu("save_and_delivery_order"), ], action => [ t8('Save and Invoice'), - call => [ 'kivi.Order.save', 'save_and_invoice', $::instance_conf->get_order_warn_duplicate_parts ], - checks => [ 'kivi.Order.check_save_active_periodic_invoices' ], + call => [ 'kivi.DeliveryOrder.save', 'save_and_invoice', $::instance_conf->get_order_warn_duplicate_parts ], + only_if => $self->type_data->show_menu("save_and_invoice"), ], action => [ t8('Save and AP Transaction'), - call => [ 'kivi.Order.save', 'save_and_ap_transaction', $::instance_conf->get_order_warn_duplicate_parts ], - only_if => (any { $self->type eq $_ } (purchase_order_type())) + call => [ 'kivi.DeliveryOrder.save', 'save_and_ap_transaction', $::instance_conf->get_order_warn_duplicate_parts ], + only_if => $self->type_data->show_menu("save_and_ap_transaction"), ], ], # end of combobox "Workflow" @@ -1956,20 +1662,20 @@ sub setup_edit_action_bar { ], action => [ t8('Save and preview PDF'), - call => [ 'kivi.Order.save', 'preview_pdf', $::instance_conf->get_order_warn_duplicate_parts, + call => [ 'kivi.DeliveryOrder.save', 'preview_pdf', $::instance_conf->get_order_warn_duplicate_parts, $::instance_conf->get_order_warn_no_deliverydate, ], ], action => [ t8('Save and print'), - call => [ 'kivi.Order.show_print_options', $::instance_conf->get_order_warn_duplicate_parts, + call => [ 'kivi.DeliveryOrder.show_print_options', $::instance_conf->get_order_warn_duplicate_parts, $::instance_conf->get_order_warn_no_deliverydate, ], ], action => [ t8('Save and E-mail'), id => 'save_and_email_action', - call => [ 'kivi.Order.save', 'save_and_show_email_dialog', $::instance_conf->get_order_warn_duplicate_parts, + call => [ 'kivi.DeliveryOrder.save', 'save_and_show_email_dialog', $::instance_conf->get_order_warn_duplicate_parts, $::instance_conf->get_order_warn_no_deliverydate, ], disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, @@ -1984,10 +1690,10 @@ sub setup_edit_action_bar { action => [ t8('Delete'), - call => [ 'kivi.Order.delete_order' ], + call => [ 'kivi.DeliveryOrder.delete_order' ], confirm => $::locale->text('Do you really want to delete this object?'), disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, - only_if => $deletion_allowed, + only_if => $self->type_data->show_menu("delete"), ], combobox => [ @@ -1996,7 +1702,7 @@ sub setup_edit_action_bar { ], action => [ t8('Follow-Up'), - call => [ 'kivi.Order.follow_up_window' ], + call => [ 'kivi.DeliveryOrder.follow_up_window' ], disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, only_if => $::auth->assert('productivity', 1), ], @@ -2104,50 +1810,11 @@ sub get_files_for_email_dialog { return %files; } -sub make_periodic_invoices_config_from_yaml { - my ($yaml_config) = @_; - - return if !$yaml_config; - my $attr = SL::YAML::Load($yaml_config); - return if 'HASH' ne ref $attr; - return SL::DB::PeriodicInvoicesConfig->new(%$attr); -} - - -sub get_periodic_invoices_status { - my ($self, $config) = @_; - - return if $self->type ne sales_order_type(); - return t8('not configured') if !$config; - - my $active = ('HASH' eq ref $config) ? $config->{active} - : ('SL::DB::PeriodicInvoicesConfig' eq ref $config) ? $config->active - : die "Cannot get status of periodic invoices config"; - - return $active ? t8('active') : t8('inactive'); -} - sub get_title_for { my ($self, $action) = @_; return '' if none { lc($action)} qw(add edit); - - # for locales: - # $::locale->text("Add Sales Order"); - # $::locale->text("Add Purchase Order"); - # $::locale->text("Add Quotation"); - # $::locale->text("Add Request for Quotation"); - # $::locale->text("Edit Sales Order"); - # $::locale->text("Edit Purchase Order"); - # $::locale->text("Edit Quotation"); - # $::locale->text("Edit Request for Quotation"); - - $action = ucfirst(lc($action)); - return $self->type eq sales_order_type() ? $::locale->text("$action Sales Order") - : $self->type eq purchase_order_type() ? $::locale->text("$action Purchase Order") - : $self->type eq sales_quotation_type() ? $::locale->text("$action Quotation") - : $self->type eq request_quotation_type() ? $::locale->text("$action Request for Quotation") - : ''; + return $self->type_data->text($action); } sub get_item_cvpartnumber { @@ -2189,28 +1856,8 @@ sub get_part_texts { return $texts; } -sub sales_order_type { - 'sales_order'; -} - -sub purchase_order_type { - 'purchase_order'; -} - -sub sales_quotation_type { - 'sales_quotation'; -} - -sub request_quotation_type { - 'request_quotation'; -} - sub nr_key { - return $_[0]->type eq sales_order_type() ? 'ordnumber' - : $_[0]->type eq purchase_order_type() ? 'ordnumber' - : $_[0]->type eq sales_quotation_type() ? 'quonumber' - : $_[0]->type eq request_quotation_type() ? 'quonumber' - : ''; + return $_[0]->type_data->nr_key; } sub save_and_redirect_to { @@ -2223,12 +1870,7 @@ sub save_and_redirect_to { return $self->js->render(); } - my $text = $self->type eq sales_order_type() ? $::locale->text('The order has been saved') - : $self->type eq purchase_order_type() ? $::locale->text('The order has been saved') - : $self->type eq sales_quotation_type() ? $::locale->text('The quotation has been saved') - : $self->type eq request_quotation_type() ? $::locale->text('The rfq has been saved') - : ''; - flash_later('info', $text); + flash_later('info', $self->type_data->text("saved")); $self->redirect_to(%params, id => $self->order->id); } @@ -2236,7 +1878,7 @@ sub save_and_redirect_to { sub save_history { my ($self, $addition) = @_; - my $number_type = $self->order->type =~ m{order} ? 'ordnumber' : 'quonumber'; + my $number_type = $self->nr_key; my $snumbers = $number_type . '_' . $self->order->$number_type; SL::DB::History->new( @@ -2288,6 +1930,14 @@ sub store_pdf_to_webdav_and_filemanagement { return @errors; } +sub init_type_data { + SL::Controller::DeliveryOrder::TypeData->new($_[0]); +} + +sub init_valid_types { + $_[0]->type_data->valid_types; +} + 1; __END__ @@ -2354,11 +2004,11 @@ and ajax. the controller -=item * C