1 package SL::Controller::CsvImport::Part;
7 use SL::DB::Buchungsgruppe;
8 use SL::DB::CustomVariable;
9 use SL::DB::CustomVariableConfig;
10 use SL::DB::PartsGroup;
11 use SL::DB::PaymentTerm;
12 use SL::DB::PriceFactor;
13 use SL::DB::Pricegroup;
15 use SL::DB::Translation;
18 use parent qw(SL::Controller::CsvImport::Base);
20 use Rose::Object::MakeMethods::Generic
22 scalar => [ qw(table makemodel_columns) ],
23 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by partsgroups_by
24 translation_columns all_pricegroups) ],
29 $self->class('SL::DB::Part');
35 my $all_bg = SL::DB::Manager::Buchungsgruppe->get_all;
36 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) };
39 sub init_price_factors_by {
42 my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
43 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
46 sub init_partsgroups_by {
49 my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
50 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
56 my $all_units = SL::DB::Manager::Unit->get_all;
57 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
63 my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
64 partnumber => { part => { },
67 foreach my $part (@{ $self->existing_objects }) {
68 next if $part->assembly;
69 $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
75 sub init_all_pricegroups {
78 return SL::DB::Manager::Pricegroup->get_all(sort => 'id');
84 return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
85 sellprice_places sellprice_adjustment sellprice_adjustment_type
86 shoparticle_if_missing parts_type default_unit) };
89 sub init_all_cvar_configs {
92 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'IC' ]);
95 sub init_translation_columns {
98 return [ map { ("description_" . $_->article_code, "notes_" . $_->article_code) } (@{ $self->all_languages }) ];
104 return unless @{ $self->controller->data };
106 $self->makemodel_columns({});
108 foreach my $entry (@{ $self->controller->data }) {
109 $self->check_buchungsgruppe($entry);
110 $self->check_type($entry);
111 $self->check_unit($entry);
112 $self->check_price_factor($entry);
113 $self->check_payment($entry);
114 $self->check_partsgroup($entry);
115 $self->handle_pricegroups($entry);
116 $self->check_existing($entry) unless @{ $entry->{errors} };
117 $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
118 $self->handle_shoparticle($entry);
119 $self->handle_translations($entry);
120 $self->handle_cvars($entry);
121 $self->handle_makemodel($entry);
122 $self->set_various_fields($entry);
125 $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
126 $self->add_columns(qw(buchungsgruppen_id unit));
127 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment partsgroup));
128 $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
129 $self->add_cvar_raw_data_columns;
130 map { $self->add_raw_data_columns("pricegroup_${_}") } (1..scalar(@{ $self->all_pricegroups }));
131 map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->translation_columns };
132 map { $self->add_raw_data_columns("make_${_}", "model_${_}", "lastcost_${_}") } sort { $a <=> $b } keys %{ $self->makemodel_columns };
135 sub get_duplicate_check_fields {
138 label => $::locale->text('Part Number'),
143 label => $::locale->text('Description'),
146 my $desc = shift->description;
147 $desc =~ s/[\s,\.\-]//g;
154 sub check_buchungsgruppe {
155 my ($self, $entry) = @_;
157 my $object = $entry->{object};
159 # Check Buchungsgruppe
161 # Store and verify default ID.
162 my $default_id = $self->settings->{default_buchungsgruppe};
163 $default_id = undef unless $self->bg_by->{id}->{ $default_id };
165 # 1. Use default ID if enforced.
166 $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
168 # 2. Use supplied ID if valid
169 $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
171 # 3. Look up name if supplied.
172 if (!$object->buchungsgruppen_id) {
173 my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
174 $object->buchungsgruppen_id($bg->id) if $bg;
177 # 4. Use default ID if not valid.
178 $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
180 return 1 if $object->buchungsgruppen_id;
182 push @{ $entry->{errors} }, $::locale->text('Error: Buchungsgruppe missing or invalid');
187 my ($self, $entry) = @_;
189 my $object = $entry->{object};
191 $entry->{part} = SL::DB::Manager::Part->find_by(
192 SL::DB::Manager::Part->type_filter($object->type),
193 ( partnumber => $object->partnumber ) x!! $object->partnumber,
196 if ($self->settings->{article_number_policy} eq 'update_prices') {
197 if ($entry->{part}) {
198 map { $entry->{part}->$_( $object->$_ ) if defined $object->$_ } qw(sellprice listprice lastcost);
201 my %prices_by_pricegroup_id = map { $_->pricegroup->id => $_ } $entry->{part}->prices, $object->prices;
202 $entry->{part}->prices(grep { $_ } map { $prices_by_pricegroup_id{$_->id} } @{ $self->all_pricegroups });
204 push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database');
205 $entry->{object_to_save} = $entry->{part};
207 } elsif ( $self->settings->{article_number_policy} eq 'skip' ) {
208 push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database')) if ( $entry->{part} );
210 $object->partnumber('####') if $entry->{part};
215 my ($self, $entry) = @_;
217 foreach my $column (qw(sellprice listprice lastcost)) {
218 next unless $self->controller->headers->{used}->{ $column };
220 my $adjustment = $self->settings->{sellprice_adjustment};
221 my $value = $entry->{object}->$column;
223 $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
224 $entry->{object}->$column($::form->round_amount($value, $self->settings->{sellprice_places}));
228 sub handle_shoparticle {
229 my ($self, $entry) = @_;
231 $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
235 my ($self, $entry) = @_;
237 my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
238 $bg ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1, income_accno_id_0 => 1, expense_accno_id_0 => 1);
240 my $type = $self->settings->{parts_type};
241 if ($type eq 'mixed') {
242 $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
243 : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
247 $entry->{object}->income_accno_id( $bg->income_accno_id_0 );
248 $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
250 if ($type eq 'part') {
251 $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
253 } elsif ($type ne 'service') {
254 push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
261 sub check_price_factor {
262 my ($self, $entry) = @_;
264 my $object = $entry->{object};
266 # Check whether or not price factor ID is valid.
267 if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
268 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
272 # Map name to ID if given.
273 if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
274 my $pf = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
277 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
281 $object->price_factor_id($pf->id);
287 sub check_partsgroup {
288 my ($self, $entry) = @_;
290 my $object = $entry->{object};
292 # Check whether or not part group ID is valid.
293 if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
294 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
298 # Map name to ID if given.
299 if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
300 my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
303 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
307 $object->partsgroup_id($pg->id);
314 my ($self, $entry) = @_;
316 my $object = $entry->{object};
318 $object->unit($self->settings->{default_unit}) unless $object->unit;
320 # Check whether or unit is valid.
321 if (!$self->units_by->{name}->{ $object->unit }) {
322 push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
329 sub handle_translations {
330 my ($self, $entry) = @_;
333 foreach my $language (@{ $self->all_languages }) {
334 my ($desc, $notes) = @{ $entry->{raw_data} }{ "description_" . $language->article_code, "notes_" . $language->article_code };
335 next unless $desc || $notes;
337 push @translations, SL::DB::Translation->new(language_id => $language->id,
338 translation => $desc,
339 longdescription => $notes);
342 $entry->{object}->translations(\@translations);
345 sub handle_pricegroups {
346 my ($self, $entry) = @_;
350 foreach my $pricegroup (@{ $self->all_pricegroups }) {
352 my $sellprice = $entry->{raw_data}->{"pricegroup_${idx}"};
353 next if $sellprice eq '';
355 push @prices, SL::DB::Price->new(pricegroup_id => $pricegroup->id,
356 price => $::form->parse_amount(\%::myconfig, $sellprice));
359 $entry->{object}->prices(\@prices);
362 sub handle_makemodel {
363 my ($self, $entry) = @_;
364 my $object = $entry->{object};
368 foreach my $idx (sort map { substr $_, 5 } grep { m/^make_\d+$/ && $entry->{raw_data}->{$_} } keys %{ $entry->{raw_data} }) {
369 my $vendor = $entry->{raw_data}->{"make_${idx}"};
370 $vendor = $self->vc_by->{id}-> { $vendor }
371 || $self->vc_by->{number}->{vendors}->{ $vendor }
372 || $self->vc_by->{name}-> {vendors}->{ $vendor };
374 if (ref($vendor) ne 'SL::DB::Vendor') {
375 push @{ $entry->{errors} }, $::locale->text('Error: Invalid vendor in column make_#1', $idx);
379 push @makemodels, SL::DB::MakeModel->new(make => $vendor->id,
380 model => $entry->{raw_data}->{"model_${idx}"},
381 lastcost_as_number => $entry->{raw_data}->{"lastcost_${idx}"});
383 $self->makemodel_columns->{$idx} = 1;
384 $entry->{raw_data}->{"make_${idx}"} = $vendor->name;
388 $object->makemodels(\@makemodels);
389 $object->makemodel(scalar(@makemodels) ? 1 : 0);
391 if ( !$entry->{part} || $self->settings->{article_number_policy} ne 'update_prices' ) {
395 my %old_makemodels_by_make = map { $_->make => $_ } $entry->{part}->makemodels;
397 foreach my $makemodel ($object->makemodels()) {
398 my $makemodel_orig = $old_makemodels_by_make{$makemodel->make};
401 if ($makemodel_orig) {
402 $makemodel_orig->model($makemodel->model);
403 $makemodel_orig->lastcost($makemodel->lastcost);
406 $entry->{part}->add_makemodels($makemodel);
410 $entry->{part}->makemodel($object->makemodel);
412 $self->save_with_cascade(1) if $found_any;
415 sub set_various_fields {
416 my ($self, $entry) = @_;
418 $entry->{object}->priceupdate(DateTime->now_local);
424 my $profile = $self->SUPER::init_profile;
425 delete @{$profile}{qw(alternate assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)};
431 my ($self, %params) = @_;
433 my $with_number = [ grep { $_->{object}->partnumber ne '####' } @{ $self->controller->data } ];
434 my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ];
436 map { $_->{object}->partnumber('') } @{ $without_number };
438 $self->SUPER::save_objects(data => $with_number);
439 $self->SUPER::save_objects(data => $without_number);
442 sub setup_displayable_columns {
445 $self->SUPER::setup_displayable_columns;
446 $self->add_cvar_columns_to_displayable_columns;
448 $self->add_displayable_columns({ name => 'bin', description => $::locale->text('Bin') },
449 { name => 'buchungsgruppen_id', description => $::locale->text('Buchungsgruppe (database ID)') },
450 { name => 'buchungsgruppe', description => $::locale->text('Buchungsgruppe (name)') },
451 { name => 'description', description => $::locale->text('Description') },
452 { name => 'drawing', description => $::locale->text('Drawing') },
453 { name => 'ean', description => $::locale->text('EAN') },
454 { name => 'formel', description => $::locale->text('Formula') },
455 { name => 'gv', description => $::locale->text('Business Volume') },
456 { name => 'has_sernumber', description => $::locale->text('Has serial number') },
457 { name => 'image', description => $::locale->text('Image') },
458 { name => 'lastcost', description => $::locale->text('Last Cost') },
459 { name => 'listprice', description => $::locale->text('List Price') },
460 { name => 'make_X', description => $::locale->text('Make (vendor\'s database ID, number or name; with X being a number)') . ' [1]' },
461 { name => 'microfiche', description => $::locale->text('Microfiche') },
462 { name => 'model_X', description => $::locale->text('Model (with X being a number)') . ' [1]' },
463 { name => 'lastcost_X', description => $::locale->text('Lastcost (with X being a number)') . ' [1]' },
464 { name => 'not_discountable', description => $::locale->text('Not Discountable') },
465 { name => 'notes', description => $::locale->text('Notes') },
466 { name => 'obsolete', description => $::locale->text('Obsolete') },
467 { name => 'onhand', description => $::locale->text('On Hand') },
468 { name => 'partnumber', description => $::locale->text('Part Number') },
469 { name => 'partsgroup_id', description => $::locale->text('Partsgroup (database ID)') },
470 { name => 'partsgroup', description => $::locale->text('Partsgroup (name)') },
471 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
472 { name => 'payment', description => $::locale->text('Payment terms (name)') },
473 { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') },
474 { name => 'price_factor', description => $::locale->text('Price factor (name)') },
475 { name => 'rop', description => $::locale->text('ROP') },
476 { name => 'sellprice', description => $::locale->text('Sellprice') },
477 { name => 'shop', description => $::locale->text('Shopartikel') },
478 { name => 'type', description => $::locale->text('Article type (see below)') },
479 { name => 'unit', description => $::locale->text('Unit (if missing or empty default unit will be used)') },
480 { name => 've', description => $::locale->text('Verrechnungseinheit') },
481 { name => 'weight', description => $::locale->text('Weight') },
484 foreach my $language (@{ $self->all_languages }) {
485 $self->add_displayable_columns({ name => 'description_' . $language->article_code,
486 description => $::locale->text('Description (translation for #1)', $language->description) },
487 { name => 'notes_' . $language->article_code,
488 description => $::locale->text('Notes (translation for #1)', $language->description) });
492 foreach my $pricegroup (@{ $self->all_pricegroups }) {
494 $self->add_displayable_columns({ name => 'pricegroup_' . $idx,
495 description => $::locale->text("Sellprice for price group '#1'", $pricegroup->pricegroup) });