Revert "Buchungsliste: Kontonamen werden nicht angezeigt."
[kivitendo-erp.git] / SL / Controller / CsvImport / Helper / Consistency.pm
1 package SL::Controller::CsvImport::Helper::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 use parent qw(Exporter);
11 our @EXPORT = qw(check_currency);
12
13 #
14 # public functions
15 #
16
17 sub check_currency {
18   my ($self, $entry, %params) = @_;
19
20   my $object = $entry->{object};
21
22   # Check whether or not currency ID is valid.
23   if ($object->currency_id && ! _currencies_by($self)->{id}->{ $object->currency_id }) {
24     push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
25     return 0;
26   }
27
28   # Map name to ID if given.
29   if (!$object->currency_id && $entry->{raw_data}->{currency}) {
30     my $currency = _currencies_by($self)->{name}->{  $entry->{raw_data}->{currency} };
31     if (!$currency) {
32       push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
33       return 0;
34     }
35
36     $object->currency_id($currency->id);
37   }
38
39   # Set default currency if none was given and take_default is true.
40   $object->currency_id(_default_currency_id($self)) if !$object->currency_id and $params{take_default};
41
42   $entry->{raw_data}->{currency_id} = $object->currency_id;
43
44   return 1;
45 }
46
47 #
48 # private functions
49 #
50
51 sub _currencies_by {
52   my ($self) = @_;
53
54   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
55 }
56
57 sub _all_currencies {
58   my ($self) = @_;
59
60   return SL::DB::Manager::Currency->get_all;
61 }
62
63 sub _default_currency_id {
64   my ($self) = @_;
65
66   return SL::DB::Default->get->currency_id;
67 }
68
69 1;