diff --git a/config.xml b/config.xml index 1df6ab9..e503195 100644 --- a/config.xml +++ b/config.xml @@ -2,10 +2,10 @@ vatchecker - + 1 1 - \ No newline at end of file + diff --git a/upgrade/upgrade-2.1.3.php b/upgrade/upgrade-2.1.3.php new file mode 100644 index 0000000..9682f6e --- /dev/null +++ b/upgrade/upgrade-2.1.3.php @@ -0,0 +1,16 @@ +Execute( + 'ALTER TABLE `' . _DB_PREFIX_ . 'vatchecker` ADD INDEX(`vat_number`);' + ); +} diff --git a/vatchecker.php b/vatchecker.php index 715999f..7c97f7b 100644 --- a/vatchecker.php +++ b/vatchecker.php @@ -94,7 +94,7 @@ public function __construct() { $this->name = 'vatchecker'; $this->tab = 'billing_invoicing'; - $this->version = '3.0.0'; + $this->version = '2.1.3'; $this->author = 'Inform-All & Keraweb'; $this->need_instance = 1; @@ -561,10 +561,16 @@ public function isValidVat( $address, $error = false ) */ $result = $this->getVatValidation( $address ); + $checkIfTimedOut = true; + // the cart list on the admin page recalculates prices, this can be many outdated carts at once, just use the db data instead of spamming VIES + if(isset($this->context->controller) && (get_class($this->context->controller) == 'AdminCartsController')){ + $checkIfTimedOut = false; + } + if ( $result ) { - // VIES API already ran successfully within 24 hours. - if ( strtotime( $result['date_modified'] ) > strtotime( '-1 day' ) ) { + // if we need to check, find if the VIES API already ran successfully within 24 hours. + if ( $checkIfTimedOut && (strtotime( $result['date_modified'] ) > strtotime( '-1 day' )) ) { $checkVat = [ 'valid' => (bool) $result['valid'], 'error' => '', @@ -632,9 +638,9 @@ private function getVatValidation( $address ) $table = _DB_PREFIX_ . 'vatchecker'; $sql = "SELECT * FROM {$table} - WHERE id_address = {$address->id} - AND id_country = {$address->id_country} - AND vat_number = '{$address->vat_number}' + WHERE id_address = ".((int)$address->id)." + AND id_country = ".((int)$address->id_country)." + AND vat_number = '".pSQL($address->vat_number)."' "; $result = Db::getInstance()->executeS( $sql ); @@ -698,10 +704,10 @@ private function setVatValidation( $record ) $values = []; foreach ( $record as $key => $value ) { $keys[ $key ] = "`{$key}`"; - if ( is_bool( $value ) ) { + if ( is_bool( $value ) || is_int($value) ) { $values[ $key ] = (int) $value; } else { - $values[ $key ] = "'{$value}'"; + $values[ $key ] = "'".pSQL($value)."'"; } } @@ -895,9 +901,8 @@ private function getPreviousValidation( $params ) $countryId = Country::getByIso( $params['countryCode'] ); $sql = "SELECT * FROM {$table} - WHERE id_country = {$countryId} - AND vat_number = '{$params['vatNumber']}' - "; + WHERE id_country = ".((int)$countryId)." + AND vat_number = '".pSQL($params['vatNumber'])."'"; $result = Db::getInstance()->executeS( $sql ); if ( ! $result ) {