PartPicker: nach Artikelauswahl custom event 'set_item:PartPicker' mit item-Daten...
[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      unit        => $_->unit,
54     }
55   } @{ $self->parts }; # neato: if exact match triggers we don't even need the init_parts
56
57   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
58 }
59
60 sub action_test_page {
61   $::request->{layout}->add_javascripts('autocomplete_part.js');
62
63   $_[0]->render('part/test_page');
64 }
65
66 sub action_part_picker_search {
67   $_[0]->render('part/part_picker_search', { layout => 0 }, parts => $_[0]->parts);
68 }
69
70 sub action_part_picker_result {
71   $_[0]->render('part/_part_picker_result', { layout => 0 });
72 }
73
74 sub init_parts {
75   $_[0]->models->get;
76 }
77
78 sub init_models {
79   my ($self) = @_;
80
81   SL::Controller::Helper::GetModels->new(
82     controller => $self,
83     sorted => {
84       _default  => {
85         by => 'partnumber',
86         dir  => 1,
87       },
88       partnumber  => t8('Partnumber'),
89     },
90     with_objects => [ qw(unit_obj) ],
91   );
92 }
93
94 1;