Verbessern von Helfer Konsistenz-Check
authorNiclas Zimmermann <niclas@kivitendo-premium.de>
Fri, 6 Dec 2013 11:45:20 +0000 (12:45 +0100)
committerNiclas Zimmermann <niclas@kivitendo-premium.de>
Fri, 6 Dec 2013 11:45:20 +0000 (12:45 +0100)
Der Konsistenz-Check wird in den Ordner SL/Controller/CsvImport/Helper/
verschoben. Weiterhin ist er jetzt als Mixin programmiert.

SL/Controller/CsvImport/CustomerVendor.pm
SL/Controller/CsvImport/Helper/Consistency.pm [new file with mode: 0644]
SL/Controller/CsvImport/Order.pm
SL/Helper/Csv/Consistency.pm [deleted file]

index 5f31614..9e0c4d5 100644 (file)
@@ -3,7 +3,7 @@ package SL::Controller::CsvImport::CustomerVendor;
 use strict;
 
 use SL::Helper::Csv;
-use SL::Helper::Csv::Consistency;
+use SL::Controller::CsvImport::Helper::Consistency;
 use SL::DB::Business;
 use SL::DB::CustomVariable;
 use SL::DB::CustomVariableConfig;
@@ -66,7 +66,7 @@ sub check_objects {
     $self->check_language($entry);
     $self->check_business($entry);
     $self->check_payment($entry);
-    SL::Helper::Csv::Consistency->check_currency($entry, take_default => 1);
+    $self->check_currency($entry, take_default => 1);
     $self->handle_cvars($entry);
 
     next if @{ $entry->{errors} };
diff --git a/SL/Controller/CsvImport/Helper/Consistency.pm b/SL/Controller/CsvImport/Helper/Consistency.pm
new file mode 100644 (file)
index 0000000..a666e6f
--- /dev/null
@@ -0,0 +1,69 @@
+package SL::Controller::CsvImport::Helper::Consistency;
+
+use strict;
+
+use SL::DB::Default;
+use SL::DB::Currency;
+
+use SL::Helper::Csv::Error;
+
+use parent qw(Exporter);
+our @EXPORT = qw(check_currency);
+
+#
+# public functions
+#
+
+sub check_currency {
+  my ($self, $entry, %params) = @_;
+
+  my $object = $entry->{object};
+
+  # Check whether or not currency ID is valid.
+  if ($object->currency_id && ! _currencies_by($self)->{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 = _currencies_by($self)->{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 and take_default is true.
+  $object->currency_id(_default_currency_id($self)) if !$object->currency_id and $params{take_default};
+
+  $entry->{raw_data}->{currency_id} = $object->currency_id;
+
+  return 1;
+}
+
+#
+# private functions
+#
+
+sub _currencies_by {
+  my ($self) = @_;
+
+  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
+}
+
+sub _all_currencies {
+  my ($self) = @_;
+
+  return SL::DB::Manager::Currency->get_all;
+}
+
+sub _default_currency_id {
+  my ($self) = @_;
+
+  return SL::DB::Default->get->currency_id;
+}
+
+1;
index 10c3433..7577ef0 100644 (file)
@@ -6,6 +6,7 @@ use strict;
 use List::MoreUtils qw(any);
 
 use SL::Helper::Csv;
+use SL::Controller::CsvImport::Helper::Consistency;
 use SL::DB::Order;
 use SL::DB::OrderItem;
 use SL::DB::Part;
@@ -324,7 +325,7 @@ sub handle_order {
   $self->check_project($entry, global => 1);
   $self->check_ct_shipto($entry);
   $self->check_taxzone($entry);
-  SL::Helper::Csv::Consistency->check_currency($entry, take_default => 0);
+  $self->check_currency($entry, take_default => 0);
 
   if ($vc_obj) {
     # copy from customer if not given
diff --git a/SL/Helper/Csv/Consistency.pm b/SL/Helper/Csv/Consistency.pm
deleted file mode 100644 (file)
index 5d52d14..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-package SL::Helper::Csv::Consistency;
-
-use strict;
-
-use SL::DB::Default;
-use SL::DB::Currency;
-
-use SL::Helper::Csv::Error;
-
-#
-# public functions
-#
-
-sub check_currency {
-  my ($self, $entry, %params) = @_;
-
-  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 and take_default is true.
-  $object->currency_id($self->_default_currency_id) if !$object->currency_id and $params{take_default};
-
-  $entry->{raw_data}->{currency_id} = $object->currency_id;
-
-  return 1;
-}
-
-#
-# private functions
-#
-
-sub _currencies_by {
-  my ($self) = @_;
-
-  return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->_all_currencies } } ) } qw(id name) };
-}
-
-sub _all_currencies {
-  my ($self) = @_;
-
-  return SL::DB::Manager::Currency->get_all;
-}
-
-sub _default_currency_id {
-  my ($self) = @_;
-
-  return SL::DB::Default->get->currency_id;
-}
-
-1;