Michael RICOIS 53e213202b CS PSR-2
2016-11-24 16:02:49 +01:00

628 lines
23 KiB
PHP
Executable File

<?php
require_once dirname(__FILE__) . '/Service.php';
/**
* Infogreffe : Document Acte
*/
class Metier_Infogreffe_DocAC extends Metier_Infogreffe_Service
{
const INT = 1000;
/**
* PDO Connection with Doctrine
* @var \Doctrine\DBAL\Connection
*/
protected $conn;
/**
* Infogreffe : Document Bilan
* @param \Doctrine\DBAL\Connection $conn
*/
public function __construct($conn = null)
{
parent::__construct();
// Set Database
if ($conn === null) {
$this->conn = Zend_Registry::get('doctrine');
} else {
$this->conn = $conn;
}
// Set type
$this->type_document = 'AC';
}
/**
* Défini le SIREN
* @param string $siren
* @return void
*/
public function setSiren($siren)
{
$this->siren = $siren;
}
/**
* @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) {
//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) {
// Set All line state to 0
try {
$this->conn->update('jo.greffes_actes',
array('actif' => 0), array('siren' => $this->siren));
} catch (\Doctrine\DBAL\DBALException $e) {
}
$actesXML = $this->formatList($xml);
}
}
// Lecture de la base de données
$columns = 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',
);
try {
$qb = $this->conn->createQueryBuilder();
$qb->select($columns)->from('jo.greffes_actes')
->where('siren=:siren')->setParameter('siren', $this->siren)->andWhere('actif=1')
->orderBy('date_depot', 'DESC')->orderBy('num_depot', 'DESC')
->orderBy('num_acte', 'ASC')->orderBy('date_acte', 'DESC');
$stmt = $qb->execute();
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception(__METHOD__ . ': ' . $e->getMessage());
}
$actes = array();
if ($stmt->rowCount() > 0) {
$rows = $stmt->fetchAll(\PDO::FETCH_OBJ);
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;
}
/**
* Commande Téléchargement
* @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
$columns = 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',
);
$qb = $this->conn->createQueryBuilder();
$qb->select($columns)->from('jo.greffes_actes')
->where('siren=:siren')->setParameter('siren', $this->siren)
->andWhere('num_depot=:depotNum')->setParameter('depotNum', $depotNum)
->andWhere('date_depot=:depotDate')->setParameter('depotDate', $depotDate)
->andWhere('num_acte=:acteNum')->setParameter('acteNum', $acteNum)
->andWhere('date_acte=:acteDate')->setParameter('acteDate', $acteDate)
->andWhere('type_acte=:acteType')->setParameter('acteType', $acteType);
$stmt = $qb->execute();
if ($stmt->rowCount() == 0) {
throw new Exception('Not exist');
}
$row = $stmt->fetch(\PDO::FETCH_OBJ);
$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;
$dir = $this->getFilePath($date);
if (! file_exists($this->config->storage->path . '/' . $dir)) {
mkdir($this->config->storage->path . '/' . $dir, 0777, true);
}
// Set filename
$filename = $dir . '/' . $this->getFileName($date, $num, $type, $options);
if ($row->pdfLink != '') {
// Set the filename
$filename = $dir . '/' . $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) {
$this->conn->update('sdv1.greffe_commandes_ac', array(
'cmdUrl' => $url,
'dateCommande' => date('YmdHis')),
array('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;
}
/**
* Commande courrier
* @param string $depotDate
* @param int $depotNum
* @param string $acteType
* @param string $acteDate
* @param int $acteNum
* @param string $reference
* @throws Exception
* @return boolean
*/
public function getCommandeC($depotDate, $depotNum, $acteType, $acteDate, $acteNum, $reference = '')
{
// Lire dans la base de données
$columns = 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',
);
$qb = $this->conn->createQueryBuilder();
$qb->select($columns)->from('jo.greffes_actes')
->where('siren=:siren')->setParameter('siren', $this->siren)
->andWhere('num_depot=:depotNum')->setParameter('depotNum', $depotNum)
->andWhere('date_depot=:depotDate')->setParameter('depotDate', $depotDate)
->andWhere('num_acte=:acteNum')->setParameter('acteNum', $acteNum)
->andWhere('date_acte=:acteDate')->setParameter('acteDate', $acteDate)
->andWhere('type_acte=:acteType')->setParameter('acteType', $acteType);
$stmt = $qb->execute();
if ($stmt->rowCount() == 0) {
throw new Exception('Not exist');
}
$this->mode_diffusion = 'C';
$this->reference_client = $reference;
$row = $stmt->fetch(\PDO::FETCH_OBJ);
// 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)
{
return 'actes/' . substr($date, 0, 4) . '/' . 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;
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,
);
try {
$sql = "SELECT * FROM jo.greffes_actes
WHERE siren=:siren AND num_depot=:depotNum AND date_depot=:depotDate
AND num_acte=:acteNum ORDER BY dateInsert";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue('siren', $this->siren);
$stmt->bindValue('depotNum', intval($list['num_depot']));
$stmt->bindValue('depotDate', $list['date_depot']);
$stmt->bindValue('acteNum', intval($depot['num_acte']));
$stmt->execute();
// Insert
if ($stmt->rowCount() == 0) {
$data['dateInsert'] = date('YmdHis');
try {
$result = $this->conn->insert('jo.greffes_actes', $data);
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception($e->getMessage());
}
}
// Update
else {
// Get first Item
$item = $stmt->fetch(\PDO::FETCH_OBJ);
// Correction anomalie item multiple
if ($stmt->rowCount() > 1) {
$this->conn->beginTransaction();
try {
while ($rowDel = $stmt->fetch(\PDO::FETCH_OBJ)) {
$this->conn->delete('jo.greffes_actes', array(
'id' => $rowDel->id));
}
$this->conn->commit();
} catch (\Doctrine\DBAL\DBALException $e) {
$this->conn->rollBack();
throw new Exception($e->getMessage());
}
}
// Real Update
try {
$result = $this->conn->update('jo.greffes_actes', $data,
array('id' => $item->id));
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception($e->getMessage());
}
}
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception($e->getMessage());
}
}
if ($result) {
return true;
}
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'),
);
$identifier = array(
'siren' => $this->siren,
'num_depot' => $this->num_depot,
'date_depot' => $this->date_depot,
'date_acte' => $this->date_acte,
'num_acte' => $this->num,
);
try {
$result = $this->conn->update('jo.greffes_actes', $data, $identifier);
} catch (\Doctrine\DBAL\DBALException $e) {
throw new Exception($e->getMessage());
}
if ($result) {
return true;
}
return false;
}
}