From 8fbb71e402a1ab11d1ed2ecb8d122b14f3b28abc Mon Sep 17 00:00:00 2001 From: Niclas Zimmermann Date: Mon, 2 Dec 2013 16:09:31 +0100 Subject: [PATCH] Auslagern von Konsistenz-Check in neue Helper-Klasse MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Es wurde mehrfach die sub check_currencies in den Csv-Importen kopiert. Dafür wurde jetzt eine neue Helper-Klasse geschrieben, wo solche Konsistenz- Prüfungen vorgenommen werden können. --- SL/Controller/CsvImport/Base.pm | 15 +-------- SL/Controller/CsvImport/CustomerVendor.pm | 41 ++--------------------- SL/Controller/CsvImport/Order.pm | 35 ++----------------- 3 files changed, 6 insertions(+), 85 deletions(-) diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index 4667677c6..a74acb66b 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -5,7 +5,6 @@ use strict; use List::MoreUtils qw(pairwise any); use SL::Helper::Csv; -use SL::DB::Currency; use SL::DB::Customer; use SL::DB::Language; use SL::DB::PaymentTerm; @@ -18,7 +17,7 @@ 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(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 all_vc vc_by) ], ); sub run { @@ -140,18 +139,6 @@ sub init_all_languages { return SL::DB::Manager::Language->get_all; } -sub init_all_currencies { - my ($self) = @_; - - return SL::DB::Manager::Currency->get_all; -} - -sub init_default_currency_id { - my ($self) = @_; - - return SL::DB::Default->get->currency_id; -} - sub init_payment_terms_by { my ($self) = @_; diff --git a/SL/Controller/CsvImport/CustomerVendor.pm b/SL/Controller/CsvImport/CustomerVendor.pm index 47964936e..5f316148f 100644 --- a/SL/Controller/CsvImport/CustomerVendor.pm +++ b/SL/Controller/CsvImport/CustomerVendor.pm @@ -3,6 +3,7 @@ package SL::Controller::CsvImport::CustomerVendor; use strict; use SL::Helper::Csv; +use SL::Helper::Csv::Consistency; use SL::DB::Business; use SL::DB::CustomVariable; use SL::DB::CustomVariableConfig; @@ -13,7 +14,7 @@ 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) ], ); sub init_table { @@ -44,12 +45,6 @@ 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) = @_; @@ -71,7 +66,7 @@ sub check_objects { $self->check_language($entry); $self->check_business($entry); $self->check_payment($entry); - $self->check_currency($entry); + SL::Helper::Csv::Consistency->check_currency($entry, take_default => 1); $self->handle_cvars($entry); next if @{ $entry->{errors} }; @@ -162,36 +157,6 @@ 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) = @_; diff --git a/SL/Controller/CsvImport/Order.pm b/SL/Controller/CsvImport/Order.pm index c8cb5b30c..10c3433b6 100644 --- a/SL/Controller/CsvImport/Order.pm +++ b/SL/Controller/CsvImport/Order.pm @@ -24,7 +24,7 @@ use parent qw(SL::Controller::CsvImport::BaseMulti); use Rose::Object::MakeMethods::Generic ( - 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by departments_by projects_by ct_shiptos_by taxzones_by price_factors_by pricegroups_by currencies_by) ], + 'scalar --get_set_init' => [ qw(settings languages_by parts_by contacts_by departments_by projects_by ct_shiptos_by taxzones_by price_factors_by pricegroups_by) ], ); @@ -244,12 +244,6 @@ sub init_pricegroups_by { return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $all_pricegroups } } ) } qw(id pricegroup) }; } -sub init_currencies_by { - my ($self) = @_; - - return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->all_currencies } } ) } qw(id name) }; -} - sub check_objects { my ($self) = @_; @@ -330,7 +324,7 @@ sub handle_order { $self->check_project($entry, global => 1); $self->check_ct_shipto($entry); $self->check_taxzone($entry); - $self->check_currency($entry); + SL::Helper::Csv::Consistency->check_currency($entry, take_default => 0); if ($vc_obj) { # copy from customer if not given @@ -649,31 +643,6 @@ sub check_pricegroup { 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); - } - - return 1; -} - sub add_items_to_order { my ($self) = @_; -- 2.20.1