partpicker mit classification_id als filter erweitert
[kivitendo-erp.git] / SL / Presenter / Part.pm
1 package SL::Presenter::Part;
2
3 use strict;
4
5 use SL::DB::Part;
6 use SL::DB::PartClassification;
7 use SL::Locale::String qw(t8);
8
9 use Exporter qw(import);
10 our @EXPORT = qw(part_picker part select_classification classification_abbreviation type_abbreviation separate_abbreviation typeclass_abbreviation);
11
12 use Carp;
13
14 sub part {
15   my ($self, $part, %params) = @_;
16
17   $params{display} ||= 'inline';
18
19   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
20
21   my $text = join '', (
22     $params{no_link} ? '' : '<a href="controller.pl?action=Part/edit&part.id=' . $self->escape($part->id) . '">',
23     $self->escape($part->partnumber),
24     $params{no_link} ? '' : '</a>',
25   );
26   return $self->escaped_text($text);
27 }
28
29 sub part_picker {
30   my ($self, $name, $value, %params) = @_;
31
32   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
33   my $id = delete($params{id}) || $self->name_to_id($name);
34   my $fat_set_item = delete $params{fat_set_item};
35
36   my @classes = $params{class} ? ($params{class}) : ();
37   push @classes, 'part_autocomplete';
38   push @classes, 'partpicker_fat_set_item' if $fat_set_item;
39
40   my $ret =
41     $self->input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id) .
42     join('', map { $params{$_} ? $self->input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(part_type classification_id unit convertible_unit)) .
43     $self->input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
44
45   $::request->layout->add_javascripts('autocomplete_part.js');
46   $::request->presenter->need_reinit_widgets($id);
47
48   $self->html_tag('span', $ret, class => 'part_picker');
49 }
50
51 #
52 # shortcut for article type
53 #
54 sub type_abbreviation {
55   my ($self, $part_type) = @_;
56   return $::locale->text('Assembly (typeabbreviation)')   if $part_type eq 'assembly';
57   return $::locale->text('Part (typeabbreviation)')       if $part_type eq 'part';
58   return $::locale->text('Assortment (typeabbreviation)') if $part_type eq 'assortment';
59   return $::locale->text('Service (typeabbreviation)');
60 }
61
62 #
63 # Translations for Abbreviations:
64 #
65 # $::locale->text('None (typeabbreviation)')
66 # $::locale->text('Purchase (typeabbreviation)')
67 # $::locale->text('Sales (typeabbreviation)')
68 # $::locale->text('Merchandise (typeabbreviation)')
69 # $::locale->text('Production (typeabbreviation)')
70 #
71 # and for descriptions
72 # $::locale->text('Purchase')
73 # $::locale->text('Sales')
74 # $::locale->text('Merchandise')
75 # $::locale->text('Production')
76
77 #
78 # shortcut for article type
79 #
80 sub classification_abbreviation {
81   my ($self, $id) = @_;
82   SL::DB::Manager::PartClassification->cache_all();
83   my $obj = SL::DB::PartClassification->load_cached($id);
84   $obj && $obj->abbreviation ? t8($obj->abbreviation) : '';
85 }
86
87 sub typeclass_abbreviation {
88   my ($self, $part) = @_;
89   return '' if !$part || !$part->isa('SL::DB::Part');
90   return $self->type_abbreviation($part->part_type).$self->classification_abbreviation($part->classification_id);
91 }
92
93 #
94 # shortcut for article type
95 #
96 sub separate_abbreviation {
97   my ($self, $id) = @_;
98   SL::DB::Manager::PartClassification->cache_all();
99   my $obj = SL::DB::PartClassification->load_cached($id);
100   $obj && $obj->abbreviation && $obj->report_separate ? t8($obj->abbreviation) : '';
101 }
102
103 #
104 # generate selection tag
105 #
106 sub select_classification {
107   my ($self, $name, %attributes) = @_;
108   $attributes{value_key} = 'id';
109   $attributes{title_key} = 'description';
110   my $collection = SL::DB::Manager::PartClassification->get_all_sorted();
111   $_->description($::locale->text($_->description)) for @{ $collection };
112   return $self->select_tag( $name, $collection, %attributes );
113 }
114
115 1;
116
117 __END__
118
119 =encoding utf-8
120
121 =head1 NAME
122
123 SL::Presenter::Part - Part related presenter stuff
124
125 =head1 SYNOPSIS
126
127   # Create an html link for editing/opening a part/service/assembly
128   my $object = my $object = SL::DB::Manager::Part->get_first;
129   my $html   = SL::Presenter->get->part($object, display => 'inline');
130
131 see also L<SL::Presenter>
132
133 =head1 DESCRIPTION
134
135 see L<SL::Presenter>
136
137 =head1 FUNCTIONS
138
139 =over 2
140
141 =item C<part, $object, %params>
142
143 Returns a rendered version (actually an instance of
144 L<SL::Presenter::EscapedText>) of the part object C<$object>
145
146 C<%params> can include:
147
148 =over 4
149
150 =item * display
151
152 Either C<inline> (the default) or C<table-cell>. At the moment both
153 representations are identical and produce the part's name linked
154 to the corresponding 'edit' action.
155
156 =back
157
158 =back
159
160 =over 2
161
162 =item C<classification_abbreviation $classification_id>
163
164 Returns the shortcut of the classification
165
166 =back
167
168 =over 2
169
170 =item C<separate_abbreviation $classification_id>
171
172 Returns the shortcut of the classification if the classifiaction has the separate flag set.
173
174 =back
175
176 =over 2
177
178 =item C<select_classification $name,%params>
179
180 Returns a HTML Select Tag with all available Classifications
181
182 C<%params> can include:
183
184 =over 4
185
186 =item * default
187
188 The Id of the selected item .
189
190 =back
191
192 =back
193
194 =over 2
195
196 =item C<part_picker $name, $value, %params>
197
198 All-in-one picker widget for parts. The name will be both id and name
199 of the resulting hidden C<id> input field (but the ID can be
200 overwritten with C<$params{id}>).
201
202 An additional dummy input will be generated which is used to find
203 parts. For a detailed description of its behaviour, see section
204 C<PART PICKER SPECIFICATION>.
205
206 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
207
208 If C<%params> contains C<part_type> only parts of this type will be used
209 for autocompletion. You may comma separate multiple types as in
210 C<part,assembly>.
211
212 If C<%params> contains C<unit> only parts with this unit will be used
213 for autocompletion. You may comma separate multiple units as in
214 C<h,min>.
215
216 If C<%params> contains C<convertible_unit> only parts with a unit
217 that's convertible to unit will be used for autocompletion.
218
219 Obsolete parts will by default not be displayed for selection. However they are
220 accepted as default values and can persist during updates. As with other
221 selectors though, they are not selectable once overridden.
222
223 C<part_picker> will register it's javascript for inclusion in the next header
224 rendering. If you write a standard controller that only call C<render> once, it
225 will just work.  In case the header is generated in a different render call
226 (multiple blocks, ajax, old C<bin/mozilla> style controllers) you need to
227 include C<js/autocomplete_part.js> yourself.
228
229 =back
230
231 =head1 PART PICKER SPECIFICATION
232
233 The following list of design goals were applied:
234
235 =over 4
236
237 =item *
238
239 Parts should not be perceived by the user as distinct inputs of partnumber and
240 description but as a single object
241
242 =item *
243
244 Easy to use without documentation for novice users
245
246 =item *
247
248 Fast to use with keyboard for experienced users
249
250 =item *
251
252 Possible to use without any keyboard interaction for mouse (or touchscreen)
253 users
254
255 =item *
256
257 Must not leave the current page in event of ambiguity (cf. current select_item
258 mechanism)
259
260 =item *
261
262 Should be useable with hand scanners or similar alternative keyboard devices
263
264 =item *
265
266 Should not require a feedback/check loop in the common case
267
268 =item *
269
270 Should not be constrained to exact matches
271
272 =back
273
274 The implementation consists of the following parts which will be referenced later:
275
276 =over 4
277
278 =item 1
279
280 A hidden input (id input), used to hold the id of the selected part. The only
281 input that gets submitted
282
283 =item 2
284
285 An input (dummy input) containing a description of the currently selected part,
286 also used by the user to search for parts
287
288 =item 3
289
290 A jquery.autocomplete mechanism attached to the dummy field
291
292 =item 4
293
294 A popup layer for both feedback and input of additional data in case of
295 ambiguity.
296
297 =item 5
298
299 An internal status of the part picker, indicating whether id input and dummy
300 input are consistent. After leaving the dummy input the part picker must
301 place itself in a consistent status.
302
303 =item 6
304
305 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
306
307 =back
308
309 =head1 BUGS
310
311 None atm :)
312
313 =head1 AUTHOR
314
315 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
316
317 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
318
319 =cut