WebshopApi: falsche sql update Abhängigkeit
[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 = $params{id} || $self->name_to_id($name);
34
35   my @classes = $params{class} ? ($params{class}) : ();
36   push @classes, 'part_autocomplete';
37
38   my $ret =
39     $self->input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
40       'data-part-picker-data' => JSON::to_json(\%params),
41     ) .
42     $self->input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
43
44   $::request->layout->add_javascripts('kivi.Part.js');
45   $::request->presenter->need_reinit_widgets($id);
46
47   $self->html_tag('span', $ret, class => 'part_picker');
48 }
49
50 #
51 # shortcut for article type
52 #
53 sub type_abbreviation {
54   my ($self, $part_type) = @_;
55   return $::locale->text('Assembly (typeabbreviation)')   if $part_type eq 'assembly';
56   return $::locale->text('Part (typeabbreviation)')       if $part_type eq 'part';
57   return $::locale->text('Assortment (typeabbreviation)') if $part_type eq 'assortment';
58   return $::locale->text('Service (typeabbreviation)');
59 }
60
61 #
62 # Translations for Abbreviations:
63 #
64 # $::locale->text('None (typeabbreviation)')
65 # $::locale->text('Purchase (typeabbreviation)')
66 # $::locale->text('Sales (typeabbreviation)')
67 # $::locale->text('Merchandise (typeabbreviation)')
68 # $::locale->text('Production (typeabbreviation)')
69 #
70 # and for descriptions
71 # $::locale->text('Purchase')
72 # $::locale->text('Sales')
73 # $::locale->text('Merchandise')
74 # $::locale->text('Production')
75
76 #
77 # shortcut for article type
78 #
79 sub classification_abbreviation {
80   my ($self, $id) = @_;
81   SL::DB::Manager::PartClassification->cache_all();
82   my $obj = SL::DB::PartClassification->load_cached($id);
83   $obj && $obj->abbreviation ? t8($obj->abbreviation) : '';
84 }
85
86 sub typeclass_abbreviation {
87   my ($self, $part) = @_;
88   return '' if !$part || !$part->isa('SL::DB::Part');
89   return $self->type_abbreviation($part->part_type).$self->classification_abbreviation($part->classification_id);
90 }
91
92 #
93 # shortcut for article type
94 #
95 sub separate_abbreviation {
96   my ($self, $id) = @_;
97   SL::DB::Manager::PartClassification->cache_all();
98   my $obj = SL::DB::PartClassification->load_cached($id);
99   $obj && $obj->abbreviation && $obj->report_separate ? t8($obj->abbreviation) : '';
100 }
101
102 #
103 # generate selection tag
104 #
105 sub select_classification {
106   my ($self, $name, %attributes) = @_;
107
108   $attributes{value_key} = 'id';
109   $attributes{title_key} = 'description';
110
111   my $classification_type_filter = delete $attributes{type} // [];
112
113   my $collection = SL::DB::Manager::PartClassification->get_all_sorted( where => $classification_type_filter );
114   $_->description($::locale->text($_->description)) for @{ $collection };
115   return $self->select_tag( $name, $collection, %attributes );
116 }
117
118 1;
119
120 __END__
121
122 =encoding utf-8
123
124 =head1 NAME
125
126 SL::Presenter::Part - Part related presenter stuff
127
128 =head1 SYNOPSIS
129
130   # Create an html link for editing/opening a part/service/assembly
131   my $object = SL::DB::Manager::Part->get_first;
132   my $html   = SL::Presenter->get->part($object, display => 'inline');
133
134 see also L<SL::Presenter>
135
136 =head1 DESCRIPTION
137
138 see L<SL::Presenter>
139
140 =head1 FUNCTIONS
141
142 =over 2
143
144 =item C<part, $object, %params>
145
146 Returns a rendered version (actually an instance of
147 L<SL::Presenter::EscapedText>) of the part object C<$object>
148
149 C<%params> can include:
150
151 =over 4
152
153 =item * display
154
155 Either C<inline> (the default) or C<table-cell>. At the moment both
156 representations are identical and produce the part's name linked
157 to the corresponding 'edit' action.
158
159 =back
160
161 =back
162
163 =over 2
164
165 =item C<classification_abbreviation $classification_id>
166
167 Returns the shortcut of the classification
168
169 =back
170
171 =over 2
172
173 =item C<separate_abbreviation $classification_id>
174
175 Returns the shortcut of the classification if the classification has the separate flag set.
176
177 =back
178
179 =over 2
180
181 =item C<select_classification $name,%params>
182
183 Returns an HTML select tag with all available classifications.
184
185 C<%params> can include:
186
187 =over 4
188
189 =item * default
190
191 The id of the selected item.
192
193 =back
194
195 =back
196
197 =over 2
198
199 =item C<part_picker $name, $value, %params>
200
201 All-in-one picker widget for parts. The name will be both id and name
202 of the resulting hidden C<id> input field (but the ID can be
203 overwritten with C<$params{id}>).
204
205 An additional dummy input will be generated which is used to find
206 parts. For a detailed description of its behaviour, see section
207 C<PART PICKER SPECIFICATION>.
208
209 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
210
211 If C<%params> contains C<part_type> only parts of this type will be used
212 for autocompletion. You may comma separate multiple types as in
213 C<part,assembly>.
214
215 If C<%params> contains C<unit> only parts with this unit will be used
216 for autocompletion. You may comma separate multiple units as in
217 C<h,min>.
218
219 If C<%params> contains C<convertible_unit> only parts with a unit
220 that's convertible to unit will be used for autocompletion.
221
222 Obsolete parts will by default not be displayed for selection. However they are
223 accepted as default values and can persist during updates. As with other
224 selectors though, they are not selectable once overridden.
225
226 C<part_picker> will register it's javascript for inclusion in the next header
227 rendering. If you write a standard controller that only calls C<render> once, it
228 will just work. In case the header is generated in a different render call
229 (multiple blocks, ajax, old C<bin/mozilla> style controllers) you need to
230 include C<kivi.Part.js> yourself.
231
232 On pressing <enter> the picker will try to commit the current selection,
233 resulting in one of the following events, whose corresponding callbacks can be
234 set in C<params.actions>:
235
236 =over 4
237
238 =item * C<commit_one>
239
240 If exactly one element matches the input, the internal id will be set to this
241 id, the internal state will be set to C<PICKED> and the C<change> event on the
242 picker will be fired. Additionally, if C<params> contains C<fat_set_item> a
243 special event C<set_item:PartPicker> will be fired which is guaranteed to
244 contain a complete JSON representation of the part.
245
246 After that the action C<commit_one> will be executed, which defaults to
247 clicking a button with id C<update_button> for backward compatibility reasons.
248
249 =item * C<commit_many>
250
251 If more than one element matches the input, the internal state will be set to
252 undefined.
253
254 After that the action C<commit_one> will be executed, which defaults to
255 opening a popup dialog for graphical interaction. If C<params> contains
256 C<multiple> an alternative popup will be opened, allowing multiple items to be
257 selected. Note however that this requires an additional callback
258 C<set_multi_items> to work.
259
260 =item * C<commit_none>
261
262 If no element matches the input, the internal state will be set to undefined.
263
264 If an action for C<commit_none> exists, it will be called with the picker
265 object and current term. The caller can then implement creation of new parts.
266
267 =back
268
269 =back
270
271 =head1 PART PICKER SPECIFICATION
272
273 The following list of design goals were applied:
274
275 =over 4
276
277 =item *
278
279 Parts should not be perceived by the user as distinct inputs of partnumber and
280 description but as a single object
281
282 =item *
283
284 Easy to use without documentation for novice users
285
286 =item *
287
288 Fast to use with keyboard for experienced users
289
290 =item *
291
292 Possible to use without any keyboard interaction for mouse (or touchscreen)
293 users
294
295 =item *
296
297 Must not leave the current page in event of ambiguity (cf. current select_item
298 mechanism)
299
300 =item *
301
302 Should be useable with hand scanners or similar alternative keyboard devices
303
304 =item *
305
306 Should not require a feedback/check loop in the common case
307
308 =item *
309
310 Should not be constrained to exact matches
311
312 =item *
313
314 Must be atomic
315
316 =item *
317
318 Action should be overridable
319
320 =back
321
322 The implementation consists of the following parts which will be referenced later:
323
324 =over 4
325
326 =item 1
327
328 A hidden input (id input), used to hold the id of the selected part. The only
329 input that gets submitted
330
331 =item 2
332
333 An input (dummy input) containing a description of the currently selected part,
334 also used by the user to search for parts
335
336 =item 3
337
338 A jquery.autocomplete mechanism attached to the dummy field
339
340 =item 4
341
342 A popup layer for both feedback and input of additional data in case of
343 ambiguity.
344
345 =item 5
346
347 An internal status of the part picker, indicating whether id input and dummy
348 input are consistent. After leaving the dummy input the part picker must
349 place itself in a consistent status.
350
351 =item 6
352
353 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
354
355 =back
356
357 =head1 BUGS
358
359 None atm :)
360
361 =head1 AUTHOR
362
363 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
364
365 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
366
367 =cut