webservice/scripts/jobs/filesGreffes.php
2014-04-11 18:42:29 +00:00

583 lines
19 KiB
PHP

<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
//Use classmap autoloader - useful with opcode and realpath cache
require_once 'Zend/Loader/AutoloaderFactory.php';
require_once 'Zend/Loader/ClassMapAutoloader.php';
Zend_Loader_AutoloaderFactory::factory(array(
'Zend_Loader_ClassMapAutoloader' => array(
__DIR__ . '/../../library/Zend/autoload_classmap.php',
__DIR__ . '/../../library/Application/autoload_classmap.php',
__DIR__ . '/../../library/Scores/autoload_classmap.php',
__DIR__ . '/../../application/autoload_classmap.php',
),
'Zend_Loader_StandardAutoloader' => array(
'prefixes' => array(
'Zend' => __DIR__ . '/../../library/Zend',
'Application' => __DIR__ . '/../../library/Application',
'Scores' => __DIR__ . '/../../library/Scores',
'Metier' => __DIR__ . '/../../library/Metier',
),
'fallback_autoloader' => true
)
));
// Zend_Application - Use it if you don't have autoloaders
//require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
try {
$opts = new Zend_Console_Getopt(
//Options
array(
'help|?' => "Affiche l'aide.",
'acte' => "Traitement des actes ",
'bilan-s' => "Traitement des bilans (année millesime à traiter)",
'siren=s' => "Ecrase les données du fichier pour les éléments du siren",
'max=s' => "Nombre d'unités maximum à traités",
'part=s' => "Offset",
'total' => "Get total",
'files' => "Lister les fichiers puis intégration",
'numdepot=s' => "Specifier repetoire pour integrer fichier nommé (SIREN_MILLESIME_NUMDEPOT_BilanComplet.pdf) ",
'debug' => "Debug",
)
);
$opts->parse();
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
}
//Usage
if(count($opts->getOptions())==0 || isset($opts->help))
{
echo "Set informations in database for greffe files.";
echo "\n\n";
echo $opts->getUsageMessage();
echo "\n";
exit;
}
$c = new Zend_Config($application->getOptions());
//@todo : Remove the use of registry
Zend_Registry::set('config', $c);
/**
* Connexion à la base de données
*/
$db = Zend_Db::factory($c->profil->db->metier);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
//Bilans
if ($opts->bilan) {
if ($opts->files) {
require_once 'Metier/Infogreffe/InfogreffeBi.php';
$nbBilans = 0;
//Type de bilan
$types = array('consolides', 'sociaux');
foreach ( $types as $type ) {
$path = $c->profil->infogreffe->storage->path . DIRECTORY_SEPARATOR . 'bilans';
echo "Bilans ".$type.PHP_EOL;
//AAAA
echo "Liste des années...".PHP_EOL;
$dirYears = array();
$path = $path . DIRECTORY_SEPARATOR . $type;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ( $file != '..' && $file != '.' && is_dir($path . DIRECTORY_SEPARATOR . $file)) {
$dirYears[] = $file;
}
}
sort($dirYears);
}
//Fichier
foreach ($dirYears as $year) {
echo "Traitement des fichiers $type $year".PHP_EOL;
$pathFile = $path . DIRECTORY_SEPARATOR . $year;
if ($handle = opendir($pathFile)) {
while (false !== ($file = readdir($handle))) {
if ( $file != '..' && $file != '.' ) {
//'bilan-' . $this->siren . '-' . $type . '-' . $dateCloture . '.pdf';
if (preg_match('/^bilan-([0-9]{9})-(sociaux|consolides)-([0-9]{8})\.pdf/', $file, $matches)) {
echo "Fichier ".$file;
$siren = $matches[1];
$typeComptes = $matches[2];
$dateCloture = substr($matches[3],0,4).'-'.substr($matches[3],4,2).'-'.substr($matches[3],6,2);
//Recherche dans la bdd
$bilanM = new Application_Model_JoGreffesBilans();
$sql = $bilanM->select()
->where('siren=?',$siren)
->where('date_cloture=?', $dateCloture);
if ($typeComptes=='sociaux') {
$sql->where('type_comptes="" OR type_comptes="sociaux"');
} else {
$sql->where('type_comptes="consolides"');
}
$sql->where('actif=1');
$sql->order('dateInsert DESC')->order('num_depot DESC')->limit(1);
$item = $bilanM->fetchRow($sql);
if ($item === null) {
//If not find write the database
$infogreffe = new Metier_Infogreffe_Bi($siren);
$infogreffe->getList();
//And get again the item in database
$item = $bilanM->fetchRow($sql);
}
if ($item === null) {
echo " = non trouvé.";
} else {
if ($item->pdfDate == '0000-00-00') {
$infogreffe = new Metier_Infogreffe_Bi($siren);
$infos = $infogreffe->pdfInfos($pathFile. DIRECTORY_SEPARATOR . $file);
if (false !== $infos) {
$data = array(
'pdfLink' => $file,
'pdfSize' => $infos['size'],
'pdfPage' => $infos['pages'],
'pdfVer' => $infos['version'],
'pdfDate' => date('Ymd'),
);
try {
$result = $bilanM->update($data, 'id='.$item->id);
echo " = enregistrement.";
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
} catch(Zend_Db_Exception $e) {
echo $e->getMessage();
}
}
} else {
echo " = présent.";
}
}
echo PHP_EOL;
}
$nbBilans++;
}
}
}
}
}
echo "Nombre de fichier : ".$nbBilans.PHP_EOL;
}
/**
* Lecture des fichiers depuis un répertoire
* siren_YYYY_NUMDEPOT_BilanComplet.pdf
*/
elseif ($opts->numdepot) {
require_once 'Metier/Infogreffe/InfogreffeBi.php';
echo $opts->numdepot."\n";
if( !is_dir($opts->numdepot) ) {
echo "Répertoire inexistant !\n"; exit;
}
if ($handle = opendir($opts->numdepot)) {
while (false !== ($file = readdir($handle))) {
if ( $file != '..' && $file != '.' ) {
if (preg_match('/^([0-9]{9})_([0-9]{4})_([0-9]{1,})_(.*)\.pdf/', $file, $matches)) {
echo $file;
$siren = $matches[1];
$millesime = $matches[2];
$depot = $matches[3];
$bilanM = new Application_Model_JoGreffesBilans();
$sql = $bilanM->select()
->from($bilanM, array('id','siren','millesime', 'date_cloture','type_comptes', 'pdfDate'))
->where('siren=?', $siren)
->where('millesime=?', $millesime)
->where('num_depot=?', $depot);
$item = $bilanM->fetchRow($sql);
if ($item === null) {
//If not find write the database
$infogreffe = new Metier_Infogreffe_Bi($siren);
$infogreffe->getList();
//And get again the item in database
$item = $bilanM->fetchRow($sql);
}
if ($item === null) {
echo " = non trouvé.";
} else {
if ($item->pdfDate == '0000-00-00') {
$infogreffe = new Metier_Infogreffe_Bi($siren);
$type = "sociaux";
if ( !empty($item->type_comptes) ) {
$type = $item->type_comptes;
}
//File path - 2012_sociaux-20121231-7501-55-B-14450-0035567
$newfile = $infogreffe->getFileName($type, $item->date_cloture);
$filepath = $c->profil->infogreffe->storage->path . '/' .
$infogreffe->getFilePath($type, $item->date_cloture) . '/' . $newfile;
if (copy($opts->numdepot . '/' .$file, $filepath)) {
$infos = $infogreffe->pdfInfos($filepath);
if (false !== $infos) {
$data = array(
'pdfLink' => $file,
'pdfSize' => $infos['size'],
'pdfPage' => $infos['pages'],
'pdfVer' => $infos['version'],
'pdfDate' => date('Ymd'),
);
try {
$result = $bilanM->update($data, 'id='.$item->id);
echo " = enregistrement.";
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
} catch(Zend_Db_Exception $e) {
echo $e->getMessage();
}
}
} else {
echo " = copie du fichier impossible.";
}
} else {
echo " = présent.";
}
}
echo PHP_EOL;
}
}
}
}
}
//Lecture de toute la base de données
else {
//Lire la base de données
$bilanM = new Application_Model_JoGreffesBilans();
$sql = $bilanM->select()
->from($bilanM, array('siren','millesime', 'date_cloture','type_comptes'))
->order('millesime DESC');
if ( is_numeric($opts->bilan) ){
$sql->where('millesime=?', $opts->bilan);
}
if ( $opts->total ) {
$nbTotal = $bilanM->fetchAll($sql)->count();
echo "Total : ".$nbTotal.PHP_EOL;
exit;
}
if ( $opts->max && $opts->part ) {
$sql->limit($opts->max, $opts->part);
} elseif ($opts->max) {
$sql->limit($opts->max);
}
$nbTotal = $bilanM->fetchAll($sql)->count();
if ( $opts->siren ) {
$sql->where('siren=?',$opts->siren);
} else {
$sql->where('pdfDate=?','0000-00-00');
}
$sql->where('actif=1');
$result = $bilanM->fetchAll($sql);
$NbLignes = $result->count();
echo "Nb Lignes à traiter : ".$NbLignes.PHP_EOL;
if ( $NbLignes > 0 ) {
require_once 'Metier/Infogreffe/InfogreffeBi.php';
$cpt = 1;
foreach ( $result as $item ) {
$siren = str_pad($item->siren, 9, '0', STR_PAD_LEFT);
echo "Ligne ".$cpt."/".$NbLignes." - siren = ".$siren." , millesime = ".$item->millesime;
//Lire les informations du fichier
$infogreffe = new Metier_Infogreffe_Bi($siren);
$type = 'sociaux';
if ( !empty($item->type_comptes) ) {
$type = $item->type_comptes;
}
//File path - 2012_sociaux-20121231-7501-55-B-14450-0035567
$file = $infogreffe->getFileName($type, $item->date_cloture);
$filepath = $c->profil->infogreffe->storage->path . '/' .
$infogreffe->getFilePath($type, $item->date_cloture) . '/' . $file;
echo " Fichier : ".$filepath;
if ( file_exists($filepath) ) {
echo " lecture.";
//Analyser le fichier - Nombre de page et taille
$infos = $infogreffe->pdfInfos($filepath);
//Enregistrer les infos du fichier dans la base de données
if (false !== $infos) {
$infogreffe->type_comptes = $item->type_comptes;
$infogreffe->date_cloture = $item->date_cloture;
$infogreffe->dbSetFile($file, $infos['size'], $infos['pages'], $infos['version']);
}
} else {
echo " non trouvé.";
}
echo PHP_EOL;
$cpt++;
}
} else {
echo "Aucun enregistrement !".PHP_EOL;
}
}
exit;
}
// Actes
if ($opts->acte) {
if ($opts->files) {
require_once 'Metier/Infogreffe/InfogreffeAc.php';
$path = $c->profil->infogreffe->storage->path . DIRECTORY_SEPARATOR . 'actes';
$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
$nbActes = 0;
//AAAA
echo "Liste des années...".PHP_EOL;
$dirYears = array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ( $file != '..' && $file != '.' && is_dir($path . DIRECTORY_SEPARATOR . $file)) {
$dirYears[] = $file;
}
}
sort($dirYears);
}
//Fichier
foreach ($dirYears as $year) {
foreach ($months as $month) {
$pathFile = $path . DIRECTORY_SEPARATOR . $year . DIRECTORY_SEPARATOR . $month;
echo "Traitement des fichiers $year/$month".PHP_EOL;
if ( is_dir($pathFile) ) {
if ($handle = opendir($pathFile)) {
while (false !== ($file = readdir($handle))) {
if ( $file != '..' && $file != '.' ) {
//acte-347614984-TB-2012-03-15-1402-00-D-50625-4-1.pdf
if (preg_match('/^acte-([0-9]{9})-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.pdf/', $file, $matches)) {
$outfile = 'acte-'.$matches[1].'-'.$matches[2].'-'.
$matches[3].$matches[4].$matches[5].'-'.
$matches[6].'-'.$matches[7].'-'.$matches[8].'-'.$matches[9].'-'.$matches[10].'-'.$matches[11].'.pdf';
echo "Fichier ".$file;
if ( rename($pathFile.'/'.$file, $pathFile.'/'.$outfile) ) {
$data = array(
'pdfLink' => $outfile,
);
try {
$acteM = new Application_Model_JoGreffesActes();
$result = $acteM->update($data, array(
'pdfLink="'.$file.'"',
'siren='.$matches[1],
));
echo " = " . $outfile . " = enregistrement.";
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
} catch(Zend_Db_Exception $e) {
echo $e->getMessage();
}
}
echo "\n";
continue;
}
//'acte-' . $this->siren . '-' . $type . '-' . $date . '-' . $options . '-' . $num . '.pdf';
//with options = NumGreffe - NumRC - NumRC - NumRC - NumDepot
//acte-876580325-XB-20120427-5602-65-B-00032-18-01.pdf
if (preg_match('/^acte-([0-9]{9})-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.pdf/', $file, $matches)) {
echo "Fichier ".$file;
$siren = $matches[1];
$type = $matches[2];
$date = substr($matches[3],0,4).'-'.substr($matches[3],4,2).'-'.substr($matches[3],6,2);
$depot = $matches[8];
$num = $matches[9];
$acteM = new Application_Model_JoGreffesActes();
$sql = $acteM->select()
->where('siren=?', $siren)
->where('num_depot=?', $depot)
->where('date_acte=?', $date)
->where('num_acte=?', intval($num));
//->where('type_acte=?', $type);
//GROUP BY type_acte_libelle
$sql->where('actif=1');
$items = $acteM->fetchAll($sql);
if (count($items)==0) {
//If not find write the database
$infogreffe = new Metier_Infogreffe_Ac($siren);
$infogreffe->getList();
//And get again the item in database
$items = $acteM->fetchAll($sql);
}
if (count($items)==0) {
echo " = non trouvé.";
} else {
foreach($items as $item) {
if ($item->pdfDate == '0000-00-00') {
$infogreffe = new Metier_Infogreffe_Ac($siren);
$infos = $infogreffe->pdfInfos($pathFile. DIRECTORY_SEPARATOR . $file);
if (false !== $infos) {
$data = array(
'pdfLink' => $file,
'pdfSize' => $infos['size'],
'pdfPage' => $infos['pages'],
'pdfVer' => $infos['version'],
'pdfDate' => date('Ymd'),
);
try {
$result = $acteM->update($data, 'id='.$item->id);
echo " = enregistrement.";
} catch(Zend_Db_Adapter_Exception $e) {
echo $e->getMessage();
} catch(Zend_Db_Exception $e) {
echo $e->getMessage();
}
}
} else {
echo " = présent.";
}
}
}
echo PHP_EOL;
}
$nbActes++;
}
}
}
}
}
}
echo "Nombre de fichier : ".$nbActes.PHP_EOL;
} else {
//Lire la base de données
$acteM = new Application_Model_JoGreffesActes();
$sql = $acteM->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'
))
->order('date_depot ASC');
if ( $opts->siren ) {
$sql->where('siren=?',$opts->siren);
} else {
$sql->where('pdfLink=?','');
}
$result = $acteM->fetchAll($sql);
if ( $result->count() > 0 ) {
$cpt = 1;
foreach ( $result as $item ) {
$date = $item->date_acte;
$num = $item->num_acte;
$type = $item->type_acte;
$infogreffe = new Metier_Infogreffe_Ac($siren);
$options = $item->numGreffe . '-' . substr($item->numRC,0,2) . '-' . substr($item->numRC,2,1) . '-' . substr($item->numRC,3) . '-' . $item->num_depot;
$file = $infogreffe->getFileName($date, $num, $type, $options);
$path = $c->profil->path->secure . '/' . $infogreffe->getFilePath($date) . '/' . $file;
if ( file_exists($filepath) ) {
//Analyser le fichier - Nombre de page et taille
$infos = $infogreffe->pdfInfos($filepath);
//Enregistrer les infos du fichier dans la base de données
if (false !== $infos) {
$infogreffe->dbSetFile($file, $infos['size'], $infos['pages'], $infos['version']);
}
}
}
} else {
echo "Aucun enregistrement !".PHP_EOL;
}
}
}