CsvImport: Kunden - payment und andere Daten aktualisierbar gemacht
[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     # register currency_id for method copying later
39     $self->clone_methods->{currency_id} = 1;
40   }
41
42   # Set default currency if none was given and take_default is true.
43   $object->currency_id(_default_currency_id($self)) if !$object->currency_id and $params{take_default};
44
45   $entry->{raw_data}->{currency_id} = $object->currency_id;
46
47   return 1;
48 }
49
50 #
51 # private functions
52 #
53
54 sub _currencies_by {
55   my ($self) = @_;
56
57   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
58 }
59
60 sub _all_currencies {
61   my ($self) = @_;
62
63   return SL::DB::Manager::Currency->get_all;
64 }
65
66 sub _default_currency_id {
67   my ($self) = @_;
68
69   return SL::DB::Default->get->currency_id;
70 }
71
72 1;