diff --git a/library/Metier/Insee/MInsee.php b/library/Metier/Insee/MInsee.php index ba774217..25ffe59f 100644 --- a/library/Metier/Insee/MInsee.php +++ b/library/Metier/Insee/MInsee.php @@ -2040,7 +2040,7 @@ class Metier_Insee_MInsee extends Metier_Insee_Table $tdeb = microtime(1); } - $iTva = new Metier_Partenaires_MTva($this->iDb); + $iTva = new Metier_Partenaires_MTva(); $iTva->setCompanyId($siren); $iTva->setRemote(); $iTva->getTVA(); diff --git a/library/Metier/Partenaires/MTva.php b/library/Metier/Partenaires/MTva.php index 92e441c5..2d8fcde1 100644 --- a/library/Metier/Partenaires/MTva.php +++ b/library/Metier/Partenaires/MTva.php @@ -21,7 +21,11 @@ class Metier_Partenaires_MTva */ protected $cle; - protected $iDb; + /** + * PDO Connection with Doctrine + * @var \Doctrine\DBAL\Connection + */ + protected $conn; /** * Remote Flag @@ -30,24 +34,12 @@ class Metier_Partenaires_MTva protected $remote = false; /** - * - * @param unknown $db + * TVA * @return boolean */ - public function __construct($db = null) + public function __construct() { - if ($this->siren * 1 < 000001000) { - $this->vatNumber = 'FR00000000000'; - $this->vatDefined = false; - - return false; - } - - if ($db === null) { - $this->iDb = new Metier_Util_Db(); - } else { - $this->iDb = $db; - } + $this->conn = Zend_Registry::get('doctrine'); } /** @@ -64,12 +56,6 @@ class Metier_Partenaires_MTva return false; } - if ($db === null) { - $this->iDb = new Metier_Util_Db(); - } else { - $this->iDb = $db; - } - $cleAlgo = $this->genereCleFr(); if ($cleAlgo < 10) { $this->cle = '0'.$cleAlgo; @@ -84,11 +70,16 @@ class Metier_Partenaires_MTva return false; } - $info = $this->iDb->select('sdv1.siren_tva', - "LPAD(cle,2,0) AS cle, DATE_FORMAT(dateMod,'%Y%m%d') as DateMAJ", - "siren=".$this->siren, false, MYSQL_ASSOC); - if (count($info) > 0) { - $tab = $info[0]; + $exist = false; + + $sql = "SELECT LPAD(cle,2,0) AS cle, DATE_FORMAT(dateMod,'%Y%m%d') as DateMAJ + FROM sdv1.siren_tva WHERE siren=:siren"; + $stmt = $this->conn->prepare($sql); + $stmt->bindValue('siren', $this->siren); + $stmt->execute(); + if ($stmt->rowCount() > 0) { + $exist = true; + $tab = $stmt->fetch(\PDO::FETCH_ASSOC); $cle = $tab['cle']; $dateRef = new DateTime(); $dateRef->sub(new DateInterval('P6M')); @@ -104,7 +95,7 @@ class Metier_Partenaires_MTva } if ($this->remote) { - return $this->getRemoteVAT(); + return $this->getRemoteVAT($exist); } else { $this->vatNumber = "FR".$this->cle.$this->siren; $this->vatDefined = false; @@ -137,9 +128,10 @@ class Metier_Partenaires_MTva /** * Get Data form website + * @param boolean $exist * @return boolean */ - protected function getRemoteVAT() + protected function getRemoteVAT($exist) { $url = 'http://ec.europa.eu/taxation_customs/vies/vatResponse.html'; @@ -167,21 +159,13 @@ class Metier_Partenaires_MTva $body = $response->getBody(); 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' => $time - ); - if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) { - $tabUpdate = array( - 'cle' => $this->cle, - 'duree' => $time - ); - 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 ($time s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__); - } + if ($exist) { + $this->conn->update('sdv1.siren_tva', array('cle' => $this->cle, + 'duree' => $time), array('siren' => $this->siren)); + } else { + $this->conn->insert('sdv1.siren_tva', array('siren' => $this->siren, + 'cle' => $this->cle, 'duree' => $time)); } - Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $siren ($time s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__); $this->vatNumber = "FR".$this->cle.$this->siren; $this->vatDefined = true; return true; @@ -191,21 +175,15 @@ class Metier_Partenaires_MTva Metier_Util_Log::write('I', "TVA ".$e->getMessage(), __LINE__, __FILE__, __FUNCTION__, __CLASS__); } - $tabInsert = array( - 'siren' => $this->siren, - 'cle' => 'NULL', - 'duree' => $time - ); - $tabUpdate = array( - 'cle' => 'NULL', - 'duree' => $time - ); - if (!$this->iDb->insert('sdv1.siren_tva', $tabInsert)) { - 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 ($time s) - ERREUR MySql n°". mysql_errno() .' : '. mysql_error(), __LINE__, __FILE__, __FUNCTION__, __CLASS__); - } + // Non disponible + if ($exist) { + $this->conn->update('sdv1.siren_tva', array('cle' => 'NULL', 'duree' => $time), + array('siren' => $this->siren)); + } else { + $this->conn->insert('sdv1.siren_tva', array('siren' => $this->siren, + 'cle' => 'NULL', 'duree' => $time)); } - Metier_Util_Log::write('I', "Siren $this->siren, numéro de TVA = FR $this->cle $this->siren vérification non disponible ($time s)", __LINE__, __FILE__, __FUNCTION__, __CLASS__); + $this->vatNumber = "FR".$this->cle.$this->siren; $this->vatDefined = false; return true; diff --git a/scripts/jobs/enrichissement.php b/scripts/jobs/enrichissement.php index bc0cd533..5a1a6df1 100644 --- a/scripts/jobs/enrichissement.php +++ b/scripts/jobs/enrichissement.php @@ -877,8 +877,7 @@ function nicSiegeData($siren, $nic, $values = false) function tvaData($siren, $nic, $values = false) { - global $iDb; - $iTva = new Metier_Partenaires_MTva($iDb); + $iTva = new Metier_Partenaires_MTva(); $iTva->setCompanyId($siren); $iTva->getTVA(); $tabData['tva'] = isset($iTva->vatNumber) ? $iTva->vatNumber : '';