- my $vendor = SL::DB::Manager::Vendor->find_by(ustid => $ustid);
- die t8("Please add a valid VAT-ID for this vendor: " . $vc) unless (ref $vendor eq 'SL::DB::Vendor');
+ my $vendor = SL::DB::Manager::Vendor->find_by(
+ ustid => $ustid,
+ or => [
+ obsolete => undef,
+ obsolete => 0,
+ ]);
+
+ if (!$vendor) {
+ # 1.2 If no vendor with the exact VAT ID number is found, the
+ # number might be stored slightly different in the database
+ # (e.g. with spaces breaking up groups of numbers). Iterate over
+ # all existing vendors with VAT ID numbers, normalize their
+ # representation and compare those.
+
+ my $vendors = SL::DB::Manager::Vendor->get_all(
+ where => [
+ '!ustid' => undef,
+ '!ustid' => '',
+ or => [
+ obsolete => undef,
+ obsolete => 0,
+ ],
+ ]);
+
+ foreach my $other_vendor (@{ $vendors }) {
+ next unless SL::VATIDNr->normalize($other_vendor->ustid) eq $ustid;
+
+ $vendor = $other_vendor;
+ last;
+ }
+ }
+
+ die t8("Please add a valid VAT-ID for this vendor: #1", $vc) unless (ref $vendor eq 'SL::DB::Vendor');