Merge branch 'master' of github.com:kivitendo/kivitendo-erp
[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   my $value = $::form->{column} || 'description';
24
25   # if someone types something, and hits enter, assume he entered the full name.
26   # if something matches, treat that as sole match
27   # unfortunately get_models can't do more than one per package atm, so we d it
28   # the oldfashioned way.
29   if ($::form->{prefer_exact}) {
30     my $exact_matches;
31     if (1 == scalar @{ $exact_matches = SL::DB::Manager::Part->get_all(
32       query => [
33         obsolete => 0,
34         SL::DB::Manager::Part->type_filter($::form->{filter}{type}),
35         or => [
36           description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
37           partnumber  => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
38         ]
39       ],
40       limit => 2,
41     ) }) {
42       $self->parts($exact_matches);
43     }
44   }
45
46   my @hashes = map {
47    +{
48      value       => $_->$value,
49      label       => $_->long_description,
50      id          => $_->id,
51      partnumber  => $_->partnumber,
52      description => $_->description,
53      type        => $_->type,
54      unit        => $_->unit,
55      cvars       => { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $_->cvars_by_config } },
56     }
57   } @{ $self->parts }; # neato: if exact match triggers we don't even need the init_parts
58
59   $self->render(\ SL::JSON::to_json(\@hashes), { layout => 0, type => 'json', process => 0 });
60 }
61
62 sub action_test_page {
63   $::request->{layout}->add_javascripts('autocomplete_part.js');
64
65   $_[0]->render('part/test_page');
66 }
67
68 sub action_part_picker_search {
69   $_[0]->render('part/part_picker_search', { layout => 0 }, parts => $_[0]->parts);
70 }
71
72 sub action_part_picker_result {
73   $_[0]->render('part/_part_picker_result', { layout => 0 });
74 }
75
76 sub action_show {
77   my ($self) = @_;
78
79   if ($::request->type eq 'json') {
80     my $part_hash;
81     if (!$self->part) {
82       # TODO error
83     } else {
84       require Rose::DB::Object::Helpers;
85         $part_hash          = $self->part->as_tree;
86         $part_hash->{cvars} = $self->part->cvar_as_hashref;
87     }
88
89     $self->render(\ SL::JSON::to_json($part_hash), { layout => 0, type => 'json', process => 0 });
90   }
91 }
92
93 sub init_parts {
94   if ($::form->{no_paginate}) {
95     $_[0]->models->disable_plugin('paginated');
96   }
97
98   $_[0]->models->get;
99 }
100
101 sub init_part {
102   SL::DB::Part->new(id => $::form->{id} || $::form->{part}{id})->load;
103 }
104
105 sub init_models {
106   my ($self) = @_;
107
108   SL::Controller::Helper::GetModels->new(
109     controller => $self,
110     sorted => {
111       _default  => {
112         by => 'description',
113         dir  => 1,
114       },
115       partnumber  => t8('Partnumber'),
116       description  => t8('Description'),
117     },
118     with_objects => [ qw(unit_obj) ],
119   );
120 }
121
122 1;