epic-ts
[kivitendo-erp.git] / SL / Controller / CsvImport / Part.pm
index 21a2982..9afaf63 100644 (file)
@@ -40,7 +40,7 @@ sub set_profile_defaults {
                        article_number_policy     => 'update_prices',
                        shoparticle_if_missing    => '0',
                        part_type                 => 'part',
-                       parts_classification      => 0,
+                       part_classification       => 0,
                        default_buchungsgruppe    => ($bugru ? $bugru->id : undef),
                        apply_buchungsgruppe      => 'all',
                       );
@@ -61,7 +61,7 @@ sub init_bg_by {
 
 sub init_classification_by {
   my ($self) = @_;
-  my $all_classifications = SL::DB::Manager::PartsClassification->get_all;
+  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) };
 }
@@ -175,7 +175,7 @@ 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 {
@@ -320,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
@@ -355,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');
 
@@ -422,29 +405,29 @@ sub check_part_type {
 
   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'
+    $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};
+               :                                                 $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);
+    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;
-      }
+    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
@@ -561,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;
   }
 
@@ -570,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;
     }
 
@@ -622,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));
@@ -667,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;
 
@@ -680,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;
 }
@@ -692,7 +675,9 @@ 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 {
@@ -713,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);
 }
@@ -748,7 +732,7 @@ 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 => 'classification_by',  description => $::locale->text('Article classification')  . ' [3]'                     },
+                                 { 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)')                           },