Artikel-Klassifizierung: Neue Option "Preis separat ausweisen"
[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::Manager::PartsClassification;
7
8 use Exporter qw(import);
9 our @EXPORT = qw(part_picker part select_classification classification_abbreviation type_abbreviation separate_abbreviation);
10
11 use Carp;
12
13 sub part {
14   my ($self, $part, %params) = @_;
15
16   $params{display} ||= 'inline';
17
18   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
19
20   my $text = join '', (
21     $params{no_link} ? '' : '<a href="controller.pl?action=Part/edit&part.id=' . $self->escape($part->id) . '">',
22     $self->escape($part->partnumber),
23     $params{no_link} ? '' : '</a>',
24   );
25   return $self->escaped_text($text);
26 }
27
28 sub part_picker {
29   my ($self, $name, $value, %params) = @_;
30
31   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
32   my $id = delete($params{id}) || $self->name_to_id($name);
33   my $fat_set_item = delete $params{fat_set_item};
34
35   my @classes = $params{class} ? ($params{class}) : ();
36   push @classes, 'part_autocomplete';
37   push @classes, 'partpicker_fat_set_item' if $fat_set_item;
38
39   my $ret =
40     $self->input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id) .
41     join('', map { $params{$_} ? $self->input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(part_type unit convertible_unit)) .
42     $self->input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
43
44   $::request->layout->add_javascripts('autocomplete_part.js');
45   $::request->presenter->need_reinit_widgets($id);
46
47   $self->html_tag('span', $ret, class => 'part_picker');
48 }
49
50 #
51 # Must be addapted to new type table
52 #
53 sub type_abbreviation {
54   my ($self, $part_type) = @_;
55   $main::lxdebug->message(LXDebug->DEBUG2(),"parttype=".$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 sub classification_abbreviation {
78   my ($self, $id) = @_;
79   $main::lxdebug->message(LXDebug->DEBUG2(),"classif=".$id);
80   return $::locale->text(SL::DB::Manager::PartsClassification->get_abbreviation($id));
81 }
82
83 sub separate_abbreviation {
84   my ($self, $id) = @_;
85   return $::locale->text(SL::DB::Manager::PartsClassification->get_separate_abbreviation($id));
86 }
87
88 sub select_classification {
89   my ($self, $name, %attributes) = @_;
90   $attributes{value_key} = 'id';
91   $attributes{title_key} = 'description';
92   my $collection = SL::DB::Manager::PartsClassification->get_all_sorted();
93   $_->description($::locale->text($_->description)) for @{ $collection };
94   return $self->select_tag( $name, $collection, %attributes );
95 }
96
97 1;
98
99 __END__
100
101 =encoding utf-8
102
103 =head1 NAME
104
105 SL::Presenter::Part - Part related presenter stuff
106
107 =head1 SYNOPSIS
108
109   # Create an html link for editing/opening a part/service/assembly
110   my $object = my $object = SL::DB::Manager::Part->get_first;
111   my $html   = SL::Presenter->get->part($object, display => 'inline');
112
113 see also L<SL::Presenter>
114
115 =head1 DESCRIPTION
116
117 see L<SL::Presenter>
118
119 =head1 FUNCTIONS
120
121 =over 2
122
123 =item C<part, $object, %params>
124
125 Returns a rendered version (actually an instance of
126 L<SL::Presenter::EscapedText>) of the part object C<$object>
127
128 C<%params> can include:
129
130 =over 4
131
132 =item * display
133
134 Either C<inline> (the default) or C<table-cell>. At the moment both
135 representations are identical and produce the part's name linked
136 to the corresponding 'edit' action.
137
138 =back
139
140 =back
141
142 =over 2
143
144 =item C<part_picker $name, $value, %params>
145
146 All-in-one picker widget for parts. The name will be both id and name
147 of the resulting hidden C<id> input field (but the ID can be
148 overwritten with C<$params{id}>).
149
150 An additional dummy input will be generated which is used to find
151 parts. For a detailed description of its behaviour, see section
152 C<PART PICKER SPECIFICATION>.
153
154 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
155
156 If C<%params> contains C<part_type> only parts of this type will be used
157 for autocompletion. You may comma separate multiple types as in
158 C<part,assembly>.
159
160 If C<%params> contains C<unit> only parts with this unit will be used
161 for autocompletion. You may comma separate multiple units as in
162 C<h,min>.
163
164 If C<%params> contains C<convertible_unit> only parts with a unit
165 that's convertible to unit will be used for autocompletion.
166
167 Obsolete parts will by default not be displayed for selection. However they are
168 accepted as default values and can persist during updates. As with other
169 selectors though, they are not selectable once overridden.
170
171 C<part_picker> will register it's javascript for inclusion in the next header
172 rendering. If you write a standard controller that only call C<render> once, it
173 will just work.  In case the header is generated in a different render call
174 (multiple blocks, ajax, old C<bin/mozilla> style controllers) you need to
175 include C<js/autocomplete_part.js> yourself.
176
177 =back
178
179 =head1 PART PICKER SPECIFICATION
180
181 The following list of design goals were applied:
182
183 =over 4
184
185 =item *
186
187 Parts should not be perceived by the user as distinct inputs of partnumber and
188 description but as a single object
189
190 =item *
191
192 Easy to use without documentation for novice users
193
194 =item *
195
196 Fast to use with keyboard for experienced users
197
198 =item *
199
200 Possible to use without any keyboard interaction for mouse (or touchscreen)
201 users
202
203 =item *
204
205 Must not leave the current page in event of ambiguity (cf. current select_item
206 mechanism)
207
208 =item *
209
210 Should be useable with hand scanners or similar alternative keyboard devices
211
212 =item *
213
214 Should not require a feedback/check loop in the common case
215
216 =item *
217
218 Should not be constrained to exact matches
219
220 =back
221
222 The implementation consists of the following parts which will be referenced later:
223
224 =over 4
225
226 =item 1
227
228 A hidden input (id input), used to hold the id of the selected part. The only
229 input that gets submitted
230
231 =item 2
232
233 An input (dummy input) containing a description of the currently selected part,
234 also used by the user to search for parts
235
236 =item 3
237
238 A jquery.autocomplete mechanism attached to the dummy field
239
240 =item 4
241
242 A popup layer for both feedback and input of additional data in case of
243 ambiguity.
244
245 =item 5
246
247 An internal status of the part picker, indicating whether id input and dummy
248 input are consistent. After leaving the dummy input the part picker must
249 place itself in a consistent status.
250
251 =item 6
252
253 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
254
255 =back
256
257 =head1 BUGS
258
259 None atm :)
260
261 =head1 AUTHOR
262
263 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
264
265 =cut