webservice/library/Metier/Infogreffe/InfogreffeAc.php

472 lines
16 KiB
PHP
Raw Normal View History

2013-11-05 11:18:30 +00:00
<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Infogreffe.php';
/**
* Infogreffe : Document Acte
*/
class Metier_Infogreffe_Ac extends Metier_Infogreffe
{
/**
* Db Adapter
* @var Zend_Db_Adapter_Abstract
*/
public $db;
/**
*
* @param string $siren
*/
public function __construct($siren, $db = null)
{
//@todo : Inject db in batch
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;
//Requete WebService
$actesXML = null;
if ( $onlyDb === false ) {
$this->debug = true;
//Infogreffe webservice
$xml = $this->callRequest();
$actesXML = $this->formatList($xml);
}
//Lecture de la base de données
$actesM = new Application_Model_JoGreffesActes($this->db);
$sql = $actesM->select()
->where('siren=?', $this->siren)
->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;
$decisions = $row->decision_nature;
if (!empty($row->decision_nature) && !empty($row->decision_libelle)) {
$decisions.= ' : ';
}
$decisions.= $row->decision_libelle;
$item->infos[] = $decisions;
//@todo : si présence de fichier alors mode T
$mode_diffusion = explode(',', $row->mode_diffusion);
if (in_array('T',$mode_diffusion)) {
$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
* @throws Exception
* @return string
* Return the full path of the file
*/
public function getCommandeT($depotDate, $depotNum, $acteType, $acteDate, $acteNum)
{
$this->mode_diffusion = 'T';
$this->reference_client = 'T'.date('YmdHis');
//Lire dans la base de données
$actesM = new Application_Model_JoGreffesActes($this->db);
//@todo : set vars
$sql = $actesM->select()
->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');
}
//Needed element for filename
$date = $row->date_acte;
$type = $row->type_acte;
$num = $row->num_acte;
$options = $row->numGreffe . '-' . substr($row->numR,0,2) . '-' . substr($row->numR,2,1) . '-' . substr($row->numR,3) . '-' . $row->num_depot;
$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;
if ( $row->pdfLink != '' ) {
//Set the filename
$filename = $this->getFilePath($date) . DIRECTORY_SEPARATOR . $this->getFileName($date, $num, $type, $options);
//Check if filename exist
if ( !file_exists($this->config->storage->path . DIRECTORY_SEPARATOR . $filename) ) {
throw new Exception('File not found');
}
} else {
$xml = $this->callRequest();
$acte = $this->formatItem($xml);
$url = $acte['url_acces'];
if (empty($url)) {
throw new Exception('File url not given');
}
//Set the filename
$filename = $this->getFilePath($date) . DIRECTORY_SEPARATOR . $this->getFileName($date, $num, $type, $options);
//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($filename, $infos['size'], $infos['pages'], $infos['version']);
}
}
return $filename;
}
public function getCommandeC($id)
{
$this->mode_diffusion = 'C';
//Enregistrer la commande dans la base de données
//Récupérer l'id de commande
$this->reference_client = 'G'.$id;
$xml = $this->callRequest();
//@todo :
}
/**
*
* @param string $date
* Date de l'acte au format AAAAMMJJ
* @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)
{
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)
{
return 'actes' . DIRECTORY_SEPARATOR . substr($date,0,4) . DIRECTORY_SEPARATOR . substr($date,5,2);
}
/**
* 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;
$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)
{
//siren
//numRC
//numGreffe
//num_depot
//date_depot
//date_acte
//num_acte
//type_acte => Attention garder la version 1 et 2
//type_acte_libelle
//nbpages_acte
//decision_nature
//decision_libelle
//mode_diffusion
$data = array(
'siren' => $list['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' => $list['date_acte'],
'num_acte' => $list['num_acte'],
'type_acte' => $list['type_acte'],
'type_acte_libelle' => $list['type_acte_libelle'],
'nbpages_acte' => $list['nbpages_acte'],
'decision_nature' => $list['decision']['nature'],
'decision_libelle' => $list['decision']['libelle'],
'mode_diffusion' => join(',',$list['mode_diffusion']),
);
//Only new element are inserted
try {
$acteM = new Application_Model_JoGreffesActes($this->db);
$acteM->select()
->where('siren=?', $list['siren'])
->where('num_depot=?', $list['num_depot'])
->where('date_depot=?', $list['date_depot'])
->where('date_acte=?', $list['date_acte'])
->where('num_acte=?', $list['num_acte'])
->where('type_acte=?', $list['type_acte']); //Attention type lors du téléchargement
if ( null === $acteM->fetchRow($sql) ) {
$result = $acteM->insert($data);
}
} 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;
}
/**
* Set file informations in database
* @param string $filename
* @param int $size
* @param int $numberOfPage
* @param string $version
* @return boolean
*/
protected function dbSetFile($filename, $size, $numberOfPage, $version)
{
$data = array(
'pdfLink' => $filename,
'pdfSize' => $size,
'pdfPage' => $numberOfPage,
'pdfVer' => $version
);
$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;
}
}