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', 'SdMetier' => __DIR__ . '/../../library/SdMetier', '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|?' => "Displays usage information.", 'since=s' => "Depuis une date (format AAAAMMJJ) ou depuis le dernier flux (flux)", 'from=s' => "depuis surveillance (surv) ou logs (log:default)", 'debug' => "Génére uniquement un fichier sans télétransmission", 'cron' => "Mandatory option for launch the cli in cron", ) ); $opts->parse(); } catch (Zend_Console_Getopt_Exception $e) { echo $e->getUsageMessage(); exit; } //Usage if( isset($opts->help) || count($opts->getOptions())==0 ) { echo "Prestation RRG Surveillance.\n"; echo $opts->getUsageMessage(); exit; } $c = new Zend_Config($application->getOptions()); Zend_Registry::set('config', $c); $login = 'rrgws'; $client = 'rrg'; $typeDepot = 'FTP'; $prestation = 'HEXAVIA'; //Output path $pathOut = $c->profil->path->storage . '/clients/' . $client . '/recv'; $fileOptionsOut = array( 'dir' => $pathOut, 'type' => 'csv', 'delimiter' => '$', /*'enclosure' => '"',*/ /*'header' => array( 'ID_S&D', 'SIRET', 'DATE_CREATION', 'CA', 'ANNEE_CA', 'CODE_PAIEMENT', 'LIBELLE_CODE_PAIE', 'INDISCORE20', 'ENCOURS' ),*/ 'columns' => array( 'SourceID', 'Siret', 'DateCreaEn', 'BilanCA', 'BilanDateCloture', 'PaiementCS', 'PaiementInfo', 'Indiscore', 'Encours', ), 'name' => 'surv_', //'encoding' => 'ISO-8859-15', ); require_once APPLICATION_PATH.'/configs/config.php'; require_once 'framework/fwk.php'; require_once 'Metier/insee/classMInsee.php'; require_once 'Metier/scores/classMScores.php'; require_once 'Metier/partenaires/classMFacto.php'; $db = Zend_Db::factory($c->profil->db->metier); $db->setFetchMode(Zend_Db::FETCH_OBJ); Zend_Db_Table_Abstract::setDefaultAdapter($db); $dateStart = new Zend_Date(); $dateEnd = new Zend_Date(); if ($opts->since == 'flux') { $fluxoutM = new Application_Model_Sdv1FluxFileOut(); $sql = $fluxoutM->select() ->where('client=?', 'rrg') ->where('name=?', 'HEXAVIA') ->where('fileOut LIKE ?', 'surv_%') ->where('depotDate!=?', '0000-00-00 00:00:00') ->order('depotDate DESC')->limit(1); $resultDate = $fluxoutM->fetchRow($sql); $dateStart->set($resultDate->dateEnd, 'yyyyMMdd')->subDay(1); $dateEnd->subDay(1); } elseif ($opts->since) { $dateStart->set($opts->since, 'yyyyMMdd'); $dateEnd->subDay(1); } else { if ( $dateStart->getWeekday() == 1 ) { $dateStart->subDay(3); } else { $dateStart->subDay(1); } $dateEnd->subDay(1); } $dateSql1 = $dateStart->toString('yyyy-MM-dd'); $dateSql2 = $dateEnd->toString('yyyy-MM-dd'); // --- Entités consultées via webservice (dateAjout) + Entités avec scores modifiés ou ajoutés (indiScoreDate) if ($opts->from == 'surv') { echo date('Y-m-d H:i:s')." - Selection dans le portefeuille entre le $dateSql1 et $dateSql2\n"; $sql = "SELECT LPAD(surv.siren,9,0) AS siren, surv.dateAjout, score.indiScore20, score.indiScoreDate, score.encours, score.cs, score.dateBilan FROM jo.surveillances_site AS surv LEFT JOIN jo.scores_surveillance AS score ON surv.siren = score.siren WHERE surv.source='score' AND surv.login='".$login."' AND ( (surv.dateAjout BETWEEN '".$dateSql1." 00:00:00' AND '".$dateSql2." 23:59:59') OR (score.indiScoreDate BETWEEN '".$dateSql1." 00:00:00' AND '".$dateSql2." 23:59:59') ) GROUP BY siren ORDER BY siren, dateAjout DESC"; } // --- Entités consultées via webservice depuis les logs (dateHeure) + Entités avec scores modifiés ou ajoutés (indiScoreDate) elseif ($opts->from == 'log') { echo date('Y-m-d H:i:s')." - Selection dans les logs entre le $dateSql1 et $dateSql2\n"; $sql = "SELECT LPAD(surv.siren,9,0) AS siren, surv.dateHeure AS dateAjout, score.indiScore20, score.indiScoreDate, score.encours, score.cs, score.dateBilan FROM sdv1.logs AS surv LEFT JOIN jo.scores_surveillance AS score ON surv.siren = score.siren WHERE surv.login='".$login."' AND page='identite' AND ( (surv.dateHeure BETWEEN '".$dateSql1." 00:00:00' AND '".$dateSql2." 23:59:59') OR (score.indiScoreDate BETWEEN '".$dateSql1." 00:00:00' AND '".$dateSql2." 23:59:59') ) GROUP BY siren ORDER BY siren, dateAjout DESC"; } try { $resultItem = $db->fetchAll($sql); } catch (Zend_Db_Exception $e) { echo $e->getMessage(); echo "\n"; } if ( count($resultItem) == 0 ) { echo date('Y-m-d H:i:s')." - Aucun élément.\n"; } else { $dateBegin = date('YmdHis'); echo date('Y-m-d H:i:s')." - NbLignes=".count($resultItem)."\n"; // --- Ecriture du fichier $file = $fileOptionsOut['dir'] . '/' .$fileOptionsOut['name'] . substr($dateBegin,0,8) . '.' . $fileOptionsOut['type']; // --- Ouverture fichier $fp = fopen($file, 'w'); if ( $fp === false ) { echo date('Y-m-d H:i:s')." - ERREUR Impossible de créer le fichier ".$file."\n"; exit; } // --- Header if ( count($fileOptionsOut['header']) > 0 ) { fputcsv($fp, $fileOptionsOut['header'], $fileOptionsOut['delimiter'], $fileOptionsOut['enclosure']); } $iDb = new WDB(); $row = 0; // --- Content foreach ($resultItem as $item) { if (intval($item->siren) == 0) { continue; } $row++; if ($opts->debug) { echo date('Y-m-d H:i:s')." - Ligne ".$row; } // --- Data $sql = $db->select() ->from('etablissements', array( 'LPAD(source_id,10,0) AS source_id', 'LPAD(siren,9,0) AS siren', 'LPAD(nic,5,0) AS nic'), 'jo')->where('siren=?', $item->siren)->where('siege=1')->order('actif DESC')->limit(1); $etablissement = $db->fetchRow($sql, null, Zend_Db::FETCH_OBJ); $DateCreaEn = ''; $BilanCA = ''; $BilanDateCloture = ''; $Indiscore = ''; $Encours = ''; // --- Calcul du score ou lecture depuis la table de sauvegarde des scores if ($item->indiScoreDate === null) { if ($opts->debug) { echo " - Calcul du score"; } $evaluation = calculIndiScore($item->siren, 0, false, 0, false, 'scores', $iDb); $PaiementCS = $evaluation['cs']; $Indiscore = $evaluation['Indiscore20']; $Encours = round($evaluation['encours']/1000) * 1000; } else { if ($opts->debug) { echo " - Données en cache"; } $PaiementCS = $item->cs; $Indiscore = $item->indiScore20; $Encours = round($item->encours/1000) * 1000; } // --- Informations identitaire $iInsee = new MInsee($iDb); $tabIdentite = $iInsee->getIdentiteEntreprise($item->siren, 0, 0, false, false); $DateCreaEn = $tabIdentite['DateCreaEn']; if ( $tabIdentite['DateCreaEt'] > 0 && $tabIdentite['DateCreaEt'] < $tabIdentite['DateCreaEn'] ) { $DateCreaEn = $tabIdentite['DateCreaEt']; } elseif ( $tabIdentite['DateCreaEn'] > 0 ) { $DateCreaEn = $tabIdentite['DateCreaEn']; } // --- Correction pour l'envoi des dates if ( !empty($DateCreaEn) ) { if (substr($DateCreaEn, -2) * 1 == 0) { $DateCreaEn = substr($DateCreaEn,0,6).'01'; } } // --- Gestion des bilans $today = new Zend_Date(); if ( !empty($tabIdentite['bilanDate']) ) { $compare = $today->sub($tabIdentite['bilanDate'], 'yyMMdd'); if ( $compare->toValue(Zend_Date::MONTH) < 37 ) { $BilanDateCloture = substr($tabIdentite['bilanDate'],6,2).substr($tabIdentite['bilanDate'],4,2).substr($tabIdentite['bilanDate'],0,4); $BilanCA = $tabIdentite['bilanFL']; } } // --- Estimation du CA bilan if (empty($BilanCA) && !empty($tabIdentite['bilanAnnee'])) { $dateCreaRecente = new Zend_Date(); $dateCreaRecente->subYear(2)->setMonth(1)->setDay(1); $dateClotureEstime = new Zend_Date(); $dateClotureEstime->setMonth(12)->setDay(31); // --- Condition RRG if ( $tabIdentite['Eff'] > 0 || substr($tabIdentite['FJ'],0,1) == 1 && $tabIdentite['Eff'] < 1 || !empty($tabIdentite['NafEnt']) && strlen($tabIdentite['NafEnt']) == 5 && $tabIdentite['NafEnt'] != '0000Z' ) { // --- Création récente if ( $dateCreaRecente->compare($DateCreaEn, 'yyMMdd') == 1 ) { $BilanDateCloture = '3112'.date('Y'); $BilanCA = $tabIdentite['bilanFLestime']; } // --- Date cloture estimée < Date de création elseif ( $dateClotureEstime->compare($DateCreaEn, 'yyMMdd') == -1) { $BilanDateCloture = '3112'.$tabIdentite['bilanAnnee']; $BilanCA = $tabIdentite['bilanFLestime']; } } } // Récupération du libellé d'information de paiement $PaiementInfo = ''; if ( in_array($PaiementCS, array('0', 'I', 'P', 'D')) ) { $iFacto = new MFacto($iDb); $info = $iFacto->getLibInfoCS($PaiementCS); $PaiementInfo = $info['LibCS']; } $values = array( 'SourceID' => $etablissement->source_id, 'Siret' => $etablissement->siren.$etablissement->nic, 'DateCreaEn' => $DateCreaEn, 'BilanCA' => $BilanCA, 'BilanDateCloture' => $BilanDateCloture, 'PaiementCS' => $PaiementCS, 'PaiementInfo' => $PaiementInfo, 'Indiscore' => $Indiscore, 'Encours' => $Encours, ); // --- Trier données $line = array(); foreach ( $fileOptionsOut['columns'] as $i => $column ) { $line[] = $values[$column]; } // --- Ecrire ligne if ($opts->debug) { echo " - Ecriture ligne"; } if ( empty($fileOptionsOut['enclosure']) ) { fwrite($fp, join($fileOptionsOut['delimiter'], $line)."\n"); } else { fputcsv($fp, $line, $fileOptionsOut['delimiter'], $fileOptionsOut['enclosure']); } if ($opts->debug) { echo "\n"; } } // --- Fermeture fichier fclose($fp); // --- Encoding if ( array_key_exists('encoding', $fileOptionsOut) ) { if ( $fileOptionsOut['encoding'] == 'ISO-8859-15' ) { passthru("iconv -f UTF-8 -t ISO-8859-15 ".$file." -o ".$file.".enc"); passthru("cp -fv ".$file.".enc ".$file); } } $nbLines = $row; echo date('Y-m-d H:i:s')." - Nb Lignes Traitées : ".$nbLines."\n"; if ( $opts->cron ) { $fluxoutM = new Application_Model_Sdv1FluxFileOut(); $dateEnd = date('YmdHis'); $fluxoutM->insert(array( 'client' => $client, 'name' => $prestation, 'nbLines' => $nbLines, 'dateBegin' => $dateBegin, 'dateEnd' => $dateEnd, 'fileOut' => basename($file), 'depotType' => $typeDepot, 'depotDate' => '0000-00-00 00:00:00', )); } } echo date('Y-m-d H:i:s')." - Fin.\n";