2015-03-24 14:00:26 +00:00

625 lines
22 KiB
PHP

<?php
require_once dirname(__FILE__) . '/Service.php';
/**
* Infogreffe : Document Acte
*/
class SdMetier_Infogreffe_DocAC extends SdMetier_Infogreffe_Service
{
const INT = 1000;
/**
* Db Adapter
* @var Zend_Db_Adapter_Abstract
*/
public $db;
/**
*
* @param string $siren
*/
public function __construct($siren, $db = null)
{
parent::__construct();
//Set type
$this->type_document = 'AC';
//Set Siren
$this->siren = $siren;
//Get defaut database adapter
if ($db === null) {
$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
} else {
$this->db = $db;
}
}
/**
* @param $onlyDb
* @return array
*/
public function getList($onlyDb = false)
{
$this->mode_diffusion = 'XL';
$this->reference_client = 'list-' . $this->siren;
$actesM = new Application_Model_JoGreffesActes($this->db);
//Requete WebService
$actesXML = null;
if ( $onlyDb === false ) {
//Infogreffe webservice
try {
$xml = $this->callRequest();
$requestOk = true;
} catch( Exception $e ) {
$requestOk = false;
//file_put_contents('debug.log', $e->getMessage());
//@todo : get error message
//echo $e->getMessage();
}
if ( $requestOk === true ) {
try {
$actesM->update(array('actif' => 0), 'siren='.$this->siren);
} catch(Zend_Db_Exception $e) {
}
$actesXML = $this->formatList($xml);
}
}
//Lecture de la base de données
$sql = $actesM->select()
->from($actesM, array(
'siren',
'numRC',
'LPAD(numGreffe,4,0) AS numGreffe',
'pdfLink',
'pdfSize',
'pdfPage',
'num_depot',
'date_depot',
'date_acte',
'LPAD(num_acte,2,0) AS num_acte',
'type_acte',
'type_acte_libelle',
'nbpages_acte',
'decision_nature',
'decision_libelle',
'mode_diffusion'
))
->where('siren=?', $this->siren)
->where('actif=1')
->order('date_depot DESC')
->order('num_depot DESC')
->order('num_acte ASC')
->order('date_acte DESC');
$rows = $actesM->fetchAll($sql);
$actes = array();
if ( count($rows)>0 ) {
foreach ( $rows as $row ) {
$item = new stdClass();
$item->File = $row->pdfLink;
$item->FileSize = $row->pdfSize;
$item->FileNumberOfPages = $row->pdfPage;
$item->DepotNum = $row->num_depot;
$item->DepotDate = $row->date_depot;
$item->ActeNum = $row->num_acte;
$item->ActeDate = $row->date_acte;
$item->ActeNumberOfPages = $row->nbpages_acte;
$item->ActeType = $row->type_acte;
$item->ActeTypeLabel = $row->type_acte_libelle;
$item->ActeDecisionNature = $row->decision_nature;
$item->ActeDecisionLabel = $row->decision_libelle;
$decisions = $row->decision_nature;
if (!empty($row->decision_nature) && !empty($row->decision_libelle)) {
$decisions.= ' : ';
}
$decisions.= $row->decision_libelle;
$item->infos[] = $decisions;
$mode_diffusion = explode(',', $row->mode_diffusion);
if (in_array('T',$mode_diffusion) || !empty($item->File)) {
$item->ModeDiffusion = 'T';
} elseif (in_array('C',$mode_diffusion)) {
$item->ModeDiffusion = 'C';
} else {
$item->ModeDiffusion = '';
}
$actes[] = $item;
}
}
return $actes;
}
/**
* @todo : En cours
* @param string $depotDate
* @param int $depotNum
* @param string $acteType
* @param string $acteDate
* @param int $acteNum
* @param int $orderId
* @throws Exception
* @return string
* Return the full path of the file
*/
public function getCommandeT($depotDate, $depotNum, $acteType, $acteDate, $acteNum, $orderId = null)
{
//Lire dans la base de données
$actesM = new Application_Model_JoGreffesActes($this->db);
$sql = $actesM->select()
->from($actesM, array(
'siren',
'numRC',
'LPAD(numGreffe,4,0) AS numGreffe',
'pdfLink',
'num_depot',
'date_depot',
'date_acte',
'LPAD(num_acte,2,0) AS num_acte',
'type_acte',
))
->where('siren=?', $this->siren)
->where('num_depot=?', $depotNum)
->where('date_depot=?', $depotDate)
->where('num_acte=?', $acteNum)
->where('date_acte=?', $acteDate)
->where('type_acte=?', $acteType);
$row = $actesM->fetchRow($sql);
if ( null === $row ) {
throw new Exception('Not exist');
}
$this->mode_diffusion = 'T';
$this->reference_client = 'T'.date('YmdHis');
$this->greffe = $row->numGreffe;
$this->dossier_millesime = substr($row->numRC,0,2);
$this->dossier_statut = substr($row->numRC,2,1);
$this->dossier_chrono = substr($row->numRC,3);
$this->date_depot = $row->date_depot;
$this->num_depot = $row->num_depot;
$this->date_acte = $row->date_acte;
$this->num = $row->num_acte;
//Needed element for filename
$date = $row->date_acte;
if ( $date == '0000-00-00' ) {
$date = $row->date_depot;
}
$type = $row->type_acte;
$num = $row->num_acte;
$options = $row->numGreffe . '-' . substr($row->numRC,0,2) . '-' . substr($row->numRC,2,1) . '-' . substr($row->numRC,3) . '-' . $row->num_depot;
//Set filename
$filename = $this->getFilePath($date) . '/' . $this->getFileName($date, $num, $type, $options);
if ( $row->pdfLink != '' ) {
//Set the filename
$filename = $this->getFilePath($date) . '/' . $row->pdfLink;
//Check if filename exist
if ( !file_exists($this->config->storage->path . '/' . $filename) ) {
throw new Exception('File not found', self::INT);
}
} elseif ( file_exists($this->config->storage->path . '/' . $filename) ) {
//Analyser le fichier - Nombre de page et taille
$infos = $this->pdfInfos($this->config->storage->path . '/' . $filename);
//Enregistrer les infos du fichier dans la base de données
if (false !== $infos) {
$this->dbSetFile(basename($filename), $infos['size'], $infos['pages'], $infos['version']);
} else {
$this->dbSetFile(basename($filename));
}
} else {
try {
$xml = $this->callRequest();
} catch(Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
$acte = $this->formatItem($xml);
$url = $acte['actes'][0]['url_acces'];
if (empty($url)) {
throw new Exception('File url not given');
}
if ( $orderId !== null ) {
$commandeM = new Application_Model_Sdv1GreffeCommandesAc();
$commandeM->update(array(
'cmdUrl'=> $url,
'dateCommande' => date('YmdHis'),
), 'id='.$orderId);
}
//Récupérer le fichier
$getfile = $this->download($url, $filename);
//Analyser le fichier - Nombre de page et taille
$infos = $this->pdfInfos($getfile);
//Enregistrer les infos du fichier dans la base de données
if (false !== $infos) {
$this->dbSetFile(basename($filename), $infos['size'], $infos['pages'], $infos['version']);
} else {
$this->dbSetFile(basename($filename));
}
}
return $filename;
}
/**
*
* @param unknown $depotDate
* @param unknown $depotNum
* @param unknown $acteType
* @param unknown $acteDate
* @param unknown $acteNum
* @param string $reference
* @throws Exception
* @return boolean
*/
public function getCommandeC($depotDate, $depotNum, $acteType, $acteDate, $acteNum, $reference = '')
{
//Lire dans la base de données
$actesM = new Application_Model_JoGreffesActes();
$sql = $actesM->select()
->from($actesM, array(
'siren',
'numRC',
'LPAD(numGreffe,4,0) AS numGreffe',
'pdfLink',
'num_depot',
'date_depot',
'date_acte',
'LPAD(num_acte,2,0) AS num_acte',
'type_acte',
))
->where('siren=?', $this->siren)
->where('num_depot=?', $depotNum)
->where('date_depot=?', $depotDate)
->where('num_acte=?', $acteNum)
->where('date_acte=?', $acteDate)
->where('type_acte=?', $acteType);
$row = $actesM->fetchRow($sql);
if ( null === $row ) {
throw new Exception('Not exist');
}
$this->mode_diffusion = 'C';
$this->reference_client = $reference;
//Générer les paramètres de commande depuis la base de données
$this->greffe = $row->numGreffe;
$this->dossier_millesime = substr($row->numRC,0,2);
$this->dossier_statut = substr($row->numRC,2,1);
$this->dossier_chrono = substr($row->numRC,3);
$this->num_depot = $row->num_depot;
$this->type_acte = $row->type_acte;
$this->date_acte = $row->date_acte;
$this->num = $row->num_acte;
//Faire la requete
try {
$xml = $this->callRequest();
} catch(Exception $e) {
//La prise en charge du courrier est effective
if ( $e->getCode() != 17 ) {
throw new Exception($e->getMessage());
}
}
return true;
}
/**
*
* @param string $date
* Date de l'acte au format AAAA-MM-JJ
* @param string $num
* Numéro de l'acte
* @param string $type
* Type de l'acte
* @param string $options
* (Numéro du Greffe)-(dossier_millesime)-(dossier_statut)-(dossier_chrono)-(num_depot)
* @return string
*/
public function getFileName($date, $num, $type, $options)
{
$date = substr($date,0,4) . substr($date,5,2) . substr($date,8,2);
return 'acte-' . $this->siren . '-' . $type . '-' . $date . '-' . $options . '-' . $num . '.pdf';
}
/**
*
* @param string $date
* Date de l'acte au format AAAA-MM-JJ
* @return string
*/
public function getFilePath($date)
{
$dir = 'actes/' . substr($date,0,4) . '/' . substr($date,5,2);
if ( !file_exists( $this->config->storage->path . '/' . $dir ) ) {
mkdir($this->config->storage->path . '/' . $dir, 0777, true);
}
return $dir;
}
/**
* Format XML to Array for a list of items
* @param string $xml
* @return array
*/
protected function formatList($xml)
{
$doc = new DOMDocument();
$doc->loadXML($xml);
$liste_depot_acte = $doc->getElementsByTagName('liste_depot_acte')->item(0);
$depot_actes = $liste_depot_acte->getElementsByTagName('depot_acte');
$actes = array();
foreach($depot_actes as $depot_acte)
{
$acte = array();
$acte['num_gest'] = array();
$num_gest = $depot_acte->getElementsByTagName('num_gest')->item(0);
$acte['num_gest']['greffe'] = $num_gest->getElementsByTagName('greffe')->item(0)->nodeValue;
$acte['num_gest']['dossier_millesime'] = $num_gest->getElementsByTagName('dossier_millesime')->item(0)->nodeValue;
$acte['num_gest']['dossier_statut'] = $num_gest->getElementsByTagName('dossier_statut')->item(0)->nodeValue;
$acte['num_gest']['dossier_chrono'] = $num_gest->getElementsByTagName('dossier_chrono')->item(0)->nodeValue;
$acte['num_siren'] = $depot_acte->getElementsByTagName('num_siren')->item(0)->nodeValue;
$acte['num_depot'] = $depot_acte->getElementsByTagName('num_depot')->item(0)->nodeValue;
$acte['date_depot'] = $depot_acte->getElementsByTagName('date_depot')->item(0)->nodeValue;
$infoActes = $depot_acte->getElementsByTagName('acte');
$acte['depot'] = array();
foreach($infoActes as $infoActe)
{
$actenum = array();
$actenum['date_acte'] = $infoActe->getElementsByTagName('date_acte')->item(0)->nodeValue;
if ($infoActe->getElementsByTagName('date_acte')->item(0)->nodeValue == '') {
$actenum['date_acte'] = '0000-00-00';
}
$actenum['num_acte'] = $infoActe->getElementsByTagName('num_acte')->item(0)->nodeValue;
$actenum['type_acte'] = $infoActe->getElementsByTagName('type_acte')->item(0)->nodeValue;
$actenum['type_acte_libelle'] = $infoActe->getElementsByTagName('type_acte_libelle')->item(0)->nodeValue;
$actenum['nbpages_acte'] = $infoActe->getElementsByTagName('nbpages_acte')->item(0)->nodeValue;
$decision = $infoActe->getElementsByTagName('decision')->item(0);
if($decision)
{
$actenum['decision'] = array();
$actenum['decision']['nature'] = $decision->getElementsByTagName('nature')->item(0)->nodeValue;
$actenum['decision']['libelle'] = $decision->getElementsByTagName('libelle')->item(0)->nodeValue;
}
$actenum['mode_diffusion'] = array();
$mode_diffusion = $infoActe->getElementsByTagName('mode_diffusion')->item(0)->getElementsByTagName('mode');
foreach($mode_diffusion as $mode)
{
$actenum['mode_diffusion'][] = $mode->getAttribute('type');
}
$acte['depot'][] = $actenum;
}
//Fin listes des infos acte
//Enregistrer dans la bdd
$this->dbUpdateItem($acte);
//Génération de l'index pour le tri
if(!empty($acte['date_depot'])){ $date = $acte['date_depot']; }
else { $date = ''; }
if(!empty($date))
{
$datef = substr($date,0,4).substr($date,5,2).substr($date,8,2);
$key = $datef.'-'.$acte['num_depot'];
//Affectation liste générale avec un index permettant le tri
$actes[$key] = $acte;
}
//Prise en compte de l'acte -1
if($acte['num_depot']==-1)
{
$actes[0] = $acte;
}
}
//Tri suivant la date d'acte ou de depot
krsort($actes);
return $actes;
}
/**
* Format XML to Array for one item
* @param string $xml
* @return array
*/
protected function formatItem($xml)
{
$doc = new DOMDocument();
$doc->loadXML($xml);
$depot_acte = $doc->getElementsByTagName('depot_acte')->item(0);
$info = array();
$info['num_gest'] = array();
$num_gest = $depot_acte->getElementsByTagName('num_gest')->item(0);
$info['num_gest']['greffe'] = $num_gest->getElementsByTagName('greffe')->item(0)->nodeValue;
$info['num_gest']['dossier_millesime'] = $num_gest->getElementsByTagName('dossier_millesime')->item(0)->nodeValue;
$info['num_gest']['dossier_statut'] = $num_gest->getElementsByTagName('dossier_statut')->item(0)->nodeValue;
$info['num_gest']['dossier_chrono'] = $num_gest->getElementsByTagName('dossier_chrono')->item(0)->nodeValue;
$info['num_siren'] = $depot_acte->getElementsByTagName('num_siren')->item(0)->nodeValue;
$info['num_depot'] = $depot_acte->getElementsByTagName('num_depot')->item(0)->nodeValue;
$info['date_depot'] = $depot_acte->getElementsByTagName('date_depot')->item(0)->nodeValue;
$infoActes = $depot_acte->getElementsByTagName('acte');
$info['actes'] = array();
foreach($infoActes as $infoActe)
{
$actenum = array();
$actenum['date_acte'] = $infoActe->getElementsByTagName('date_acte')->item(0)->nodeValue;
$actenum['num_acte'] = $infoActe->getElementsByTagName('num_acte')->item(0)->nodeValue;
$actenum['type_acte'] = $infoActe->getElementsByTagName('type_acte')->item(0)->nodeValue;
$actenum['type_acte_libelle'] = $infoActe->getElementsByTagName('type_acte_libelle')->item(0)->nodeValue;
$actenum['nbpages_acte'] = $infoActe->getElementsByTagName('nbpages_acte')->item(0)->nodeValue;
$decision = $infoActe->getElementsByTagName('decision')->item(0);
if($decision)
{
$actenum['decision'] = array();
$actenum['decision']['nature'] = $decision->getElementsByTagName('nature')->item(0)->nodeValue;
$actenum['decision']['libelle'] = $decision->getElementsByTagName('libelle')->item(0)->nodeValue;
}
$actenum['url_acces'] = htmlspecialchars_decode($infoActe->getElementsByTagName('url_acces')->item(0)->nodeValue);
$info['actes'][] = $actenum;
}
return $info;
}
/**
* Update informations about an item in database
* @param array $list
*/
protected function dbUpdateItem($list)
{
foreach ($list['depot'] as $depot) {
$data = array(
'siren' => $list['num_siren'],
'numRC' => $list['num_gest']['dossier_millesime'].
$list['num_gest']['dossier_statut'].$list['num_gest']['dossier_chrono'],
'numGreffe' => $list['num_gest']['greffe'],
'num_depot' => $list['num_depot'],
'date_depot' => $list['date_depot'],
'date_acte' => $depot['date_acte'],
'num_acte' => $depot['num_acte'],
'type_acte' => $depot['type_acte'],
'type_acte_libelle' => $depot['type_acte_libelle'],
'nbpages_acte' => $depot['nbpages_acte'],
'decision_nature' => empty($depot['decision']['nature']) ? '' : $depot['decision']['nature'] ,
'decision_libelle' => empty($depot['decision']['libelle']) ? '' : $depot['decision']['libelle'] ,
'mode_diffusion' => join(',',$depot['mode_diffusion']),
'actif' => 1,
);
//Only new element are inserted
try {
$acteM = new Application_Model_JoGreffesActes($this->db);
$sql = $acteM->select()
->where('siren=?', $list['num_siren'])
->where('num_depot=?', intval($list['num_depot']))
->where('date_depot=?', $list['date_depot'])
//->where('date_acte=?', $depot['date_acte'])
->where('num_acte=?', intval($depot['num_acte']))
->order('dateInsert DESC');
$rows = $acteM->fetchAll($sql);
} catch(Zend_Db_Adapter_Exception $e) {
throw new Exception($e->getMessage());
}
//Insert new element
if ( count($rows)==0 ) {
try {
//Add dateInsert
$data['dateInsert'] = date('YmdHis');
$result = $acteM->insert($data);
} catch(Zend_Exception $e) {
throw new Exception($e->getMessage());
}
}
//Update information
else {
//Correct multiple item
$item = $rows[0];
if ( count($rows) > 1 ) {
try {
$result = $acteM->delete(array(
'siren='.$this->siren,
'num_depot='.intval($list['num_depot']),
'date_depot="'.$list['date_depot'].'"',
'num_acte='.intval($depot['num_acte']),
'id!='.$item->id,
));
} catch(Zend_Exception $e) {
throw new Exception($e->getMessage());
}
}
try {
$result = $acteM->update($data, 'id='.$item->id);
} catch(Zend_Exception $e) {
throw new Exception($e->getMessage());
}
}
}
return true;
}
/**
* Set file informations in database
* @param string $filename
* @param int $size
* @param int $numberOfPage
* @param string $version
* @return boolean
*/
protected function dbSetFile($filename, $size = 0, $numberOfPage = '', $version = '')
{
$data = array(
'pdfLink' => $filename,
'pdfSize' => $size,
'pdfPage' => $numberOfPage,
'pdfVer' => $version,
'pdfDate' => date('Ymd'),
);
$where = array(
'siren='.$this->siren,
'num_depot='.$this->num_depot,
'date_depot="'.$this->date_depot.'"',
'date_acte="'.$this->date_acte.'"',
'num_acte='.$this->num,
);
try {
$acteM = new Application_Model_JoGreffesActes();
$result = $acteM->update($data, $where);
} catch(Zend_Db_Adapter_Exception $e) {
throw new Exception($e->getMessage());
} catch(Zend_Db_Exception $e) {
throw new Exception($e->getMessage());
}
if ($result) {
return true;
}
return false;
}
}