epic-ts
[kivitendo-erp.git] / SL / Controller / CsvImport / Base.pm
index 09a4886..3090182 100644 (file)
@@ -2,13 +2,16 @@ package SL::Controller::CsvImport::Base;
 
 use strict;
 
+use English qw(-no_match_vars);
 use List::MoreUtils qw(pairwise any);
 
 use SL::Helper::Csv;
-use SL::DB::Currency;
+
+use SL::DB::BankAccount;
 use SL::DB::Customer;
 use SL::DB::Language;
 use SL::DB::PaymentTerm;
+use SL::DB::DeliveryTerm;
 use SL::DB::Vendor;
 use SL::DB::Contact;
 use SL::DB::History;
@@ -18,13 +21,13 @@ use parent qw(Rose::Object);
 use Rose::Object::MakeMethods::Generic
 (
  scalar                  => [ qw(controller file csv test_run save_with_cascade) ],
- 'scalar --get_set_init' => [ qw(is_multiplexed profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_currencies default_currency_id all_vc vc_by) ],
+ 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by delivery_terms_by all_bank_accounts all_vc vc_by clone_methods) ],
 );
 
 sub run {
   my ($self, %params) = @_;
 
-  $self->test_run($params{test_run});
+  $self->test_run($params{test});
 
   $self->controller->track_progress(phase => 'parsing csv', progress => 0);
 
@@ -47,22 +50,18 @@ sub run {
 
   $self->controller->track_progress(progress => 50);
 
-  if ($self->csv->is_multiplexed) {
-    die "controller for multiplex data is not implemented yet";
-  }
-
   $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
 
   return if ( !$self->csv->header || $self->csv->errors );
 
-  my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header->[0] } ] };
+  my $headers         = { headers => [ grep { $profile->{$_} } @{ $self->csv->header } ] };
   $headers->{methods} = [ map { $profile->{$_} } @{ $headers->{headers} } ];
   $headers->{used}    = { map { ($_ => 1) }      @{ $headers->{headers} } };
   $self->controller->headers($headers);
   $self->controller->raw_data_headers({ used => { }, headers => [ ] });
   $self->controller->info_headers({ used => { }, headers => [ ] });
 
-  my @objects  = $self->csv->get_objects;
+  my $objects  = $self->csv->get_objects;
 
   $self->controller->track_progress(progress => 70);
 
@@ -70,7 +69,7 @@ sub run {
 
   $self->controller->track_progress(progress => 80);
 
-  $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]);
+  $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]);
 
   $self->controller->track_progress(progress => 90);
 
@@ -144,23 +143,24 @@ sub init_all_languages {
   return SL::DB::Manager::Language->get_all;
 }
 
-sub init_all_currencies {
+sub init_all_bank_accounts {
   my ($self) = @_;
 
-  return SL::DB::Manager::Currency->get_all;
+  return SL::DB::Manager::BankAccount->get_all_sorted( query => [ obsolete => 0 ] );
 }
 
-sub init_default_currency_id {
+sub init_payment_terms_by {
   my ($self) = @_;
 
-  return SL::DB::Default->get->currency_id;
+  my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
 }
 
-sub init_payment_terms_by {
+sub init_delivery_terms_by {
   my ($self) = @_;
 
-  my $all_payment_terms = SL::DB::Manager::PaymentTerm->get_all;
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_payment_terms } } ) } qw(id description) };
+  my $all_delivery_terms = SL::DB::Manager::DeliveryTerm->get_all;
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_delivery_terms } } ) } qw(id description) };
 }
 
 sub init_all_vc {
@@ -170,6 +170,10 @@ sub init_all_vc {
            vendors   => SL::DB::Manager::Vendor->get_all };
 }
 
+sub init_clone_methods {
+  {}
+}
+
 sub force_allow_columns {
   return ();
 }
@@ -226,15 +230,29 @@ sub handle_cvars {
                          bool      => 'bool_value' );
 
   my @cvars;
+  my %changed_cvars;
   foreach my $config (@{ $self->all_cvar_configs }) {
     next unless exists $entry->{raw_data}->{ "cvar_" . $config->name };
     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, sub_module => '');
