]> wagnertech.de Git - mfinanz.git/blob - SL/Presenter/Part.pm
restart apache2 in postinst
[mfinanz.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 use SL::Presenter::EscapedText qw(escape is_escaped);
9 use SL::Presenter::Tag qw(input_tag html_tag name_to_id select_tag link_tag);
10
11 use Exporter qw(import);
12 our @EXPORT_OK = qw(
13   part_picker part select_classification classification_abbreviation
14   type_abbreviation separate_abbreviation typeclass_abbreviation
15   vendor_price_dropdown
16 );
17 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
18
19 use Carp;
20
21 sub part {
22   my ($part, %params) = @_;
23
24   $params{display} ||= 'inline';
25
26   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
27
28   my $text = escape($part->partnumber);
29   if (! delete $params{no_link}) {
30     my $href = 'controller.pl?action=Part/edit'
31                . '&part.id=' . escape($part->id);
32     $text = link_tag($href, $text, %params);
33   }
34
35   is_escaped($text);
36 }
37
38 sub part_picker {
39   my ($name, $value, %params) = @_;
40
41   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
42   my $id = $params{id} || name_to_id($name);
43
44   my @classes = $params{class} ? ($params{class}) : ();
45   push @classes, 'part_autocomplete';
46
47   # If there is no 'onClick' parameter, set it to 'this.select()',
48   # so that the user can type directly in the input field
49   # to search another part.
50   if (!grep { m{onclick}i } keys %params) {
51     $params{onClick} = 'this.select()';
52   }
53
54   my $ret =
55     input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
56       'data-part-picker-data' => JSON::to_json(\%params),
57     ) .
58     input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
59
60   $::request->layout->add_javascripts('kivi.Part.js');
61   $::request->presenter->need_reinit_widgets($id);
62
63   html_tag('span', $ret, class => 'part_picker');
64 }
65
66 sub picker { goto &part_picker }
67
68 #
69 # shortcut for article type
70 #
71 sub type_abbreviation {
72   my ($part_type) = @_;
73
74   return ''                                               if !$part_type;
75   return $::locale->text('Assembly (typeabbreviation)')   if $part_type eq 'assembly';
76   return $::locale->text('Part (typeabbreviation)')       if $part_type eq 'part';
77   return $::locale->text('Assortment (typeabbreviation)') if $part_type eq 'assortment';
78   return $::locale->text('Service (typeabbreviation)');
79 }
80
81 #
82 # Translations for Abbreviations:
83 #
84 # $::locale->text('None (typeabbreviation)')
85 # $::locale->text('Purchase (typeabbreviation)')
86 # $::locale->text('Sales (typeabbreviation)')
87 # $::locale->text('Merchandise (typeabbreviation)')
88 # $::locale->text('Production (typeabbreviation)')
89 #
90 # and for descriptions
91 # $::locale->text('Purchase')
92 # $::locale->text('Sales')
93 # $::locale->text('Merchandise')
94 # $::locale->text('Production')
95
96 #
97 # shortcut for article type
98 #
99 sub classification_abbreviation {
100   my ($id) = @_;
101
102   return '' if !$id;
103
104   SL::DB::Manager::PartClassification->cache_all();
105   my $obj = SL::DB::PartClassification->load_cached($id);
106   $obj && $obj->abbreviation ? t8($obj->abbreviation) : '';
107 }
108
109 sub typeclass_abbreviation {
110   my ($part) = @_;
111   return '' if !$part || !$part->isa('SL::DB::Part');
112   return type_abbreviation($part->part_type) . classification_abbreviation($part->classification_id);
113 }
114
115 #
116 # shortcut for article type
117 #
118 sub separate_abbreviation {
119   my ($id) = @_;
120
121   return '' if !$id;
122
123   SL::DB::Manager::PartClassification->cache_all();
124   my $obj = SL::DB::PartClassification->load_cached($id);
125   $obj && $obj->abbreviation && $obj->report_separate ? t8($obj->abbreviation) : '';
126 }
127
128 #
129 # generate selection tag
130 #
131 sub select_classification {
132   my ($name, %attributes) = @_;
133
134   $attributes{value_key} = 'id';
135   $attributes{title_key} = 'description';
136
137   my $classification_type_filter = delete $attributes{type} // [];
138
139   my $collection = SL::DB::Manager::PartClassification->get_all_sorted( where => $classification_type_filter );
140   $_->description($::locale->text($_->description)) for @{ $collection };
141   select_tag( $name, $collection, %attributes );
142 }
143
144 sub vendor_price_dropdown {
145   my ($name, $part, %attributes) = @_;
146
147   $attributes{value_key} = 'id';
148   $attributes{title_key} = 'text';
149
150   my @vendor_price_list = map {{
151         id => $_->vendor->id,
152         text => $_->vendor->vendornumber . "--" .$_->vendor->name
153                    . " ("
154                      . $::form->format_amount(\%::myconfig, $_->lastcost, 2)
155                    . ")",
156       }}
157     @{$part->makemodels};
158
159   my $count = scalar(@vendor_price_list);
160   if ($count) {
161     $attributes{size}    = $count;
162     $attributes{default} = $vendor_price_list[0]->{id};
163     select_tag( $name, \@vendor_price_list, %attributes );
164   } else {
165     html_tag('span', $::locale->text('No vendor available'));
166   }
167 }
168
169 1;
170
171 __END__
172
173 =encoding utf-8
174
175 =head1 NAME
176
177 SL::Presenter::Part - Part related presenter stuff
178
179 =head1 SYNOPSIS
180
181   # Create an html link for editing/opening a part/service/assembly
182   my $object = SL::DB::Manager::Part->get_first;
183   my $html   = SL::Presenter->get->part($object, display => 'inline');
184
185 see also L<SL::Presenter>
186
187 =head1 DESCRIPTION
188
189 see L<SL::Presenter>
190
191 =head1 FUNCTIONS
192
193 =over 2
194
195 =item C<part, $object, %params>
196
197 Returns a rendered version (actually an instance of
198 L<SL::Presenter::EscapedText>) of the part object C<$object>
199
200 Remaining C<%params> are passed to the function
201 C<SL::Presenter::Tag::link_tag>. It can include:
202
203 =over 4
204
205 =item * display
206
207 Either C<inline> (the default) or C<table-cell>. Is passed to the function
208 C<SL::Presenter::Tag::link_tag>.
209
210 =item * no_link
211
212 If falsish (the default) then the part number will be linked to the "edit"
213 dialog.
214
215 =back
216
217 =back
218
219 =over 2
220
221 =item C<classification_abbreviation $classification_id>
222
223 Returns the shortcut of the classification
224
225 =back
226
227 =over 2
228
229 =item C<separate_abbreviation $classification_id>
230
231 Returns the shortcut of the classification if the classification has the separate flag set.
232
233 =back
234
235 =over 2
236
237 =item C<select_classification $name,%params>
238
239 Returns an HTML select tag with all available classifications.
240
241 C<%params> can include:
242
243 =over 4
244
245 =item * default
246
247 The id of the selected item.
248
249 =back
250
251 =back
252
253 =over 2
254
255 =item C<vendor_price_dropdown $name, $part, %params>
256
257 Returns an HTML select tag with all available vendors with the best price
258 for the given part. The value of the select tag is the vendor id.
259
260 C<%params> can include:
261
262 =over 4
263
264 =item * default
265
266 The id of the selected vendor.
267
268 =back
269
270 =back
271
272 =over 2
273
274 =item C<part_picker $name, $value, %params>
275
276 All-in-one picker widget for parts. The name will be both id and name
277 of the resulting hidden C<id> input field (but the ID can be
278 overwritten with C<$params{id}>).
279
280 An additional dummy input will be generated which is used to find
281 parts. For a detailed description of its behaviour, see section
282 C<PART PICKER SPECIFICATION>.
283
284 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
285
286 If C<%params> contains C<part_type> only parts of this type will be used
287 for autocompletion. You may comma separate multiple types as in
288 C<part,assembly>.
289
290 If C<%params> contains C<status> only parts of this status will be used
291 for autocompletion. C<status> can be one of the following strings:
292 C<active>, C<obsolete> or C<all>. C<active> is the default if C<status> is
293 not given.
294
295 If C<%params> contains C<unit> only parts with this unit will be used
296 for autocompletion. You may comma separate multiple units as in
297 C<h,min>.
298
299 If C<%params> contains C<convertible_unit> only parts with a unit
300 that's convertible to unit will be used for autocompletion.
301
302 If C<%params> contains C<with_makemodel> or C<with_customer_partnumber> even
303 parts will be used for autocompletion which partnumber is a vendor partnumber
304 (makemodel) or a customer partnumber.
305
306 If C<%params> contains C<multiple> an alternative popup will be opened,
307 allowing multiple items to be selected. Note however that this requires
308 an additional callback C<set_multi_items> to work.
309 Also note that you can set C<multiple> to 0 (or not set C<multiple>) on
310 creation of the picker, but can open the alternative multi select popup
311 with js like this:
312 C<$("#pp_id").data("part_picker").o.multiple=1; $("#pp_id").data("part_picker").open_dialog()'>
313 where C<pp_id> is the dom id of the part picker.
314 Or you can even do it the other way round setting C<multiple> to 1 on creation
315 and open a single selection popup with js.
316
317 If C<%params> contains C<multiple_pos_input> an input field with the dom id
318 C<multi_items_position> will be rendered in the alternative popup.
319 This can be used in the callback for C<set_multi_items> to controll the
320 input postion for the items.
321
322 If C<%params> contains C<multiple_limit> the alternative popup will not
323 show any results if there are more than C<multiple_limit> results. A warning
324 message is displayed in this case. Set C<multiple_limit> to 0 to disable
325 the limitation. The limit defaults to 100.
326
327 Obsolete parts will by default not be displayed for selection. However they are
328 accepted as default values and can persist during updates. As with other
329 selectors though, they are not selectable once overridden.
330
331 C<part_picker> will register it's javascript for inclusion in the next header
332 rendering. If you write a standard controller that only calls C<render> once, it
333 will just work. In case the header is generated in a different render call
334 (multiple blocks, ajax, old C<bin/mozilla> style controllers) you need to
335 include C<kivi.Part.js> yourself.
336
337 On pressing <enter> the picker will try to commit the current selection,
338 resulting in one of the following events, whose corresponding callbacks can be
339 set in C<params.actions>:
340
341 =over 4
342
343 =item * C<commit_one>
344
345 If exactly one element matches the input, the internal id will be set to this
346 id, the internal state will be set to C<PICKED> and the C<change> event on the
347 picker will be fired. Additionally, if C<params> contains C<fat_set_item> a
348 special event C<set_item:PartPicker> will be fired which is guaranteed to
349 contain a complete JSON representation of the part.
350
351 After that the action C<commit_one> will be executed, which defaults to
352 clicking a button with id C<update_button> for backward compatibility reasons.
353
354 =item * C<commit_many>
355
356 If more than one element matches the input, the internal state will be set to
357 undefined.
358
359 After that the action C<commit_one> will be executed, which defaults to
360 opening a popup dialog for graphical interaction.
361
362 =item * C<commit_none>
363
364 If no element matches the input, the internal state will be set to undefined.
365
366 If an action for C<commit_none> exists, it will be called with the picker
367 object and current term. The caller can then implement creation of new parts.
368
369 =back
370
371 =back
372
373 =head1 PART PICKER SPECIFICATION
374
375 The following list of design goals were applied:
376
377 =over 4
378
379 =item *
380
381 Parts should not be perceived by the user as distinct inputs of partnumber and
382 description but as a single object
383
384 =item *
385
386 Easy to use without documentation for novice users
387
388 =item *
389
390 Fast to use with keyboard for experienced users
391
392 =item *
393
394 Possible to use without any keyboard interaction for mouse (or touchscreen)
395 users
396
397 =item *
398
399 Must not leave the current page in event of ambiguity (cf. current select_item
400 mechanism)
401
402 =item *
403
404 Should be useable with hand scanners or similar alternative keyboard devices
405
406 =item *
407
408 Should not require a feedback/check loop in the common case
409
410 =item *
411
412 Should not be constrained to exact matches
413
414 =item *
415
416 Must be atomic
417
418 =item *
419
420 Action should be overridable
421
422 =back
423
424 The implementation consists of the following parts which will be referenced later:
425
426 =over 4
427
428 =item 1
429
430 A hidden input (id input), used to hold the id of the selected part. The only
431 input that gets submitted
432
433 =item 2
434
435 An input (dummy input) containing a description of the currently selected part,
436 also used by the user to search for parts
437
438 =item 3
439
440 A jquery.autocomplete mechanism attached to the dummy field
441
442 =item 4
443
444 A popup layer for both feedback and input of additional data in case of
445 ambiguity.
446
447 =item 5
448
449 An internal status of the part picker, indicating whether id input and dummy
450 input are consistent. After leaving the dummy input the part picker must
451 place itself in a consistent status.
452
453 =item 6
454
455 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
456
457 =back
458
459 =head1 BUGS
460
461 None atm :)
462
463 =head1 AUTHOR
464
465 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
466
467 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
468
469 =cut