diff --git a/library/Metier/Partenaires/MTva.php b/library/Metier/Partenaires/MTva.php index 4aad6b8c..07b82124 100644 --- a/library/Metier/Partenaires/MTva.php +++ b/library/Metier/Partenaires/MTva.php @@ -88,18 +88,17 @@ class Metier_Partenaires_MTva "siren=$this->siren", false, MYSQL_ASSOC); if (count($tab) > 0) { $tab = $info[0]; - if ($tab['cle'] !== null || ($tab['cle'] === null && - $tab['DateMAJ'] >= date('Ymd', mktime(0, 0, 0, date('m')-6, date('d'), date('Y'))))) { - $cle = $tab['cle']; - if ($cle == null) { - $attribue = false; - $cle = $this->cle; - } else { - $attribue = true; - } - $this->vatNumber = "FR".$cle.$this->siren; - $this->vatDefined = $attribue; - + $cle = $tab['cle']; + $dateRef = new DateTime(); + $dateRef->sub(new DateInterval('P6M')); + $dateMaj = DateTime::createFromFormat('Ymd', $tab['DateMAJ']); + $this->vatNumber = "FR".$cle.$this->siren; + if ($cle !== null) { + $this->vatDefined = true; + return true; + } elseif ($cle === null && $dateMaj >= $dateRef) { + $cle = $this->cle; + $this->vatDefined = false; return true; } } @@ -107,11 +106,10 @@ class Metier_Partenaires_MTva if ($this->remote) { return $this->getRemoteVAT(); } else { - $this->errnum = 999; - $this->errmsg = "Pas d'accès à la base TVA"; $this->vatNumber = "FR".$this->cle.$this->siren; $this->vatDefined = false; - + $this->errnum = 999; + $this->errmsg = "Pas d'accès à la base TVA"; return true; } @@ -146,13 +144,19 @@ class Metier_Partenaires_MTva $nbIndispo = 0; while (true) { $postData = array( - 'ms' => 'FR', - 'iso' => 'FR', - 'vat' => $this->cle.$this->siren, - 'BtnSubmitVat' => 'Verify' + 'memberStateCode' => "FR", + 'number' => $this->cle.$this->siren, + 'traderName' => "", + 'traderStreet' => "", + 'traderPostalCode' => "", + 'traderCity' => "", + 'requesterMemberStateCode' => "", + 'requesterNumber' => "", + 'action' => "check", + 'check' => "Vérifier", ); $tdeb = microtime(true); - $url = 'http://ec.europa.eu/taxation_customs/vies/viesquer.do'; + $url = 'http://ec.europa.eu/taxation_customs/vies/vatRequest.html'; $referer = 'http://ec.europa.eu/taxation_customs/vies/'; $page = getUrl($url, '', $postData, $referer, false, 'ec.europa.eu', '', 15); $duree = round(microtime(true)-$tdeb, 3); @@ -167,24 +171,24 @@ class Metier_Partenaires_MTva $code = $page['code'];// Code réponse Serveur $body = $page['body']; $header = $page['header']; - if (preg_match('/Yes, valid VAT number/i', $body)) { + if (preg_match('/Yes, valid VAT number/i', $body) || preg_match('/Oui, numéro de TVA valide/i', $body)) { $tabInsert = array( 'siren' => $this->siren, 'cle' => $this->cle, 'duree' => $duree ); - $tabUpdate = array( - 'cle' => $this->cle, - 'duree' => $duree - ); if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) { + $tabUpdate = array( + 'cle' => $this->cle, + 'duree' => $duree + ); if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) { Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($duree s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__); } } Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $$this->cle $siren ($duree s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__); $this->vatNumber = "FR".$this->cle.$this->siren; - $this->vatDefined=true; + $this->vatDefined = true; return true; } elseif (strpos($body, 'Service non disponible') > 0 || strpos($body, 'ponse trop long.') > 0) { $nbIndispo++; @@ -209,20 +213,20 @@ class Metier_Partenaires_MTva } Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren vérification non disponible ($duree s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__); $this->vatNumber = "FR".$this->cle.$this->siren; - $this->vatDefined=false; + $this->vatDefined = false; return true; } } else { $tabInsert = array( 'siren' => $this->siren, - 'cle' => 'NULL', - 'duree' => $duree - ); - $tabUpdate = array( - 'cle' => 'NULL', + 'cle' => 'NULL', 'duree' => $duree ); if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) { + $tabUpdate = array( + 'cle' => 'NULL', + 'duree' => $duree + ); if (!$this->iDb->update('sdv1.siren_tva', $tabUpdate, "siren=$this->siren")) { Metier_Util_Log::write('W', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren, impossible de MAJ la clef ($duree s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__); }