1010use Craft ;
1111use craft \base \Component ;
1212use craft \commerce \base \AdjusterInterface ;
13+ use craft \commerce \base \TaxIdValidatorInterface ;
1314use craft \commerce \elements \Order ;
1415use craft \commerce \helpers \Currency ;
1516use craft \commerce \models \OrderAdjustment ;
1617use craft \commerce \models \TaxAddressZone ;
1718use craft \commerce \models \TaxRate ;
1819use craft \commerce \Plugin ;
1920use craft \commerce \records \TaxRate as TaxRateRecord ;
21+ use craft \commerce \services \Taxes ;
22+ use craft \commerce \taxidvalidators \EuVatIdValidator ;
2023use craft \elements \Address ;
2124use DvK \Vat \Validator ;
2225use Exception ;
@@ -35,11 +38,6 @@ class Tax extends Component implements AdjusterInterface
3538{
3639 public const ADJUSTMENT_TYPE = 'tax ' ;
3740
38- /**
39- * @var Validator|null
40- */
41- private ?Validator $ _vatValidator = null ;
42-
4341 /**
4442 * @var Order
4543 */
@@ -118,17 +116,17 @@ private function _adjustInternal(): array
118116 private function _getAdjustments (TaxRate $ taxRate ): array
119117 {
120118 $ adjustments = [];
121- $ hasValidEuVatId = false ;
119+ $ hasValidTaxId = false ;
122120
123121 $ zoneMatches = $ taxRate ->getIsEverywhere () || ($ taxRate ->getTaxZone () && $ this ->_matchAddress ($ taxRate ->getTaxZone ()));
124122
125- if ($ zoneMatches && $ taxRate ->isVat ) {
126- $ hasValidEuVatId = $ this ->organizationTaxId ( );
123+ if ($ zoneMatches && $ taxRate ->hasTaxIdValidators () ) {
124+ $ hasValidTaxId = $ this ->organizationTaxIdIsValidTaxId ( $ taxRate -> getSelectedEnabledTaxIdValidators () );
127125 }
128126
129127 $ removeIncluded = (!$ zoneMatches && $ taxRate ->removeIncluded );
130- $ removeDueToVat = ($ zoneMatches && $ hasValidEuVatId && $ taxRate ->removeVatIncluded );
131- if ($ removeIncluded || $ removeDueToVat ) {
128+ $ removeDueToVatId = ($ zoneMatches && $ hasValidTaxId && $ taxRate ->removeVatIncluded );
129+ if ($ removeIncluded || $ removeDueToVatId ) {
132130
133131 // Is this an order level tax rate?
134132 if (in_array ($ taxRate ->taxable , TaxRateRecord::ORDER_TAXABALES , false )) {
@@ -195,7 +193,7 @@ private function _getAdjustments(TaxRate $taxRate): array
195193 return $ adjustments ;
196194 }
197195
198- if (!$ zoneMatches || ($ taxRate ->isVat && $ hasValidEuVatId )) {
196+ if (!$ zoneMatches || ($ taxRate ->hasTaxIdValidators () && $ hasValidTaxId )) {
199197 return [];
200198 }
201199
@@ -323,7 +321,7 @@ private function _matchAddress(TaxAddressZone $zone): bool
323321 /**
324322 * @return bool
325323 */
326- private function organizationTaxId ( ): bool
324+ private function organizationTaxIdIsValidTaxId ( array $ validators ): bool
327325 {
328326 if (!$ this ->_address ) {
329327 return false ;
@@ -340,7 +338,7 @@ private function organizationTaxId(): bool
340338
341339 // If we do not have a valid VAT ID in cache, see if we can get one from the API
342340 if (!$ validOrganizationTaxId ) {
343- $ validOrganizationTaxId = $ this ->validateVatNumber ($ this ->_address ->organizationTaxId );
341+ $ validOrganizationTaxId = $ this ->validateTaxIdNumber ($ this ->_address ->organizationTaxId , $ validators );
344342 }
345343
346344 if ($ validOrganizationTaxId ) {
@@ -355,25 +353,34 @@ private function organizationTaxId(): bool
355353 /**
356354 * @param string $businessVatId
357355 * @return bool
356+ * @deprecated in 4.8.0. Use `validateTaxIdNumber()` instead, passing the validators you want to check the ID with.
358357 */
359358 protected function validateVatNumber (string $ businessVatId ): bool
359+ {
360+ $ oldValidator = [new EuVatIdValidator ()];
361+ return $ this ->validateTaxIdNumber ($ businessVatId , $ oldValidator );
362+ }
363+
364+ /**
365+ * @param string $organizationTaxId
366+ * @param TaxIdValidatorInterface[] $validators
367+ * @return bool
368+ */
369+ protected function validateTaxIdNumber (string $ organizationTaxId , array $ validators = []): bool
360370 {
361371 try {
362- return $ this ->_getVatValidator ()->validate ($ businessVatId );
372+ foreach ($ validators as $ validator ) {
373+ if ($ validator ->validate ($ organizationTaxId )) {
374+ return true ;
375+ }
376+ }
363377 } catch (Exception $ e ) {
364378 Craft::error ('Communication with VAT API failed: ' . $ e ->getMessage (), __METHOD__ );
365379
366380 return false ;
367381 }
368- }
369-
370- private function _getVatValidator (): Validator
371- {
372- if ($ this ->_vatValidator === null ) {
373- $ this ->_vatValidator = new Validator ();
374- }
375382
376- return $ this -> _vatValidator ;
383+ return false ;
377384 }
378385
379386 private function _createAdjustment (TaxRate $ rate ): OrderAdjustment
0 commit comments