536 lines
14 KiB
PHP
536 lines
14 KiB
PHP
<?php
|
|
class MLiens2
|
|
{
|
|
/**
|
|
* Company Reference id
|
|
* @var int
|
|
*/
|
|
protected $idRef = null;
|
|
|
|
/**
|
|
* Company SIREN
|
|
* @var string
|
|
*/
|
|
protected $siren = null;
|
|
|
|
/**
|
|
* @var Zend_Db_Adapter_Abstract
|
|
*/
|
|
protected $db;
|
|
|
|
/**
|
|
* 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;
|
|
|
|
/**
|
|
* Databas table name
|
|
* @var string
|
|
*/
|
|
protected $_schema = 'jo';
|
|
|
|
/**
|
|
*
|
|
* @param string $id
|
|
* @param string $type ref|siren
|
|
* @param Zend_Db_Adapter $db
|
|
* @throws SoapFault
|
|
*/
|
|
public function __construct($id, $type = 'ref', $db = null)
|
|
{
|
|
//Get defaut database adapter
|
|
if( $db === null ) {
|
|
$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
|
|
} else {
|
|
$this->db = $db;
|
|
Zend_Db_Table_Abstract::setDefaultAdapter($db);
|
|
}
|
|
|
|
//Get Id
|
|
if ( $type == 'siren' ) {
|
|
$this->siren = $id;
|
|
$refM = new Application_Model_JoLiensRef();
|
|
$sql = $refM->select()->where('siren=?', $id)->where('dateSuppr=0');
|
|
$result = $refM->fetchRow($sql);
|
|
if ( $result !== null ) {
|
|
$this->idRef = $result->id;
|
|
}
|
|
} 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 {
|
|
$sql = $this->db->select()
|
|
->from(array('l'=>$this->_schema.'.liens2'),
|
|
array('id', 'idAct', 'PDetention', 'Pvote', 'MajMin', 'idPar',
|
|
'dateEffetLien', 'dateInsert', 'dateUpdate'))
|
|
->where('idPar=?', $id)
|
|
->join(array('r'=>$this->_schema.'.liensRef'), 'l.idAct=r.id',
|
|
array('LPAD(siren, 9, 000000000) AS siren','PpPm', 'RS', 'sigle', 'civilite', 'nom', 'prenom', 'nom_usage',
|
|
'naissance_date', 'naissance_dept_pays', 'naissance_lieu', 'nat',
|
|
'adresse_num', 'adresse_btq', 'adresse_codvoie', 'adresse_libvoie',
|
|
'adresse_comp', 'adresse_cp', 'adresse_ville', 'adresse_pays',
|
|
'idLoc1Type', 'idLoc1Num', 'idLoc2Type', 'idLoc2Num', 'idLoc3Type', 'idLoc3Num',
|
|
))
|
|
->order('PDetention DESC');
|
|
|
|
//Actif / Inactif
|
|
if ( null !== $actif ) {
|
|
if ( false === $actif ) {
|
|
$sql->where('l.actif=?',0);
|
|
} else {
|
|
$sql->where('l.actif=?',1);
|
|
}
|
|
}
|
|
|
|
//Don't display deleted - anomaly
|
|
$sql->where('l.dateSuppr=?', '0000-00-00 00:00:00');
|
|
|
|
$liens = $this->db->fetchAll($sql, null, Zend_Db::FETCH_OBJ);
|
|
} catch (Zend_Db_Exception $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 {
|
|
$sql = $this->db->select()
|
|
->from(array('l'=>$this->_schema.'.liens2'),
|
|
array('id', 'idAct', 'PDetention', 'Pvote', 'MajMin', 'idPar',
|
|
'dateEffetLien', 'dateInsert', 'dateUpdate'))
|
|
->where('idAct=?', $id)
|
|
->join(array('r'=>$this->_schema.'.liensRef'), 'l.idPar=r.id',
|
|
array('LPAD(siren, 9, 000000000) AS siren','PpPm', 'RS', 'sigle', 'civilite', 'nom', 'prenom', 'nom_usage',
|
|
'naissance_date', 'naissance_dept_pays', 'naissance_lieu', 'nat',
|
|
'adresse_num', 'adresse_btq', 'adresse_codvoie', 'adresse_libvoie',
|
|
'adresse_comp', 'adresse_cp', 'adresse_ville', 'adresse_pays',
|
|
'idLoc1Type', 'idLoc1Num', 'idLoc2Type', 'idLoc2Num', 'idLoc3Type', 'idLoc3Num',
|
|
))
|
|
->order('PDetention DESC');
|
|
|
|
//Actif / Inactif
|
|
if ( null !== $actif ) {
|
|
if ( false === $actif ) {
|
|
$sql->where('l.actif=?',0);
|
|
} else {
|
|
$sql->where('l.actif=?',1);
|
|
}
|
|
}
|
|
|
|
//Don't display deleted - anomaly
|
|
$sql->where('l.dateSuppr=?', '0000-00-00 00:00:00');
|
|
|
|
$liens = $this->db->fetchAll($sql, null, Zend_Db::FETCH_OBJ);
|
|
} catch (Zend_Db_Exception $e) {
|
|
throw new Exception(__METHOD__ . ': ' . $e->getMessage());
|
|
}
|
|
|
|
return $liens;
|
|
}
|
|
|
|
/**
|
|
* Fonctions de direction
|
|
* @param boolean $actif
|
|
* @return Zend_Db_Table_Rowset_Abstract
|
|
*/
|
|
public function getDirections($actif = null)
|
|
{
|
|
if ( null === $this->siren ) {
|
|
$refM = new Application_Model_JoLiensRef();
|
|
$rows = $refM->find($this->idRef);
|
|
$siren = str_pad($rows->current()->siren, 9, '0', STR_PAD_LEFT);
|
|
} else {
|
|
$siren = $this->siren;
|
|
}
|
|
|
|
$result = array();
|
|
|
|
if ( null !== $siren && intval($siren) != 0 )
|
|
{
|
|
try {
|
|
$directionsM = new Application_Model_JoRncsDirigeants();
|
|
$sql = $directionsM->select()->from($directionsM, array(
|
|
'LPAD(siren, 9, 000000000) AS siren','raisonSociale', 'dirSiren', 'dirRS', 'civilite', 'nom',
|
|
'prenom', 'naissance_date', 'naissance_lieu', 'fonction_code', 'fonction_lib'
|
|
))->where("typeDir IN ('PM', 'PP')")->where('dirSiren=?', $siren);
|
|
|
|
//Actif / Inactif
|
|
if ( null !== $actif ) {
|
|
if ( false === $actif ) {
|
|
$sql->where('actif=?',0);
|
|
} else {
|
|
$sql->where('actif=?',1);
|
|
}
|
|
}
|
|
$sql->order('fonction_code DESC');
|
|
$sql->order('raisonSociale ASC');
|
|
|
|
$result = $directionsM->fetchAll($sql);
|
|
} catch (Zend_Db_Exception $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' ) {
|
|
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 Zend_Db_Table_Rowset_Abstract
|
|
*/
|
|
public function getIdentity($id = null)
|
|
{
|
|
if ( null === $id ) {
|
|
$id = $this->idRef;
|
|
}
|
|
|
|
$refM = new Application_Model_JoLiensRef();
|
|
$row = $refM->find($id);
|
|
if (null !== $row) {
|
|
return $row->current();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return multitype:NULL
|
|
*/
|
|
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;";
|
|
$result = $this->db->query($sql);
|
|
|
|
$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);
|
|
|
|
$refM = new Application_Model_JoLiensRef();
|
|
$sql = $refM->select()
|
|
->where("idLoc1Type=63")->where("idLoc1Num IN (?)", $isin)
|
|
->orWhere("idLoc2Type=63")->where("idLoc2Num IN (?)", $isin)
|
|
->orWhere("idLoc3Type=63")->where("idLoc3Num IN (?)", $isin);
|
|
|
|
$result = $refM->fetchAll($sql);
|
|
|
|
$this->findId = array();
|
|
|
|
$output = array();
|
|
if ( $result->count()>0 ) {
|
|
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()
|
|
{
|
|
$countryM = new Application_Model_JoTabPays();
|
|
$sql = $countryM->select()
|
|
->from($countryM, array('codPays3', 'libPays'))
|
|
->where('codPays3 IS NOT NULL');
|
|
$rows = $countryM->fetchAll($sql);
|
|
if ( $rows !== null ) {
|
|
$data = array();
|
|
foreach($rows as $item) {
|
|
$data[$item->codPays3] = $item->libPays;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |