Part-CSV-Import: Bei Update model_X und lastcost_X bei make_X berücksichtigen
authorThomas Heck <theck@linet-services.de>
Mon, 20 Aug 2012 12:54:01 +0000 (14:54 +0200)
committerSven Schöling <s.schoeling@linet-services.de>
Wed, 22 Aug 2012 15:42:25 +0000 (17:42 +0200)
implementiert #1901

SL/Controller/CsvImport/Base.pm
SL/Controller/CsvImport/Part.pm

index eda921f..31d1d9e 100644 (file)
@@ -15,7 +15,7 @@ use parent qw(Rose::Object);
 
 use Rose::Object::MakeMethods::Generic
 (
- scalar                  => [ qw(controller file csv) ],
+ scalar                  => [ qw(controller file csv save_with_cascade) ],
  'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_vc vc_by) ],
 );
 
@@ -305,7 +305,7 @@ sub save_objects {
 
     my $object = $entry->{object_to_save} || $entry->{object};
 
-    if (!$object->save) {
+    if ( !$object->save(cascade => !!$self->save_with_cascade()) ) {
       push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
     } else {
       $self->controller->num_imported($self->controller->num_imported + 1);
index 5d203a9..d39927a 100644 (file)
@@ -367,6 +367,8 @@ sub handle_pricegroups {
 
 sub handle_makemodel {
   my ($self, $entry) = @_;
+  my $object = $entry->{object};
+  my $found_any;
 
   my @makemodels;
   foreach my $idx (map { substr $_, 5 } grep { m/^make_\d+$/ && $entry->{raw_data}->{$_} } keys %{ $entry->{raw_data} }) {
@@ -379,6 +381,7 @@ sub handle_makemodel {
       push @{ $entry->{errors} }, $::locale->text('Error: Invalid vendor in column make_#1', $idx);
 
     } else {
+      $found_any = 1;
       push @makemodels, SL::DB::MakeModel->new(make               => $vendor->id,
                                                model              => $entry->{raw_data}->{"model_${idx}"},
                                                lastcost_as_number => $entry->{raw_data}->{"lastcost_${idx}"});
@@ -388,8 +391,31 @@ sub handle_makemodel {
     }
   }
 
-  $entry->{object}->makemodels(\@makemodels);
-  $entry->{object}->makemodel(scalar(@makemodels) ? 1 : 0);
+  $object->makemodels(\@makemodels);
+  $object->makemodel(scalar(@makemodels) ? 1 : 0);
+
+  if ( !$entry->{part} || $self->settings->{article_number_policy} ne 'update_prices' ) {
+    return;
+  }
+
+  my %old_makemodels_by_make = map { $_->make => $_ } $entry->{part}->makemodels;
+
+  foreach my $makemodel ($object->makemodels()) {
+    my $makemodel_orig = $old_makemodels_by_make{$makemodel->make};
+    $found_any = 1;
+
+    if ($makemodel_orig) {
+      $makemodel_orig->model($makemodel->model);
+      $makemodel_orig->lastcost($makemodel->lastcost);
+
+    } else {
+      $entry->{part}->add_makemodels($makemodel);
+    }
+  }
+
+  $entry->{part}->makemodel($object->makemodel);
+
+  $self->save_with_cascade(1) if $found_any;
 }
 
 sub set_various_fields {