Michael RICOIS 53e213202b CS PSR-2
2016-11-24 16:02:49 +01:00

406 lines
11 KiB
PHP
Executable File

<?php
class Metier_Defaillance_Detect
{
protected $debug = false;
/**
* Code de la forme juridique
* @var string
*/
protected $FJ;
/**
* Date de cloture du bilan (AAAAMMJJ)
* @var string
*/
protected $BilanCloture;
/**
* Actif Insee
* @var int
*/
protected $InseeActif;
/**
* Actif RCS
* @var unknown
*/
protected $RcsActif;
/**
* Nombre d'établissement actif
* @var int
*/
protected $EtabActifNb;
/**
* Rgsitre des métiers
* @var int
*/
protected $NumRM;
/**
* RCS
* @var int
*/
protected $IsRCS;
protected $Situation = '';
protected $SituationDateStart;
protected $SituationDateEnd;
protected $SituationLastEven = '';
protected $SituationNbMonthPast = 0;
protected $PlanDateStart;
protected $PlanDateEnd;
protected $PlanPeriod = 120;
protected $ListEvenProcol = array();
protected $ListEvenProcolDelete = array();
protected $ListEvenAbsorption = array();
protected $ListEvenRadiation = array();
protected $ListEvenDissolution = array();
protected $ProcolMaxYear = 12;
protected $Even;
protected $EvenDateJugement;
protected $EvenTxt;
protected $EvenDelete = 0;
protected $DateToday;
/**
* Timeline des événements de défaillance
* { Situation, Even, DateStart, DateEnd }
* @var array
*/
protected $Timeline = array();
/**
* Détection des défaillances
* Instancier la class, puis sur chaque annonce faire le traitement.
*/
public function __construct()
{
if ($this->debug) {
file_put_contents('procol.log', "=== Situation Juridique ===\n");
}
// Définir la date du jour
$this->DateToday = date('Ymd');
// Liste des événements au départ d'une procol
$this->ListEvenProcol = include __DIR__ . '/EvenProcol.php';
// Liste des événements d'absorption
$this->ListEvenAbsorption = include __DIR__ . '/EvenAbsorption.php';
// Liste des événements de radiation
$this->ListEvenRadiation = include __DIR__ . '/EvenRadiation.php';
// Liste des événements de dissolution
$this->ListEvenDissolution = include __DIR__ . '/EvenDissolution.php';
// Définir la liste des évenéments de la rubrique procol avec le paramètre d'effacement
$this->ListEvenProcolDelete = include __DIR__ . '/ProcolDelete.php';
}
public function setFJ($val)
{
$this->FJ = $val;
}
public function setBilanCloture($val)
{
$this->BilanCloture = $val;
}
public function setInseeActif($val)
{
$this->InseeActif = $val;
}
public function setRcsActif($val)
{
$this->RcsActif = $val;
}
public function setEtabActif($val)
{
$this->EtabActifNb = $val;
}
public function setRM($val)
{
$this->IsRM = $val;
}
public function setGreffe($val)
{
$this->IsRCS = $val;
}
/**
* Retourne la situation
* @return string
* Code situation juridique
*/
public function getSituation()
{
return $this->Situation;
}
/**
* Retourne la timeline
* @return array
*/
public function getTimeline()
{
return $this->Timeline;
}
/**
* Lecture de la période et de la date
* @param string $txt
*/
public function planPeriod($txt)
{
if (preg_match('/dur.e(?:.*)plan(?:.*)(\d+)\s+ans?/Uisu', $txt, $matches)) {
$this->PlanPeriod = $matches[1] * 12; // 10 ans = 120 mois
}
}
/**
* Calcul de la date de fin du plan
*/
public function planEnd()
{
$calc = \DateTime::createFromFormat('Ymd', $this->PlanDateStart);
$interval = new \DateInterval('P'.$this->PlanPeriod.'M');
$calc->add($interval);
$this->PlanDateEnd = $calc->format('Ymd');
}
public function situationEnd()
{
$calc = \DateTime::createFromFormat('Ymd', $this->SituationDateStart);
$interval = new \DateInterval('P'.$this->ProcolMaxYear.'Y');
$calc->add($interval);
$this->SituationDateEnd = $calc->format('Ymd');
}
public function nbMonthPast($date)
{
$date1 = new \DateTime();
$date1->createFromFormat('Ymd', $date);
$date2 = new \DateTime();
$diff = $date2->diff($date1)->m;
return $diff;
}
/**
* Traite l'annonce dans le flux en executant les règles
* @param stdClass $ann
* date, code, txt
*/
public function parse($ann)
{
$this->Even = $ann->code;
$this->EvenDateJugement = $ann->date;
$this->EvenTxt = $ann->txt;
if ($this->debug) {
file_put_contents('procol.log', "Annonce : ".$ann->date . '-' . $ann->code."\n", FILE_APPEND);
}
// Marqueur d'effacement de la procol
if (array_key_exists($this->Even, $this->ListEvenProcolDelete)) {
$this->EvenDelete = $this->ListEvenProcolDelete[$this->Even];
}
// Evenement procol précédent
if (count($this->Timeline) > 0) {
$last = end($this->Timeline);
$this->SituationLastEven = $last['even'];
$this->SituationNbMonthPast = $this->nbMonthPast($last['DateStart']);
}
// --- Variable pour le plan
$this->PlanDateStart = $this->EvenDateJugement;
$this->planPeriod($this->EvenTxt);
$this->planEnd();
// Lancement des règles
$this->rules();
if ($this->debug) {
file_put_contents('procol.log', "\n", FILE_APPEND);
}
}
/**
* Execution des règles
*/
protected function rules()
{
$rules = include __DIR__ . '/DetectTable.php';
$setSituation = false;
// Parcours des règles
foreach ($rules as $rule) {
if ($this->debug) {
file_put_contents('procol.log', $rule['name']."\n", FILE_APPEND);
}
$result = $this->params($rule['params']);
if ($result === true) {
// Defintion paramètres situation
$this->SituationDateStart = $this->EvenDateJugement;
$this->situationEnd();
// Enregistrement dans la timeline
$this->Timeline[] = array(
'Situation' => $this->Situation,
'Even' => $this->Even,
'DateStart' => $this->SituationDateStart,
'DateEnd' => $this->SituationDateEnd,
);
file_put_contents('procol.log', print_r($this->Timeline, 1)."\n", FILE_APPEND);
}
if ($this->debug) {
if ($result === true) {
file_put_contents('procol.log', "=> OK\n", FILE_APPEND);
} else {
file_put_contents('procol.log', "=> PASS\n", FILE_APPEND);
}
}
}
}
/**
* Execution des conditions
* @param array $conditions
*/
protected function params($conditions)
{
$cNb = count($conditions);
$cIncr = 1;
foreach ($conditions as $cond) {
if ($this->debug) {
file_put_contents('procol.log', "\t".$cond['var']." ".$cond['op']." ".$cond['value']."\n", FILE_APPEND);
}
$result = $this->paramEval($cond['var'], $cond['op'], $cond['value']);
if ($result === false) {
return false;
}
if ($cNb == $cIncr) {
return true;
}
$cIncr++;
}
}
/**
* Transformation des variables
* @param string $val
* @return number|mixed|NULL
*/
protected function paramValue($val)
{
if (is_numeric($val)) {
return (float) $val;
}
if (is_array($val)) {
return $val;
}
if (is_string($val) && property_exists($this, $val)) {
return $this->{$val};
}
if (is_string($val) && defined('self::'.$val)) {
return constant('self::'.$val);
}
if (is_string($val)) {
return $val;
}
return null;
}
/**
* Evaluation de la condition
* @param string $var
* @param string $op
* @param string $value
* @return boolean|NULL
*/
protected function paramEval($var, $op, $value)
{
// Operation
switch ($op) {
case 'SET':
$this->{$var} = $value;
if ($this->debug) {
file_put_contents('procol.log', "\t\tSET ".$var." = ".$value."\n", FILE_APPEND);
}
return true;
break;
case 'MIN':
$valueReal = $this->paramValue($value);
if ($this->debug) {
file_put_contents('procol.log', "\t".$this->{$var}." > ".$valueReal."\n", FILE_APPEND);
}
if ($this->{$var} !== null && $this->{$var} > $valueReal) {
return true;
}
return false;
break;
case 'MAX':
$valueReal = $this->paramValue($value);
if ($this->debug) {
file_put_contents('procol.log', "\t".$this->{$var}." < ".$valueReal."\n", FILE_APPEND);
}
if ($this->{$var} !== null && $this->{$var} < $valueReal) {
return true;
}
return false;
break;
case 'EGAL':
$valueReal = $this->paramValue($value);
if (is_array($valueReal)) {
if ($this->debug) {
file_put_contents('procol.log', "\t".$this->{$var}." = ".$value."\n", FILE_APPEND);
}
if (in_array($this->{$var}, $valueReal)) {
return true;
}
} elseif ($this->{$var} == $valueReal) {
if ($this->debug) {
file_put_contents('procol.log', "\t".$this->{$var}." = ".$valueReal."\n", FILE_APPEND);
}
return true;
}
return false;
break;
case 'LIST':
$valueReal = $this->paramValue($value);
if ($this->debug) {
file_put_contents('procol.log', "\t".$this->{$var}." = ".print_r($valueReal, 1)."\n", FILE_APPEND);
}
if ($valueReal !== null && in_array($this->{$var}, $valueReal)) {
return true;
}
return false;
break;
}
return null;
}
}