Anzeigen und Importieren von benutzerdefinierten Variablen bei Kunden, Lieferanten...
[kivitendo-erp.git] / SL / Controller / CsvImport / Part.pm
1 package SL::Controller::CsvImport::Part;
2
3 use strict;
4
5 use SL::Helper::Csv;
6
7 use SL::DB::Buchungsgruppe;
8 use SL::DB::CustomVariable;
9 use SL::DB::CustomVariableConfig;
10 use SL::DB::Language;
11 use SL::DB::PartsGroup;
12 use SL::DB::PaymentTerm;
13 use SL::DB::PriceFactor;
14 use SL::DB::Translation;
15 use SL::DB::Unit;
16
17 use parent qw(SL::Controller::CsvImport::Base);
18
19 use Rose::Object::MakeMethods::Generic
20 (
21  scalar                  => [ qw(table) ],
22  'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by payment_terms_by packing_types_by partsgroups_by
23                                  all_languages translation_columns) ],
24 );
25
26 sub init_class {
27   my ($self) = @_;
28   $self->class('SL::DB::Part');
29 }
30
31 sub init_bg_by {
32   my ($self) = @_;
33
34   my $all_bg = SL::DB::Manager::Buchungsgruppe->get_all;
35   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) };
36 }
37
38 sub init_price_factors_by {
39   my ($self) = @_;
40
41   my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
42   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
43 }
44
45 sub init_payment_terms_by {
46   my ($self) = @_;
47
48   my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
49   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
50 }
51
52 sub init_packing_types_by {
53   my ($self) = @_;
54
55   my $all_packing_types = SL::DB::Manager::PackingType->get_all;
56   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_packing_types } } ) } qw(id description) };
57 }
58
59 sub init_partsgroups_by {
60   my ($self) = @_;
61
62   my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
63   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
64 }
65
66 sub init_units_by {
67   my ($self) = @_;
68
69   my $all_units = SL::DB::Manager::Unit->get_all;
70   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
71 }
72
73 sub init_parts_by {
74   my ($self) = @_;
75
76   my $parts_by = { id         => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
77                    partnumber => { part    => { },
78                                    service => { } } };
79
80   foreach my $part (@{ $self->existing_objects }) {
81     next if $part->assembly;
82     $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
83   }
84
85   return $parts_by;
86 }
87
88 sub init_settings {
89   my ($self) = @_;
90
91   return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
92                                                                     sellprice_places sellprice_adjustment sellprice_adjustment_type
93                                                                     shoparticle_if_missing parts_type) };
94 }
95
96 sub init_all_languages {
97   my ($self) = @_;
98
99   return SL::DB::Manager::Language->get_all;
100 }
101
102 sub init_all_cvar_configs {
103   my ($self) = @_;
104
105   return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'IC' ]);
106 }
107
108 sub init_translation_columns {
109   my ($self) = @_;
110
111   return [ map { ("description_" . $_->article_code, "notes_" . $_->article_code) } (@{ $self->all_languages }) ];
112 }
113
114 sub check_objects {
115   my ($self) = @_;
116
117   return unless @{ $self->controller->data };
118
119   foreach my $entry (@{ $self->controller->data }) {
120     my $object   = $entry->{object};
121     my $raw_data = $entry->{raw_data};
122
123     next unless $self->check_buchungsgruppe($entry);
124     next unless $self->check_type($entry);
125     next unless $self->check_unit($entry);
126     next unless $self->check_price_factor($entry);
127     next unless $self->check_payment($entry);
128     next unless $self->check_packing_type($entry);
129     next unless $self->check_partsgroup($entry);
130     $self->check_existing($entry);
131     $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
132     $self->handle_shoparticle($entry);
133     $self->handle_translations($entry);
134     $self->handle_cvars($entry);
135     $self->set_various_fields($entry);
136   }
137
138   $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
139   $self->add_columns(qw(buchungsgruppen_id unit));
140   $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment packing_type partsgroup));
141   $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
142   $self->add_cvar_raw_data_columns;
143   map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->translation_columns };
144 }
145
146 sub check_duplicates {
147   my ($self, %params) = @_;
148
149   my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
150   my $name_maker = sub { return $normalizer->($_[0]->description) };
151
152   my %by_name;
153   if ('check_db' eq $self->controller->profile->get('duplicates')) {
154     %by_name = map { ( $name_maker->($_) => 'db' ) } @{ $self->existing_objects };
155   }
156
157   foreach my $entry (@{ $self->controller->data }) {
158     next if @{ $entry->{errors} };
159
160     my $name = $name_maker->($entry->{object});
161
162     if (!$by_name{ $name }) {
163       $by_name{ $name } = 'csv';
164
165     } else {
166       push @{ $entry->{errors} }, $by_name{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
167     }
168   }
169 }
170
171 sub check_buchungsgruppe {
172   my ($self, $entry) = @_;
173
174   my $object = $entry->{object};
175
176   # Check Buchungsgruppe
177
178   # Store and verify default ID.
179   my $default_id = $self->settings->{default_buchungsgruppe};
180   $default_id    = undef unless $self->bg_by->{id}->{ $default_id };
181
182   # 1. Use default ID if enforced.
183   $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
184
185   # 2. Use supplied ID if valid
186   $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
187
188   # 3. Look up name if supplied.
189   if (!$object->buchungsgruppen_id) {
190     my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
191     $object->buchungsgruppen_id($bg->id) if $bg;
192   }
193
194   # 4. Use default ID if not valid.
195   $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
196
197   return 1 if $object->buchungsgruppen_id;
198
199   push @{ $entry->{errors} }, $::locale->text('Error: Buchungsgruppe missing or invalid');
200   return 0;
201 }
202
203 sub check_existing {
204   my ($self, $entry) = @_;
205
206   my $object = $entry->{object};
207
208   my $entry->{part} = $self->parts_by->{partnumber}->{ $object->type }->{ $object->partnumber };
209
210   if ($self->settings->{article_number_policy} eq 'update_prices') {
211     if ($entry->{part}) {
212       map { $object->$_( $entry->{part}->$_ ) } qw(sellprice listprice lastcost);
213       $entry->{priceupdate} = 1;
214     }
215
216   } else {
217     $object->partnumber('####') if $entry->{part};
218   }
219 }
220
221 sub handle_prices {
222   my ($self, $entry) = @_;
223
224   foreach my $column (qw(sellprice listprice lastcost)) {
225     next unless $self->controller->headers->{used}->{ $column };
226
227     my $adjustment = $self->settings->{sellprice_adjustment};
228     my $value      = $entry->{object}->$column;
229
230     $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
231     $entry->{object}->$column($::form->round_amount($value, $self->settings->{sellprice_places}));
232   }
233 }
234
235 sub handle_shoparticle {
236   my ($self, $entry) = @_;
237
238   $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
239 }
240
241 sub check_type {
242   my ($self, $entry) = @_;
243
244   my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
245   die "Program logic error" if !$bg;
246
247   my $type = $self->settings->{parts_type};
248   if ($type eq 'mixed') {
249     $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
250           : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
251           :                                        undef;
252   }
253
254   $entry->{object}->income_accno_id(  $bg->income_accno_id_0 );
255   $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
256
257   if ($type eq 'part') {
258     $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
259
260   } elsif ($type ne 'service') {
261     push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
262     return 0;
263   }
264
265   return 1;
266 }
267
268 sub check_price_factor {
269   my ($self, $entry) = @_;
270
271   my $object = $entry->{object};
272
273   # Check whether or not price factor ID is valid.
274   if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
275     push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
276     return 0;
277   }
278
279   # Map name to ID if given.
280   if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
281     my $pf = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
282
283     if (!$pf) {
284       push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
285       return 0;
286     }
287
288     $object->price_factor_id($pf->id);
289   }
290
291   return 1;
292 }
293
294 sub check_payment {
295   my ($self, $entry) = @_;
296
297   my $object = $entry->{object};
298
299   # Check whether or not payment ID is valid.
300   if ($object->payment_id && !$self->payment_terms_by->{id}->{ $object->payment_id }) {
301     push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
302     return 0;
303   }
304
305   # Map name to ID if given.
306   if (!$object->payment_id && $entry->{raw_data}->{payment}) {
307     my $terms = $self->payment_terms_by->{description}->{ $entry->{raw_data}->{payment} };
308
309     if (!$terms) {
310       push @{ $entry->{errors} }, $::locale->text('Error: Invalid payment terms');
311       return 0;
312     }
313
314     $object->payment_id($terms->id);
315   }
316
317   return 1;
318 }
319
320 sub check_packing_type {
321   my ($self, $entry) = @_;
322
323   my $object = $entry->{object};
324
325   # Check whether or not packing type ID is valid.
326   if ($object->packing_type_id && !$self->packing_types_by->{id}->{ $object->packing_type_id }) {
327     push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
328     return 0;
329   }
330
331   # Map name to ID if given.
332   if (!$object->packing_type_id && $entry->{raw_data}->{packing_type}) {
333     my $type = $self->packing_types_by->{description}->{ $entry->{raw_data}->{packing_type} };
334
335     if (!$type) {
336       push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
337       return 0;
338     }
339
340     $object->packing_type_id($type->id);
341   }
342
343   return 1;
344 }
345
346 sub check_partsgroup {
347   my ($self, $entry) = @_;
348
349   my $object = $entry->{object};
350
351   # Check whether or not part group ID is valid.
352   if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
353     push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
354     return 0;
355   }
356
357   # Map name to ID if given.
358   if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
359     my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
360
361     if (!$pg) {
362       push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
363       return 0;
364     }
365
366     $object->partsgroup_id($pg->id);
367   }
368
369   return 1;
370 }
371
372 sub check_unit {
373   my ($self, $entry) = @_;
374
375   my $object = $entry->{object};
376
377   # Check whether or unit is valid.
378   if (!$self->units_by->{name}->{ $object->unit }) {
379     push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
380     return 0;
381   }
382
383   return 1;
384 }
385
386 sub handle_translations {
387   my ($self, $entry) = @_;
388
389   my @translations;
390   foreach my $language (@{ $self->all_languages }) {
391     my ($desc, $notes) = @{ $entry->{raw_data} }{ "description_" . $language->article_code, "notes_" . $language->article_code };
392     next unless $desc || $notes;
393
394     push @translations, SL::DB::Translation->new(language_id     => $language->id,
395                                                  translation     => $desc,
396                                                  longdescription => $notes);
397   }
398
399   $entry->{object}->translations(\@translations);
400 }
401
402 sub set_various_fields {
403   my ($self, $entry) = @_;
404
405   $entry->{object}->priceupdate(DateTime->now_local);
406 }
407
408 sub init_profile {
409   my ($self) = @_;
410
411   my $profile = $self->SUPER::init_profile;
412   delete @{$profile}{qw(type priceupdate)};
413
414   return $profile;
415 }
416
417 sub save_objects {
418   my ($self, %params) = @_;
419
420   my $with_number    = [ grep { $_->{object}->partnumber ne '####' } @{ $self->controller->data } ];
421   my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ];
422
423   map { $_->{object}->partnumber('') } @{ $without_number };
424
425   $self->SUPER::save_objects(data => $with_number);
426   $self->SUPER::save_objects(data => $without_number);
427 }
428
429 1;