1 package SL::Controller::CsvImport::Helper::Consistency;
9 use SL::Helper::Csv::Error;
11 use parent qw(Exporter);
12 our @EXPORT = qw(check_currency check_taxzone);
19 my ($self, $entry, %params) = @_;
21 my $object = $entry->{object};
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');
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} };
33 push @{ $entry->{errors} }, $::locale->text('Error: Invalid currency');
37 $object->currency_id($currency->id);
39 # register currency_id for method copying later
40 $self->clone_methods->{currency_id} = 1;
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};
46 $entry->{raw_data}->{currency_id} = $object->currency_id;
52 my ($self, $entry, %params) = @_;
54 my $object = $entry->{object};
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
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)
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');
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} };
78 push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone');
82 $object->taxzone_id($taxzone->id);
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
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)
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');
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.
116 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_currencies($self) } } ) } qw(id name) };
119 sub _all_currencies {
122 return SL::DB::Manager::Currency->get_all;
125 sub _default_currency_id {
128 return SL::DB::Default->get->currency_id;
134 return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_taxzones($self) } } ) } qw(id description) };
140 return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]);