epic-ts
[kivitendo-erp.git] / SL / Controller / CsvImport / CustomerVendor.pm
index 4796493..4f78721 100644 (file)
@@ -3,6 +3,7 @@ package SL::Controller::CsvImport::CustomerVendor;
 use strict;
 
 use SL::Helper::Csv;
+use SL::Controller::CsvImport::Helper::Consistency;
 use SL::DB::Business;
 use SL::DB::CustomVariable;
 use SL::DB::CustomVariableConfig;
@@ -13,9 +14,14 @@ use parent qw(SL::Controller::CsvImport::Base);
 
 use Rose::Object::MakeMethods::Generic
 (
- 'scalar --get_set_init' => [ qw(table languages_by businesses_by currencies_by) ],
+ 'scalar --get_set_init' => [ qw(table languages_by businesses_by salesmen_by) ],
 );
 
+sub set_profile_defaults {
+  my ($self) = @_;
+  $self->controller->profile->_set_defaults(table => 'customer');
+};
+
 sub init_table {
   my ($self) = @_;
   $self->table($self->controller->profile->get('table') eq 'customer' ? 'customer' : 'vendor');
@@ -44,10 +50,10 @@ sub init_languages_by {
   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_languages } } ) } qw(id description article_code) };
 }
 
-sub init_currencies_by {
+sub init_salesmen_by {
   my ($self) = @_;
 
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_currencies } } ) } qw(id name) };
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ SL::DB::Manager::Employee->get_all } } ) } qw(id name) };
 }
 
 sub check_objects {
@@ -71,13 +77,16 @@ sub check_objects {
     $self->check_language($entry);
     $self->check_business($entry);
     $self->check_payment($entry);
-    $self->check_currency($entry);
+    $self->check_delivery_term($entry);
+    $self->check_taxzone($entry,  take_default => 1);
+    $self->check_currency($entry, take_default => 1);
+    $self->check_salesman($entry);
     $self->handle_cvars($entry);
 
     next if @{ $entry->{errors} };
 
     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));
+                                                                     taxnumber account_number bank_code bank username greeting taxzone));
 
     push @{ $entry->{information} }, $::locale->text('Illegal characters have been removed from the following fields: #1', join(', ', @cleaned_fields))
       if @cleaned_fields;
@@ -93,7 +102,7 @@ sub check_objects {
       # Update existing customer/vendor records.
       $entry->{object_to_save} = $existing_vc;
 
-      $existing_vc->$_( $entry->{object}->$_ ) for @{ $methods };
+      $existing_vc->$_( $entry->{object}->$_ ) for @{ $methods }, keys %{ $self->clone_methods };
 
       push @{ $entry->{information} }, $::locale->text('Updating existing entry in database');
 
@@ -104,7 +113,7 @@ sub check_objects {
     $i++;
   }
 
-  $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(language business payment));
+  $self->add_columns(map { "${_}_id" } grep { exists $self->controller->data->[0]->{raw_data}->{$_} } qw(language business payment delivery_term taxzone));
   $self->add_cvar_raw_data_columns;
 }
 
@@ -157,62 +166,67 @@ sub check_language {
     }
 
     $object->language_id($language->id);
+
+    # register language_id for method copying later
+    $self->clone_methods->{language_id} = 1;
   }
 
   return 1;
 }
 
