2012-11-23 15:34:49 +00:00
< ? php
class MLiens2
{
2012-12-03 10:49:19 +00:00
/**
* Company Reference id
* @ var int
*/
protected $idRef = null ;
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
/**
* Company SIREN
* @ var string
*/
protected $siren = null ;
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
/**
2013-02-21 17:03:56 +00:00
* @ var Zend_Db_Adapter_Abstract
2012-12-03 10:49:19 +00:00
*/
protected $db ;
2013-02-11 13:26:20 +00:00
2013-08-22 13:01:01 +00:00
/**
*
* @ var unknown
*/
protected $_schema = 'jo' ;
2012-12-03 10:49:19 +00:00
/**
* Array to list id find during list of childrens
* @ var array
*/
2012-11-23 15:34:49 +00:00
protected $findId = array ();
2013-02-11 13:26:20 +00:00
2013-08-22 13:01:01 +00:00
/**
2012-11-23 15:34:49 +00:00
*
* @ param string $id
* @ param string $type ref | siren
2013-08-22 13:01:01 +00:00
* @ param Zend_Db_Adapter $db
2012-11-23 15:34:49 +00:00
* @ throws SoapFault
*/
2013-08-22 13:01:01 +00:00
public function __construct ( $id , $type = 'ref' , $db = null )
2012-11-23 15:34:49 +00:00
{
2013-02-21 17:03:56 +00:00
//Get defaut database adapter
2013-08-22 13:01:01 +00:00
if ( $db === null ) {
$this -> db = Zend_Db_Table_Abstract :: getDefaultAdapter ();
} else {
$this -> db = $db ;
Zend_Db_Table_Abstract :: setDefaultAdapter ( $db );
}
2013-02-21 17:03:56 +00:00
//Get Id
2012-12-04 15:54:34 +00:00
if ( $type == 'siren' ) {
2012-12-03 10:49:19 +00:00
$this -> siren = $id ;
2013-02-21 17:03:56 +00:00
$refM = new Application_Model_JoLiensRef ();
2012-11-23 15:34:49 +00:00
$sql = $refM -> select () -> where ( 'siren=?' , $id );
$result = $refM -> fetchRow ( $sql );
2012-12-04 15:54:34 +00:00
if ( $result !== null ) {
$this -> idRef = $result -> id ;
2012-11-23 15:34:49 +00:00
}
} else {
$this -> idRef = $id ;
}
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
/**
* Retourne les actionnaires
* @ param int $id
2012-12-03 10:49:19 +00:00
* @ param boolean $actif
2012-11-23 15:34:49 +00:00
* @ return array Tableau d ' actionnaires
*/
2012-12-03 10:49:19 +00:00
public function getActionnaires ( $id = null , $actif = null )
2012-11-23 15:34:49 +00:00
{
if ( null === $id ) {
$id = $this -> idRef ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
$liens = array ();
2013-02-11 13:26:20 +00:00
2012-12-04 15:54:34 +00:00
if ( null === $id ) {
return $liens ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
try {
$sql = $this -> db -> select ()
2013-02-21 17:03:56 +00:00
-> from ( array ( 'l' => $this -> _schema . '.liens2' ),
2012-11-23 15:34:49 +00:00
array ( 'id' , 'idAct' , 'PDetention' , 'Pvote' , 'MajMin' , 'idPar' ,
2013-02-18 08:35:19 +00:00
'dateEffetLien' , 'dateInsert' , 'dateUpdate' ))
2012-12-03 10:49:19 +00:00
-> where ( 'idPar=?' , $id )
2013-02-21 17:03:56 +00:00
-> join ( array ( 'r' => $this -> _schema . '.liensRef' ), 'l.idAct=r.id' ,
2012-11-23 15:34:49 +00:00
array ( 'LPAD(siren, 9, 000000000) AS siren' , 'PpPm' , 'RS' , 'civilite' , 'nom' , 'prenom' , 'nom_usage' ,
'naissance_date' , 'naissance_dept_pays' , 'naissance_lieu' , 'nat' ,
'adresse_num' , 'adresse_btq' , 'adresse_codvoie' , 'adresse_libvoie' ,
2013-02-18 08:35:19 +00:00
'adresse_comp' , 'adresse_cp' , 'adresse_ville' , 'adresse_pays' ,
'idLoc1Type' , 'idLoc1Num' , 'idLoc2Type' , 'idLoc2Num' , 'idLoc3Type' , 'idLoc3Num' ,
2012-11-23 15:34:49 +00:00
))
2012-12-03 10:49:19 +00:00
-> order ( 'PDetention DESC' );
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
//Actif / Inactif
if ( null !== $actif ) {
if ( false === $actif ) {
$sql -> where ( 'l.actif=?' , 0 );
} else {
$sql -> where ( 'l.actif=?' , 1 );
}
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
$liens = $this -> db -> fetchAll ( $sql , null , Zend_Db :: FETCH_OBJ );
2013-02-11 13:26:20 +00:00
} catch ( Zend_Db_Exception $e ) {
2013-02-21 17:03:56 +00:00
throw new Exception ( __METHOD__ . ': ' . $e -> getMessage ());
2012-11-23 15:34:49 +00:00
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
return $liens ;
}
/**
* Retourne les participations
* @ param int $id
2012-12-03 10:49:19 +00:00
* @ param boolean $actif
2012-11-23 15:34:49 +00:00
* @ return array Tableau des participations
*/
2012-12-03 10:49:19 +00:00
public function getParticipations ( $id = null , $actif = null )
2012-11-23 15:34:49 +00:00
{
if ( null === $id ) {
$id = $this -> idRef ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
if ( false === $actif ) {
$actif = 0 ;
} else {
$actif = 1 ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
$liens = array ();
2013-02-11 13:26:20 +00:00
2012-12-04 15:54:34 +00:00
if ( null === $id ) {
return $liens ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
try {
$sql = $this -> db -> select ()
2013-02-21 17:03:56 +00:00
-> from ( array ( 'l' => $this -> _schema . '.liens2' ),
2012-11-23 15:34:49 +00:00
array ( 'id' , 'idAct' , 'PDetention' , 'Pvote' , 'MajMin' , 'idPar' ,
2013-02-18 08:35:19 +00:00
'dateEffetLien' , 'dateInsert' , 'dateUpdate' ))
2012-12-03 10:49:19 +00:00
-> where ( 'idAct=?' , $id )
2013-02-21 17:03:56 +00:00
-> join ( array ( 'r' => $this -> _schema . '.liensRef' ), 'l.idPar=r.id' ,
2012-11-23 15:34:49 +00:00
array ( 'LPAD(siren, 9, 000000000) AS siren' , 'PpPm' , 'RS' , 'civilite' , 'nom' , 'prenom' , 'nom_usage' ,
'naissance_date' , 'naissance_dept_pays' , 'naissance_lieu' , 'nat' ,
'adresse_num' , 'adresse_btq' , 'adresse_codvoie' , 'adresse_libvoie' ,
2013-02-18 08:35:19 +00:00
'adresse_comp' , 'adresse_cp' , 'adresse_ville' , 'adresse_pays' ,
'idLoc1Type' , 'idLoc1Num' , 'idLoc2Type' , 'idLoc2Num' , 'idLoc3Type' , 'idLoc3Num' ,
2012-11-23 15:34:49 +00:00
))
2012-12-03 10:49:19 +00:00
-> order ( 'PDetention DESC' );
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
//Actif / Inactif
if ( null !== $actif ) {
if ( false === $actif ) {
$sql -> where ( 'l.actif=?' , 0 );
} else {
$sql -> where ( 'l.actif=?' , 1 );
}
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
$liens = $this -> db -> fetchAll ( $sql , null , Zend_Db :: FETCH_OBJ );
2013-02-11 13:26:20 +00:00
} catch ( Zend_Db_Exception $e ) {
2013-02-21 17:03:56 +00:00
throw new Exception ( __METHOD__ . ': ' . $e -> getMessage ());
2012-11-23 15:34:49 +00:00
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
return $liens ;
}
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
/**
* Fonctions de direction
2012-12-04 15:54:34 +00:00
* @ param boolean $actif
2012-12-03 10:49:19 +00:00
* @ return Zend_Db_Table_Rowset_Abstract
*/
public function getDirections ( $actif = null )
{
if ( null === $this -> siren ) {
2013-02-21 17:03:56 +00:00
$refM = new Application_Model_JoLiensRef ();
2012-12-03 10:49:19 +00:00
$rows = $refM -> find ( $this -> idRef );
2012-12-04 15:54:34 +00:00
$siren = str_pad ( $rows -> current () -> siren , 9 , '0' , STR_PAD_LEFT );
2012-12-03 10:49:19 +00:00
} else {
$siren = $this -> siren ;
}
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
$result = array ();
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
if ( null !== $siren && intval ( $siren ) != 0 )
{
2013-02-21 17:03:56 +00:00
try {
$directionsM = new Application_Model_JoRncsDirigeants ();
$sql = $directionsM -> select () -> from ( $directionsM , array (
'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 ());
2012-12-03 10:49:19 +00:00
}
}
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
return $result ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
/**
* Retourne la maison mère
* @ param int $id
* @ return int
*/
public function getHead ( $id = null )
{
if ( null === $id ) {
$id = $this -> idRef ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
//Add ID to the list of known
$this -> findId [] = $id ;
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
//Through the list
2012-12-03 10:49:19 +00:00
$liens = $this -> getActionnaires ( $id , true );
2013-02-27 10:41:32 +00:00
2012-11-23 15:34:49 +00:00
//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 ) ) {
2013-02-27 09:58:11 +00:00
return $item -> idAct ;
2012-11-23 15:34:49 +00:00
}
//Same id
2012-12-03 10:49:19 +00:00
elseif ( $item -> idAct == $id ) {
2012-11-23 15:34:49 +00:00
return $id ;
}
//PDetention>50
2013-04-22 19:11:50 +00:00
elseif ( $item -> PDetention > 50 && $item -> idAct > 1000 ) {
2012-11-23 15:34:49 +00:00
return $this -> getHead ( $item -> idAct );
}
//MajMin=+
2013-04-22 19:11:50 +00:00
elseif ( $item -> MajMin == '+' && $item -> idAct > 1000 ) {
2012-11-23 15:34:49 +00:00
return $this -> getHead ( $item -> idAct );
}
//--
2012-12-03 10:49:19 +00:00
elseif ( $item -> idAct > 1000 ) {
2013-02-27 09:58:11 +00:00
return $this -> getHead ( $item -> idAct );
2012-11-23 15:34:49 +00:00
}
}
}
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
return $id ;
2012-11-23 15:34:49 +00:00
}
/**
* 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 ;
}
2013-02-11 13:26:20 +00:00
2013-02-21 17:03:56 +00:00
$refM = new Application_Model_JoLiensRef ();
2012-11-23 15:34:49 +00:00
$row = $refM -> find ( $id );
if ( null !== row ) {
return $row -> current ();
}
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
/**
* Retourne l ' arborescence pour les groupes
* @ param int $pctMin
* @ param int $nbNiveaux
* @ return array
*/
public function getTree ( $pctMin = 33 , $nbNiveaux = 10 )
{
//Récupération de la maison mère
$id = $this -> getHead ();
//Informations de la maison mère
$identity = $this -> getIdentity ( $id );
$this -> findId = array ();
$this -> findId [] = $identity -> id ;
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
$nom = $identity -> RS ;
if ( $identity -> nom != '' ) {
$nom = $identity -> civilite . ' ' . $identity -> nom . ' ' . $identity -> prenom ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
//Retour
$tabRet = array (
'id' => $identity -> id ,
2012-12-03 10:49:19 +00:00
'name' => $nom ,
2012-11-23 15:34:49 +00:00
'siren' => str_pad ( $identity -> siren , 9 , '0' , STR_PAD_LEFT ),
2012-12-03 10:49:19 +00:00
'pmin' => $item -> PDetention ,
2012-11-23 15:34:49 +00:00
'pays' => $identity -> adresse_pays ,
'children' => $this -> getTreeRecursive ( $identity -> id , $pctMin , 1 , $nbNiveaux ),
);
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
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 ();
2012-12-03 10:49:19 +00:00
$participations = $this -> getParticipations ( $id , true );
2012-11-23 15:34:49 +00:00
if ( count ( $participations ) > 0 ) {
foreach ( $participations as $item ) {
if ( $item -> PDetention > $pctMin ) {
$identity = $this -> getIdentity ( $item -> idPar );
2013-02-11 13:26:20 +00:00
2012-12-03 10:49:19 +00:00
$nom = $identity -> RS ;
if ( $identity -> nom != '' ) {
$nom = $identity -> civilite . ' ' . $identity -> nom . ' ' . $identity -> prenom ;
}
2013-02-11 13:26:20 +00:00
2012-11-23 15:34:49 +00:00
$data = array (
'id' => $identity -> id ,
2012-12-03 10:49:19 +00:00
'name' => $nom ,
2012-11-23 15:34:49 +00:00
'siren' => str_pad ( $identity -> siren , 9 , '0' , STR_PAD_LEFT ),
2012-12-03 10:49:19 +00:00
'pmin' => $item -> PDetention ,
2012-11-23 15:34:49 +00:00
'pays' => $identity -> adresse_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 ;
}
2013-08-22 13:01:01 +00:00
/**
*
* @ 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 [] = $result -> 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 ;
}
2012-11-23 15:34:49 +00:00
2013-08-22 13:01:01 +00:00
/**
*
* @ 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 ;
}
2012-11-23 15:34:49 +00:00
}