webservice/scripts/jobs/filesGreffes.php

296 lines
8.3 KiB
PHP
Raw Normal View History

2013-11-05 11:18:30 +00:00
<?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(),
)));
/** Zend_Application */
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",
'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"');
}
$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 " = entregistrement.";
} 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;
}
//
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');
}
$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;
}
//Format date cloture
$dateCloture = substr($item->date_cloture,0,4) . substr($item->date_cloture,5,2) . substr($item->date_cloture,8,2);
//File path - 2012_sociaux-20121231-7501-55-B-14450-0035567
$file = $infogreffe->getFileName($type, $dateCloture);
$filepath = $c->profil->infogreffe->storage->path . DIRECTORY_SEPARATOR .
$infogreffe->getFilePath($type, $dateCloture) . DIRECTORY_SEPARATOR . $file;
if ( file_exists($filepath) ) {
echo " Fichier : ".$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->type_comptes = $item->type_comptes;
$infogreffe->date_cloture = $dateCloture;
$infogreffe->dbSetFile($file, $infos['size'], $infos['pages'], $infos['version']);
}
}
echo PHP_EOL;
$cpt++;
}
} else {
echo "Aucun enregistrement !".PHP_EOL;
}
}
exit;
}
// Actes
if ($opts->acte) {
//Lire la base de données
$acteM = new Application_Model_JoGreffesActes();
$sql = $acteM->select()->order('date_depot ASC');
if ( $opts->siren ) {
$sql->where('siren=?',$opts->siren);
} else {
$sql->where('pdfLink=?','');
}
$result = $actenM->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 . DIRECTORY_SEPARATOR . $infogreffe->getFilePath($date) . DIRECTORY_SEPARATOR . $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;
}
}