epic-ts
[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 part) ],
14 );
15
16 # safety
17 __PACKAGE__->run_before(sub { $::auth->assert('part_service_assembly_edit') },
18                         except => [ qw(ajax_autocomplete part_picker_search part_picker_result) ]);
19
20 sub action_ajax_autocomplete {
21   my ($self, %params) = @_;
22
23   # if someone types something, and hits enter, assume he entered the full name.
24   # if something matches, treat that as sole match
25   # unfortunately get_models can't do more than one per package atm, so we d it
26   # the oldfashioned way.
27   if ($::form->{prefer_exact}) {
28     my $exact_matches;
29     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Part->get_all(
30       query => [
31         obsolete => 0,
32         SL::DB::Manager::Part->type_filter($::form->{filter}{type}),
33         or => [
34           description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
35           partnumber  => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
36         ]
37       ],
38       limit => 2,
39     ) }) {
40       $self->parts($exact_matches);
41     }
42   }
43
44   my @hashes = map {
45    +{
46      value       => $_->displayable_name,
47      label       => $_->displayable_name,
48      id          => $_->id,
49      partnumber  => $_->partnumber,
50      description => $_->description,
51      type        => $_->type,
52      unit        => $_->unit,
53      cvars       => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
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   $_[0]->render('part/test_page');
62 }
63
64 sub action_part_picker_search {
65   $_[0]->render('part/part_picker_search', { layout => 0 }, parts => $_[0]->parts);
66 }
67
68 sub action_part_picker_result {
69   $_[0]->render('part/_part_picker_result', { layout => 0 });
70 }
71
72 sub action_show {
73   my ($self) = @_;
74
75   if ($::request->type eq 'json') {
76     my $part_hash;
77     if (!$self->part) {
78       # TODO error
79     } else {
80       $part_hash          = $self->part->as_tree;
81       $part_hash->{cvars} = $self->part->cvar_as_hashref;
82     }
83
84     $self->render(\ SL::JSON::to_json($part_hash), { layout => 0, type => 'json', process => 0 });
85   }
86 }
87
88 sub init_parts {
89   if ($::form->{no_paginate}) {
90     $_[0]->models->disable_plugin('paginated');
91   }
92
93   $_[0]->models->get;
94 }
95
96 sub init_part {
97   SL::DB::Part->new(id => $::form->{id} || $::form->{part}{id})->load;
98 }
99
100 sub init_models {
101   my ($self) = @_;
102
103   SL::Controller::Helper::GetModels->new(
104     controller => $self,
105     sorted => {
106       _default  => {
107         by => 'partnumber',
108         dir  => 1,
109       },
110       partnumber  => t8('Partnumber'),
111       description  => t8('Description'),
112     },
113     with_objects => [ qw(unit_obj) ],
114   );
115 }
116
117 1;