652 lines
14 KiB
PHP
Raw Normal View History

2013-06-26 15:42:32 +00:00
<?php
2013-08-07 15:39:24 +00:00
require_once 'Metier/insee/classMinsee.php';
require_once 'Metier/scores/classMScores.php';
2013-06-26 15:42:32 +00:00
/**
*
*
*/
class Metier_Sfr
{
/**
* Version of rules (for loading static files)
* @var double
*/
protected $version = '1.0';
/**
* Debug mode
* @var boolean
*/
2013-08-07 15:39:24 +00:00
protected $debug = true;
2013-06-26 15:42:32 +00:00
/**
* True, to use static file for set vars otherwise read in the database
* @var boolean
*/
protected $compile = true;
2013-08-07 15:39:24 +00:00
/**
*
* @var Zend_Db_Adapter_Abstract
*/
protected $db = null;
2013-06-26 15:42:32 +00:00
/**
* 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;
2013-07-03 09:33:55 +00:00
/**
* Constante valeur UNDEFINE
* @var null
*/
const UNDEFINE = null;
2013-06-26 15:42:32 +00:00
/**
* Liste des NAF Administration
* @var array
*/
2013-08-07 15:39:24 +00:00
protected $TabAdminNaf = array(
'8411Z',
'8412Z',
'8413Z',
'8421Z',
'8422Z',
'8423Z',
'8424Z',
'8425Z',
'8430A',
'8430B',
'8430C',
);
2013-06-26 15:42:32 +00:00
/**
* Liste des FJ Administration
* @var array
*/
2013-08-07 15:39:24 +00:00
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',
'81',
'8110',
'8120',
'8130',
'8140',
'8150',
'8160',
'8170',
'8190',
'8210',
'8250',
'8290',
'8310',
'8311',
'8510',
'8520',
);
/**
* Liste des NAFs société étrangère
* @var unknown
*/
protected $TabEtrangerNaf = array(
'9900Z'
);
/**
* Liste des FJ société étrangère
* @var unknown
*/
protected $TabEtrangerFj = array(
'31',
'32',
);
/**
* Liste des NAF clés
* @var array
*/
protected $TabNafSFR = array(
'4791A',
'4791B',
'4799B',
'6110Z',
'6120Z',
'6130Z',
'6190Z',
'7320Z',
);
2013-06-26 15:42:32 +00:00
//Valeurs à remplir
protected $ValIsCAC = null;
2013-08-07 15:39:24 +00:00
protected $ValTypeEntrep = null;
2013-06-26 15:42:32 +00:00
protected $ValIsAdmin = null;
protected $ValIsEtranger = null;
protected $ValEffectif = null;
protected $ValNAF = null;
protected $ValFJ = null;
2013-08-07 15:39:24 +00:00
protected $ValRJ = 0;
protected $ValLJ = 0;
protected $ValSV = 0;
2013-06-26 15:42:32 +00:00
protected $ValIndiscore = null;
protected $ValInseeActif = null;
protected $ValInseeAge = null;
protected $ValEntrepRecente = null;
protected $ValContratDate = null;
protected $ValIR = null;
2013-08-07 15:39:24 +00:00
protected $ValFEU = null;
2013-06-26 15:42:32 +00:00
protected $ValPO = null;
protected $ValComment = null;
//Rules
protected $RulesVORP = array();
protected $RulesVORD = array();
protected $RulesPO = array();
2013-07-03 09:33:55 +00:00
/**
*
* @param string $version
* @param string $compile
*/
2013-06-26 15:42:32 +00:00
public function __construct( $version = null, $compile = true )
{
$this->setVersion($version);
$this->setCompile($compile);
2013-08-07 15:39:24 +00:00
//Charger les Tabs - ou pas
2013-06-26 15:42:32 +00:00
2013-08-07 15:39:24 +00:00
//Get default database adapter
//$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
2013-06-26 15:42:32 +00:00
//Charger les Rules (dans l'ordre)
2013-08-07 15:39:24 +00:00
$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");
2013-06-26 15:42:32 +00:00
}
public function evaluate($siren)
{
2013-08-07 15:39:24 +00:00
$classInsee = new MInsee();
2013-07-03 09:33:55 +00:00
2013-08-07 15:39:24 +00:00
//Identite
$identite = $classInsee->getIdentiteEntreprise($siren);
2013-07-03 09:33:55 +00:00
2013-08-07 15:39:24 +00:00
//Naf de l'entreprise
$this->ValNAF = $identite['NafEnt'];
2013-07-03 09:33:55 +00:00
2013-08-07 15:39:24 +00:00
//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 ) {
Zend_Date::setOptions(array('extend_month' => true));
$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'];
}
}
//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']) ) {
Zend_Date::setOptions(array('extend_month' => true));
$dateC = new Zend_Date($identite['DateCreaEn'], 'yyyyMMdd');
$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' ) {
Zend_Date::setOptions(array('extend_month' => true));
$dateC = new Zend_Date($identite['dateImmat'], 'yyyyMMdd');
$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();
}
//Entreprise coté au CAC
$this->ValIsCAC = null;
/**
* Liens - remonté le groupe pour voir si SIREN dans CAC40
* Tete de groupe - ISIN
*
* Tete de groupe compléte et SIREN dans CAC
*/
//GetAnnoncesLegales
//$classInsee->getAnnoncesLegales($siren, 0, array());
$this->ValRJ = 0; //Liste des codes - 1200
$this->ValSV = 0; //Liste des codes - 1100
$this->ValLJ = 0; //Liste des codes - 1300
//Indiscore - aller chercher scores dans l'historique
$indiscore = calculIndiScore($siren);
$this->ValIndiscore = $indiscore['Indiscore20'];
if ($this->debug) file_put_contents('sfr.log', "INDISCORE = ".$this->ValIndiscore."\n", FILE_APPEND);
2013-07-03 09:33:55 +00:00
//Rules
$ruleType = array( 'VORP' , 'VORD' , 'PO');
foreach ( $ruleType as $type ) {
$this->rules($type);
}
2013-06-26 15:42:32 +00:00
}
2013-07-03 09:33:55 +00:00
/**
* Execution des règles
* Si les conditions contenu dans la regle
*
*
* @param string $type
*/
2013-06-26 15:42:32 +00:00
protected function rules($type)
{
2013-07-03 09:33:55 +00:00
$rules = $this->{'Rules'.$type};
foreach ( $rules as $rule ) {
2013-08-07 15:39:24 +00:00
if ($this->debug) file_put_contents('sfr.log', "\nREGLE - ".$rule['name']."\n", FILE_APPEND);
$all = false;
if ( $rule['value'] == 'CONTINUE' ) {
$all = true;
}
$return = $this->params($rule['params'], $all);
2013-06-26 15:42:32 +00:00
2013-07-03 09:33:55 +00:00
// Continue always
if ( $rule['value'] == 'CONTINUE' ) {
continue;
}
//Continue
elseif ( $return === false ){
continue;
}
// Stop
elseif ( $rule['value'] == 'STOP' && $return === true ) {
break;
}
//Set Value
else {
2013-08-07 15:39:24 +00:00
if( $type == 'PO' ) {
$this->ValPO = $this->paramValue($rule['value']);
} else {
$this->ValFEU = $this->paramValue($rule['value']);
2013-08-12 09:16:24 +00:00
$this->ValComment = $rule['comment'];
2013-08-07 15:39:24 +00:00
}
2013-07-03 09:33:55 +00:00
break;
}
}
2013-06-26 15:42:32 +00:00
}
/**
* Traitement des conditions
* - Si la condition est FAUSSE
2013-07-03 09:33:55 +00:00
* => arrêt pour passer à la règle suivante
2013-06-26 15:42:32 +00:00
* - Si la condition est VRAI
2013-07-03 09:33:55 +00:00
* => passage à la condition suivante
2013-06-26 15:42:32 +00:00
* @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"
2013-07-03 09:33:55 +00:00
* @return boolean
2013-06-26 15:42:32 +00:00
*/
2013-08-07 15:39:24 +00:00
protected function params($conditions = array(), $all = false)
2013-06-26 15:42:32 +00:00
{
2013-07-03 09:33:55 +00:00
$test = false;
2013-06-26 15:42:32 +00:00
if ( count($conditions) > 0 ) {
foreach ( $conditions as $c ) {
2013-08-07 15:39:24 +00:00
2013-06-26 15:42:32 +00:00
$test = $this->paramEval($c['var'], $c['type'], $c['value']);
2013-08-07 15:39:24 +00:00
if ($this->debug) file_put_contents('sfr.log', "PARAM - RETURN = ".$test."\n", FILE_APPEND);
2013-06-26 15:42:32 +00:00
if ( null === $test ) {
2013-07-03 09:33:55 +00:00
// Oops ! Problem !
2013-06-26 15:42:32 +00:00
}
2013-08-07 15:39:24 +00:00
// Stopper
elseif ( $test === false && $all === false ) {
break;
// Continuer ou Stopper sur element define
} elseif ( $test === true ) {
2013-07-03 09:33:55 +00:00
if ( array_key_exists('define', $c) ) {
2013-08-07 15:39:24 +00:00
$this->paramDefine($c['define']['var'], $c['define']['value']);
}
if ($all === true) {
break;
}
2013-06-26 15:42:32 +00:00
}
2013-07-03 09:33:55 +00:00
}
2013-06-26 15:42:32 +00:00
}
2013-07-03 09:33:55 +00:00
return $test;
2013-06-26 15:42:32 +00:00
}
/**
* 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->{'Tab'.$val}) ) {
return $this->{'Tab'.$val};
}
return null;
}
2013-07-03 09:33:55 +00:00
/**
* Evaluation de la condition
* @param string $var
* @param string $type
* @param string $value
* @return boolean|NULL
*/
2013-06-26 15:42:32 +00:00
protected function paramEval($var, $type, $value)
{
$valueReal = $this->paramValue($value);
2013-08-07 15:39:24 +00:00
if ($this->debug) file_put_contents('sfr.log', "PARAM - ".$var." ".$type." ".$value." = ".$this->{'Val'.$var}."\n", FILE_APPEND);
2013-06-26 15:42:32 +00:00
switch ($type) {
case 'MIN':
if ( $this->{'Val'.$var} > $valueReal ) {
return true;
}
return false;
break;
case 'MAX':
if ( $this->{'Val'.$var} < $valueReal ) {
return true;
}
return false;
break;
case 'EGAL':
2013-08-07 15:39:24 +00:00
if ( $this->{'Val'.$var} == $valueReal ) {
2013-06-26 15:42:32 +00:00
return true;
}
return false;
break;
case 'LIST':
if ( in_array($this->{'Val'.$var}, $valueReal) ) {
return true;
}
return false;
break;
}
return null;
}
2013-08-07 15:39:24 +00:00
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;
}
2013-07-03 09:33:55 +00:00
/**
*
* @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);
}
2013-08-07 15:39:24 +00:00
if ($this->debug) file_put_contents('sfr.log', "SET VAR - Val".$name." = ".$value."\n", FILE_APPEND);
2013-07-03 09:33:55 +00:00
$this->{'Val'.$name} = $value;
}
2013-06-26 15:42:32 +00:00
2013-08-12 09:16:24 +00:00
/**
*
*/
public function getComment()
{
return $this->ValComment;
}
2013-06-26 15:42:32 +00:00
/**
*
*/
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;
}
}
}