epic-ts
[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 use SL::DB::TaxZone;
8
9 use SL::Helper::Csv::Error;
10
11 use parent qw(Exporter);
12 our @EXPORT = qw(check_currency check_taxzone);
13
14 #
15 # public functions
16 #
17
18 sub check_currency {
19   my ($self, $entry, %params) = @_;
20
21   my $object = $entry->{object};
22
23   # Check whether or not currency ID is valid.
24   if ($object->currency_id && ! _currencies_by($self)->{id}->{ $object->currency_id }) {
25     push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
26     return 0;
27   }
28
29   # Map name to ID if given.
30   if (!$object->currency_id && $entry->{raw_data}->{currency}) {
31     my $currency = _currencies_by($self)->{name}->{  $entry->{raw_data}->{currency} };
32     if (!$currency) {
33       push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
34       return 0;
35     }
36
37     $object->currency_id($currency->id);
38
39     # register currency_id for method copying later
40     $self->clone_methods->{currency_id} = 1;
41   }
42
43   # Set default currency if none was given and take_default is true.
44   $object->currency_id(_default_currency_id($self)) if !$object->currency_id and $params{take_default};
45
46   $entry->{raw_data}->{currency_id} = $object->currency_id;
47
48   return 1;
49 }
50
51 sub check_taxzone {
52   my ($self, $entry, %params) = @_;
53
54   my $object = $entry->{object};
55
56   # Check whether the CSV contains the parameters taxzone_id or taxzone, and
57   # check them for validity.
58   # If one of them was given, but is invalid, return an error
59
60   # If neither was given:
61   # a) if param take_default was set, use the taxzone_id from the profile
62   #    (customer/vendor import)
63   # b) if param take_default was not set, do nothing, return without error, and
64   #    taxzone_id may be set later by other means (order import uses cv settings)
65
66
67   # if $object->taxzone_id is defined (from CSV line), check if it is valid
68   if ($object->taxzone_id && ! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
69     push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
70     return 0;
71   }
72
73   # if there was no taxzone_id in CSV, but a taxzone entry, check if it is a
74   # valid taxzone and set the id
75   if (!$object->taxzone_id && $entry->{raw_data}->{taxzone}) {
76     my $taxzone = _taxzones_by($self)->{description}->{ $entry->{raw_data}->{taxzone} };
77     if (!$taxzone) {
78       push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
79       return 0;
80     }
81
82     $object->taxzone_id($taxzone->id);
83   }
84
85   # The take_default option should only be used for the customer/vendor case,
86   # as the default for imported orders is the taxzone according to the customer
87   # or vendor
88   # if neither taxzone_id nor taxzone were defined, use the default taxzone as
89   # defined from the import settings (a default/fallback taxzone that is to be
90   # used will always be selected)
91
92   if (!$object->taxzone_id && $params{take_default}) {
93     # my $default_id = $self->settings->{'default_taxzone'};
94     my $default_id = $self->controller->profile->get('default_taxzone');
95     $object->taxzone_id($default_id);
96     # check if default taxzone_id is valid just to be sure
97     if (! _taxzones_by($self)->{id}->{ $object->taxzone_id }) {
98       push @{ $entry->{errors} }, $::locale->text('Error with default taxzone');
99       return 0;
100     };
101   };
102
103   # for the order import at this stage $object->taxzone_id may still not be
104   # defined, in this case the customer/vendor taxzone will be used.
105
106   return 1;
107 }
108
109 #
110 # private functions
111 #
112
113 sub _currencies_by {
114   my ($self) = @_;
115
116   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
117 }
118
119 sub _all_currencies {
120   my ($self) = @_;
121
122   return SL::DB::Manager::Currency->get_all;
123 }
124
125 sub _default_currency_id {
126   my ($self) = @_;
127
128   return SL::DB::Default->get->currency_id;
129 }
130
131 sub _taxzones_by {
132   my ($self) = @_;
133
134   return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_taxzones($self) } } ) } qw(id description) };
135 }
136
137 sub _all_taxzones {
138   my ($self) = @_;
139
140   return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);
141 }
142
143 1;