Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Controller / CsvImport / Helper / Consistency.pm
index 82ec563..5538a73 100644 (file)
@@ -2,14 +2,17 @@ package SL::Controller::CsvImport::Helper::Consistency;
 
 use strict;
 
+use Data::Dumper;
 use SL::DB::Default;
 use SL::DB::Currency;
 use SL::DB::TaxZone;
+use SL::DB::Project;
+use SL::DB::Department;
 
 use SL::Helper::Csv::Error;
 
 use parent qw(Exporter);
-our @EXPORT = qw(check_currency check_taxzone);
+our @EXPORT = qw(check_currency check_taxzone check_project check_department check_customer_vendor handle_salesman handle_employee);
 
 #
 # public functions
@@ -92,20 +95,142 @@ sub check_taxzone {
   if (!$object->taxzone_id && $params{take_default}) {
     # my $default_id = $self->settings->{'default_taxzone'};
     my $default_id = $self->controller->profile->get('default_taxzone');
+    $default_id ||= _default_taxzone_id($self);
     $object->taxzone_id($default_id);
     # check if default taxzone_id is valid just to be sure
     if (! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
       push @{ $entry->{errors} }, $::locale->text('Error with default taxzone');
       return 0;
-    };
+    }
   };
 
   # for the order import at this stage $object->taxzone_id may still not be
-  # defined, in this case the customer/vendor taxzone will be used.
+  # defined, in this case the customer/vendor taxzone will be used later
+  if ( defined $object->taxzone_id ) {
+    $entry->{info_data}->{taxzone} = _taxzones_by($self)->{id}->{ $object->taxzone_id }->description;
+  };
+
+  return 1;
+}
+
+sub check_project {
+  my ($self, $entry, %params) = @_;
+
+  my $id_column          = ($params{global} ? 'global' : '') . 'project_id';
+  my $number_column      = ($params{global} ? 'global' : '') . 'projectnumber';
+  my $description_column = ($params{global} ? 'global' : '') . 'project';
+
+  my $object = $entry->{object};
+
+  # Check whether or not project ID is valid.
+  if ($object->$id_column) {
+    if (! _projects_by($self)->{id}->{ $object->$id_column }) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
+      return 0;
+    } else {
+      $entry->{info_data}->{$number_column} = _projects_by($self)->{id}->{ $object->$id_column }->description;
+    };
+  }
+
+  my $proj;
+  # Map number to ID if given.
+  if (!$object->$id_column && $entry->{raw_data}->{$number_column}) {
+    $proj = _projects_by($self)->{projectnumber}->{ $entry->{raw_data}->{$number_column} };
+    if (!$proj) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
+      return 0;
+    }
+
+    $object->$id_column($proj->id);
+  }
+
+  # Map description to ID if given.
+  if (!$object->$id_column && $entry->{raw_data}->{$description_column}) {
+    $proj = _projects_by($self)->{description}->{ $entry->{raw_data}->{$description_column} };
+    if (!$proj) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid project');
+      return 0;
+    }
+
+    $object->$id_column($proj->id);
+  }
+
+  if ( $proj ) {
+    $entry->{info_data}->{"$description_column"} = $proj->description;
+    $entry->{info_data}->{"$number_column"}      = $proj->projectnumber;
+  };
+
+  return 1;
+}
+
+sub check_department {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+
+  # Check whether or not department ID was assigned and is valid.
+  if ($object->department_id) {
+    if (!_departments_by($self)->{id}->{ $object->department_id }) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
+      return 0;
+    } else {
+      # add department description as well, more feedback for user
+      $entry->{info_data}->{department} = _departments_by($self)->{id}->{ $object->department_id }->description;
+    };
+  }
+
+  # Map department description to ID if given.
+  if (!$object->department_id && $entry->{raw_data}->{department}) {
+    $entry->{info_data}->{department} = $entry->{raw_data}->{department};
+    my $dep = _departments_by($self)->{description}->{ $entry->{raw_data}->{department} };
+    if (!$dep) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid department');
+      return 0;
+    }
+    $entry->{info_data}->{department} = $dep->description;
+    $object->department_id($dep->id);
+  }
 
   return 1;
 }
 
+# ToDo: salesman by name
+sub handle_salesman {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+  my $vc_obj;
+  $vc_obj    = SL::DB::Customer->new(id => $object->customer_id)->load if $object->can('customer') && $object->customer_id;
+  $vc_obj    = SL::DB::Vendor->new(id   => $object->vendor_id)->load   if (!$vc_obj && $object->can('vendor') && $object->vendor_id);
+
+  # salesman from customer/vendor or login if not given
+  if (!$object->salesman) {
+    if ($vc_obj && $vc_obj->salesman_id) {
+      $object->salesman(SL::DB::Manager::Employee->find_by(id => $vc_obj->salesman_id));
+    } else {
+      $object->salesman(SL::DB::Manager::Employee->current);
+    }
+  }
+}
+
+# ToDo: employee by name
+sub handle_employee {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+
+  # employee from front end if not given
+  if (!$object->employee_id) {
+    $object->employee_id($self->controller->{employee_id});
+  }
+  # employee from login if not given
+  if (!$object->employee_id) {
+    $object->employee_id(SL::DB::Manager::Employee->current->id) if SL::DB::Manager::Employee->current;
+  }
+}
+
+
+
 #
 # private functions
 #
@@ -128,6 +253,26 @@ sub _default_currency_id {
   return SL::DB::Default->get->currency_id;
 }
 
+sub _default_taxzone_id {
+  my ($self) = @_;
+
+  return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ])->[0]->id;
+}
+
+sub _departments_by {
+  my ($self) = @_;
+
+  my $all_departments = SL::DB::Manager::Department->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_departments } } ) } qw(id description) };
+}
+
+sub _projects_by {
+  my ($self) = @_;
+
+  my $all_projects = SL::DB::Manager::Project->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_projects } } ) } qw(id projectnumber description) };
+}
+
 sub _taxzones_by {
   my ($self) = @_;