+sub _check_ustid_taxnumber_unique {
+ my ($self) = @_;
+
+ my %cfg;
+ if ($self->is_vendor()) {
+ %cfg = (should_check => $::instance_conf->get_vendor_ustid_taxnummer_unique,
+ manager_class => 'SL::DB::Manager::Vendor',
+ err_ustid => t8('A vendor with the same VAT ID already exists.'),
+ err_taxnumber => t8('A vendor with the same taxnumber already exists.'),
+ );
+
+ } elsif ($self->is_customer()) {
+ %cfg = (should_check => $::instance_conf->get_customer_ustid_taxnummer_unique,
+ manager_class => 'SL::DB::Manager::Customer',
+ err_ustid => t8('A customer with the same VAT ID already exists.'),
+ err_taxnumber => t8('A customer with the same taxnumber already exists.'),
+ );
+
+ } else {
+ return;
+ }
+
+ my @errors;
+
+ if ($cfg{should_check}) {
+ my $do_clean_taxnumber = sub { my $n = $_[0]; $n //= ''; $n =~ s{[[:space:].-]+}{}g; return $n};
+
+ my $clean_ustid = SL::VATIDNr->clean($self->{cv}->ustid);
+ my $clean_taxnumber = $do_clean_taxnumber->($self->{cv}->taxnumber);
+
+ if (!($clean_ustid || $clean_taxnumber)) {
+ return t8('VAT ID and/or taxnumber must be given.');
+
+ } else {
+ my $clean_number = $clean_ustid;
+ if ($clean_number) {
+ my $entries = $cfg{manager_class}->get_all(query => ['!id' => $self->{cv}->id, '!ustid' => undef, '!ustid' => ''], select => ['ustid'], distinct => 1);
+ if (any { $clean_number eq SL::VATIDNr->clean($_->ustid) } @$entries) {
+ push @errors, $cfg{err_ustid};
+ }
+ }
+
+ $clean_number = $clean_taxnumber;
+ if ($clean_number) {
+ my $entries = $cfg{manager_class}->get_all(query => ['!id' => $self->{cv}->id, '!taxnumber' => undef, '!taxnumber' => ''], select => ['taxnumber'], distinct => 1);
+ if (any { $clean_number eq $do_clean_taxnumber->($_->taxnumber) } @$entries) {
+ push @errors, $cfg{err_taxnumber};
+ }
+ }
+ }
+ }
+
+ return join "\n", @errors if @errors;
+ return;
+}
+