1 package SL::Controller::CsvImport::Part;
8 use SL::DB::Buchungsgruppe;
9 use SL::DB::CustomVariable;
10 use SL::DB::CustomVariableConfig;
11 use SL::DB::PartsGroup;
12 use SL::DB::PaymentTerm;
13 use SL::DB::PriceFactor;
14 use SL::DB::Pricegroup;
16 use SL::DB::Translation;
19 use List::MoreUtils qw(none);
21 use parent qw(SL::Controller::CsvImport::Base);
23 use Rose::Object::MakeMethods::Generic
25 scalar => [ qw(table makemodel_columns) ],
26 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by partsgroups_by
27 translation_columns all_pricegroups) ],
30 sub set_profile_defaults {
33 my $bugru = SL::DB::Manager::Buchungsgruppe->find_by(description => { like => 'Standard%19%' });
35 $self->controller->profile->_set_defaults(
36 sellprice_places => 2,
37 sellprice_adjustment => 0,
38 sellprice_adjustment_type => 'percent',
39 article_number_policy => 'update_prices',
40 shoparticle_if_missing => '0',
42 default_buchungsgruppe => ($bugru ? $bugru->id : undef),
43 apply_buchungsgruppe => 'all',
50 $self->class('SL::DB::Part');
56 my $all_bg = SL::DB::Manager::Buchungsgruppe->get_all;
57 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) };
60 sub init_price_factors_by {
63 my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
64 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
67 sub init_partsgroups_by {
70 my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
71 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
77 my $all_units = SL::DB::Manager::Unit->get_all;
78 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
84 # my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
85 # partnumber => { part => { },
88 # foreach my $part (@{ $self->existing_objects }) {
89 # next if $part->assembly;
90 # $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
94 my $sth = prepare_execute_query($::form, SL::DB::Object->new->db->dbh, 'SELECT partnumber FROM parts');
95 while (my ($partnumber) = $sth->fetchrow_array()) {
96 $parts_by->{partnumber}{$partnumber} = 1;
102 sub init_all_pricegroups {
105 return SL::DB::Manager::Pricegroup->get_all(sort => 'id');
111 return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
112 sellprice_places sellprice_adjustment sellprice_adjustment_type
113 shoparticle_if_missing parts_type default_unit) };
116 sub init_all_cvar_configs {
119 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'IC' ]);
122 sub init_translation_columns {
125 return [ map { ("description_" . $_->article_code, "notes_" . $_->article_code) } (@{ $self->all_languages }) ];
131 return unless @{ $self->controller->data };
133 $self->controller->track_progress(phase => 'building data', progress => 0);
135 $self->makemodel_columns({});
138 my $num_data = scalar @{ $self->controller->data };
139 foreach my $entry (@{ $self->controller->data }) {
140 $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
142 $self->check_buchungsgruppe($entry);
143 $self->check_type($entry);
144 $self->check_unit($entry);
145 $self->check_price_factor($entry);
146 $self->check_payment($entry);
147 $self->check_partsgroup($entry);
148 $self->handle_pricegroups($entry);
149 $self->check_existing($entry) unless @{ $entry->{errors} };
150 $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
151 $self->handle_shoparticle($entry);
152 $self->handle_translations($entry);
153 $self->handle_cvars($entry);
154 $self->handle_makemodel($entry);
155 $self->set_various_fields($entry);
160 $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
161 $self->add_columns(qw(buchungsgruppen_id unit));
162 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment partsgroup));
163 $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
164 $self->add_cvar_raw_data_columns;
165 map { $self->add_raw_data_columns("pricegroup_${_}") if exists $self->controller->data->[0]->{raw_data}->{"pricegroup_$_"} } (1..scalar(@{ $self->all_pricegroups }));
166 map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->translation_columns };
167 map { $self->add_raw_data_columns("make_${_}", "model_${_}", "lastcost_${_}") } sort { $a <=> $b } keys %{ $self->makemodel_columns };
170 sub get_duplicate_check_fields {
173 label => $::locale->text('Part Number'),
178 label => $::locale->text('Description'),
181 my $desc = shift->description;
182 $desc =~ s/[\s,\.\-]//g;
189 sub check_buchungsgruppe {
190 my ($self, $entry) = @_;
192 my $object = $entry->{object};
194 # Check Buchungsgruppe
196 # Store and verify default ID.
197 my $default_id = $self->settings->{default_buchungsgruppe};
198 $default_id = undef unless $self->bg_by->{id}->{ $default_id };
200 # 1. Use default ID if enforced.
201 $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
203 # 2. Use supplied ID if valid
204 $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
206 # 3. Look up name if supplied.
207 if (!$object->buchungsgruppen_id) {
208 my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
209 $object->buchungsgruppen_id($bg->id) if $bg;
212 # 4. Use default ID if not valid.
213 $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
215 return 1 if $object->buchungsgruppen_id;
217 push @{ $entry->{errors} }, $::locale->text('Error: booking group missing or invalid');
222 my ($self, $entry) = @_;
224 my $object = $entry->{object};
226 if ($object->partnumber && $self->parts_by->{partnumber}{$object->partnumber}) {
227 $entry->{part} = SL::DB::Manager::Part->find_by(partnumber => $object->partnumber);
230 if ($entry->{part}) {
231 if ($self->settings->{article_number_policy} eq 'update_prices') {
232 if ($self->settings->{parts_type} eq 'mixed' && $entry->{part}->type ne $object->type) {
233 push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database with different type'));
235 map { $entry->{part}->$_( $object->$_ ) if defined $object->$_ } qw(sellprice listprice lastcost);
238 my %prices_by_pricegroup_id = map { $_->pricegroup->id => $_ } $entry->{part}->prices, $object->prices;
239 $entry->{part}->prices(grep { $_ } map { $prices_by_pricegroup_id{$_->id} } @{ $self->all_pricegroups });
241 push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database');
242 $entry->{object_to_save} = $entry->{part};
244 } elsif ( $self->settings->{article_number_policy} eq 'skip' ) {
245 push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database'));
248 $object->partnumber('####');
249 push(@{$entry->{errors}}, $::locale->text('Skipping, for assemblies are not importable (yet)')) if $object->type eq 'assembly';
252 push(@{$entry->{errors}}, $::locale->text('Skipping, for assemblies are not importable (yet)')) if $object->type eq 'assembly';
257 my ($self, $entry) = @_;
259 foreach my $column (qw(sellprice)) {
260 my $object = $entry->{object_to_save} || $entry->{object};
261 my $adjustment = $self->settings->{sellprice_adjustment};
262 my $value = $object->$column;
264 $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
265 $object->$column($::form->round_amount($value, $self->settings->{sellprice_places}));
269 sub handle_shoparticle {
270 my ($self, $entry) = @_;
272 $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
276 my ($self, $entry) = @_;
278 my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
279 $bg ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1); # does this case ever occur?
281 my $type = $self->settings->{parts_type};
282 if ($type eq 'mixed') {
283 $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
284 : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
285 : $entry->{raw_data}->{type} =~ m/^a/i ? 'assembly'
289 $entry->{object}->assembly($type eq 'assembly');
291 # when saving income_accno_id or expense_accno_id use ids from the selected
292 # $bg according to the default tax_zone (the one with the highest sort
293 # order). Alternatively one could use the ids from defaults, but they might
296 $entry->{object}->income_accno_id( $bg->income_accno_id( SL::DB::Manager::TaxZone->get_default->id ) );
298 if ($type eq 'part' || $type eq 'service') {
299 $entry->{object}->expense_accno_id( $bg->expense_accno_id( SL::DB::Manager::TaxZone->get_default->id ) );
302 if ($type eq 'part') {
303 $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
306 if (none { $_ eq $type } qw(part service assembly)) {
307 push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
314 sub check_price_factor {
315 my ($self, $entry) = @_;
317 my $object = $entry->{object};
319 # Check whether or not price factor ID is valid.
320 if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
321 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
325 # Map name to ID if given.
326 if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
327 my $pf = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
330 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
334 $object->price_factor_id($pf->id);
340 sub check_partsgroup {
341 my ($self, $entry) = @_;
343 my $object = $entry->{object};
345 # Check whether or not part group ID is valid.
346 if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
347 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
351 # Map name to ID if given.
352 if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
353 my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
356 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
360 $object->partsgroup_id($pg->id);
367 my ($self, $entry) = @_;
369 my $object = $entry->{object};
371 $object->unit($self->settings->{default_unit}) unless $object->unit;
373 # Check whether or unit is valid.
374 if (!$self->units_by->{name}->{ $object->unit }) {
375 push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
382 sub handle_translations {
383 my ($self, $entry) = @_;
386 foreach my $language (@{ $self->all_languages }) {
387 my ($desc, $notes) = @{ $entry->{raw_data} }{ "description_" . $language->article_code, "notes_" . $language->article_code };
388 next unless $desc || $notes;
390 push @translations, SL::DB::Translation->new(language_id => $language->id,
391 translation => $desc,
392 longdescription => $notes);
395 $entry->{object}->translations(\@translations);
398 sub handle_pricegroups {
399 my ($self, $entry) = @_;
403 foreach my $pricegroup (@{ $self->all_pricegroups }) {
405 my $sellprice = $entry->{raw_data}->{"pricegroup_${idx}"};
406 next if $sellprice eq '';
408 push @prices, SL::DB::Price->new(pricegroup_id => $pricegroup->id,
409 price => $::form->parse_amount(\%::myconfig, $sellprice));
412 $entry->{object}->prices(\@prices);
415 sub handle_makemodel {
416 my ($self, $entry) = @_;
417 my $object = $entry->{object};
421 foreach my $idx (sort map { substr $_, 5 } grep { m/^make_\d+$/ && $entry->{raw_data}->{$_} } keys %{ $entry->{raw_data} }) {
422 my $vendor = $entry->{raw_data}->{"make_${idx}"};
423 $vendor = $self->vc_by->{id}-> { $vendor }
424 || $self->vc_by->{number}->{vendors}->{ $vendor }
425 || $self->vc_by->{name}-> {vendors}->{ $vendor };
427 if (ref($vendor) ne 'SL::DB::Vendor') {
428 push @{ $entry->{errors} }, $::locale->text('Error: Invalid vendor in column make_#1', $idx);
432 push @makemodels, SL::DB::MakeModel->new(make => $vendor->id,
433 model => $entry->{raw_data}->{"model_${idx}"},
434 lastcost_as_number => $entry->{raw_data}->{"lastcost_${idx}"});
436 $self->makemodel_columns->{$idx} = 1;
437 $entry->{raw_data}->{"make_${idx}"} = $vendor->name;
441 $object->makemodels(\@makemodels);
442 $object->makemodel(scalar(@makemodels) ? 1 : 0);
444 if ( !$entry->{part} || $self->settings->{article_number_policy} ne 'update_prices' ) {
448 my %old_makemodels_by_mm = map { $_->make . $; . $_->model => $_ } $entry->{part}->makemodels;
451 foreach my $makemodel ($object->makemodels()) {
452 my $makemodel_orig = $old_makemodels_by_mm{$makemodel->make,$makemodel->model};
455 if ($makemodel_orig) {
456 $makemodel_orig->model($makemodel->model);
457 $makemodel_orig->lastcost($makemodel->lastcost);
460 push @new_makemodels, $makemodel;
464 $entry->{part}->makemodels([ $entry->{part}->makemodels, @new_makemodels ]) if @new_makemodels;
468 $_->sortorder(++$i) for @{ $entry->{part}->makemodels };
470 $self->save_with_cascade(1) if $found_any;
473 sub set_various_fields {
474 my ($self, $entry) = @_;
476 $entry->{object}->priceupdate(DateTime->now_local);
482 my $profile = $self->SUPER::init_profile;
483 delete @{$profile}{qw(assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)};
485 $profile->{"pricegroup_$_"} = '' for 1 .. scalar @{ $_[0]->all_pricegroups };
491 my ($self, %params) = @_;
493 my $with_number = [ grep { $_->{object}->partnumber ne '####' } @{ $self->controller->data } ];
494 my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ];
496 map { $_->{object}->partnumber('') } @{ $without_number };
498 $self->SUPER::save_objects(data => $with_number);
499 $self->SUPER::save_objects(data => $without_number);
502 sub setup_displayable_columns {
505 $self->SUPER::setup_displayable_columns;
506 $self->add_cvar_columns_to_displayable_columns;
508 $self->add_displayable_columns({ name => 'bin', description => $::locale->text('Bin') },
509 { name => 'buchungsgruppen_id', description => $::locale->text('Booking group (database ID)') },
510 { name => 'buchungsgruppe', description => $::locale->text('Booking group (name)') },
511 { name => 'description', description => $::locale->text('Description') },
512 { name => 'drawing', description => $::locale->text('Drawing') },
513 { name => 'ean', description => $::locale->text('EAN') },
514 { name => 'formel', description => $::locale->text('Formula') },
515 { name => 'gv', description => $::locale->text('Business Volume') },
516 { name => 'has_sernumber', description => $::locale->text('Has serial number') },
517 { name => 'image', description => $::locale->text('Image') },
518 { name => 'lastcost', description => $::locale->text('Last Cost') },
519 { name => 'listprice', description => $::locale->text('List Price') },
520 { name => 'make_X', description => $::locale->text('Make (vendor\'s database ID, number or name; with X being a number)') . ' [1]' },
521 { name => 'microfiche', description => $::locale->text('Microfiche') },
522 { name => 'model_X', description => $::locale->text('Model (with X being a number)') . ' [1]' },
523 { name => 'lastcost_X', description => $::locale->text('Lastcost (with X being a number)') . ' [1]' },
524 { name => 'not_discountable', description => $::locale->text('Not Discountable') },
525 { name => 'notes', description => $::locale->text('Notes') },
526 { name => 'obsolete', description => $::locale->text('Obsolete') },
527 { name => 'onhand', description => $::locale->text('On Hand') . ' [2]' },
528 { name => 'partnumber', description => $::locale->text('Part Number') },
529 { name => 'partsgroup_id', description => $::locale->text('Partsgroup (database ID)') },
530 { name => 'partsgroup', description => $::locale->text('Partsgroup (name)') },
531 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
532 { name => 'payment', description => $::locale->text('Payment terms (name)') },
533 { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') },
534 { name => 'price_factor', description => $::locale->text('Price factor (name)') },
535 { name => 'rop', description => $::locale->text('ROP') },
536 { name => 'sellprice', description => $::locale->text('Sellprice') },
537 { name => 'shop', description => $::locale->text('Shop article') },
538 { name => 'type', description => $::locale->text('Article type') . ' [3]' },
539 { name => 'unit', description => $::locale->text('Unit (if missing or empty default unit will be used)') },
540 { name => 've', description => $::locale->text('Verrechnungseinheit') },
541 { name => 'weight', description => $::locale->text('Weight') },
544 foreach my $language (@{ $self->all_languages }) {
545 $self->add_displayable_columns({ name => 'description_' . $language->article_code,
546 description => $::locale->text('Description (translation for #1)', $language->description) },
547 { name => 'notes_' . $language->article_code,
548 description => $::locale->text('Notes (translation for #1)', $language->description) });
552 foreach my $pricegroup (@{ $self->all_pricegroups }) {
554 $self->add_displayable_columns({ name => 'pricegroup_' . $idx,
555 description => $::locale->text("Sellprice for price group '#1'", $pricegroup->pricegroup) });