2017-02-15 11:48:45 +01:00

549 lines
17 KiB
PHP
Executable File

<?php
class Metier_Liens_Base
{
/**
* Company Reference id
* @var int
*/
protected $idRef = null;
/**
* Company SIREN
* @var string
*/
protected $siren = null;
/**
* Array to list id find during list of childrens
* @var array
*/
protected $findId = array();
/**
* Coutry List ISO3 => Label
* @var array
*/
protected $country = null;
/**
* Stop the process to looks for company group head
* @var boolean
*/
public $stopAtFirstIsin = false;
/**
* Stop the process to looks for physical person
* @var boolean
*/
public $stopAtPP = true;
/**
* PDO Connection with Doctrine
* @var \Doctrine\DBAL\Connection
*/
protected $conn;
/**
* Gestion des liens inter entreprises
* @param \Doctrine\DBAL\Connection $conn
*/
public function __construct($conn = null)
{
if ($conn === null) {
$this->conn = Zend_Registry::get('doctrine');
} else {
$this->conn = $conn;
}
}
/**
* Identifiant
* @param string $id
* @param string $type ref|siren
*/
public function setId($id, $type = 'ref')
{
// Get Id
if ($type == 'siren') {
$this->siren = $id;
try {
$sql = "SELECT * FROM jo.liensRef WHERE siren = :id AND dateSuppr = 0";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('id', $id);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetch(\PDO::FETCH_OBJ);
$this->idRef = $result->id;
}
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception(__METHOD__ . ': ' . $e->getMessage());
}
} else {
$this->idRef = $id;
}
}
/**
* Retourne les actionnaires
* @param int $id
* @param boolean $actif
* @return array Tableau d'actionnaires
*/
public function getActionnaires($id = null, $actif = null)
{
if (null === $id) {
$id = $this->idRef;
}
$liens = array();
if (null === $id) {
return $liens;
}
try {
$qb = $this->conn->createQueryBuilder();
$qb->select(array(
'l.id', 'l.idAct', 'l.PDetention', 'l.Pvote', 'l.MajMin', 'l.idPar',
'l.dateEffetLien', 'l.dateInsert', 'l.dateUpdate',
'LPAD(r.siren, 9, 0) AS siren', 'r.PpPm', 'r.RS', 'r.sigle', 'r.civilite', 'r.nom',
'r.prenom', 'r.nom_usage', 'r.naissance_date', 'r.naissance_dept_pays',
'r.naissance_lieu', 'r.nat', 'r.adresse_num', 'r.adresse_btq', 'r.adresse_codvoie',
'r.adresse_libvoie', 'r.adresse_comp', 'r.adresse_cp', 'r.adresse_ville',
'r.adresse_pays', 'r.idLoc1Type', 'r.idLoc1Num', 'r.idLoc2Type', 'r.idLoc2Num',
'r.idLoc3Type', 'r.idLoc3Num'
)
)->from('jo.liens2', 'l')->join('l', 'jo.liensRef', 'r', 'l.idAct = r.id')
->where('l.idPar = :id')->setParameter('id', $id)
->orderBy('l.PDetention', 'DESC');
// Actif / Inactif
if (null !== $actif) {
if (false === $actif) {
$qb->andWhere('l.actif = 0');
} else {
$qb->andWhere('l.actif = 1');
}
}
// Don't display deleted - anomaly
$qb->andWhere("l.dateSuppr = '0000-00-00 00:00:00'");
$stmt = $qb->execute();
$liens = $stmt->fetchAll(\PDO::FETCH_OBJ);
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception(__METHOD__ . ': ' . $e->getMessage());
}
return $liens;
}
/**
* Retourne les participations
* @param int $id
* @param boolean $actif
* @return array Tableau des participations
*/
public function getParticipations($id = null, $actif = null)
{
if (null === $id) {
$id = $this->idRef;
}
if (false === $actif) {
$actif = 0;
} else {
$actif = 1;
}
$liens = array();
if (null === $id) {
return $liens;
}
try {
$qb = $this->conn->createQueryBuilder();
$qb->select(array(
'l.id', 'l.idAct', 'l.PDetention', 'l.Pvote', 'l.MajMin', 'l.idPar',
'l.dateEffetLien', 'l.dateInsert', 'l.dateUpdate',
'LPAD(r.siren, 9, 0) AS siren', 'r.PpPm', 'r.RS', 'r.sigle', 'r.civilite', 'r.nom',
'r.prenom', 'r.nom_usage', 'r.naissance_date', 'r.naissance_dept_pays',
'r.naissance_lieu', 'r.nat', 'r.adresse_num', 'r.adresse_btq', 'r.adresse_codvoie',
'r.adresse_libvoie', 'r.adresse_comp', 'r.adresse_cp', 'r.adresse_ville',
'r.adresse_pays', 'r.idLoc1Type', 'r.idLoc1Num', 'r.idLoc2Type', 'r.idLoc2Num',
'r.idLoc3Type', 'r.idLoc3Num'
)
)->from('jo.liens2', 'l')->join('l', 'jo.liensRef', 'r', 'l.idPar=r.id')
->where('l.idAct = :id')->setParameter('id', $id)
->orderBy('l.PDetention', 'DESC');
// Actif / Inactif
if (null !== $actif) {
if (false === $actif) {
$qb->andWhere('l.actif = 0');
} else {
$qb->andWhere('l.actif = 1');
}
}
//Don't display deleted - anomaly
$qb->andWhere("l.dateSuppr = '0000-00-00 00:00:00'");
$stmt = $qb->execute();
$liens = $stmt->fetchAll(\PDO::FETCH_OBJ);
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception(__METHOD__ . ': ' . $e->getMessage());
}
return $liens;
}
/**
* Fonctions de direction
* @param boolean $actif
* @return array
*/
public function getDirections($actif = null)
{
if (null === $this->siren) {
$sql = "SELECT LPAD(siren, 9, 0) AS siren FROM jo.liensRef WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('id', $this->idRef);
$result = $stmt->fetch(\PDO::FETCH_OBJ);
$siren = $result->siren;
} else {
$siren = $this->siren;
}
$result = array();
if (null !== $siren && intval($siren) != 0) {
try {
$qb = $this->conn->createQueryBuilder();
$qb->select(array('LPAD(siren, 9, 0) AS siren', 'raisonSociale',
'dirSiren', 'dirRS', 'civilite', 'nom', 'prenom', 'naissance_date',
'naissance_lieu', 'fonction_code', 'fonction_lib')
)->from('jo.rncs_dirigeants')->where("typeDir IN ('PM', 'PP')")
->andWhere('dirSiren = :siren')->setParameter('siren', $siren);
// Actif / Inactif
if (null !== $actif) {
if (false === $actif) {
$qb->andWhere('actif = 0');
} else {
$qb->andWhere('actif = 1');
}
}
$qb->orderBy('fonction_code', 'DESC');
$qb->orderBy('raisonSociale', 'ASC');
$stmt = $qb->execute();
$result = $stmt->fetchAll(\PDO::FETCH_OBJ);
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception(__METHOD__ . ': ' . $e->getMessage());
}
}
return $result;
}
/**
* Retourne la maison mère
* @param int $id
* Id de la fiche de départ
* @param int $detention
* Niveau de détention (Une filiale est une entreprise détenue à plus de 50% par une autre entreprise)
* @return int
*/
public function getHead($id = null, $detention = 0)
{
if (null === $id) {
$id = $this->idRef;
}
//Add ID to the list of known
$this->findId[] = $id;
//Through the list
$liens = $this->getActionnaires($id, true);
//Find the following up entity
if (count($liens)>0) {
foreach ($liens as $item) {
//Don't through again and again
if (in_array($item->idAct, $this->findId)) {
return $item->idAct;
} elseif ($this->stopAtFirstIsin === true && !empty($item->isin)) {
break;
}
//Remove physical person
elseif ($item->PpPm == 'PP' && $this->stopAtPP) {
continue;
}
//Same id
elseif ($item->idAct == $id) {
return $id;
}
//PDetention>50
elseif ($item->PDetention > 50 && $item->idAct > 1000) {
return $this->getHead($item->idAct, $detention);
}
//MajMin=+
elseif ($item->PDetention > $detention && $item->MajMin == '+' && $item->idAct > 1000) {
return $this->getHead($item->idAct, $detention);
}
//--
elseif ($item->PDetention > $detention && $item->idAct > 1000) {
return $this->getHead($item->idAct, $detention);
}
}
}
return $id;
}
/**
* Retourne les éléments identitaire présent dans lienRef
* @param string $id
* @return array
*/
public function getIdentity($id = null)
{
if (null === $id) {
$id = $this->idRef;
}
$sql = "SELECT * FROM jo.liensRef WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('id', $id);
$stmt->execute();
if ($stmt->rowCount() > 0) {
return $stmt->fetch(\PDO::FETCH_OBJ);
}
return null;
}
/**
* Retourne l'arborescence pour les groupes
* @param int $pctMin
* @param int $nbNiveaux
* @return array
*/
public function getTree($pctMin = 33, $nbNiveaux = 10)
{
//Get identity to stop at isin
$itemWithIsin = null;
if ($this->stopAtFirstIsin === true) {
$id = $this->idRef;
$identity = $this->getIdentity($this->idRef);
if (!empty($identity->isin)) {
$itemWithIsin = true;
}
}
if ($itemWithIsin !== true) {
//Récupération de la maison mère
$id = $this->getHead(null, 50);
//Informations de la maison mère
$identity = $this->getIdentity($id);
}
$this->findId = array();
$this->findId[] = $identity->id;
$nom = $identity->RS;
if ($identity->nom != '') {
$nom = $identity->civilite.' '.$identity->nom.' '.$identity->prenom;
}
if ($this->country === null) {
$this->country = $this->getCountry();
}
$pays = $identity->adresse_pays;
if (array_key_exists($identity->adresse_pays, $this->country)) {
$pays = $this->country[$identity->adresse_pays];
}
//Retour
$tabRet = array(
'id' => $identity->id,
'name' => $nom,
'siren' => str_pad($identity->siren, 9, '0', STR_PAD_LEFT),
'pmin' => $item->PDetention,
'pays' => $pays,
'children' => $this->getTreeRecursive($identity->id, $pctMin, 1, $nbNiveaux),
);
return $tabRet;
}
/**
* Retourne un sous élement de l'arborescence pour les groupes
* @param int $id
* @param int $pctMin
* @param int $niveau
* @param int $nbNiveaux
* @return array
*/
public function getTreeRecursive($id, $pctMin=33, $niveau=0, $nbNiveaux=10)
{
if ($niveau > $nbNiveaux) {
return array();
}
$niveau++;
$tabRet = array();
$participations = $this->getParticipations($id, true);
if (count($participations)>0) {
foreach ($participations as $item) {
if ($item->PDetention > $pctMin) {
$identity = $this->getIdentity($item->idPar);
$nom = $identity->RS;
if ($identity->nom != '') {
$nom = $identity->civilite.' '.$identity->nom.' '.$identity->prenom;
}
if ($this->country === null) {
$this->country = $this->getCountry();
}
$pays = $identity->adresse_pays;
if (array_key_exists($identity->adresse_pays, $this->country)) {
$pays = $this->country[$identity->adresse_pays];
}
$data = array(
'id' => $identity->id,
'name' => $nom,
'siren' => str_pad($identity->siren, 9, '0', STR_PAD_LEFT),
'pmin' => $item->PDetention,
'pays' => $pays,
'children' => array(),
);
//Pour éviter d'avoir des boucles infinis
if (!in_array($identity->id, $this->findId)) {
$this->findId[] = $identity->id;
$data['children'] = $this->getTreeRecursive($identity->id, $pctMin, $niveau, $nbNiveaux);
}
$tabRet[] = $data;
}
}
}
return $tabRet;
}
/**
* Liste des entités du CAC40
* @return array
*/
public function getCAC40()
{
$sql = "SELECT isin, nom, MAX(date) AS dateMAJ FROM sdv1.bourse_listes
WHERE lstCode='xcac40p' GROUP BY lstCode, isin HAVING MAX(date) ORDER BY dateMAJ DESC;";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(\PDO::FETCH_OBJ);
$output = array();
foreach ($result as $item) {
$output[] = $item->isin;
}
return $output;
}
/**
*
* @param number $pctMin
* @return array
*/
public function getGroupeCAC40($pctMin = 50)
{
$listeIsin = $this->getCAC40();
$isin = "'".implode("','", $listeIsin)."'";
$this->findId = array();
$sql = "SELECT id FROM jo.liensRef WHERE (idLoc1Type=63 AND idLoc1Num IN (".$isin."))
OR (idLoc2Type=63 AND idLoc2Num IN (".$isin.")) OR (idLoc3Type=63 AND idLoc3Num IN (".$isin."))";
$stmt = $this->conn->executeQuery($sql);
$output = array();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll(\PDO::FETCH_OBJ);
foreach ($result as $item) {
$output = $output + $this->getListeGroupeCAC40($item->id, $pctMin);
}
}
return $output;
}
/**
*
* @param int $id
* @param number $pctMin
* @return array
*/
public function getListeGroupeCAC40($id, $pctMin=50)
{
$participations = $this->getParticipations($id, true);
$output = array();
if (count($participations)>0) {
foreach ($participations as $item) {
if ($item->PDetention > $pctMin) {
$identity = $this->getIdentity($item->idPar);
if (intval($identity->siren)!=0) {
$output[] = $identity->siren;
}
if (!in_array($identity->id, $this->findId)) {
$this->findId[] = $identity->id;
$output = $output + $this->getListeGroupeCAC40($identity->id, $pctMin);
}
}
}
}
return $output;
}
/**
*
* @param number $pctMin
* @return boolean
*/
public function isInGroupeCAC40($pctMin = 50)
{
//Si pas d'actionnaires => false
if (count($this->getActionnaires()) == 0) {
return false;
}
$listeInGroupeCAC40 = $this->getGroupeCAC40($pctMin);
if (in_array($this->siren, $listeInGroupeCAC40)) {
return true;
}
return false;
}
/**
* Get all countries
*/
protected function getCountry()
{
$sql = "SELECT codPays3, libPays FROM jo.tabPays WHERE codPays3 IS NOT NULL";
$stmt = $this->conn->prepare($sql);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$data = array();
while($item = $stmt->fetch(PDO::FETCH_OBJ)) {
$data[$item->codPays3] = $item->libPays;
}
return $data;
}
return false;
}
}