Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
index 6a66e8c..656495e 100644 (file)
@@ -9,12 +9,13 @@ use SL::DB::Customer;
 use SL::DB::Language;
 use SL::DB::PaymentTerm;
 use SL::DB::Vendor;
+use SL::DB::Contact;
 
 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) ],
 );
 
@@ -28,6 +29,7 @@ sub run {
                                   profile                => $profile,
                                   ignore_unknown_columns => 1,
                                   strict_profile         => 1,
+                                  case_insensitive_header => 1,
                                   map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
                                  ));
 
@@ -38,7 +40,7 @@ sub run {
 
   $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
 
-  return unless $self->csv->header;
+  return if ( !$self->csv->header || $self->csv->errors );
 
   my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
   $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
@@ -52,7 +54,10 @@ sub run {
   $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]);
 
   $self->check_objects;
-  $self->check_duplicates if $self->controller->profile->get('duplicates', 'no_check') ne 'no_check';
+  if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) {
+    $self->check_std_duplicates();
+    $self->check_duplicates();
+  }
   $self->fix_field_lengths;
 
   $::myconfig{numberformat} = $old_numberformat;
@@ -184,7 +189,7 @@ sub handle_cvars {
     my $value  = $entry->{raw_data}->{ "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);
+    push @cvars, SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => '');
   }
 
   $entry->{object}->custom_variables(\@cvars);
@@ -208,6 +213,12 @@ sub init_profile {
     $profile{$col} = $name;
   }
 
+  if ($self->can('all_cvar_configs')) {
+    for (@{ $self->all_cvar_configs }) {
+      $profile{ 'cvar_' . $_->name } = '';
+    }
+  }
+
   $self->profile(\%profile);
 }
 
@@ -268,6 +279,60 @@ sub check_objects {
 sub check_duplicates {
 }
 
+sub check_std_duplicates {
+  my $self = shift;
+
+  my $duplicates = {};
+
+  my $all_fields = $self->get_duplicate_check_fields();
+
+  foreach my $key (keys(%{ $all_fields })) {
+    if ( $self->controller->profile->get('duplicates_'. $key) && (!exists($all_fields->{$key}->{std_check}) || $all_fields->{$key}->{std_check} )  ) {
+      $duplicates->{$key} = {};
+    }
+  }
+
+  my @duplicates_keys = keys(%{ $duplicates });
+
+  if ( !scalar(@duplicates_keys) ) {
+    return;
+  }
+
+  if ( $self->controller->profile->get('duplicates') eq 'check_db' ) {
+    foreach my $object (@{ $self->existing_objects }) {
+      foreach my $key (@duplicates_keys) {
+        my $value = exists($all_fields->{$key}->{maker}) ? $all_fields->{$key}->{maker}->($object, $self) : $object->$key;
+        $duplicates->{$key}->{$value} = 'db';
+      }
+    }
+  }
+
+  foreach my $entry (@{ $self->controller->data }) {
+    if ( @{ $entry->{errors} } ) {
+      next;
+    }
+
+    my $object = $entry->{object};
+
+    foreach my $key (@duplicates_keys) {
+      my $value = exists($all_fields->{$key}->{maker}) ? $all_fields->{$key}->{maker}->($object, $self) : $object->$key;
+
+      if ( exists($duplicates->{$key}->{$value}) ) {
+        push(@{ $entry->{errors} }, $duplicates->{$key}->{$value} eq 'db' ? $::locale->text('Duplicate in database') : $::locale->text('Duplicate in CSV file'));
+        last;
+      } else {
+        $duplicates->{$key}->{$value} = 'csv';
+      }
+
+    }
+  }
+
+}
+
+sub get_duplicate_check_fields {
+  return {};
+}
+
 sub check_payment {
   my ($self, $entry) = @_;
 
@@ -304,7 +369,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);
@@ -327,3 +392,4 @@ sub fix_field_lengths {
 }
 
 1;
+