X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FPart.pm;h=9afaf637c4a0649e1fed108bdf324eb3cc0c8afd;hb=9dfd320ff258071e3ad78dd6cf2d76e215efd4f7;hp=80fd3cec43ef1ad6a7cb57a8e3818232c88c2b8e;hpb=df2057b857d530bc01adfb68fb3a1fddeffc322c;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Part.pm b/SL/Controller/CsvImport/Part.pm index 80fd3cec4..9afaf637c 100644 --- a/SL/Controller/CsvImport/Part.pm +++ b/SL/Controller/CsvImport/Part.pm @@ -23,7 +23,7 @@ use parent qw(SL::Controller::CsvImport::Base); use Rose::Object::MakeMethods::Generic ( scalar => [ qw(table makemodel_columns) ], - 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by units_by partsgroups_by + 'scalar --get_set_init' => [ qw(bg_by settings parts_by price_factors_by classification_by units_by partsgroups_by warehouses_by bins_by translation_columns all_pricegroups) ], ); @@ -39,7 +39,8 @@ sub set_profile_defaults { sellprice_adjustment_type => 'percent', article_number_policy => 'update_prices', shoparticle_if_missing => '0', - parts_type => 'part', + part_type => 'part', + part_classification => 0, default_buchungsgruppe => ($bugru ? $bugru->id : undef), apply_buchungsgruppe => 'all', ); @@ -58,6 +59,13 @@ sub init_bg_by { return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_bg } } ) } qw(id description) }; } +sub init_classification_by { + my ($self) = @_; + my $all_classifications = SL::DB::Manager::PartClassification->get_all; + $_->abbreviation($::locale->text($_->abbreviation)) for @{ $all_classifications }; + return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_classifications } } ) } qw(id abbreviation) }; +} + sub init_price_factors_by { my ($self) = @_; @@ -97,12 +105,12 @@ sub init_warehouses_by { sub init_parts_by { my ($self) = @_; -# my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->assembly } @{ $self->existing_objects } }, +# my $parts_by = { id => { map { ( $_->id => $_ ) } grep { !$_->part_type = 'assembly' } @{ $self->existing_objects } }, # partnumber => { part => { }, # service => { } } }; # # foreach my $part (@{ $self->existing_objects }) { -# next if $part->assembly; +# next if $part->part_type eq 'assembly'; # $parts_by->{partnumber}->{ $part->type }->{ $part->partnumber } = $part; # } @@ -118,7 +126,7 @@ sub init_parts_by { sub init_all_pricegroups { my ($self) = @_; - return SL::DB::Manager::Pricegroup->get_all(sort => 'id'); + return SL::DB::Manager::Pricegroup->get_all_sorted; } sub init_settings { @@ -126,7 +134,7 @@ sub init_settings { return { map { ( $_ => $self->controller->profile->get($_) ) } qw(apply_buchungsgruppe default_buchungsgruppe article_number_policy sellprice_places sellprice_adjustment sellprice_adjustment_type - shoparticle_if_missing parts_type default_unit) }; + shoparticle_if_missing part_type classification_id default_unit) }; } sub init_all_cvar_configs { @@ -156,7 +164,7 @@ sub check_objects { $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0; $self->check_buchungsgruppe($entry); - $self->check_type($entry); + $self->check_part_type($entry); $self->check_unit($entry); $self->check_price_factor($entry); $self->check_payment($entry); @@ -167,13 +175,14 @@ sub check_objects { $self->handle_prices($entry) if $self->settings->{sellprice_adjustment}; $self->handle_shoparticle($entry); $self->handle_translations($entry); - $self->handle_cvars($entry); + $self->handle_cvars($entry) unless $entry->{dont_handle_cvars}; $self->handle_makemodel($entry); $self->set_various_fields($entry); } continue { $i++; } - $self->add_columns(qw(type)) if $self->settings->{parts_type} eq 'mixed'; + + $self->add_columns(qw(part_type classification_id)) if $self->settings->{part_type} eq 'mixed'; $self->add_columns(qw(buchungsgruppen_id unit)); $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw (price_factor payment partsgroup warehouse bin)); $self->add_columns(qw(shop)) if $self->settings->{shoparticle_if_missing}; @@ -272,13 +281,13 @@ sub check_existing { } if ($entry->{part}) { - if ($entry->{part}->type ne $object->type ) { + if ($entry->{part}->part_type ne $object->part_type ) { push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database with different type')); return; } - if ( $entry->{part}->unit ne $object->unit || $entry->{part}->inventory_accno_id != $object->inventory_accno_id ) { + if ( $entry->{part}->unit ne $object->unit ) { if ( $entry->{part}->onhand != 0 || $self->_part_is_used($entry->{part})) { - push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry with different unit or inventory_accno_id')); + push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry with different unit')); return; } } @@ -311,7 +320,8 @@ sub check_existing { $entry->{part}->prices(grep { $_ } map { $prices_by_pricegroup_id{$_->id} } @{ $self->all_pricegroups }); push @{ $entry->{information} }, $::locale->text('Updating prices of existing entry in database'); - $entry->{object_to_save} = $entry->{part}; + $entry->{object_to_save} = $entry->{part}; + $entry->{dont_handle_cvars} = 1; } elsif ( $self->settings->{article_number_policy} eq 'update_parts' || $self->settings->{article_number_policy} eq 'update_parts_sn') { # Update parts table @@ -346,24 +356,6 @@ sub check_existing { } $entry->{part}->translations(\@translations) if @translations; - # Update cvars - my %type_to_column = ( text => 'text_value', - textfield => 'text_value', - select => 'text_value', - date => 'timestamp_value_as_date', - timestamp => 'timestamp_value_as_date', - number => 'number_value_as_number', - bool => 'bool_value' ); - my @cvars; - push @cvars, $entry->{part}->custom_variables; - foreach my $config (@{ $self->all_cvar_configs }) { - next unless exists $raw->{ "cvar_" . $config->name }; - my $value = $raw->{ "cvar_" . $config->name }; - my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type"; - push @cvars, SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => ''); - } - $entry->{part}->custom_variables(\@cvars) if @cvars; - # save Part Update push @{ $entry->{information} }, $::locale->text('Updating data of existing entry in database'); @@ -403,16 +395,39 @@ sub handle_shoparticle { $entry->{object}->shop(1) if $self->settings->{shoparticle_if_missing} && !$self->controller->headers->{used}->{shop}; } -sub check_type { +sub check_part_type { my ($self, $entry) = @_; - my $type = $self->settings->{parts_type}; + # TODO: assemblies or assortments can't be imported + + # my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id }; + # $bg ||= SL::DB::Buchungsgruppe->new(inventory_accno_id => 1); # does this case ever occur? - if ($type eq 'mixed' && $entry->{raw_data}->{type}) { - $type = $entry->{raw_data}->{type} =~ m/^p/i ? 'part' - : $entry->{raw_data}->{type} =~ m/^s/i ? 'service' - : $entry->{raw_data}->{type} =~ m/^a/i ? 'assembly' - : undef; + my $part_type = $self->settings->{part_type}; + if ($part_type eq 'mixed' && $entry->{raw_data}->{part_type}) { + $part_type = $entry->{raw_data}->{part_type} =~ m/^p/i ? 'part' + : $entry->{raw_data}->{part_type} =~ m/^s/i ? 'service' + : $entry->{raw_data}->{part_type} =~ m/^assem/i ? 'assembly' + : $entry->{raw_data}->{part_type} =~ m/^assor/i ? 'assortment' + : $self->settings->{part_type}; + } + my $classification_id = $self->settings->{classification_id}; + + if ( $entry->{raw_data}->{pclass} && length($entry->{raw_data}->{pclass}) >= 2 ) { + my $abbr1 = substr($entry->{raw_data}->{pclass},0,1); + my $abbr2 = substr($entry->{raw_data}->{pclass},1); + + if ( $self->classification_by->{abbreviation}->{$abbr2} ) { + my $tmp_classification_id = $self->classification_by->{abbreviation}->{$abbr2}->id; + $classification_id = $tmp_classification_id if $tmp_classification_id; + } + if ($part_type eq 'mixed') { + $part_type = $abbr1 eq $::locale->text('Part (typeabbreviation)') ? 'part' + : $abbr1 eq $::locale->text('Service (typeabbreviation)') ? 'service' + : $abbr1 eq $::locale->text('Assembly (typeabbreviation)') ? 'assembly' + : $abbr1 eq $::locale->text('Assortment (typeabbreviation)') ? 'assortment' + : undef; + } } # when saving income_accno_id or expense_accno_id use ids from the selected @@ -421,32 +436,24 @@ sub check_type { # not all be set. # Only use existing bg - my $bg = $self->bg_by->{id}->{ $entry->{object}->buchungsgruppen_id }; + # $entry->{object}->income_accno_id( $bg->income_accno_id( SL::DB::Manager::TaxZone->get_default->id ) ); - # if not set there is an error occurred in check_buchungsgruppe() - # but if the part exists the new values for accno are ignored + # if ($part_type eq 'part' || $part_type eq 'service') { + # $entry->{object}->expense_accno_id( $bg->expense_accno_id( SL::DB::Manager::TaxZone->get_default->id ) ); + # } - if ( $bg ) { - $entry->{object}->income_accno_id( $bg->income_accno_id( SL::DB::Manager::TaxZone->get_default->id ) ); - $self->clone_methods->{income_accno_id} = 1; + # if ($part_type eq 'part') { + # $entry->{object}->inventory_accno_id( $bg->inventory_accno_id ); + # } - if ($type eq 'part' || $type eq 'service') { - $entry->{object}->expense_accno_id( $bg->expense_accno_id( SL::DB::Manager::TaxZone->get_default->id ) ); - $self->clone_methods->{expense_accno_id} = 1; - } + if (none { $_ eq $part_type } qw(part service assembly assortment)) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid part type'); + return 0; } - if ($type eq 'part') { - if ( $bg ) { - $entry->{object}->inventory_accno_id( $bg->inventory_accno_id ); - } - else { - #use an existent bg - $entry->{object}->inventory_accno_id( SL::DB::Manager::Buchungsgruppe->get_first->id ); - } - } elsif ($type eq 'assembly') { - $entry->{object}->assembly(1); - } + $entry->{object}->part_type($part_type); + $entry->{object}->classification_id( $classification_id ); + return 1; } @@ -537,7 +544,7 @@ sub check_partsgroup { # Check whether or not part group ID is valid. if ($object->partsgroup_id && !$self->partsgroups_by->{id}->{ $object->partsgroup_id }) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group'); + push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group id #1', $object->partsgroup_id); return 0; } @@ -546,7 +553,7 @@ sub check_partsgroup { my $pg = $self->partsgroups_by->{partsgroup}->{ $entry->{raw_data}->{partsgroup} }; if (!$pg) { - push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group'); + push @{ $entry->{errors} }, $::locale->text('Error: Invalid parts group name #1', $entry->{raw_data}->{partsgroup}); return 0; } @@ -598,7 +605,7 @@ sub handle_pricegroups { foreach my $pricegroup (@{ $self->all_pricegroups }) { $idx++; my $sellprice = $entry->{raw_data}->{"pricegroup_${idx}"}; - next if $sellprice eq ''; + next if ($sellprice // '') eq ''; push @prices, SL::DB::Price->new(pricegroup_id => $pricegroup->id, price => $::form->parse_amount(\%::myconfig, $sellprice)); @@ -643,7 +650,7 @@ sub handle_makemodel { my %old_makemodels_by_mm = map { $_->make . $; . $_->model => $_ } $entry->{part}->makemodels; my @new_makemodels; - foreach my $makemodel ($object->makemodels()) { + foreach my $makemodel (@{ $object->makemodels_sorted }) { my $makemodel_orig = $old_makemodels_by_mm{$makemodel->make,$makemodel->model}; $found_any = 1; @@ -656,11 +663,11 @@ sub handle_makemodel { } } - $entry->{part}->makemodels([ $entry->{part}->makemodels, @new_makemodels ]) if @new_makemodels; + $entry->{part}->makemodels([ @{$entry->{part}->makemodels_sorted}, @new_makemodels ]) if @new_makemodels; # reindex makemodels my $i = 0; - $_->sortorder(++$i) for @{ $entry->{part}->makemodels }; + $_->sortorder(++$i) for @{ $entry->{part}->makemodels_sorted }; $self->save_with_cascade(1) if $found_any; } @@ -668,14 +675,16 @@ sub handle_makemodel { sub set_various_fields { my ($self, $entry) = @_; - $entry->{object}->priceupdate(DateTime->now_local); + my $object = $entry->{object_to_save} || $entry->{object}; + + $object->priceupdate(DateTime->now_local); } sub init_profile { my ($self) = @_; my $profile = $self->SUPER::init_profile; - delete @{$profile}{qw(alternate assembly bom expense_accno_id income_accno_id inventory_accno_id makemodel priceupdate stockable type)}; + delete @{$profile}{qw(bom makemodel priceupdate stockable type)}; $profile->{"pricegroup_$_"} = '' for 1 .. scalar @{ $_[0]->all_pricegroups }; @@ -689,7 +698,6 @@ sub save_objects { my $without_number = [ grep { $_->{object}->partnumber eq '####' } @{ $self->controller->data } ]; map { $_->{object}->partnumber('') } @{ $without_number }; - $self->SUPER::save_objects(data => $with_number); $self->SUPER::save_objects(data => $without_number); } @@ -700,11 +708,10 @@ sub setup_displayable_columns { $self->SUPER::setup_displayable_columns; $self->add_cvar_columns_to_displayable_columns; - $self->add_displayable_columns({ name => 'assembly', description => $::locale->text('assembly') }, - { name => 'bin_id', description => $::locale->text('Bin (database ID)') }, + $self->add_displayable_columns({ name => 'bin_id', description => $::locale->text('Bin (database ID)') }, { name => 'bin', description => $::locale->text('Bin (name)') }, - { name => 'buchungsgruppen_id', description => $::locale->text('Booking group (database ID)') }, - { name => 'buchungsgruppe', description => $::locale->text('Booking group (name)') }, + { name => 'buchungsgruppen_id', description => $::locale->text('Booking group (database ID)') }, + { name => 'buchungsgruppe', description => $::locale->text('Booking group (name)') }, { name => 'description', description => $::locale->text('Description') }, { name => 'drawing', description => $::locale->text('Drawing') }, { name => 'ean', description => $::locale->text('EAN') }, @@ -712,7 +719,6 @@ sub setup_displayable_columns { { name => 'gv', description => $::locale->text('Business Volume') }, { name => 'has_sernumber', description => $::locale->text('Has serial number') }, { name => 'image', description => $::locale->text('Image') }, - { name => 'inventory_accno_id', description => $::locale->text('part') }, { name => 'lastcost', description => $::locale->text('Last Cost') }, { name => 'listprice', description => $::locale->text('List Price') }, { name => 'make_X', description => $::locale->text('Make (vendor\'s database ID, number or name; with X being a number)') . ' [1]' }, @@ -726,18 +732,19 @@ sub setup_displayable_columns { { name => 'partnumber', description => $::locale->text('Part Number') }, { name => 'partsgroup_id', description => $::locale->text('Partsgroup (database ID)') }, { name => 'partsgroup', description => $::locale->text('Partsgroup (name)') }, + { name => 'part_classification',description => $::locale->text('Article classification') . ' [3]' }, { name => 'payment_id', description => $::locale->text('Payment terms (database ID)') }, { name => 'payment', description => $::locale->text('Payment terms (name)') }, { name => 'price_factor_id', description => $::locale->text('Price factor (database ID)') }, { name => 'price_factor', description => $::locale->text('Price factor (name)') }, { name => 'rop', description => $::locale->text('ROP') }, { name => 'sellprice', description => $::locale->text('Sellprice') }, - { name => 'shop', description => $::locale->text('Shop article') }, - { name => 'type', description => $::locale->text('Article type') . ' [3]' }, + { name => 'shop', description => $::locale->text('Shop article') }, + { name => 'type', description => $::locale->text('Article type') . ' [3]' }, { name => 'unit', description => $::locale->text('Unit (if missing or empty default unit will be used)') }, { name => 've', description => $::locale->text('Verrechnungseinheit') }, { name => 'warehouse_id', description => $::locale->text('Warehouse (database ID)') }, - { name => 'warehouse', description => $::locale->text('Warehouse (name)') }, + { name => 'warehouse', description => $::locale->text('Warehouse (name)') }, { name => 'weight', description => $::locale->text('Weight') }, );