exact match funktion wieder hergestellt nach dem letzten commit
[kivitendo-erp.git] / SL / Controller / Part.pm
1 package SL::Controller::Part;
2
3 use strict;
4 use parent qw(SL::Controller::Base);
5
6 use Clone qw(clone);
7 use SL::DB::Part;
8 use SL::Controller::Helper::GetModels;
9 use SL::Controller::Helper::Filtered;
10 use SL::Controller::Helper::Sorted;
11 use SL::Controller::Helper::Paginated;
12 use SL::Controller::Helper::Filtered;
13 use SL::Locale::String qw(t8);
14 use SL::JSON;
15
16 use Rose::Object::MakeMethods::Generic (
17   'scalar --get_set_init' => [ qw(parts) ],
18 );
19
20 # safety
21 __PACKAGE__->run_before(sub { $::auth->assert('part_service_assembly_edit') });
22
23 __PACKAGE__->make_filtered(
24   ONLY          => [ qw(part_picker_search part_picker_result ajax_autocomplete) ],
25   LAUNDER_TO    => 'filter',
26 );
27 __PACKAGE__->make_paginated(
28   ONLY          => [ qw(part_picker_search part_picker_result ajax_autocomplete) ],
29 );
30
31 __PACKAGE__->make_sorted(
32   ONLY              => [ qw(part_picker_search part_picker_result ajax_autocomplete) ],
33
34   DEFAULT_BY        => 'partnumber',
35   DEFAULT_DIR       => 1,
36
37   partnumber        => t8('Partnumber'),
38 );
39
40 sub action_ajax_autocomplete {
41   my ($self, %params) = @_;
42
43   my $value = $::form->{column} || 'description';
44
45   # if someone types something, and hits enter, assume he entered the full name.
46   # if something matches, treat that as sole match
47   # unfortunately get_models can't do more than one per package atm, so we d it
48   # the oldfashioned way.
49   if ($::form->{prefer_exact}) {
50     my $exact_matches;
51     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Part->get_all(
52       query => [
53         obsolete => 0,
54         SL::DB::Manager::Part->type_filter($::form->{filter}{type}),
55         or => [
56           description => { ilike => $::form->{filter}{'all:substr::ilike'} },
57           partnumber  => { ilike => $::form->{filter}{'all:substr::ilike'} },
58         ]
59       ],
60       limit => 2,
61     ) }) {
62       $self->parts($exact_matches);
63     }
64   }
65
66   my @hashes = map {
67    +{
68      value       => $_->$value,
69      label       => $_->long_description,
70      id          => $_->id,
71      partnumber  => $_->partnumber,
72      description => $_->description,
73      type        => $_->type,
74     }
75   } @{ $self->parts }; # neato: if exact match triggers we don't even need the init_parts
76
77   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
78 }
79
80 sub action_test_page {
81   $::request->{layout}->add_javascripts('autocomplete_part.js');
82
83   $_[0]->render('part/test_page');
84 }
85
86 sub action_part_picker_search {
87   $_[0]->render('part/part_picker_search', { layout => 0 }, parts => $_[0]->parts);
88 }
89
90 sub action_part_picker_result {
91   $_[0]->render('part/_part_picker_result', { layout => 0 });
92 }
93
94 sub init_parts {
95   $_[0]->get_models;
96 }
97
98 1;