model wird jetzt wieder korrekt inferiert
[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::Locale::String qw(t8);
10 use SL::JSON;
11
12 use Rose::Object::MakeMethods::Generic (
13   'scalar --get_set_init' => [ qw(parts models) ],
14 );
15
16 # safety
17 __PACKAGE__->run_before(sub { $::auth->assert('part_service_assembly_edit') });
18
19 sub action_ajax_autocomplete {
20   my ($self, %params) = @_;
21
22   my $value = $::form->{column} || 'description';
23
24   # if someone types something, and hits enter, assume he entered the full name.
25   # if something matches, treat that as sole match
26   # unfortunately get_models can't do more than one per package atm, so we d it
27   # the oldfashioned way.
28   if ($::form->{prefer_exact}) {
29     my $exact_matches;
30     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Part->get_all(
31       query => [
32         obsolete => 0,
33         SL::DB::Manager::Part->type_filter($::form->{filter}{type}),
34         or => [
35           description => { ilike => $::form->{filter}{'all:substr::ilike'} },
36           partnumber  => { ilike => $::form->{filter}{'all:substr::ilike'} },
37         ]
38       ],
39       limit => 2,
40     ) }) {
41       $self->parts($exact_matches);
42     }
43   }
44
45   my @hashes = map {
46    +{
47      value       => $_->$value,
48      label       => $_->long_description,
49      id          => $_->id,
50      partnumber  => $_->partnumber,
51      description => $_->description,
52      type        => $_->type,
53     }
54   } @{ $self->parts }; # neato: if exact match triggers we don't even need the init_parts
55
56   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
57 }
58
59 sub action_test_page {
60   $::request->{layout}->add_javascripts('autocomplete_part.js');
61
62   $_[0]->render('part/test_page');
63 }
64
65 sub action_part_picker_search {
66   $_[0]->render('part/part_picker_search', { layout => 0 }, parts => $_[0]->parts);
67 }
68
69 sub action_part_picker_result {
70   $_[0]->render('part/_part_picker_result', { layout => 0 });
71 }
72
73 sub init_parts {
74   $_[0]->models->get;
75 }
76
77 sub init_models {
78   my ($self) = @_;
79
80   SL::Controller::Helper::GetModels->new(
81     controller => $self,
82     sorted => {
83       _default  => {
84         by => 'partnumber',
85         dir  => 1,
86       },
87       partnumber  => t8('Partnumber'),
88     },
89     with_objects => [ qw(unit_obj) ],
90   );
91 }
92
93 1;