-sub check_currency {
+sub check_business {
   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');
+  # Check whether or not business ID is valid.
+  if ($object->business_id && !$self->businesses_by->{id}->{ $object->business_id }) {
+    push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
     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');
+  if (!$object->business_id && $entry->{raw_data}->{business}) {
+    my $business = $self->businesses_by->{description}->{ $entry->{raw_data}->{business} };
+
+    if (!$business) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
       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;
+    $object->business_id($business->id);
 
-  $entry->{raw_data}->{currency_id} = $object->currency_id;
+    # register business_id for method copying later
+    $self->clone_methods->{business_id} = 1;
+  }
 
   return 1;
 }
 
-sub check_business {
+sub check_salesman {
   my ($self, $entry) = @_;
 
   my $object = $entry->{object};
 
-  # Check whether or not business ID is valid.
-  if ($object->business_id && !$self->businesses_by->{id}->{ $object->business_id }) {
-    push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
+  # Check whether or not salesman ID is valid.
+  if ($object->salesman_id && !$self->salesmen_by->{id}->{ $object->salesman_id }) {
+    push @{ $entry->{errors} }, $::locale->text('Error: Invalid salesman');
     return 0;
   }
 
   # Map name to ID if given.
-  if (!$object->business_id && $entry->{raw_data}->{business}) {
-    my $business = $self->businesses_by->{description}->{ $entry->{raw_data}->{business} };
+  if (!$object->salesman_id && $entry->{raw_data}->{salesman}) {
+    my $salesman = $self->salesmen_by->{name}->{ $entry->{raw_data}->{salesman} };
 
-    if (!$business) {
-      push @{ $entry->{errors} }, $::locale->text('Error: Invalid business');
+    if (!$salesman) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid salesman');
       return 0;
     }
 
-    $object->business_id($business->id);
+    $object->salesman_id($salesman->id);
+
+    # register salesman_id for method copying later
+    $self->clone_methods->{salesman_id} = 1;
   }
 
   return 1;
@@ -242,31 +256,12 @@ sub save_objects {
   $self->SUPER::save_objects(data => $without_number);
 }
 
-sub field_lengths {
-  return ( name           => 75,
-           department_1   => 75,
-           department_2   => 75,
-           street         => 75,
-           zipcode        => 10,
-           city           => 75,
-           country        => 75,
-           contact        => 75,
-           fax            => 30,
-           account_number => 15,
-           bank_code      => 10,
-           language       => 5,
-           username       => 50,
-           ustid          => 14,
-           iban           => 100,
-           bic            => 100,
-         );
-}
-
 sub init_profile {
   my ($self) = @_;
 
   my $profile = $self->SUPER::init_profile;
-  delete @{$profile}{qw(business datevexport language payment salesman salesman_id taxincluded terms)};
+  delete @{$profile}{qw(business datevexport language payment delivery_term taxincluded terms)};
+  delete @{$profile}{qw(salesman salesman_id)}    if $::instance_conf->get_vertreter;
 
   return $profile;
 }
@@ -295,6 +290,8 @@ sub setup_displayable_columns {
                                  { name => 'customernumber',    description => $::locale->text('Customer Number')                 },
                                  { name => 'department_1',      description => $::locale->text('Department 1')                    },
                                  { name => 'department_2',      description => $::locale->text('Department 2')                    },
+                                 { name => 'delivery_term_id',  description => $::locale->text('Delivery terms (database ID)')    },
+                                 { name => 'delivery_term',     description => $::locale->text('Delivery terms (name)')           },
                                  { name => 'direct_debit',      description => $::locale->text('direct debit')                    },
                                  { name => 'discount',          description => $::locale->text('Discount')                        },
                                  { name => 'email',             description => $::locale->text('E-mail')                          },
@@ -313,12 +310,19 @@ sub setup_displayable_columns {
                                  { name => 'phone',             description => $::locale->text('Phone')                           },
                                  { name => 'street',            description => $::locale->text('Street')                          },
                                  { name => 'taxnumber',         description => $::locale->text('Tax Number / SSN')                },
-                                 { name => 'taxzone_id',        description => $::locale->text('Steuersatz')                      },
+                                 { name => 'taxzone',           description => $::locale->text('Tax zone (description)')          },
+                                 { name => 'taxzone_id',        description => $::locale->text('Tax zone (database ID)')          },
                                  { name => 'user_password',     description => $::locale->text('Password')                        },
                                  { name => 'username',          description => $::locale->text('Username')                        },
                                  { name => 'ustid',             description => $::locale->text('sales tax identification number') },
                                  { name => 'zipcode',           description => $::locale->text('Zipcode')                         },
                                 );
+
+  if (!$::instance_conf->get_vertreter) {
+    $self->add_displayable_columns({ name => 'salesman_id', description => $::locale->text('Salesman (database ID)') });
+    $self->add_displayable_columns({ name => 'salesman',    description => $::locale->text('Salesman') });
+  }
+
 }
 
 # TODO: