Esempio n. 1
0
 /**
  * @return ErrorElements
  */
 public function validateNoTVATiers()
 {
     if (method_exists($this->_object, 'getNoTVATiers')) {
         $value = $this->_object->getNoTVATiers();
         $field = 'no_TVA_tiers';
     } else {
         if (method_exists($this->_object, 'getCEE')) {
             $value = $this->_object->getCEE();
             $field = 'CEE';
         } else {
             return $this;
         }
     }
     if (!$value) {
         return $this;
     }
     //For V05, DEB EXPED, V09, there is an additional rule : "The VAT number must not start with FR".
     $class = explode('\\', get_class($this->_object));
     $class = end($class);
     $noFRcode = false;
     if (in_array($class, array('V05LIC', 'DEBExped', 'V09DES'))) {
         $noFRcode = true;
         if (preg_match('/^FR/', $value)) {
             $this->_errorElement->with($field)->addViolation('Pas de N°TVA commençant par FR pour ce type d\'opération.')->end();
             return $this;
         }
     }
     $doctrine = \AppKernel::getStaticContainer()->get('doctrine');
     $em = $doctrine->getManager();
     $emConfig = $em->getConfiguration();
     $emConfig->addCustomStringFunction('REPLACE', 'Application\\Sonata\\ClientOperationsBundle\\DQL\\ReplaceFunction');
     $numeros = $em->getRepository('ApplicationSonataClientBundle:NumeroTVA')->createQueryBuilder('o')->where("REPLACE(o.n_de_TVA, ' ', '') = '" . str_replace(' ', '', $value) . "'")->andWhere('o.client = ' . $this->_object->getClientId())->getQuery()->getResult();
     if (count($numeros) == 0) {
         $this->_errorElement->with($field)->addViolation('Le N° de TVA n\'est pas dans la liste des N° TVA des clients du client.')->end();
         return $this;
     }
     $validationDef = array('GB' => array(5, 9, 12), 'CZ' => array(8, 9, 10), 'DK' => array(8), 'FI' => array(8), 'IE' => array(8), 'LU' => array(8), 'HU' => array(8), 'MT' => array(8), 'SI' => array(8), 'LT' => array(9, 12), 'ES' => array(9), 'A/B' => array(8), 'DE' => array(9), 'AT' => array(9), 'BE' => array(9, 10), 'EL' => array(9), 'CY' => array(9), 'EE' => array(9), 'PT' => array(9), 'PL' => array(10), 'SK' => array(10), 'BG' => array(9, 10), 'RO' => array(2, 3, 4, 5, 6, 7, 8, 9, 10), 'FR' => array(11), 'IT' => array(11), 'LV' => array(11), 'HR' => array(11), 'NL' => array(12), 'SE' => array(12));
     if ($noFRcode === true) {
         unset($validationDef['FR']);
     }
     $value = str_replace(' ', '', $value);
     $key = substr($value, 0, 2);
     $trail = substr($value, 2);
     //trailing characters
     // for 'A/B' key
     if (strstr($key, '/')) {
         $key = substr($value, 0, 3);
         $trail = substr($value, 3);
         //trailing characters
     }
     $validated = function ($key, $trail) use($validationDef) {
         if (!array_key_exists($key, $validationDef)) {
             return false;
         }
         if (isset($validationDef[$key])) {
             $lengths = $validationDef[$key];
             if (!in_array(strlen($trail), $lengths)) {
                 return false;
             }
         }
         return true;
     };
     if (!$key || $validated($key, $trail) === false) {
         $this->_errorElement->with($field)->addViolation('Mauvais format de TVA.')->end();
     }
     return $this;
 }