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::Translation;
16 use parent qw(SL::Controller::CsvImport::Base);
18 use Rose::Object::MakeMethods::Generic
20 scalar => [ qw(table) ],
21 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by packing_types_by partsgroups_by
22 translation_columns) ],
27 $self->class('SL::DB::Part');
33 my $all_bg = SL::DB::Manager::Buchungsgruppe->get_all;
34 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) };
37 sub init_price_factors_by {
40 my $all_price_factors = SL::DB::Manager::PriceFactor->get_all;
41 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_price_factors } } ) } qw(id description) };
44 sub init_packing_types_by {
47 my $all_packing_types = SL::DB::Manager::PackingType->get_all;
48 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_packing_types } } ) } qw(id description) };
51 sub init_partsgroups_by {
54 my $all_partsgroups = SL::DB::Manager::PartsGroup->get_all;
55 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_partsgroups } } ) } qw(id partsgroup) };
61 my $all_units = SL::DB::Manager::Unit->get_all;
62 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_units } } ) } qw(name) };
68 my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } },
69 partnumber => { part => { },
72 foreach my $part (@{ $self->existing_objects }) {
73 next if $part->assembly;
74 $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part;
83 return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy
84 sellprice_places sellprice_adjustment sellprice_adjustment_type
85 shoparticle_if_missing parts_type) };
88 sub init_all_cvar_configs {
91 return SL::DB::Manager::CustomVariableConfig->get_all(where => [ module => 'IC' ]);
94 sub init_translation_columns {
97 return [ map { ("description_" . $_->article_code, "notes_" . $_->article_code) } (@{ $self->all_languages }) ];
103 return unless @{ $self->controller->data };
105 foreach my $entry (@{ $self->controller->data }) {
106 $self->check_buchungsgruppe($entry);
107 $self->check_type($entry);
108 $self->check_unit($entry);
109 $self->check_price_factor($entry);
110 $self->check_payment($entry);
111 $self->check_packing_type($entry);
112 $self->check_partsgroup($entry);
113 $self->check_existing($entry) unless @{ $entry->{errors} };
114 $self->handle_prices($entry) if $self->settings->{sellprice_adjustment};
115 $self->handle_shoparticle($entry);
116 $self->handle_translations($entry);
117 $self->handle_cvars($entry);
118 $self->set_various_fields($entry);
121 $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed';
122 $self->add_columns(qw(buchungsgruppen_id unit));
123 $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment packing_type partsgroup));
124 $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing};
125 $self->add_cvar_raw_data_columns;
126 map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->translation_columns };
129 sub check_duplicates {
130 my ($self, %params) = @_;
132 my $normalizer = sub { my $name = $_[0]; $name =~ s/[\s,\.\-]//g; return $name; };
133 my $name_maker = sub { return $normalizer->($_[0]->description) };
136 if ('check_db' eq $self->controller->profile->get('duplicates')) {
137 %by_name = map { ( $name_maker->($_) => 'db' ) } @{ $self->existing_objects };
140 foreach my $entry (@{ $self->controller->data }) {
141 next if @{ $entry->{errors} };
143 my $name = $name_maker->($entry->{object});
145 if (!$by_name{ $name }) {
146 $by_name{ $name } = 'csv';
149 push @{ $entry->{errors} }, $by_name{ $name } eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file');
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} = $self->parts_by->{partnumber}->{ $object->type }->{ $object->partnumber };
193 if ($self->settings->{article_number_policy} eq 'update_prices') {
194 if ($entry->{part}) {
195 map { $object->$_( $entry->{part}->$_ ) } qw(sellprice listprice lastcost);
196 push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database');
197 $entry->{object_to_save} = $entry->{part};
201 $object->partnumber('####') if $entry->{part};
206 my ($self, $entry) = @_;
208 foreach my $column (qw(sellprice listprice lastcost)) {
209 next unless $self->controller->headers->{used}->{ $column };
211 my $adjustment = $self->settings->{sellprice_adjustment};
212 my $value = $entry->{object}->$column;
214 $value = $self->settings->{sellprice_adjustment_type} eq 'percent' ? $value * (100 + $adjustment) / 100 : $value + $adjustment;
215 $entry->{object}->$column($::form->round_amount($value, $self->settings->{sellprice_places}));
219 sub handle_shoparticle {
220 my ($self, $entry) = @_;
222 $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop};
226 my ($self, $entry) = @_;
228 my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id };
229 $bg ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1, income_accno_id_0 => 1, expense_accno_id_0 => 1);
231 my $type = $self->settings->{parts_type};
232 if ($type eq 'mixed') {
233 $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part'
234 : $entry->{raw_data}->{type} =~ m/^s/i ? 'service'
238 $entry->{object}->income_accno_id( $bg->income_accno_id_0 );
239 $entry->{object}->expense_accno_id( $bg->expense_accno_id_0 );
241 if ($type eq 'part') {
242 $entry->{object}->inventory_accno_id( $bg->inventory_accno_id );
244 } elsif ($type ne 'service') {
245 push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type');
252 sub check_price_factor {
253 my ($self, $entry) = @_;
255 my $object = $entry->{object};
257 # Check whether or not price factor ID is valid.
258 if ($object->price_factor_id && !$self->price_factors_by->{id}->{ $object->price_factor_id }) {
259 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
263 # Map name to ID if given.
264 if (!$object->price_factor_id && $entry->{raw_data}->{price_factor}) {
265 my $pf = $self->price_factors_by->{description}->{ $entry->{raw_data}->{price_factor} };
268 push @{ $entry->{errors} }, $::locale->text('Error: Invalid price factor');
272 $object->price_factor_id($pf->id);
278 sub check_packing_type {
279 my ($self, $entry) = @_;
281 my $object = $entry->{object};
283 # Check whether or not packing type ID is valid.
284 if ($object->packing_type_id && !$self->packing_types_by->{id}->{ $object->packing_type_id }) {
285 push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
289 # Map name to ID if given.
290 if (!$object->packing_type_id && $entry->{raw_data}->{packing_type}) {
291 my $type = $self->packing_types_by->{description}->{ $entry->{raw_data}->{packing_type} };
294 push @{ $entry->{errors} }, $::locale->text('Error: Invalid packing type');
298 $object->packing_type_id($type->id);
304 sub check_partsgroup {
305 my ($self, $entry) = @_;
307 my $object = $entry->{object};
309 # Check whether or not part group ID is valid.
310 if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) {
311 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
315 # Map name to ID if given.
316 if (!$object->partsgroup_id && $entry->{raw_data}->{partsgroup}) {
317 my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} };
320 push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group');
324 $object->partsgroup_id($pg->id);
331 my ($self, $entry) = @_;
333 my $object = $entry->{object};
335 # Check whether or unit is valid.
336 if (!$self->units_by->{name}->{ $object->unit }) {
337 push @{ $entry->{errors} }, $::locale->text('Error: Unit missing or invalid');
344 sub handle_translations {
345 my ($self, $entry) = @_;
348 foreach my $language (@{ $self->all_languages }) {
349 my ($desc, $notes) = @{ $entry->{raw_data} }{ "description_" . $language->article_code, "notes_" . $language->article_code };
350 next unless $desc || $notes;
352 push @translations, SL::DB::Translation->new(language_id => $language->id,
353 translation => $desc,
354 longdescription => $notes);
357 $entry->{object}->translations(\@translations);
360 sub set_various_fields {
361 my ($self, $entry) = @_;
363 $entry->{object}->priceupdate(DateTime->now_local);
369 my $profile = $self->SUPER::init_profile;
370 delete @{$profile}{qw(alternate assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)};
376 my ($self, %params) = @_;
378 my $with_number = [ grep { $_->{object}->partnumber ne '####' } @{ $self->controller->data } ];
379 my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ];
381 map { $_->{object}->partnumber('') } @{ $without_number };
383 $self->SUPER::save_objects(data => $with_number);
384 $self->SUPER::save_objects(data => $without_number);
387 sub setup_displayable_columns {
390 $self->SUPER::setup_displayable_columns;
391 $self->add_cvar_columns_to_displayable_columns;
393 $self->add_displayable_columns({ name => 'bin', description => $::locale->text('Bin') },
394 { name => 'binding_max_qty', description => $::locale->text('Binding Max Qty') },
395 { name => 'buchungsgruppen_id', description => $::locale->text('Buchungsgruppe (database ID)') },
396 { name => 'buchungsgruppe', description => $::locale->text('Buchungsgruppe (name)') },
397 { name => 'description', description => $::locale->text('Description') },
398 { name => 'drawing', description => $::locale->text('Drawing') },
399 { name => 'ean', description => $::locale->text('EAN') },
400 { name => 'formel', description => $::locale->text('Formula') },
401 { name => 'gv', description => $::locale->text('Business Volume') },
402 { name => 'has_sernumber', description => $::locale->text('Has serial number') },
403 { name => 'image', description => $::locale->text('Image') },
404 { name => 'lastcost', description => $::locale->text('Last Cost') },
405 { name => 'listprice', description => $::locale->text('List Price') },
406 { name => 'microfiche', description => $::locale->text('Microfiche') },
407 { name => 'min_sellprice', description => $::locale->text('Minimum Sell Price') },
408 { name => 'not_discountable', description => $::locale->text('Not Discountable') },
409 { name => 'notes', description => $::locale->text('Notes') },
410 { name => 'obsolete', description => $::locale->text('Obsolete') },
411 { name => 'onhand', description => $::locale->text('On Hand') },
412 { name => 'packing_type_id', description => $::locale->text('Packing type (database ID)') },
413 { name => 'packing_type', description => $::locale->text('Packing type (name)') },
414 { name => 'partnumber', description => $::locale->text('Part Number') },
415 { name => 'partsgroup_id', description => $::locale->text('Partsgroup (database ID)') },
416 { name => 'partsgroup', description => $::locale->text('Partsgroup (name)') },
417 { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') },
418 { name => 'payment', description => $::locale->text('Payment terms (name)') },
419 { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') },
420 { name => 'price_factor', description => $::locale->text('Price factor (name)') },
421 { name => 'rop', description => $::locale->text('ROP') },
422 { name => 'sellprice', description => $::locale->text('Sellprice') },
423 { name => 'shop', description => $::locale->text('Shopartikel') },
424 { name => 'unit', description => $::locale->text('Unit') },
425 { name => 've', description => $::locale->text('Verrechnungseinheit') },
426 { name => 'weight', description => $::locale->text('Weight') },
429 foreach my $language (@{ $self->all_languages }) {
430 $self->add_displayable_columns({ name => 'description_' . $language->article_code,
431 description => $::locale->text('Description (translation for #1)', $language->description) },
432 { name => 'notes_' . $language->article_code,
433 description => $::locale->text('Notes (translation for #1)', $language->description) });
439 # Preisaktualisierung