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) ],
23 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by packing_types_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_packing_types_by {
49 my $all_packing_types = SL::DB::Manager::PackingType->get_all;
50 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_packing_types } } ) } qw(id description) };
53 sub init_partsgroups_by {
56 my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
57 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
63 my $all_units = SL::DB::Manager::Unit->get_all;
64 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
70 my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
71 partnumber => { part => { },
74 foreach my $part (@{ $self->existing_objects }) {
75 next if $part->assembly;
76 $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
82 sub init_all_pricegroups {
85 return SL::DB::Manager::Pricegroup->get_all(sort => 'id');
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) };
96 sub init_all_cvar_configs {
99 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'IC' ]);
102 sub init_translation_columns {
105 return [ map { ("description_" . $_->article_code, "notes_" . $_->article_code) } (@{ $self->all_languages }) ];
111 return unless @{ $self->controller->data };
113 foreach my $entry (@{ $self->controller->data }) {
114 $self->check_buchungsgruppe($entry);
115 $self->check_type($entry);
116 $self->check_unit($entry);
117 $self->check_price_factor($entry);
118 $self->check_payment($entry);
119 $self->check_packing_type($entry);
120 $self->check_partsgroup($entry);
121 $self->handle_pricegroups($entry);
122 $self->check_existing($entry) unless @{ $entry->{errors} };
123 $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
124 $self->handle_shoparticle($entry);
125 $self->handle_translations($entry);
126 $self->handle_cvars($entry);
127 $self->set_various_fields($entry);
130 $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
131 $self->add_columns(qw(buchungsgruppen_id unit));
132 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment packing_type partsgroup));
133 $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
134 $self->add_cvar_raw_data_columns;
135 map { $self->add_raw_data_columns("pricegroup_${_}") } (1..scalar(@{ $self->all_pricegroups }));
136 map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->translation_columns };
139 sub check_duplicates {
140 my ($self, %params) = @_;
142 my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
143 my $name_maker = sub { return $normalizer->($_[0]->description) };
146 if ('check_db' eq $self->controller->profile->get('duplicates')) {
147 %by_name = map { ( $name_maker->($_) => 'db' ) } @{ $self->existing_objects };
150 foreach my $entry (@{ $self->controller->data }) {
151 next if @{ $entry->{errors} };
153 my $name = $name_maker->($entry->{object});
155 if (!$by_name{ $name }) {
156 $by_name{ $name } = 'csv';
159 push @{ $entry->{errors} }, $by_name{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
164 sub check_buchungsgruppe {
165 my ($self, $entry) = @_;
167 my $object = $entry->{object};
169 # Check Buchungsgruppe
171 # Store and verify default ID.
172 my $default_id = $self->settings->{default_buchungsgruppe};
173 $default_id = undef unless $self->bg_by->{id}->{ $default_id };
175 # 1. Use default ID if enforced.
176 $object->buchungsgruppen_id($default_id) if $default_id && ($self->settings->{apply_buchungsgruppe} eq 'all');
178 # 2. Use supplied ID if valid
179 $object->buchungsgruppen_id(undef) if $object->buchungsgruppen_id && !$self->bg_by->{id}->{ $object->buchungsgruppen_id };
181 # 3. Look up name if supplied.
182 if (!$object->buchungsgruppen_id) {
183 my $bg = $self->bg_by->{description}->{ $entry->{raw_data}->{buchungsgruppe} };
184 $object->buchungsgruppen_id($bg->id) if $bg;
187 # 4. Use default ID if not valid.
188 $object->buchungsgruppen_id($default_id) if !$object->buchungsgruppen_id && $default_id && ($self->settings->{apply_buchungsgruppe} eq 'missing');
190 return 1 if $object->buchungsgruppen_id;
192 push @{ $entry->{errors} }, $::locale->text('Error: Buchungsgruppe missing or invalid');
197 my ($self, $entry) = @_;
199 my $object = $entry->{object};
201 $entry->{part} = $self->parts_by->{partnumber}->{ $object->type }->{ $object->partnumber };
203 if ($self->settings->{article_number_policy} eq 'update_prices') {
204 if ($entry->{part}) {
205 map { $entry->{part}->$_( $object->$_ ) } qw(sellprice listprice lastcost min_sellprice prices);
206 push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database');
207 $entry->{object_to_save} = $entry->{part};
209 $::lxdebug->dump(0, "P1", $entry->{object}->prices);
210 $::lxdebug->dump(0, "P1", $entry->{object_to_save}->prices);
214 $object->partnumber('####') if $entry->{part};
219 my ($self, $entry) = @_;
221 foreach my $column (qw(sellprice listprice lastcost)) {
222 next unless $self->controller->headers->{used}->{ $column };
224 my $adjustment = $self->settings->{sellprice_adjustment};
225 my $value = $entry->{object}->$column;
227 $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
228 $entry->{object}->$column($::form->round_amount($value, $self->settings->{sellprice_places}));
232 sub handle_shoparticle {
233 my ($self, $entry) = @_;
235 $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
239 my ($self, $entry) = @_;
241 my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
242 $bg ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1, income_accno_id_0 => 1, expense_accno_id_0 => 1);
244 my $type = $self->settings->{parts_type};
245 if ($type eq 'mixed') {
246 $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
247 : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
251 $entry->{object}->income_accno_id( $bg->income_accno_id_0 );
252 $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
254 if ($type eq 'part') {
255 $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
257 } elsif ($type ne 'service') {
258 push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
265 sub check_price_factor {
266 my ($self, $entry) = @_;
268 my $object = $entry->{object};
270 # Check whether or not price factor ID is valid.
271 if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
272 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
276 # Map name to ID if given.
277 if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
278 my $pf = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
281 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
285 $object->price_factor_id($pf->id);
291 sub check_packing_type {
292 my ($self, $entry) = @_;
294 my $object = $entry->{object};
296 # Check whether or not packing type ID is valid.
297 if ($object->packing_type_id && !$self->packing_types_by->{id}->{ $object->packing_type_id }) {
298 push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
302 # Map name to ID if given.
303 if (!$object->packing_type_id && $entry->{raw_data}->{packing_type}) {
304 my $type = $self->packing_types_by->{description}->{ $entry->{raw_data}->{packing_type} };
307 push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
311 $object->packing_type_id($type->id);
317 sub check_partsgroup {
318 my ($self, $entry) = @_;
320 my $object = $entry->{object};
322 # Check whether or not part group ID is valid.
323 if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
324 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
328 # Map name to ID if given.
329 if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
330 my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
333 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
337 $object->partsgroup_id($pg->id);
344 my ($self, $entry) = @_;
346 my $object = $entry->{object};
348 # Check whether or unit is valid.
349 if (!$self->units_by->{name}->{ $object->unit }) {
350 push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
357 sub handle_translations {
358 my ($self, $entry) = @_;
361 foreach my $language (@{ $self->all_languages }) {
362 my ($desc, $notes) = @{ $entry->{raw_data} }{ "description_" . $language->article_code, "notes_" . $language->article_code };
363 next unless $desc || $notes;
365 push @translations, SL::DB::Translation->new(language_id => $language->id,
366 translation => $desc,
367 longdescription => $notes);
370 $entry->{object}->translations(\@translations);
373 sub handle_pricegroups {
374 my ($self, $entry) = @_;
378 foreach my $pricegroup (@{ $self->all_pricegroups }) {
380 my $sellprice = $entry->{raw_data}->{"pricegroup_${idx}"};
381 next if $sellprice eq '';
383 push @prices, SL::DB::Price->new(pricegroup_id => $pricegroup->id,
384 price => $::form->parse_amount(\%::myconfig, $sellprice));
387 $entry->{object}->prices(\@prices);
390 sub set_various_fields {
391 my ($self, $entry) = @_;
393 $entry->{object}->priceupdate(DateTime->now_local);
399 my $profile = $self->SUPER::init_profile;
400 delete @{$profile}{qw(alternate assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)};
406 my ($self, %params) = @_;
408 my $with_number = [ grep { $_->{object}->partnumber ne '####' } @{ $self->controller->data } ];
409 my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ];
411 map { $_->{object}->partnumber('') } @{ $without_number };
413 $self->SUPER::save_objects(data => $with_number);
414 $self->SUPER::save_objects(data => $without_number);
417 sub setup_displayable_columns {
420 $self->SUPER::setup_displayable_columns;
421 $self->add_cvar_columns_to_displayable_columns;
423 $self->add_displayable_columns({ name => 'bin', description => $::locale->text('Bin') },
424 { name => 'binding_max_qty', description => $::locale->text('Binding Max Qty') },
425 { name => 'buchungsgruppen_id', description => $::locale->text('Buchungsgruppe (database ID)') },
426 { name => 'buchungsgruppe', description => $::locale->text('Buchungsgruppe (name)') },
427 { name => 'description', description => $::locale->text('Description') },
428 { name => 'drawing', description => $::locale->text('Drawing') },
429 { name => 'ean', description => $::locale->text('EAN') },
430 { name => 'formel', description => $::locale->text('Formula') },
431 { name => 'gv', description => $::locale->text('Business Volume') },
432 { name => 'has_sernumber', description => $::locale->text('Has serial number') },
433 { name => 'image', description => $::locale->text('Image') },
434 { name => 'lastcost', description => $::locale->text('Last Cost') },
435 { name => 'listprice', description => $::locale->text('List Price') },
436 { name => 'microfiche', description => $::locale->text('Microfiche') },
437 { name => 'min_sellprice', description => $::locale->text('Minimum Sell Price') },
438 { name => 'not_discountable', description => $::locale->text('Not Discountable') },
439 { name => 'notes', description => $::locale->text('Notes') },
440 { name => 'obsolete', description => $::locale->text('Obsolete') },
441 { name => 'onhand', description => $::locale->text('On Hand') },
442 { name => 'packing_type_id', description => $::locale->text('Packing type (database ID)') },
443 { name => 'packing_type', description => $::locale->text('Packing type (name)') },
444 { name => 'partnumber', description => $::locale->text('Part Number') },
445 { name => 'partsgroup_id', description => $::locale->text('Partsgroup (database ID)') },
446 { name => 'partsgroup', description => $::locale->text('Partsgroup (name)') },
447 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
448 { name => 'payment', description => $::locale->text('Payment terms (name)') },
449 { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') },
450 { name => 'price_factor', description => $::locale->text('Price factor (name)') },
451 { name => 'rop', description => $::locale->text('ROP') },
452 { name => 'sellprice', description => $::locale->text('Sellprice') },
453 { name => 'shop', description => $::locale->text('Shopartikel') },
454 { name => 'unit', description => $::locale->text('Unit') },
455 { name => 've', description => $::locale->text('Verrechnungseinheit') },
456 { name => 'weight', description => $::locale->text('Weight') },
459 foreach my $language (@{ $self->all_languages }) {
460 $self->add_displayable_columns({ name => 'description_' . $language->article_code,
461 description => $::locale->text('Description (translation for #1)', $language->description) },
462 { name => 'notes_' . $language->article_code,
463 description => $::locale->text('Notes (translation for #1)', $language->description) });
467 foreach my $pricegroup (@{ $self->all_pricegroups }) {
469 $self->add_displayable_columns({ name => 'pricegroup_' . $idx,
470 description => $::locale->text("Sellprice for price group '#1'", $pricegroup->pricegroup) });