Helper-Klasse für Konsistenz-Checks
[kivitendo-erp.git] / SL / Helper / Csv / Consistency.pm
1 package SL::Helper::Csv::Consistency;
2
3 use strict;
4
5 use SL::DB::Default;
6 use SL::DB::Currency;
7
8 use SL::Helper::Csv::Error;
9
10 #
11 # public functions
12 #
13
14 sub check_currency {
15   my ($self, $entry, %params) = @_;
16
17   my $object = $entry->{object};
18
19   # Check whether or not currency ID is valid.
20   if ($object->currency_id && !$self->_currencies_by->{id}->{ $object->currency_id }) {
21     push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
22     return 0;
23   }
24
25   # Map name to ID if given.
26   if (!$object->currency_id && $entry->{raw_data}->{currency}) {
27     my $currency = $self->_currencies_by->{name}->{  $entry->{raw_data}->{currency} };
28     if (!$currency) {
29       push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
30       return 0;
31     }
32
33     $object->currency_id($currency->id);
34   }
35
36   # Set default currency if none was given and take_default is true.
37   $object->currency_id($self->_default_currency_id) if !$object->currency_id and $params{take_default};
38
39   $entry->{raw_data}->{currency_id} = $object->currency_id;
40
41   return 1;
42 }
43
44 #
45 # private functions
46 #
47
48 sub _currencies_by {
49   my ($self) = @_;
50
51   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ $self->_all_currencies } } ) } qw(id name) };
52 }
53
54 sub _all_currencies {
55   my ($self) = @_;
56
57   return SL::DB::Manager::Currency->get_all;
58 }
59
60 sub _default_currency_id {
61   my ($self) = @_;
62
63   return SL::DB::Default->get->currency_id;
64 }
65
66 1;