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_${_}") } sort { $a <=> $b } keys %{ $self->makemodel_columns };
135 sub check_duplicates {
136 my ($self, %params) = @_;
138 my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
139 my $name_maker = sub { return $normalizer->($_[0]->description) };
142 if ('check_db' eq $self->controller->profile->get('duplicates')) {
143 %by_name = map { ( $name_maker->($_) => 'db' ) } @{ $self->existing_objects };
146 foreach my $entry (@{ $self->controller->data }) {
147 next if @{ $entry->{errors} };
149 my $name = $name_maker->($entry->{object});
151 if (!$by_name{ $name }) {
152 $by_name{ $name } = 'csv';
155 push @{ $entry->{errors} }, $by_name{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
160 sub check_buchungsgruppe {
161 my ($self, $entry) = @_;
163 my $object = $entry->{object};
165 # Check Buchungsgruppe
167 # Store and verify default ID.
168 my $default_id = $self->settings->{default_buchungsgruppe};
169 $default_id = undef unless $self->bg_by->{id}->{ $default_id };
171 # 1. Use default ID if enforced.
172 $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
174 # 2. Use supplied ID if valid
175 $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
177 # 3. Look up name if supplied.
178 if (!$object->buchungsgruppen_id) {
179 my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
180 $object->buchungsgruppen_id($bg->id) if $bg;
183 # 4. Use default ID if not valid.
184 $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
186 return 1 if $object->buchungsgruppen_id;
188 push @{ $entry->{errors} }, $::locale->text('Error: Buchungsgruppe missing or invalid');
193 my ($self, $entry) = @_;
195 my $object = $entry->{object};
197 $entry->{part} = $self->parts_by->{partnumber}->{ $object->type }->{ $object->partnumber };
199 if ($self->settings->{article_number_policy} eq 'update_prices') {
200 if ($entry->{part}) {
201 map { $entry->{part}->$_( $object->$_ ) if defined $object->$_ } qw(sellprice listprice lastcost);
204 my %prices_by_pricegroup_id = map { $_->pricegroup->id => $_ } $entry->{part}->prices, $object->prices;
205 $entry->{part}->prices(grep { $_ } map { $prices_by_pricegroup_id{$_->id} } @{ $self->all_pricegroups });
207 push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database');
208 $entry->{object_to_save} = $entry->{part};
212 $object->partnumber('####') if $entry->{part};
217 my ($self, $entry) = @_;
219 foreach my $column (qw(sellprice listprice lastcost)) {
220 next unless $self->controller->headers->{used}->{ $column };
222 my $adjustment = $self->settings->{sellprice_adjustment};
223 my $value = $entry->{object}->$column;
225 $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
226 $entry->{object}->$column($::form->round_amount($value, $self->settings->{sellprice_places}));
230 sub handle_shoparticle {
231 my ($self, $entry) = @_;
233 $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
237 my ($self, $entry) = @_;
239 my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
240 $bg ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1, income_accno_id_0 => 1, expense_accno_id_0 => 1);
242 my $type = $self->settings->{parts_type};
243 if ($type eq 'mixed') {
244 $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
245 : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
249 $entry->{object}->income_accno_id( $bg->income_accno_id_0 );
250 $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
252 if ($type eq 'part') {
253 $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
255 } elsif ($type ne 'service') {
256 push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
263 sub check_price_factor {
264 my ($self, $entry) = @_;
266 my $object = $entry->{object};
268 # Check whether or not price factor ID is valid.
269 if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
270 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
274 # Map name to ID if given.
275 if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
276 my $pf = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
279 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
283 $object->price_factor_id($pf->id);
289 sub check_partsgroup {
290 my ($self, $entry) = @_;
292 my $object = $entry->{object};
294 # Check whether or not part group ID is valid.
295 if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
296 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
300 # Map name to ID if given.
301 if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
302 my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
305 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
309 $object->partsgroup_id($pg->id);
316 my ($self, $entry) = @_;
318 my $object = $entry->{object};
320 $object->unit($self->settings->{default_unit}) unless $object->unit;
322 # Check whether or unit is valid.
323 if (!$self->units_by->{name}->{ $object->unit }) {
324 push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
331 sub handle_translations {
332 my ($self, $entry) = @_;
335 foreach my $language (@{ $self->all_languages }) {
336 my ($desc, $notes) = @{ $entry->{raw_data} }{ "description_" . $language->article_code, "notes_" . $language->article_code };
337 next unless $desc || $notes;
339 push @translations, SL::DB::Translation->new(language_id => $language->id,
340 translation => $desc,
341 longdescription => $notes);
344 $entry->{object}->translations(\@translations);
347 sub handle_pricegroups {
348 my ($self, $entry) = @_;
352 foreach my $pricegroup (@{ $self->all_pricegroups }) {
354 my $sellprice = $entry->{raw_data}->{"pricegroup_${idx}"};
355 next if $sellprice eq '';
357 push @prices, SL::DB::Price->new(pricegroup_id => $pricegroup->id,
358 price => $::form->parse_amount(\%::myconfig, $sellprice));
361 $entry->{object}->prices(\@prices);
364 sub handle_makemodel {
365 my ($self, $entry) = @_;
368 foreach my $idx (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);
378 push @makemodels, SL::DB::MakeModel->new(make => $vendor->id,
379 model => $entry->{raw_data}->{"model_${idx}"});
380 $self->makemodel_columns->{$idx} = 1;
381 $entry->{raw_data}->{"make_${idx}"} = $vendor->name;
385 $entry->{object}->makemodels(\@makemodels);
386 $entry->{object}->makemodel(scalar(@makemodels) ? 1 : 0);
389 sub set_various_fields {
390 my ($self, $entry) = @_;
392 $entry->{object}->priceupdate(DateTime->now_local);
398 my $profile = $self->SUPER::init_profile;
399 delete @{$profile}{qw(alternate assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)};
405 my ($self, %params) = @_;
407 my $with_number = [ grep { $_->{object}->partnumber ne '####' } @{ $self->controller->data } ];
408 my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ];
410 map { $_->{object}->partnumber('') } @{ $without_number };
412 $self->SUPER::save_objects(data => $with_number);
413 $self->SUPER::save_objects(data => $without_number);
416 sub setup_displayable_columns {
419 $self->SUPER::setup_displayable_columns;
420 $self->add_cvar_columns_to_displayable_columns;
422 $self->add_displayable_columns({ name => 'bin', description => $::locale->text('Bin') },
423 { name => 'buchungsgruppen_id', description => $::locale->text('Buchungsgruppe (database ID)') },
424 { name => 'buchungsgruppe', description => $::locale->text('Buchungsgruppe (name)') },
425 { name => 'description', description => $::locale->text('Description') },
426 { name => 'drawing', description => $::locale->text('Drawing') },
427 { name => 'ean', description => $::locale->text('EAN') },
428 { name => 'formel', description => $::locale->text('Formula') },
429 { name => 'gv', description => $::locale->text('Business Volume') },
430 { name => 'has_sernumber', description => $::locale->text('Has serial number') },
431 { name => 'image', description => $::locale->text('Image') },
432 { name => 'lastcost', description => $::locale->text('Last Cost') },
433 { name => 'listprice', description => $::locale->text('List Price') },
434 { name => 'make_X', description => $::locale->text('Make (with X being a number)') },
435 { name => 'microfiche', description => $::locale->text('Microfiche') },
436 { name => 'model_X', description => $::locale->text('Model (with X being a number)') },
437 { name => 'not_discountable', description => $::locale->text('Not Discountable') },
438 { name => 'notes', description => $::locale->text('Notes') },
439 { name => 'obsolete', description => $::locale->text('Obsolete') },
440 { name => 'onhand', description => $::locale->text('On Hand') },
441 { name => 'partnumber', description => $::locale->text('Part Number') },
442 { name => 'partsgroup_id', description => $::locale->text('Partsgroup (database ID)') },
443 { name => 'partsgroup', description => $::locale->text('Partsgroup (name)') },
444 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
445 { name => 'payment', description => $::locale->text('Payment terms (name)') },
446 { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') },
447 { name => 'price_factor', description => $::locale->text('Price factor (name)') },
448 { name => 'rop', description => $::locale->text('ROP') },
449 { name => 'sellprice', description => $::locale->text('Sellprice') },
450 { name => 'shop', description => $::locale->text('Shopartikel') },
451 { name => 'type', description => $::locale->text('Article type (see below)') },
452 { name => 'unit', description => $::locale->text('Unit (if missing or empty default unit will be used)') },
453 { name => 've', description => $::locale->text('Verrechnungseinheit') },
454 { name => 'weight', description => $::locale->text('Weight') },
457 foreach my $language (@{ $self->all_languages }) {
458 $self->add_displayable_columns({ name => 'description_' . $language->article_code,
459 description => $::locale->text('Description (translation for #1)', $language->description) },
460 { name => 'notes_' . $language->article_code,
461 description => $::locale->text('Notes (translation for #1)', $language->description) });
465 foreach my $pricegroup (@{ $self->all_pricegroups }) {
467 $self->add_displayable_columns({ name => 'pricegroup_' . $idx,
468 description => $::locale->text("Sellprice for price group '#1'", $pricegroup->pricegroup) });