From 09619b3bc445ac9872ed0776293bc673f5b6f68d Mon Sep 17 00:00:00 2001 From: Niclas Zimmermann Date: Fri, 6 Dec 2013 12:08:34 +0100 Subject: [PATCH] =?utf8?q?Helper-Klasse=20f=C3=BCr=20Konsistenz-Checks?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Habe ich in Commit 8fbb71e402a1ab11d1ed2ecb8d122b14f3b28abc ver- gessen mit zu committen. --- SL/Helper/Csv/Consistency.pm | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 SL/Helper/Csv/Consistency.pm diff --git a/SL/Helper/Csv/Consistency.pm b/SL/Helper/Csv/Consistency.pm new file mode 100644 index 000000000..5d52d14a7 --- /dev/null +++ b/SL/Helper/Csv/Consistency.pm @@ -0,0 +1,66 @@ +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; -- 2.20.1