setVersion($version); $this->setCompile($compile); //Charger les Tabs - ou pas //Get default database adapter //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter(); //Charger les Rules (dans l'ordre) $this->RulesVORP = include realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'RulesVorp-'.$this->version.'.php'; $this->RulesVORD = include realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'RulesVord-'.$this->version.'.php'; $this->RulesPO = include realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'RulesPo-'.$this->version.'.php'; if ($this->debug) file_put_contents('sfr.log', "DEBUT\n"); } public function evaluate($siren) { Zend_Date::setOptions(array('extend_month' => true)); $situationJuridique = ''; $classInsee = new MInsee(); $classInsee->AnnoncesInMemory = true; //Vérifier la présence d'un calcul dans la table scores_surveillances $survM = new Application_Model_JoScoresSurveillance(); $sql = $survM->select()->where('siren=?', $siren); $result = $survM->fetchRow($sql); if ($result !== null ) { //Indiscore - aller chercher scores dans l'historique moins de 3 mois $dateIndiscore = new Zend_Date($result->indiScoreDate, 'yyyy-MM-dd'); $dateUpdate = new Zend_Date(substr($result->dateUpdate,0,10), 'yyyy-MM-dd'); $dateNow = new Zend_Date(); $diffIndiscore = $dateNow->sub($dateIndiscore); $measureIndiscore = new Zend_Measure_Time($diffIndiscore->toValue(), Zend_Measure_Time::SECOND); $measureIndiscore->convertTo(Zend_Measure_Time::MONTH); $dateNow = new Zend_Date(); $diffUpdate = $dateNow->sub($dateUpdate); $measureUpdate = new Zend_Measure_Time($diffUpdate->toValue(), Zend_Measure_Time::SECOND); $measureUpdate->convertTo(Zend_Measure_Time::MONTH); if ($this->debug) file_put_contents('sfr.log', "INDISCORE DIFF = ". $measureIndiscore->getValue() . ' ou ' . $measureUpdate->getValue() . "\n", FILE_APPEND); if ( $measureIndiscore->getValue() <= 3 || $measureUpdate->getValue() <= 3 ) { $this->ValIndiscore = $result->indiScore20; $this->dateCalculIndiscore = $result->indiScoreDate; } //Définition des marqueurs $this->ValNAF = $result->naf; $this->ValFJ = $result->CJ; $this->ValInseeActif = $result->actif; if ( !empty($result->dateCrea) && $result->dateCrea!='0000-00-00 00:00:00' ) { $dateC = new Zend_Date($result->dateCrea, 'yyyy-MM-dd'); $dateNow = new Zend_Date(); $difference = $dateNow->sub($dateC); $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND); $measure->convertTo(Zend_Measure_Time::MONTH); $this->ValInseeAge = $measure->getValue(); } elseif ( !empty($result->dateImmat) && $result->dateImmat!='0000-00-00 00:00:00' ) { $dateC = new Zend_Date($result->dateImmat, 'yyyy-MM-dd'); $dateNow = new Zend_Date(); $difference = $dateNow->sub($dateC); $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND); $measure->convertTo(Zend_Measure_Time::MONTH); $this->ValInseeAge = $measure->getValue(); } $situationJuridique = $result->procol; } //Aucun résultat, faire le calcul || donnée manquante if ( $result === null ) { //Identite - @todo : Gérer les erreurs techniques $identite = $classInsee->getIdentiteEntreprise($siren, 0, 0, false, false); //Naf de l'entreprise $this->ValNAF = $identite['NafEnt']; //Forme juridique de l'entreprise $this->ValFJ = $identite['FJ']; //insee /** * Effectif de l'entreprise * 1 - Chiffre au bilan * 2 - Info insee Identite::Effectif à la centaine approché */ $this->ValEffectif = $identite['Effectif']; $bilanAgeMax = 3*12; //Age maximum du dernier bilan en mois if (!empty($identite['bilanDate']) && intval($identite['bilanYP'])>0 ) { $dateBilan = new Zend_Date($identite['bilanDate'], 'yyyyMMdd'); $dateNow = new Zend_Date(); $difference = $dateNow->sub($dateBilan); $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND); $measure->convertTo(Zend_Measure_Time::MONTH); $nbMonth = $measure->getValue(); if ( $nbMonth < $bilanAgeMax ) { $this->ValEffectif = $identite['bilanYP']; } if ($this->debug) file_put_contents('sfr.log', "Bilan : nbMonth = ".$nbMonth."\n", FILE_APPEND); } if ($this->debug) file_put_contents('sfr.log', "ValEffectif = ".$this->ValEffectif."\n", FILE_APPEND); //Entreprise Active à l'insee $this->ValInseeActif = $identite['Actif']; //Age de l'entreprise en mois $this->ValInseeAge = null; //Date création Insee (ATTENTION vide) / Date immat RNCS if ( !empty($identite['DateCreaEn']) && $identite['DateCreaEn']!='0000-00-00 00:00:00' ) { $dateC = new Zend_Date($identite['DateCreaEn'], 'yyyy-MM-dd'); $dateNow = new Zend_Date(); $difference = $dateNow->sub($dateC); $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND); $measure->convertTo(Zend_Measure_Time::MONTH); $this->ValInseeAge = $measure->getValue(); } elseif ( !empty($identite['dateImmat']) && $identite['dateImmat']!='0000-00-00 00:00:00' ) { $dateC = new Zend_Date($identite['dateImmat'], 'yyyy-MM-dd'); $dateNow = new Zend_Date(); $difference = $dateNow->sub($dateC); $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND); $measure->convertTo(Zend_Measure_Time::MONTH); $this->ValInseeAge = $measure->getValue(); } if ($this->debug) file_put_contents('sfr.log', "ValInseeAge = ".$this->ValInseeAge."\n", FILE_APPEND); $situationJuridique = $identite['SituationJuridique']; } //Entreprise coté au CAC $this->ValIsCAC = 0; require_once 'Metier/partenaires/classMLiens2.php'; $lienM = new MLiens2($siren, 'siren'); if ( $lienM->isInGroupeCAC40() ) { $this->ValIsCAC = 1; } if ($this->debug) file_put_contents('sfr.log', "CAC40 = ".$this->ValIsCAC."\n", FILE_APPEND); //GetAnnoncesLegales //Si procédure collective ou autre if ( $identite['SituationJuridique']!='' ) { $this->ValRJ = 0; //Liste des codes - 1200 $retRJ = $classInsee->getAnnoncesLegales($siren, 0, array('1200','1201','1202','1203','1204','1205','1206','1207','1208','1209','1210','1211','1212','1214','1215','1216')); if (count($retRJ)>0) { $this->ValRJ = 1; } if ($this->debug) file_put_contents('sfr.log', "RJ = ".$this->ValRJ."\n", FILE_APPEND); $this->ValSV = 0; //Liste des codes - 1100 $retSV = $classInsee->getAnnoncesLegales($siren, 0, array('1100','1101','1102','1110','1119')); if (count($retSV)>0) { $this->ValSV = 1; } if ($this->debug) file_put_contents('sfr.log', "SV = ".$this->ValSV."\n", FILE_APPEND); $this->ValLJ = 0; //Liste des codes - 1300 $retLJ = $classInsee->getAnnoncesLegales($siren, 0, array('1300','1301','1302', '1303', '1304', '1305', '1306', '1307', '1308', '1309', '13010', '1311', '1312', '1313', '1346')); if (count($retLJ)>0) { $this->ValLJ = 1; } if ($this->debug) file_put_contents('sfr.log', "LJ = ".$this->ValLJ."\n", FILE_APPEND); } if ( $this->ValIndiscore === null ) { $indiscore = calculIndiScore($siren, '', false, 0); // @todo : Gérer les erreurs techniques $this->ValIndiscore = $indiscore['Indiscore20']; $this->dateCalculIndiscore = date('Y-m-d'); } if ($this->debug) file_put_contents('sfr.log', "INDISCORE = ".$this->ValIndiscore."\n", FILE_APPEND); //Set ValContratAge if ( $this->ValContratDate !== null ) { $dateContrat = new Zend_Date($this->ValContratDate, 'yyyyMMdd'); $dateNow = new Zend_Date(); $diff = $dateNow->sub($dateContrat); $measure = new Zend_Measure_Time($diff->toValue(), Zend_Measure_Time::SECOND); $measure->convertTo(Zend_Measure_Time::MONTH); $this->ValContratAge = $measure->getValue(); } //Rules $ruleType = array( 'VORP' , 'VORD' , 'PO'); foreach ( $ruleType as $type ) { $this->rules($type); } } /** * Execution des règles * Si les conditions contenu dans la regle * * * @param string $type */ protected function rules($type) { $rules = $this->{'Rules'.$type}; foreach ( $rules as $rule ) { if ($this->debug) file_put_contents('sfr.log', "\nREGLE - ".$rule['name']."\n", FILE_APPEND); $all = false; if ( $rule['value'] == 'DEFINE' ) { $all = true; } $return = $this->params($rule['params'], $all); // Continue always if ( $rule['value'] == 'CONTINUE' || $rule['value'] == 'DEFINE' ) { continue; } //Continue elseif ( $return === false ){ continue; } // Stop elseif ( $rule['value'] == 'STOP' && $return === true ) { break; } //Set Value else { if( $type == 'PO' ) { if ( $this->displayPo ) { $this->ValPO = $this->paramValue($rule['value']); } } else { $this->RuleLabel = $rule['name']; $this->displayPo = intval($rule['po']); $this->ValFEU = $this->paramValue($rule['value']); if ($this->debug) file_put_contents('sfr.log', "VALIDATION : ValFEU = ".$this->ValFEU."\n", FILE_APPEND); $this->ValComment = $rule['comment']; } break; } } } /** * Traitement des conditions * - Si la condition est FAUSSE * => arrêt pour passer à la règle suivante * - Si la condition est VRAI * => passage à la condition suivante * @param array $conditions * - var : Nom de la variable * - type : Type du test de la condition * - value : Valeur à tester * - define : "var" à définir avec la "value" * @return boolean */ protected function params($conditions = array(), $all = false) { $test = false; if ( count($conditions) > 0 ) { foreach ( $conditions as $c ) { $test = $this->paramEval($c['var'], $c['type'], $c['value']); if ($this->debug) file_put_contents('sfr.log', "PARAM - RETURN = ".$test."\n", FILE_APPEND); if ( null === $test ) { // Oops ! Problem ! } // Stopper elseif ( $test === false && $all === false ) { break; // Continuer ou Stopper sur element define } elseif ( $test === true ) { if ( array_key_exists('define', $c) ) { $this->paramDefine($c['define']['var'], $c['define']['value']); } if ($all === true) { break; } } } } return $test; } /** * Transformation des variables * @param string $val * @return number|mixed|NULL */ protected function paramValue($val) { if ( is_numeric($val) ) { return (float) $val; } if ( is_string($val) && defined('self::'.$val) ) { return constant('self::'.$val); } if ( is_string($val) && is_array($this->{$val}) ) { return $this->{$val}; } return null; } /** * Evaluation de la condition * @param string $var * @param string $type * @param string $value * @return boolean|NULL */ protected function paramEval($var, $type, $value) { $valueReal = $this->paramValue($value); if ($this->debug) file_put_contents('sfr.log', "PARAM - ".$var." ".$type." ".$value." = ".$this->{'Val'.$var}."\n", FILE_APPEND); switch ($type) { case 'MIN': if ( $this->{'Val'.$var}!==null && $this->{'Val'.$var} > $valueReal ) { return true; } return false; break; case 'MAX': if ( $this->{'Val'.$var}!==null && $this->{'Val'.$var} < $valueReal ) { return true; } return false; break; case 'EGAL': if ( $this->{'Val'.$var} == $valueReal ) { return true; } return false; break; case 'LIST': if ( $valueReal !== null && in_array($this->{'Val'.$var}, $valueReal) ) { return true; } return false; break; } return null; } protected function _Indicateur($value) { switch($value) { case self::VERT: return 'VERT'; break; case self::ROUGE: return 'ROUGE'; break; case self::ORANGE: return 'ORANGE'; break; } } public function getIndicateur() { return $this->_Indicateur($this->ValFEU); } public function getPo() { return $this->ValPO; } /** * * @param string $name * @param string $val */ protected function paramDefine($name, $val) { $value = null; if ( is_numeric($val) ) { $value = (int) $val; } if ( is_string($val) && defined('self::'.$val) ) { $value = constant('self::'.$val); } if ($this->debug) file_put_contents('sfr.log', "SET VAR - Val".$name." = ".$value."\n", FILE_APPEND); $this->{'Val'.$name} = $value; } /** * */ public function getComment() { return $this->ValComment; } public function getDateCalculIndiscore() { return $this->dateCalculIndiscore; } public function getValDebug() { return array( 'ValIsCAC' => $this->ValIsCAC, 'ValTypeEntrep' => $this->ValTypeEntrep, 'ValIsAdmin' => $this->ValIsAdmin, 'ValIsEtranger' => $this->ValIsEtranger, 'ValEffectif' => $this->ValEffectif, 'ValNAF' => $this->ValNAF, 'ValFJ' => $this->ValFJ, 'ValRJ' => $this->ValRJ, 'ValLJ' => $this->ValLJ, 'ValSV' => $this->ValSV, 'ValIR' => $this->ValIR, 'ValIndiscore' => $this->ValIndiscore, 'ValInseeActif' => $this->ValInseeActif, 'ValInseeAge' => $this->ValInseeAge, 'ValEntrepRecente' => $this->ValEntrepRecente, 'ValContratAge' => $this->ValContratAge, 'dateCalculIndiscore' => $this->dateCalculIndiscore, 'Rule' => $this->RuleLabel, ); } public function setVal($name, $value) { if ( empty($value) ) { $this->{'Val'.$name} = self::UNDEFINE; } else { $this->{'Val'.$name} = $value; } } /** * */ protected function setTabs() { if ( $this->compile ) { } else { return $this->loadTabsFromDb(); } } /** * */ protected function loadTabsFromDb(){} /** * */ protected function setRules() { if ( $this->compile ) { $this->RulesVORp = include_once 'Metier/Sfr/RulesVORp-'.$this->version.'.php'; $this->RulesVORd = include_once 'Metier/Sfr/RulesVORd-'.$this->version.'.php'; } else { $this->RulesVORp = $this->loadRulesFromDb('VORp'); $this->RulesVORd = $this->loadRulesFromDb('VORd'); } } protected function loadRulesFromDb($type = null) { } protected function setCompile($compile = true) { $this->compile = $compile; } /** * Define static file and version * @param string $version */ protected function setVersion($version = null) { if ( null !== $version ) { $this->version = $version; } } }