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