Währung beim CSV-Import Kunden/Lieferanten unterstützen
[kivitendo-erp.git] / SL / Controller / CsvImport / CustomerVendor.pm
index 757c52c..4796493 100644 (file)
@@ -13,7 +13,7 @@ use parent qw(SL::Controller::CsvImport::Base);
 
 use Rose::Object::MakeMethods::Generic
 (
- 'scalar --get_set_init' => [ qw(table languages_by businesses_by) ],
+ 'scalar --get_set_init' => [ qw(table languages_by businesses_by currencies_by) ],
 );
 
 sub init_table {
@@ -44,13 +44,22 @@ sub init_languages_by {
   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
 }
 
+sub init_currencies_by {
+  my ($self) = @_;
+
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_currencies } } ) } qw(id name) };
+}
+
 sub check_objects {
   my ($self) = @_;
 
   $self->controller->track_progress(phase => 'building data', progress => 0);
 
-  my $numbercolumn  = $self->controller->profile->get('table') . "number";
-  my %vcs_by_number = map { ( $_->$numbercolumn => 1 ) } @{ $self->existing_objects };
+  my $vc            = $self->controller->profile->get('table');
+  my $update_policy = $self->controller->profile->get('update_policy') || 'update_existing';
+  my $numbercolumn  = "${vc}number";
+  my %vcs_by_number = map { ( $_->$numbercolumn => $_ ) } @{ $self->existing_objects };
+  my $methods       = $self->controller->headers->{methods};
 
   my $i;
   my $num_data = scalar @{ $self->controller->data };
@@ -62,14 +71,34 @@ sub check_objects {
     $self->check_language($entry);
     $self->check_business($entry);
     $self->check_payment($entry);
+    $self->check_currency($entry);
     $self->handle_cvars($entry);
 
     next if @{ $entry->{errors} };
 
-    if ($vcs_by_number{ $object->$numbercolumn }) {
-      $entry->{object}->$numbercolumn('####');
+    my @cleaned_fields = $self->clean_fields(qr{[\r\n]}, $object, qw(name department_1 department_2 street zipcode city country contact phone fax homepage email cc bcc
+                                                                     taxnumber account_number bank_code bank username greeting));
+
+    push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
+      if @cleaned_fields;
+
+    my $existing_vc = $vcs_by_number{ $object->$numbercolumn };
+    if (!$existing_vc) {
+      $vcs_by_number{ $object->$numbercolumn } = $object if $object->$numbercolumn;
+
+    } elsif ($update_policy eq 'skip') {
+      push(@{$entry->{errors}}, $::locale->text('Skipping due to existing entry in database'));
+
+    } elsif ($update_policy eq 'update_existing') {
+      # Update existing customer/vendor records.
+      $entry->{object_to_save} = $existing_vc;
+
+      $existing_vc->$_( $entry->{object}->$_ ) for @{ $methods };
+
+      push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
+
     } else {
-      $vcs_by_number{ $object->$numbercolumn } = $object;
+      $object->$numbercolumn('####');
     }
   } continue {
     $i++;
@@ -133,13 +162,43 @@ sub check_language {
   return 1;
 }
 
+sub check_currency {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+
+  # Check whether or not currency ID is valid.
+  if ($object->currency_id && !$self->currencies_by->{id}->{ $object->currency_id }) {
+    push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
+    return 0;
+  }
+
+  # Map name to ID if given.
+  if (!$object->currency_id && $entry->{raw_data}->{currency}) {
+    my $currency = $self->currencies_by->{name}->{  $entry->{raw_data}->{currency} };
+    if (!$currency) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
+      return 0;
+    }
+
+    $object->currency_id($currency->id);
+  }
+
+  # Set default currency if none was given.
+  $object->currency_id($self->default_currency_id) if !$object->currency_id;
+
+  $entry->{raw_data}->{currency_id} = $object->currency_id;
+
+  return 1;
+}
+
 sub check_business {
   my ($self, $entry) = @_;
 
   my $object = $entry->{object};
 
   # Check whether or not business ID is valid.
-  if ($object->business_id && !$self->businesss_by->{id}->{ $object->business_id }) {
+  if ($object->business_id && !$self->businesses_by->{id}->{ $object->business_id }) {
     push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
     return 0;
   }
@@ -231,6 +290,8 @@ sub setup_displayable_columns {
                                  { name => 'contact',           description => $::locale->text('Contact')                         },
                                  { name => 'country',           description => $::locale->text('Country')                         },
                                  { name => 'creditlimit',       description => $::locale->text('Credit Limit')                    },
+                                 { name => 'currency',          description => $::locale->text('Currency')                        },
+                                 { name => 'currency_id',       description => $::locale->text('Currency (database ID)')          },
                                  { name => 'customernumber',    description => $::locale->text('Customer Number')                 },
                                  { name => 'department_1',      description => $::locale->text('Department 1')                    },
                                  { name => 'department_2',      description => $::locale->text('Department 2')                    },