Skip to content

Commit

Permalink
Added extra logics for offline VIES handler
Browse files Browse the repository at this point in the history
  • Loading branch information
NKoonen committed Dec 21, 2023
1 parent 5c72395 commit cd8a741
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions vatchecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected function getConfigForm()
'query' => [
['id'=>1,'name'=>"Always mark VAT as valid"],
['id'=>2,'name'=>"Always mark VAT as invalid"],
['id'=>3,'name'=>"Use previous address logic, if none a mark VAT valid"],
['id'=>3,'name'=>"Use previous address logic, if none mark VAT valid"],
['id'=>4,'name'=>"Use previous address logic, if none mark VAT invalid"]
],
'id' => 'id',
Expand Down Expand Up @@ -816,10 +816,8 @@ protected function checkVies( $countryCode, $vatNumber )

//$return['error'] = $this->l( $e->getMessage() );
$return['error'] = $this->l( 'EU VIES server not responding' );
$return['valid'] = $this->VIESOfflineLogicHander($params);

if ( Configuration::get( 'VATCHECKER_ALLOW_OFFLINE' ) ) {
$return['valid'] = null;
}

PrestaShopLogger::addLog( 'VAT check failed! (params: ' . implode( ', ', $params ) . ' , error: ' . $e->getMessage() . ')', ERROR_SEVERITY );
}
Expand All @@ -829,6 +827,56 @@ protected function checkVies( $countryCode, $vatNumber )
return $return;
}

public function VIESOfflineLogicHander($params)
{
switch (Configuration::get( 'VATCHECKER_ALLOW_OFFLINE' )) {
case 1:
return true;
break;
case 2:
return false;
break;
case 3:
$previous = $this->getPreviousValidation($params);
return (!empty($previous)) ? $previous->valid : true;
break;
case 4:
$previous = $this->getPreviousValidation($params);
return (!empty($previous)) ? $previous->valid : false;
break;
default:
return false;
}
}

/**
* Gets previous validation by countryId and Vatnumber
*
* @since 2.1.0
*
* @param string $vatNumber
*
* @return bool
*/
private function getPreviousValidation( $params )
{
$table = _DB_PREFIX_ . 'vatchecker';
$countryId = Country::getByIso( $params['countryCode'] );

$sql = "SELECT * FROM {$table}
WHERE id_country = {$countryId}
AND vat_number = '{$params['vatNumber']}'
";

$result = Db::getInstance()->executeS( $sql );
if ( ! $result ) {
return null;
}

// Only one result.
return reset( $result );
}

/**
* Check vat number format before calling VIES API.
*
Expand Down

0 comments on commit cd8a741

Please sign in to comment.