+    my $new_cvar = SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => '');
+
+    push @cvars, $new_cvar;
+    $changed_cvars{$config->name} = $new_cvar;
   }
 
-  $entry->{object}->custom_variables(\@cvars);
+  # merge existing with new cvars. swap every existing with the imported one, push the rest
+  if (@cvars) {
+    my @orig_cvars = ($entry->{object_to_save} || $entry->{object})->custom_variables;
+    for (@orig_cvars) {
+      $_ = $changed_cvars{ $_->config->name } if $changed_cvars{ $_->config->name };
+      delete $changed_cvars{ $_->config->name };
+    }
+    push @orig_cvars, values %changed_cvars;
+
+    $entry->{object}->custom_variables(\@orig_cvars);
+  }
 }
 
 sub init_profile {
@@ -311,11 +329,7 @@ sub init_manager_class {
   $self->manager_class("SL::DB::Manager::" . $1);
 }
 
-sub init_is_multiplexed {
-  my ($self) = @_;
-
-  $self->is_multiplexed('ARRAY' eq ref ($self->class) && scalar @{ $self->class } > 1);
-}
+sub is_multiplexed { 0 }
 
 sub check_objects {
 }
@@ -398,6 +412,38 @@ sub check_payment {
     }
 
     $object->payment_id($terms->id);
+
+    # register payment_id for method copying later
+    $self->clone_methods->{payment_id} = 1;
+  }
+
+  return 1;
+}
+
+sub check_delivery_term {
+  my ($self, $entry) = @_;
+
+  my $object = $entry->{object};
+
+  # Check whether or not delivery term ID is valid.
+  if ($object->delivery_term_id && !$self->delivery_terms_by->{id}->{ $object->delivery_term_id }) {
+    push @{ $entry->{errors} }, $::locale->text('Error: Invalid delivery terms');
+    return 0;
+  }
+
+  # Map name to ID if given.
+  if (!$object->delivery_term_id && $entry->{raw_data}->{delivery_term}) {
+    my $terms = $self->delivery_terms_by->{description}->{ $entry->{raw_data}->{delivery_term} };
+
+    if (!$terms) {
+      push @{ $entry->{errors} }, $::locale->text('Error: Invalid delivery terms');
+      return 0;
+    }
+
+    $object->delivery_term_id($terms->id);
+
+    # register delivery_term_id for method copying later
+    $self->clone_methods->{delivery_term_id} = 1;
   }
 
   return 1;
@@ -424,9 +470,9 @@ sub save_objects {
 
     my $ret;
     if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) {
-      push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $@);
+      push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $EVAL_ERROR);
     } elsif ( !$ret ) {
-      push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error);
+      push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $object->db->error);
     } else {
       $self->_save_history($object);
       $self->controller->num_imported($self->controller->num_imported + 1);
@@ -442,7 +488,9 @@ sub save_objects {
 }
 
 sub field_lengths {
-  return ();
+  my ($self) = @_;
+
+  return map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{$self->class->meta->columns};
 }
 
 sub fix_field_lengths {
@@ -474,17 +522,22 @@ sub clean_fields {
 sub _save_history {
   my ($self, $object) = @_;
 
-  if (any { $_ eq $self->controller->{type} } qw(parts customers_vendors)) {
+  if (any { $_ eq $self->controller->{type} } qw(parts customers_vendors orders)) {
     my $snumbers = $self->controller->{type} eq 'parts'             ? 'partnumber_' . $object->partnumber
                  : $self->controller->{type} eq 'customers_vendors' ?
                      ($self->table eq 'customer' ? 'customernumber_' . $object->customernumber : 'vendornumber_' . $object->vendornumber)
+                 : $self->controller->{type} eq 'orders'            ? 'ordnumber_' . $object->ordnumber
                  : '';
 
+    my $what_done = $self->controller->{type} eq 'orders' ? 'sales_order'
+                  : '';
+
     SL::DB::History->new(
       trans_id    => $object->id,
       snumbers    => $snumbers,
       employee_id => $self->controller->{employee_id},
       addition    => 'SAVED',
+      what_done   => $what_done,
     )->save();
   }
 }