X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FRequirementSpecOrder.pm;h=398c86a47ed23faeb0279526a73cbc6763eee2a8;hb=53593baa211863fbf66540cf1bcc36c8fb37257f;hp=b02c985bca1e30f94ecd37a2aa6ad5a646ad258f;hpb=30e1cefc44fe755fcfd222e60ba895815420fb66;p=kivitendo-erp.git diff --git a/SL/Controller/RequirementSpecOrder.pm b/SL/Controller/RequirementSpecOrder.pm index b02c985bc..398c86a47 100644 --- a/SL/Controller/RequirementSpecOrder.pm +++ b/SL/Controller/RequirementSpecOrder.pm @@ -8,7 +8,6 @@ use parent qw(SL::Controller::Base); use List::MoreUtils qw(uniq); use List::Util qw(first); -use SL::ClientJS; use SL::DB::Customer; use SL::DB::Order; use SL::DB::Part; @@ -23,7 +22,7 @@ use constant FORMS_SELECTOR => '#quotations_and_orders_article_assignment,#quota use Rose::Object::MakeMethods::Generic ( scalar => [ qw(parts) ], - 'scalar --get_set_init' => [ qw(requirement_spec rs_order js h_unit_name all_customers all_parts_time_unit) ], + 'scalar --get_set_init' => [ qw(requirement_spec rs_order h_unit_name all_customers all_parts_time_unit section_order_part) ], ); __PACKAGE__->run_before('setup'); @@ -42,20 +41,20 @@ sub action_new { my ($self) = @_; if (!@{ $self->all_parts_time_unit }) { - return $self->js->flash('error', t8('This function requires the presence of articles with a time-based unit such as "h" or "min".'))->render($self); + return $self->js->flash('error', t8('This function requires the presence of articles with a time-based unit such as "h" or "min".'))->render; } my $html = $self->render('requirement_spec_order/new', { output => 0 }, make_part_title => sub { $_[0]->partnumber . ' ' . $_[0]->description }); $self->js->hide(LIST_SELECTOR()) ->after(LIST_SELECTOR(), $html) - ->render($self); + ->render; } sub action_create { my ($self) = @_; if (!$::auth->assert($::form->{quotation} ? 'sales_quotation_edit' : 'sales_order_edit', 1)) { - return $self->js->flash('error', t8("You do not have the permissions to access this function."))->render($self); + return $self->js->flash('error', t8("You do not have the permissions to access this function."))->render; } # 1. Update sections with selected part IDs. @@ -85,7 +84,7 @@ sub action_create { $self->js->replaceWith(LIST_SELECTOR(), $html) ->remove(FORMS_SELECTOR()) ->flash('info', $::form->{quotation} ? t8('Sales quotation #1 has been created.', $order->quonumber) : t8('Sales order #1 has been created.', $order->ordnumber)) - ->render($self); + ->render; } sub action_update { @@ -95,7 +94,7 @@ sub action_update { my $sections = $self->requirement_spec->sections_sorted; if (!$::auth->assert($order->quotation ? 'sales_quotation_edit' : 'sales_order_edit', 1)) { - return $self->js->flash('error', t8("You do not have the permissions to access this function."))->render($self); + return $self->js->flash('error', t8("You do not have the permissions to access this function."))->render; } my (@orderitems, %sections_seen); @@ -116,7 +115,7 @@ sub action_update { $self->js->hide(LIST_SELECTOR()) ->after(LIST_SELECTOR(), $html) - ->render($self); + ->render; } sub action_do_update { @@ -143,20 +142,21 @@ sub action_do_update { $self->js->replaceWith(LIST_SELECTOR(), $html) ->remove(FORMS_SELECTOR()) ->flash('info', $::form->{quotation} ? t8('Sales quotation #1 has been updated.', $order->quonumber) : t8('Sales order #1 has been updated.', $order->ordnumber)) - ->render($self); + ->render; } sub action_edit_assignment { my ($self) = @_; if (!@{ $self->all_parts_time_unit }) { - return $self->js->flash('error', t8('This function requires the presence of articles with a time-based unit such as "h" or "min".'))->render($self); + return $self->js->flash('error', t8('This function requires the presence of articles with a time-based unit such as "h" or "min".'))->render; } my $html = $self->render('requirement_spec_order/edit_assignment', { output => 0 }, make_part_title => sub { $_[0]->partnumber . ' ' . $_[0]->description }); $self->js->hide(LIST_SELECTOR()) ->after(LIST_SELECTOR(), $html) - ->render($self); + ->reinit_widgets + ->render; } sub action_save_assignment { @@ -167,7 +167,7 @@ sub action_save_assignment { my $html = $self->render('requirement_spec_order/list', { output => 0 }); $self->js->replaceWith(LIST_SELECTOR(), $html) ->remove(FORMS_SELECTOR()) - ->render($self); + ->render; } sub action_delete { @@ -181,7 +181,7 @@ sub action_delete { my $html = $self->render('requirement_spec_order/list', { output => 0 }); $self->js->replaceWith(LIST_SELECTOR(), $html) ->flash('info', $order->quotation ? t8('Sales quotation #1 has been deleted.', $order->quonumber) : t8('Sales order #1 has been deleted.', $order->ordnumber)) - ->render($self); + ->render; } # @@ -192,7 +192,7 @@ sub setup { my ($self) = @_; $::auth->assert('requirement_spec_edit'); - $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec autocomplete_part); + $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec); $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec); return 1; @@ -203,21 +203,17 @@ sub init_requirement_spec { $self->requirement_spec(SL::DB::RequirementSpec->new(id => $::form->{requirement_spec_id})->load) if $::form->{requirement_spec_id}; } -sub init_js { - my ($self) = @_; - $self->js(SL::ClientJS->new); -} - sub init_all_customers { SL::DB::Manager::Customer->get_all_sorted } -sub init_h_unit_name { first { SL::DB::Manager::Unit->find_by(name => $_) } qw(Std h Stunde) }; +sub init_h_unit_name { SL::DB::Manager::Unit->find_h_unit->name }; sub init_rs_order { SL::DB::RequirementSpecOrder->new(id => $::form->{rs_order_id})->load }; +sub init_section_order_part { my $id = $::instance_conf->get_requirement_spec_section_order_part_id; return $id ? SL::DB::Part->new(id => $id)->load : undef } sub init_all_parts_time_unit { my ($self) = @_; return [] unless $self->h_unit_name; - my @convertible_unit_names = map { $_->name } @{ SL::DB::Manager::Unit->find_by(name => $self->h_unit_name)->convertible_units }; + my @convertible_unit_names = map { $_->name } @{ SL::DB::Manager::Unit->time_based_units }; return SL::DB::Manager::Part->get_all_sorted(where => [ unit => \@convertible_unit_names ]); } @@ -226,6 +222,17 @@ sub init_all_parts_time_unit { # helpers # +sub cache_parts { + my ($self, @ids) = @_; + + my $parts = !@ids ? [] : SL::DB::Manager::Part->get_all( + where => [ id => \@ids ], + with_objects => [ qw(unit_obj) ], + ); + + $self->parts({ map { ($_->id => $_) } @{ $parts } }); +} + sub do_update_sections { my ($self) = @_; @@ -233,9 +240,10 @@ sub do_update_sections { my $sections = $self->requirement_spec->sections_sorted; my %orderitems_by_id = map { ($_->id => $_) } @{ $order->orderitems }; my %sections_by_id = map { ($_->id => $_) } @{ $sections }; - $self->{parts} = { map { ($_->id => $_) } @{ SL::DB::Manager::Part->get_all(where => [ id => [ uniq map { $_->order_part_id } @{ $sections } ] ]) } }; my $language_id = $self->requirement_spec->customer->language_id; + $self->cache_parts(uniq map { $_->order_part_id } @{ $sections }); + my %sections_seen; foreach my $attributes (@{ $::form->{orderitems} || [] }) { @@ -260,9 +268,10 @@ sub do_update_additional_parts { my $order = $self->rs_order->order; my $add_parts = $self->requirement_spec->parts_sorted; my %orderitems_by = map { (($_->parts_id . '-' . $_->description) => $_) } @{ $order->items }; - $self->{parts} = { map { ($_->id => $_) } @{ SL::DB::Manager::Part->get_all(where => [ id => [ uniq map { $_->part_id } @{ $add_parts } ] ]) } }; my $language_id = $self->requirement_spec->customer->language_id; + $self->cache_parts(uniq map { $_->part_id } @{ $add_parts }); + my %add_part_seen; my @new_orderitems; @@ -287,6 +296,7 @@ sub create_order_item { my $section = $params{section}; my $item = $params{item} || SL::DB::OrderItem->new; my $part = $self->parts->{ $section->order_part_id }; + my $is_time_based = $part->unit_obj->is_time_based; my $translation = $params{language_id} ? first { $params{language_id} == $_->language_id } @{ $part->translations } : {}; my $description = $section->{keep_description} ? $item->description : ($translation->{translation} || $part->description); my $longdescription = $translation->{longdescription} || $part->notes; @@ -306,10 +316,10 @@ sub create_order_item { parts_id => $part->id, description => $description, longdescription => $longdescription, - qty => $section->time_estimation * 1, - unit => $self->h_unit_name, - sellprice => $::form->round_amount($self->requirement_spec->hourly_rate, 2), - lastcost => $part->lastcost, + qty => $is_time_based ? $section->time_estimation * 1 : 1, + unit => $is_time_based ? $self->h_unit_name : $part->unit, + sellprice => $::form->round_amount($self->requirement_spec->hourly_rate * ($is_time_based ? 1 : $section->time_estimation * $section->sellprice_factor), 2), + lastcost => $part->lastcost * $section->sellprice_factor, discount => 0, project_id => $self->requirement_spec->project_id, ); @@ -355,10 +365,13 @@ sub create_order { my @orderitems = map { $self->create_order_item( section => $_, language_id => $customer->language_id) } @{ $params{sections} }; my @add_items = map { $self->create_additional_part_order_item(additional_part => $_, language_id => $customer->language_id) } @{ $params{additional_parts} }; my $employee = SL::DB::Manager::Employee->current; + my $reqdate = !$::form->{quotation} ? undef + : $customer->payment_id ? $customer->payment->calc_date + : DateTime->today_local->next_workday(extra_days => $::instance_conf->get_reqdate_interval)->to_kivitendo; my $order = SL::DB::Order->new( globalproject_id => $self->requirement_spec->project_id, transdate => DateTime->today_local, - reqdate => $::form->{quotation} && $customer->payment_id ? $customer->payment->calc_date : undef, + reqdate => $reqdate, quotation => !!$::form->{quotation}, orderitems => [ @orderitems, @add_items ], customer_id => $customer->id,