faire une requête SQL * Lundi au Vendredi : à H du jour, prendre de -24h à H-2 * Samedi : Reprendre les événements de la semaine, du mois ? * Dimanche : Reprendre les événements du mois * * Processus : * Une fois la liste des sirens obtenues, vérifier dans la table des surveillances l'horodatage du dernier calcul * Si plus récent faire le calcul * Si plus vieux suivant * */ error_reporting(E_ALL ^ E_STRICT ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED); require_once __DIR__ . '/../application/bin.bootstrap.php'; // --- Options $displayUsage = false; try { $opts = new Zend_Console_Getopt(array( 'help|?' => "Displays usage information.", 'verbose|v' => "Mode bavard ou debug", 'source-s' => ".....", 'date-s' => "AAAAMMJJ, re-calcul depuis la date", )); $opts->parse(); $optionsNb = count($opts->getOptions()); } catch (Zend_Console_Getopt_Exception $e) { $displayUsage = true; } // --- Aide / Options if ($optionsNb == 0 || isset($opts->help)) { $displayUsage = true; } // --- Usage if ($displayUsage) { echo "Scoring.\n"; echo $opts->getUsageMessage(); exit; } if (empty($opts->source)) { echo date('Y-md-d H:i:s') . " - Aucune source.\n"; exit; } // --- Date pour la sélection $dateBegin = null; $dateEnd = new DateTime(); if ($opts->date) { $dateBegin = DateTime::createFromFormat('Ymd', $opts->date); } // --- Lecture dans la base de données else { } // Date de debut par default if ($dateBegin === null) { $dateBegin = new DateTime(); $dateBegin->sub(new DateInterval('P1D')); // Lundi prendre aussi dimanche if ($dateBegin->format('w') == 1) { $dateBegin->sub(new DateInterval('P1D')); } } $dateBegin->setTime(0, 0, 0); // --- Requetes de selection (toujours avec retour date AS dateEven) $sourcesList = array( // --- 'declencheur' => "SQL '{DATEBEGIN}' AND '{DATEEND}'", // Ajout Insee 'inseeadd' => array( 'declencheur' => 'ajout', 'sql' => "SELECT LPAD(insSIREN,9,0) AS siren, dateInsert AS dateEven FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND insVMAJ='C' AND insEVE IN('CS', 'CC', 'I', 'RS') AND DATE_FORMAT(idFlux, '%Y%m%d%H%i%s') BETWEEN '{DATEBEGIN}' AND '{DATEEND}'", ), // Scores ajouté manuellement 'manualadd' => array( 'declencheur' => 'ajout', 'sql' => "SELECT LPAD(siren,9,0) AS siren, dateUpdate AS dateEven FROM jo.scores_surveillance WHERE siren>1000 AND dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}' AND indiScoreDate=0", ), // Bilan Association 'bilanasso' => array( 'declencheur' => 'bilanasso', 'sql' => "SELECT LPAD(siren,9,0) AS siren,dateInsert AS dateEven FROM jo.asso_bilans WHERE siren>1000 AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'", ), // Bilan 'bilan' => array( 'declencheur' => 'bilans1', 'sql' => "SELECT LPAD(siren,9,0) AS siren, dateInsert AS dateEven FROM jo.bilans WHERE siren>1000 AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'", ), // Privilege 'privilege' => array( 'declencheur' => 'privileges', 'sql' => "SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.greffes_privileges WHERE siren>1000 AND (dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}' OR dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}')", ), // Dirigeant 'dirigeant' => array( 'declencheur' => 'dirigeants', 'sql' => "SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.rncs_dirigeants WHERE siren>1000 AND (dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}' OR dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}')", ), // Insee 'insee' => array( 'declencheur' => 'insee', 'sql' => "SELECT LPAD(insSIREN,9,0) AS siren, dateInsert AS dateEven FROM insee.insee_even WHERE insSIREN>1000 AND insSIEGE=1 AND DATE_FORMAT(idFlux, '%Y%m%d%H%i%s') BETWEEN '{DATEBEGIN}' AND '{DATEEND}'", ), // Bodacc 'bodacc' => array( 'declencheur' => 'bodacc', 'sql' => "SELECT LPAD(siren,9,0) AS siren, IF(dateUpdate=0,dateInsert,dateUpdate) AS dateEven FROM jo.bodacc_detail WHERE siren>1000 AND Rubrique NOT IN('creations','comptes') AND (dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}' OR dateUpdate BETWEEN '{DATEBEGIN}' AND '{DATEEND}')", ), // Collecte 'collecte' => array( 'declencheur' => 'collecte', 'sql' => "SELECT LPAD(siren,9,0) AS siren, dateInsert AS dateEven FROM jo.annonces WHERE siren>1000 AND dateInsert BETWEEN '{DATEBEGIN}' AND '{DATEEND}'", ), // Ancien 'ancien' => array( 'declencheur' => 'ancien', 'sql' => "SELECT LPAD(siren,9,0) AS siren, dateUpdate AS dateEven FROM jo.scores_surveillance WHERE siren>1000 AND (DATEDIFF(NOW(), indiScoreDate) > 365) AND procol NOT IN('P') AND dateUpdate < '{DATEBEGIN}'", ), ); foreach ($sourcesList as $source => $item) { if ($opts->source !== null && $source != $opts->source) { continue; } $declencheur = $item['declencheur']; $sql = $item['sql']; echo date('Y-m-d H:i:s') . " - Selection $declencheur entre " .$dateBegin->format('Y-m-d H:i:s') . " et ". $dateEnd->format('Y-m-d H:i:s')."\n"; // Date Last Declencheur // @todo : Lire dans une table le dernier declencheur // --- Remplacement des variables dans la requete $sql = str_replace( array( '{DATEBEGIN}', '{DATEEND}', ), array( $dateBegin->format('YmdHis'), $dateEnd->format('YmdHis'), ), $sql ); // --- Selection try { // --- Execution de la requete $stmtItem = $conn->executeQuery($sql); } catch (\Doctrine\DBAL\DBALException $e) { echo date('Y-m-d H:i:s') . " - Erreur ".$e->getMessage().".\n"; exit; } $total = $stmtItem->rowCount(); // --- Boucle sur les entités $cpt = 0; $cptCalc = 0; if ($total > 0) { while ($item = $stmtItem->fetch(\PDO::FETCH_OBJ)) { $cpt++; if ($opts->verbose) { echo date('Y-m-d H:i:s')." - Ligne ".str_pad($cpt, strlen($total), ' ', STR_PAD_LEFT)." / ".$total; } // --- Vérification si score déjà calculé try { $sql = "SELECT siren, indiScoreDate, dateUpdate FROM jo.scores_surveillance WHERE siren = :siren"; $stmt = $conn->prepare($sql); $stmt->bindValue('siren', $item->siren); $stmt->execute(); } catch (\Doctrine\DBAL\DBALException $e) { echo " : ".$item->siren." - Erreur ".$e->getMessage()."\n"; continue; } $toCalculate = false; if ($stmt->rowCount() == 0) { $toCalculate = true; } else { $survResult = $stmt->fetch(\PDO::FETCH_OBJ); if (empty($item->dateEven)) { if ($opts->verbose) { echo " : dateEven = NULL\n"; } continue; } if ($survResult->indiScoreDate == '0000-00-00') { $toCalculate = true; } else { $calculDate = DateTime::createFromFormat('YmdHis', $survResult->dateUpdate); $evenDate = DateTime::createFromFormat('YmdHis', $item->dateEven); if ($evenDate > $calculDate) { $toCalculate = true; } } } // --- Calcul if ($toCalculate) { require_once 'Metier/Scores/MScores.php'; $tabScore = calculIndiScore($item->siren, 0, false, 0, false, 'scores', $declencheur); $indiScore = $tabScore['Indiscore']; $encours = round($tabScore['encours']); if ($opts->verbose) { echo " : ".$item->siren." - Calcul $indiScore ($encours)\n"; } $cptCalc++; } else { if ($opts->verbose) { echo " : ".$item->siren." - Pas de Calcul\n"; } } } } echo date('Y-m-d H:i:s') . " - Fin $declencheur calcul de $cptCalc / $cpt\n"; }