86ce4263ecfd765fa4da432fc39e0dcafaa01eaf
[kivitendo-erp.git] / SL / Controller / TopQuickSearch / Article.pm
1 package SL::Controller::TopQuickSearch::Article;
2
3 use strict;
4 use parent qw(Rose::Object);
5
6 use SL::Locale::String qw(t8);
7 use SL::DB::Part;
8 use SL::Controller::Helper::GetModels;
9 use SL::Controller::Base;
10
11 use Rose::Object::MakeMethods::Generic (
12   'scalar --get_set_init' => [ qw(parts models part) ],
13 );
14
15 sub auth { 'part_service_assembly_edit' }
16
17 sub name { 'article' }
18
19 sub description_config { t8('Articles') }
20
21 sub description_field { t8('Articles') }
22
23 sub query_autocomplete {
24   my ($self) = @_;
25
26   my $objects = $self->models->get;
27
28   [
29     map {
30      value       => $_->displayable_name,
31      label       => $_->displayable_name,
32      id          => $_->id,
33     }, @$objects
34   ];
35 }
36
37 sub select_autocomplete {
38   redirect_to_part($::form->{id});
39 }
40
41 sub do_search {
42   my ($self) = @_;
43
44   my $objects = $self->models->get;
45
46   return !@$objects     ? ()
47        : @$objects == 1 ? redirect_to_part($objects->[0]->id)
48        :                  redirect_to_search($::form->{term});
49 }
50
51 sub redirect_to_search {
52   SL::Controller::Base->new->url_for(
53     controller  => 'ic.pl',
54     action      => 'generate_report',
55     searchitems => 'assembly',
56     all         => $_[0],
57   );
58 }
59
60 sub redirect_to_part {
61   SL::Controller::Base->new->url_for(
62     controller => 'ic.pl',
63     action     => 'edit',
64     id         => $_[0],
65   );
66 }
67
68 sub type {
69   ()
70 }
71
72 sub init_models {
73   my ($self) = @_;
74
75   SL::Controller::Helper::GetModels->new(
76     controller => $self,
77     model      => 'Part',
78     source     => {
79       filter => {
80         ($self->type),
81         'all:substr:multi::ilike' => $::form->{term},
82       },
83     },
84     sorted     => {
85       _default   => {
86         by  => 'partnumber',
87         dir => 1,
88       },
89       partnumber => t8('Partnumber'),
90     },
91     paginated  => {
92       per_page => 10,
93     },
94   )
95 }
96
97 1;