X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=sidebyside;f=SL%2FController%2FCsvImport%2FHelper%2FConsistency.pm;h=82ec56371d5b68c1c47550b0b07dc56f446b29e2;hb=deb4d2dbb676d7d6f69dfe7815d6e0cb09bd4a44;hp=a666e6fe58370811a61e33aeda1e5f1769db8739;hpb=e0e3eee4eb59b7d6fbe17fe9ad12167b6b8a388b;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Helper/Consistency.pm b/SL/Controller/CsvImport/Helper/Consistency.pm index a666e6fe5..82ec56371 100644 --- a/SL/Controller/CsvImport/Helper/Consistency.pm +++ b/SL/Controller/CsvImport/Helper/Consistency.pm @@ -4,11 +4,12 @@ use strict; use SL::DB::Default; use SL::DB::Currency; +use SL::DB::TaxZone; use SL::Helper::Csv::Error; use parent qw(Exporter); -our @EXPORT = qw(check_currency); +our @EXPORT = qw(check_currency check_taxzone); # # public functions @@ -34,6 +35,9 @@ sub check_currency { } $object->currency_id($currency->id); + + # register currency_id for method copying later + $self->clone_methods->{currency_id} = 1; } # Set default currency if none was given and take_default is true. @@ -44,6 +48,64 @@ sub check_currency { return 1; } +sub check_taxzone { + my ($self, $entry, %params) = @_; + + my $object = $entry->{object}; + + # Check whether the CSV contains the parameters taxzone_id or taxzone, and + # check them for validity. + # If one of them was given, but is invalid, return an error + + # If neither was given: + # a) if param take_default was set, use the taxzone_id from the profile + # (customer/vendor import) + # b) if param take_default was not set, do nothing, return without error, and + # taxzone_id may be set later by other means (order import uses cv settings) + + + # if $object->taxzone_id is defined (from CSV line), check if it is valid + if ($object->taxzone_id && ! _taxzones_by($self)->{id}->{ $object->taxzone_id }) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone'); + return 0; + } + + # if there was no taxzone_id in CSV, but a taxzone entry, check if it is a + # valid taxzone and set the id + if (!$object->taxzone_id && $entry->{raw_data}->{taxzone}) { + my $taxzone = _taxzones_by($self)->{description}->{ $entry->{raw_data}->{taxzone} }; + if (!$taxzone) { + push @{ $entry->{errors} }, $::locale->text('Error: Invalid tax zone'); + return 0; + } + + $object->taxzone_id($taxzone->id); + } + + # The take_default option should only be used for the customer/vendor case, + # as the default for imported orders is the taxzone according to the customer + # or vendor + # if neither taxzone_id nor taxzone were defined, use the default taxzone as + # defined from the import settings (a default/fallback taxzone that is to be + # used will always be selected) + + if (!$object->taxzone_id && $params{take_default}) { + # my $default_id = $self->settings->{'default_taxzone'}; + my $default_id = $self->controller->profile->get('default_taxzone'); + $object->taxzone_id($default_id); + # check if default taxzone_id is valid just to be sure + if (! _taxzones_by($self)->{id}->{ $object->taxzone_id }) { + push @{ $entry->{errors} }, $::locale->text('Error with default taxzone'); + return 0; + }; + }; + + # for the order import at this stage $object->taxzone_id may still not be + # defined, in this case the customer/vendor taxzone will be used. + + return 1; +} + # # private functions # @@ -66,4 +128,16 @@ sub _default_currency_id { return SL::DB::Default->get->currency_id; } +sub _taxzones_by { + my ($self) = @_; + + return { map { my $col = $_; ( $col => { map { ( $_->$col => $_ ) } @{ _all_taxzones($self) } } ) } qw(id description) }; +} + +sub _all_taxzones { + my ($self) = @_; + + return SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]); +} + 1;