Michael RICOIS be4fd9ada7 Typo
2016-11-10 16:51:44 +01:00

1005 lines
23 KiB
PHP

<?php
require_once 'Metier/Scores/MScores.php';
class Metier_Sfr_Scoring
{
/**
* Version of rules (for loading static files)
* @var double
*/
protected $version = '1.4';
/**
* Put in error when not calculated
* @var boolean
*/
public $notCalculateInError = false;
/**
* Force la récupération de l'identite et de toutes les valeurs
* @var boolean
*/
public $forceIdentite = false;
/**
* Debug mode
* @var boolean
*/
protected $debug = false;
/**
* True, to use static file for set vars otherwise read in the database
* @var boolean
*/
protected $compile = true;
/**
*
* @var Zend_Db_Adapter_Abstract
*/
protected $db = null;
/**
* Feu vert
* @var int
*/
const VERT = 3;
/**
* Feu orange
* @var int
*/
const ORANGE = 2;
/**
* Feu rouge
* @var int
*/
const ROUGE = 1;
/**
* CAC = Entreprise du CAC
* @var int
*/
const CAC = 4;
/**
* GE = Grande Entreprise
* @var int
*/
const GE = 3;
/**
* Petites et Moyennes Entreprise
* @var int
*/
const PME = 2;
/**
* Très Petites Entreprise
* @var int
*/
const TPE = 1;
/**
* Constante valeur UNDEFINE
* @var null
*/
const UNDEFINE = null;
/**
* Liste des NAF Administration
* @var array
*/
protected $TabAdminNaf = array(
'8411Z',
'8412Z',
'8413Z',
'8421Z',
'8422Z',
'8423Z',
'8424Z',
'8425Z',
'8430A',
'8430B',
'8430C',
);
/**
* Liste des FJ Administration
* @var array
*/
protected $TabAdminFj = array(
'4110',
'4120',
'4130',
'4140',
'4150',
'4160',
'7111',
'7112',
'7113',
'7120',
'7130',
'7150',
'7160',
'7171',
'7172',
'7179',
'7210',
'7220',
'7225',
'7229',
'7230',
'7312',
'7313',
'7314',
'7321',
'7322',
'7323',
'7331',
'7340',
'7341',
'7342',
'7343',
'7344',
'7345',
'7346',
'7347',
'7348',
'7349',
'7351',
'7352',
'7353',
'7354',
'7355',
'7356',
'7361',
'7362',
'7363',
'7364',
'7365',
'7366',
'7371',
'7372',
'7373',
'7378',
'7379',
'7381',
'7382',
'7383',
'7384',
'7385',
'7389',
'7410',
'7430',
'7450',
'7470',
'7490',
'8110',
'8120',
'8130',
'8140',
'8150',
'8160',
'8170',
'8190',
'8210',
'8250',
'8290',
'8310',
'8311',
'8510',
'8520',
);
/**
* Liste des NAFs société étrangère
* @var array
*/
protected $TabEtrangerNaf = array(
'9900Z'
);
/**
* Liste des FJ société étrangère
* @var array
*/
protected $TabEtrangerFj = array(
'3110',
'3120',
'3205',
'3210',
'3220',
'3290',
);
/**
* Liste des NAF clés
* @var array
*/
protected $TabNafSFR = array(
'4791A',
'4791B',
'4799B',
'6110Z',
'6120Z',
'6130Z',
'6190Z',
'7320Z',
);
/**
* Flag Entreprise dans le CAC40 ou dans un groupe du CAC40 (0|1)
* @var int
*/
protected $ValIsCAC = 0;
/**
* Type Entreprise (See contants CAC, GE, PME, TPE)
* @var int
*/
protected $ValTypeEntrep = null;
/**
* Flag Entreprise Administration (0|1) (see TabNafAdmin)
* @var int
*/
protected $ValIsAdmin = 0;
/**
* Flag Entreprise étrangère
* @var int
*/
protected $ValIsEtranger = 0;
/**
* Effectif de l'entreprise
* @var int
*/
protected $ValEffectif = null;
/**
* NAF de l'entreprise
* @var string
*/
protected $ValNAF = null;
/**
* FJ de l'entreprise
* @var string
*/
protected $ValFJ = null;
/**
* Flag RJ
* @var int
*/
protected $ValRJ = 0;
/**
* Flag LJ
* @var int
*/
protected $ValLJ = 0;
/**
* Flag SV
* @var int
*/
protected $ValSV = 0;
/**
* Valeur de l'indiscore
* @var int
*/
protected $ValIndiscore = null;
/**
* Flag Actif à l'INSEE
* @var int
*/
protected $ValInseeActif = null;
/**
* Age de l'entreprise en mois INSEE
* @var float
*/
protected $ValInseeAge = null;
/**
* Flag Entreprise Récente
* @var int
*/
protected $ValEntrepRecente = 0;
/**
* Date du plus vieux contrat actif depuis le fichier
* (format AAAAMMJJ)
* @var string
*/
protected $ValContratDate = null;
/**
* Age du plus vieux contrat en mois
* @var int
*/
protected $ValContratAge = null;
/**
* Valeur de l'Indice de Recouvrement
* @var float
*/
protected $ValIR = null;
/**
* Indicateur VERT, ORANGE, ROUGE
* @var int
*/
protected $ValFEU = null;
/**
* Prise d'ordre
* @var int
*/
protected $ValPO = null;
/**
* Flag to display PO
* @var int
*/
protected $displayPo = 0;
/**
* Commentaire
* @var string
*/
protected $ValComment = null;
/**
* Date de calcul du score
* @var string
*/
protected $ValDateCalculIndiscore = null;
/**
*
* @var unknown
*/
protected $ValDateUpdate = null;
/**
*
* @var unknown
*/
protected $ValDateCrea = null;
/**
*
* @var unknown
*/
protected $ValDateImmat = null;
/**
*
* @var unknown
*/
protected $ValSituationJuridique = '';
/**
* Label of rule use
* @var unknown
*/
protected $RuleLabel = null;
//Rules
protected $RulesVORP = array();
protected $RulesVORD = array();
protected $RulesPO = array();
/**
*
* @param string $version
* @param string $compile
*/
public function __construct( $version = null, $compile = true )
{
$this->setVersion($version);
$this->setCompile($compile);
//Charger les Tabs - ou pas
//Charger les Rules (dans l'ordre)
$this->RulesVORP = include realpath(dirname(__FILE__)) . '/RulesVorp-'.$this->version.'.php';
$this->RulesVORD = include realpath(dirname(__FILE__)) . '/RulesVord-'.$this->version.'.php';
$this->RulesPO = include realpath(dirname(__FILE__)) . '/RulesPo-'.$this->version.'.php';
if ($this->debug) file_put_contents('sfr.log', "\nDEBUT\n", FILE_APPEND);
}
public function evaluate($siren)
{
if ($this->debug) file_put_contents('sfr.log', "SIREN = $siren\n", FILE_APPEND);
//Set ValContratAge
if ( !empty($this->ValContratDate) ) {
$dateContrat = DateTime::createFromFormat('Ymd', $this->ValContratDate);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateContrat);
$this->ValContratAge = $interval->format('%y') * 12 + $interval->format('%m');
}
$classInsee = new Metier_Insee_MInsee();
//Values are already define ?
if ( $this->ValDateCalculIndiscore === null ) {
//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);
//No results
if ( $result === null && $this->notCalculateInError ) {
return false;
}
//No score
if ( intval($result->indiScoreDate) == 0 && $this->notCalculateInError ) {
return false;
}
if ( $result !== null ) {
//Indiscore - aller chercher scores dans l'historique moins de X mois
$dateUpdate = DateTime::createFromFormat('Y-m-d', substr($result->dateUpdate,0,10));
$dateNow = new DateTime();
$interval = $dateNow->diff($dateUpdate);
$diff = $interval->format('%y') * 12 + $interval->format('%m');
if ($this->debug) file_put_contents('sfr.log', "INDISCORE DIFF = " . $diff . "\n", FILE_APPEND);
if ( $diff <= 12 ) {
$this->ValIndiscore = $result->indiScore20;
$this->ValDateCalculIndiscore = $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 = DateTime::createFromFormat('Y-m-d', $result->dateCrea);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateC);
$this->ValInseeAge = $interval->format('%y') * 12 + $interval->format('%m');
} elseif ( !empty($result->dateImmat) && $result->dateImmat!='0000-00-00 00:00:00' ) {
$dateC = DateTime::createFromFormat('Y-m-d', $result->dateImmat);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateC);
$this->ValInseeAge = $interval->format('%y') * 12 + $interval->format('%m');
}
$this->SituationJuridique = $result->procol;
}
//Aucun résultat, faire le calcul || donnée manquante || force identite
if ( $this->ValNAF === null
|| $this->ValFJ === null
|| $this->ValInseeActif === null
|| $this->ValInseeAge === null
|| $this->forceIdentite === true
) {
//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 = DateTime::createFromFormat('Ymd', $identite['bilanDate']);
$dateCompare = new DateTime();
$dateCompare->sub(new DateInterval('P'.$bilanAgeMax.'M'));
if ($dateBilan > $dateCompare) {
$this->ValEffectif = $identite['bilanYP'];
}
if ($this->debug) file_put_contents('sfr.log', "Bilan : ".$dateBilan->format('Ymd')." > ".$dateCompare->format('Ymd')."\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
if ( !empty($identite['DateCreaEn']) && $identite['DateCreaEn'] != 0 ) {
$dateC = DateTime::createFromFormat('Ymd', $identite['DateCreaEn']);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateC);
$this->ValInseeAge = $interval->format('%y') * 12 + $interval->format('%m');
} elseif ( !empty($identite['dateImmat']) && $identite['dateImmat'] != '0000-00-00 00:00:00' ) {
$dateC = DateTime::createFromFormat('Y-m-d', $identite['dateImmat']);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateC);
$this->ValInseeAge = $interval->format('%y') * 12 + $interval->format('%m');
}
if ($this->debug) file_put_contents('sfr.log', "ValInseeAge = ".$this->ValInseeAge."\n", FILE_APPEND);
$this->ValSituationJuridique = $identite['SituationJuridique'];
}
}
//Values are defined from outside
else {
//Age de l'entreprise en mois
if ( !empty($this->ValDateCrea) && $this->ValDateCrea!='0000-00-00 00:00:00' ) {
$dateC = DateTime::createFromFormat('Y-m-d', $this->ValDateCrea);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateC);
$this->ValInseeAge = $interval->format('%y') * 12 + $interval->format('%m');
} elseif ( !empty($this->ValDateImmat) && $this->ValDateImmat!='0000-00-00 00:00:00' ) {
$dateC = DateTime::createFromFormat('Y-m-d', $this->ValDateImmat);
$dateNow = new DateTime();
$interval = $dateNow->diff($dateC);
$this->ValInseeAge = $interval->format('%y') * 12 + $interval->format('%m');
}
if ($this->debug) file_put_contents('sfr.log', "ValInseeAge = ".$this->ValInseeAge."\n", FILE_APPEND);
//Vérification validité du score
$dateUpdate = DateTime::createFromFormat('Y-m-d', substr($this->ValDateUpdate,0,10));
$dateNow = new DateTime();
$interval = $dateNow->diff($dateUpdate);
$diff = $interval->format('%y') * 12 + $interval->format('%m');
if ($this->debug) file_put_contents('sfr.log', "INDISCORE DIFF = " . $diff . "\n", FILE_APPEND);
if ( $diff > 12 ) {
$this->ValIndiscore = null;
$this->ValDateCalculIndiscore = null;
}
}
//GetAnnoncesLegales
//Si procédure collective ou autre
if ( $this->ValInseeActif==1 && !empty($this->ValSituationJuridique) ) {
$this->ValRJ = 0; //Liste des codes - 1200
$classInsee->AnnoncesLegalesVisu = true;
$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
$classInsee->AnnoncesLegalesVisu = true;
$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
$classInsee->AnnoncesLegalesVisu = true;
$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);
}
//Entreprise coté au CAC
$this->ValIsCAC = 0;
$liensM = new Metier_Liens_Base();
$liensM->setId($siren, 'siren');
if ($liensM->isInGroupeCAC40()) {
$this->ValIsCAC = 1;
}
if ($this->debug) file_put_contents('sfr.log', "CAC40 = ".$this->ValIsCAC."\n", FILE_APPEND);
//Si Indiscore = null alors calcul
if ( $this->ValIndiscore === null ) {
$indiscore = calculIndiScore($siren, '', false, 0); // @todo : Gérer les erreurs techniques
$this->ValIndiscore = $indiscore['Indiscore20'];
$this->ValDateCalculIndiscore = date('Y-m-d');
}
//Vérification des valeurs avant traitement
$verif = array('NAF', 'FJ', 'InseeActif', 'InseeAge');
foreach($verif as $val) {
if ( $this->{'Val'.$val} === null ) {
if ($this->debug) file_put_contents('sfr.log', "ERREUR = ".$val."\n", FILE_APPEND);
return false;
}
}
if ($this->ValEffectif == '') {
$this->ValEffectif = null;
}
if ($this->debug) file_put_contents('sfr.log', "INDISCORE = ".$this->ValIndiscore."\n", FILE_APPEND);
if ($this->debug) file_put_contents('sfr.log', "EFFECTIF = ".$this->ValEffectif."\n", FILE_APPEND);
//Rules
$ruleType = array( 'VORP' , 'VORD' , 'PO');
foreach ( $ruleType as $type ) {
$this->rules($type);
}
return true;
}
/**
* 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']);
}
if ($this->debug) file_put_contents('sfr.log', "VALIDATION : ValPO = ".$this->ValPO."\n", FILE_APPEND);
} else {
$this->RuleLabel = $rule['name'];
$this->displayPo = intval($rule['po']);
$this->ValFEU = $this->paramValue($rule['value']);
$this->ValComment = $rule['comment'];
if ($this->debug) file_put_contents('sfr.log', "VALIDATION : ValComment = ".$this->ValComment."\n", FILE_APPEND);
if ($this->debug) file_put_contents('sfr.log', "VALIDATION : ValFEU = ".$this->ValFEU."\n", FILE_APPEND);
if ($this->debug) file_put_contents('sfr.log', "VALIDATION : AFFICHE PO = ".$this->displayPo."\n", FILE_APPEND);
}
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->ValDateCalculIndiscore;
}
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,
'ValDateCalculIndiscore' => $this->ValDateCalculIndiscore,
'Rule' => $this->RuleLabel,
);
}
public function setVal($name, $value)
{
if ( in_array($name, array(
'TypeEntrep',
'NAF',
'FJ',
'InseeAge',
'DateCalculIndiscore',
'DateCrea',
'DateImmat',
'DateUpdate')) && 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 realpath(dirname(__FILE__)) . '/RulesVorp-'.$this->version.'.php';
$this->RulesVORd = include realpath(dirname(__FILE__)) . '/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;
}
}
}