X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/deb4d2dbb676d7d6f69dfe7815d6e0cb09bd4a44..53593baa211863fbf66540cf1bcc36c8fb37257f:/SL/DB/Helper/VATIDNrValidation.pm 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