From: Moritz Bunkus Date: Wed, 11 Mar 2020 10:57:15 +0000 (+0100) Subject: Kunden/Lieferanten: UStID-Nummern beim Speichern validieren X-Git-Tag: release-3.5.6.1~245^2~6 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=29a13714c18417c2c8c3e62055ac6d67d0993e65;p=kivitendo-erp.git Kunden/Lieferanten: UStID-Nummern beim Speichern validieren --- diff --git a/SL/DB/Customer.pm b/SL/DB/Customer.pm index 7d36338dd..71c762a5e 100644 --- a/SL/DB/Customer.pm +++ b/SL/DB/Customer.pm @@ -10,6 +10,7 @@ use SL::DB::MetaSetup::Customer; use SL::DB::Manager::Customer; use SL::DB::Helper::IBANValidation; use SL::DB::Helper::TransNumberGenerator; +use SL::DB::Helper::VATIDNrValidation; use SL::DB::Helper::CustomVariables ( module => 'CT', cvars_alias => 1, @@ -61,6 +62,7 @@ sub validate { my @errors; push @errors, $::locale->text('The customer name is missing.') if !$self->name; push @errors, $self->validate_ibans; + push @errors, $self->validate_vat_id_numbers; return @errors; } diff --git a/SL/DB/Helper/VATIDNrValidation.pm b/SL/DB/Helper/VATIDNrValidation.pm new file mode 100644 index 000000000..8e4047a7c --- /dev/null +++ b/SL/DB/Helper/VATIDNrValidation.pm @@ -0,0 +1,100 @@ +package SL::DB::Helper::VATIDNrValidation; + +use strict; + +use Carp; +use SL::Locale::String qw(t8); +use SL::VATIDNr; + +my $_validator; + +sub _validate { + my ($self, $attribute) = @_; + + my $number = SL::VATIDNr->clean($self->$attribute); + + return () unless length($number); + return () if SL::VATIDNr->validate($number); + return ($::locale->text("The VAT ID number '#1' is invalid.", $self->$attribute)); +} + +sub import { + my ($package, @attributes) = @_; + + my $caller_package = caller; + @attributes = qw(ustid) unless @attributes; + + no strict 'refs'; + + *{ $caller_package . '::validate_vat_id_numbers' } = sub { + my ($self) = @_; + + return map { SL::DB::Helper::VATIDNrValidation::_validate($self, $_) } @attributes; + }; +} + +1; + +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::Helper::VATIDNrValidation - Mixin for validating VAT ID number attributes + +=head1 SYNOPSIS + + package SL::DB::SomeObject; + use SL::DB::Helper::VATIDNrValidation [ ATTRIBUTES ]; + + sub validate { + my ($self) = @_; + + my @errors; + … + push @errors, $self->validate_vat_id_numbers; + + return @errors; + } + +This mixin provides a function C that returns +a list of error messages, one for each attribute that fails the VAT ID +number validation. If all attributes are valid or empty then an empty +list is returned. + +The names of attributes to check can be given as an import list to the +mixin package. If no attributes are given the single attribute C +is used. + +=head1 FUNCTIONS + +=over 4 + +=item C + +This function iterates over all configured attributes and validates +their content according to how VAT ID numbers are supposed to be +formatted in the European Union (or the enterprise identification +numbers in Switzerland). An attribute that is undefined, empty or +consists solely of whitespace is considered valid, too. + +The function returns a list of human-readable error messages suitable +for use in a general C function (see SYNOPSIS). For each +attribute failing the check the list will include one error message. + +If all attributes are valid then an empty list is returned. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut diff --git a/SL/DB/Vendor.pm b/SL/DB/Vendor.pm index 1c1bffda1..6844b6314 100644 --- a/SL/DB/Vendor.pm +++ b/SL/DB/Vendor.pm @@ -10,6 +10,7 @@ use SL::DB::MetaSetup::Vendor; use SL::DB::Manager::Vendor; use SL::DB::Helper::IBANValidation; use SL::DB::Helper::TransNumberGenerator; +use SL::DB::Helper::VATIDNrValidation; use SL::DB::Helper::CustomVariables ( module => 'CT', cvars_alias => 1, @@ -60,6 +61,7 @@ sub validate { my @errors; push @errors, $::locale->text('The vendor name is missing.') if !$self->name; push @errors, $self->validate_ibans; + push @errors, $self->validate_vat_id_numbers; return @errors; } diff --git a/locale/de/all b/locale/de/all index 675fa1ffb..9ed72f372 100755 --- a/locale/de/all +++ b/locale/de/all @@ -3197,6 +3197,7 @@ $self->{texts} = { 'The SQL query can be parameterized with variables named as follows: <%name%>.' => 'Die SQL-Abfrage kann mittels Variablen wie folgt parametrisiert werden: <%Variablenname%>.', 'The SQL query does not contain any parameter that need to be configured.' => 'Die SQL-Abfrage enthält keine Parameter, die angegeben werden müssten.', 'The URL is missing.' => 'URL fehlt', + 'The VAT ID number \'#1\' is invalid.' => 'Die UStID-Nummer »#1« ist ungültig.', 'The VAT registration number is missing in the client configuration.' => 'Die Umsatzsteuer-ID-Nummer fehlt in der Mandantenkonfiguration.', 'The WebDAV feature has been used.' => 'Das WebDAV-Feature wurde benutzt.', 'The XMP metadata does not declare the ZUGFeRD data.' => 'Die XMP-Metadaten enthalten keine ZUGFeRD-Deklaration.',