diff --git a/console/validate.php b/console/validate.php
new file mode 100644
index 0000000..8de6b89
--- /dev/null
+++ b/console/validate.php
@@ -0,0 +1,60 @@
+
+ */
+
+namespace Skeleton\Console\Command;
+
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Vat_Check_Validate extends \Skeleton\Console\Command {
+
+ /**
+ * Configure the Create command
+ *
+ * @access protected
+ */
+ protected function configure() {
+ $this->setName('vat-check:validate');
+ $this->setDescription('Validate a specific VAT number and country vat code');
+ $this->addArgument('vat_code', InputArgument::REQUIRED, 'Country vat code');
+ $this->addArgument('vat_number', InputArgument::REQUIRED, 'VAT number');
+ $this->addOption('ignore_cache');
+ }
+
+ /**
+ * Execute the Command
+ *
+ * @access protected
+ * @param InputInterface $input
+ * @param OutputInterface $output
+ */
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ try {
+ $country = \Country::get_by_vat_code($input->getArgument('vat_code'));
+ } catch (\Exception $e) {
+ $output->writeln('' . $e->getMessage() . '');
+ return 1;
+ }
+
+ $ignore_cache = false;
+ if ($input->getOption('ignore_cache')) {
+ $ignore_cache = true;
+ }
+
+ $is_valid = \Skeleton\Vat\Check\Check::validate($input->getArgument('vat_number'), $country, $reason, $ignore_cache);
+ if ($is_valid) {
+ $output->writeln($input->getArgument('vat_code') . ' ' . $input->getArgument('vat_number') . " is valid \t" . ' ok');
+ } else {
+ $output->writeln($input->getArgument('vat_code') . ' ' . $input->getArgument('vat_number') . " is NOT valid (" . $reason . ") \t" . ' nok');
+ }
+
+ return 0;
+ }
+
+}
diff --git a/lib/Skeleton/Vat/Check/Check.php b/lib/Skeleton/Vat/Check/Check.php
index fb03808..c95fc4b 100644
--- a/lib/Skeleton/Vat/Check/Check.php
+++ b/lib/Skeleton/Vat/Check/Check.php
@@ -17,37 +17,53 @@ class Check {
* @access public
* @param string $vat_number
* @param Country $country
+ * @param boolean $ignore_cache
* @return boolean $valid
*/
- public static function validate($vat_number, \Country $country) {
+ public static function validate($vat_number, \Country $country, &$reason, $ignore_cache) {
// 1. Check syntax
if (!self::validate_syntax($vat_number, $country)) {
+ $reason = 'invalid syntax';
return false;
}
// 2. Check if the vat is a european vat-number
if ($country->european != 1) {
+ $reason = 'country is not european';
return true;
}
//3. Check if vat-number is in cache
- try {
- $vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
- if ($vat_cache->valid == 1) {
- return true;
- } else {
- return false;
- }
- } catch (\Exception $e) {}
+ if ($ignore_cache === false) {
+ try {
+ $vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
+ if ($vat_cache->valid == 1) {
+ $reason = 'from cache';
+ return true;
+ } else {
+ $reason = 'from cache';
+ return false;
+ }
+ } catch (\Exception $e) {}
+ }
//4. Check online (VIES-server)
$result = self::validate_online($vat_number, $country);
if ($result['save'] === true) {
+ try {
+ $vat_cache = Cache::get_by_vat_number_country($vat_number, $country);
+ $vat_cache->delete();
+ } catch (\Exception $e) {}
+
$vat_cache = new Cache();
$vat_cache->vat_number = $vat_number;
$vat_cache->country_id = $country->id;
$vat_cache->valid = $result['result'];
$vat_cache->save();
+ } elseif ($result['reachable'] === false) {
+ $reason = 'VIES service not reachable';
+ } else {
+ $reason = 'invalid vat number';
}
return $result['result'];