<?php
|
|
$MODULE="Compta";
|
|
$TITLE="Consolidation Compta";
|
|
$DFTENDSTATUS="OK";
|
|
$ARGS="";
|
|
/** Laisser les lignes 2 à 5 sans changer la casse et sans espaces et dans cet ordre
|
|
* car le script est lu et analysé quand il est exécuté via .exe ou Jenkins
|
|
* pour gestion des fichiers .pid dans system\batch\logs
|
|
*/
|
|
|
|
require_once("../../../modules/base/php/lib/cti/Batch/iCTI_batch_common.php" );
|
|
require_once("../../../modules/base/php/lib/cti/Environnement/Environnement.class.php");
|
|
require_once("../../../modules/base/php/lib/cti/EnvironmentSettings/EnvironmentDatabaseSettings.php");
|
|
require_once("../../../modules/base/php/lib/cti/Database/Database2Factory.class.php");
|
|
require_once("../../../modules/base/php/classSimpleXML.php");
|
|
require_once("../../../modules/outils/php/httpService_cacheManager_cleanCache.php");
|
|
|
|
global $ENDSTATUS;
|
|
global $iCTI_connexion;
|
|
global $cti_args;
|
|
|
|
global $nbE;
|
|
|
|
$moduleName = strtolower($MODULE);
|
|
|
|
logStartMsg();
|
|
|
|
// Arguments transmis ?
|
|
$cti_args = new CTI_Args_class();
|
|
|
|
if (iCTI_connect()) {
|
|
|
|
if (! checkIsConsolidate($moduleName)) {
|
|
logErrorMsg('Environnement NON CONSOLIDÉ');
|
|
} else {
|
|
|
|
$childEnvironments = getChildEnvironments($moduleName);
|
|
|
|
// Retourne false en cas d'échec de configuration d'un "établissement enfant" (settings ou database)
|
|
if ($childEnvironments !== false) {
|
|
|
|
// Création du répertoire utilisé par la fonction base.cti_copy_table(i_fromdblink text, i_fromtable text, i_totable text)
|
|
if (!is_dir('C:\TEMP')) {
|
|
mkdir('C:\TEMP');
|
|
}
|
|
|
|
consolidateEnvironments($moduleName, $childEnvironments);
|
|
|
|
logInfoMsg('Nettoyage cache');
|
|
cleanAllCache('iCTI_'.$moduleName, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
iCTI_disconnect();
|
|
|
|
logEndMsg();
|
|
|
|
/** Consolide le module dont on précise le nom avec les "environnemnts enfants" concernés
|
|
* @param $moduleName
|
|
* @param $childEnvironments
|
|
*/
|
|
function consolidateEnvironments($moduleName, $childEnvironments) {
|
|
|
|
global $iCTI_connexion;
|
|
global $childEnvironments;
|
|
global $cti_args;
|
|
|
|
// Chargement requêtes SQL
|
|
$environmentVars = array();
|
|
$sqlOptions = array();
|
|
|
|
// Raz forcé
|
|
$cti_args->args['-raz'] = 'ONLYKEY';
|
|
|
|
// Initialisation
|
|
|
|
$cti_sqlStatements = new CTI_SqlStatements_class('iCTI_import_'.$moduleName.'_CONSOLIDATION.XML', $iCTI_connexion, $environmentVars, $sqlOptions);
|
|
$cti_sqlStatements->executeStatementsNode('DISABLE_INDEX');
|
|
$cti_sqlStatements->executeStatementsNode('RAZ');
|
|
|
|
foreach ($childEnvironments as $environment) {
|
|
|
|
$environmentVars = array();
|
|
|
|
$environmentVars['TX'] = $environment->name;
|
|
$environmentVars['FINESS'] = $environment->finess;
|
|
$environmentVars['FINESS_TEXTE'] = $environment->name;
|
|
$environmentVars['DBHOST'] = $environment->host;
|
|
$environmentVars['DBPORT'] = $environment->port;
|
|
$environmentVars['DBNAME'] = $environment->dbname;
|
|
$environmentVars['DBUSERNAME'] = $environment->username;
|
|
$environmentVars['DBPASSWORD'] = $environment->password;
|
|
$environmentVars['TPX'] = $environment->code.'_';
|
|
$environmentVars['CPX'] = $environment->code.'_';
|
|
$environmentVars['SPX'] = '_'.$environment->code;
|
|
$environmentVars['IPX'] = $environment->oid.'000000000';
|
|
$environmentVars['PPX'] = $environment->oid;
|
|
$environmentVars['PX'] = $environment->code;
|
|
|
|
$cti_sqlStatementsEnvironment = new CTI_SqlStatements_class('iCTI_import_'.$moduleName.'_CONSOLIDATION.XML', $iCTI_connexion, $environmentVars, $sqlOptions);
|
|
$cti_sqlStatementsEnvironment->executeStatementsNode('INIT');
|
|
$cti_sqlStatementsEnvironment->executeStatementsNode('PARAM');
|
|
$cti_sqlStatementsEnvironment->executeStatementsNode('COMPTA');
|
|
}
|
|
|
|
$cti_sqlStatements->executeStatementsNode('POST');
|
|
$cti_sqlStatements->executeStatementsNode('ENABLE_INDEX');
|
|
}
|
|
|
|
/** Récupère les "environnements enfants" qui consolident le module dont on précise le nom
|
|
* Retourne un tableau de stclass contenant les propriétés suivantes :
|
|
* - name
|
|
* - directory
|
|
* - code
|
|
* - oid
|
|
* - finess
|
|
* - host ('localhost' si non trouvé)
|
|
* - port ('5432' si non trouvé)
|
|
* - dbname
|
|
* - username
|
|
* - password
|
|
* Ou false en cas d'erreur
|
|
* @param $moduleName
|
|
* @return array|bool Tableau des "environnements enfants" ou false en cas d'erreur
|
|
*/
|
|
function getChildEnvironments($moduleName) {
|
|
|
|
$environmentError = false;
|
|
$childEnvironments = array();
|
|
|
|
$file = Environnement::getInstance()->getRootPath()."\settings\settings.XML";
|
|
if (file_exists($file)) {
|
|
$xml = simplexml_load_file($file);
|
|
if ($xml) {
|
|
|
|
$nodes = $xml->xpath("/SETTINGS/CONSOLIDATION" . strtoupper($moduleName) . "/ENVIRONMENT");
|
|
|
|
foreach ($nodes as $environmentNode) {
|
|
|
|
$environment = new stdClass();
|
|
|
|
$environment->name = '';
|
|
$environment->finess = '';
|
|
$environment->directory = '';
|
|
$environment->code = '';
|
|
$environment->oid = '';
|
|
$environment->host = '';
|
|
$environment->port = '';
|
|
$environment->dbname = '';
|
|
$environment->username = '';
|
|
$environment->password = '';
|
|
|
|
$environmentName = '';
|
|
$propertyNodes = $environmentNode->xpath("PROPERTY[@name='ENVIRONMENT']");
|
|
// Le noeud a été trouvé
|
|
if (count($propertyNodes) == 1) {
|
|
$environmentName = trim($propertyNodes[0]['value']);
|
|
}
|
|
|
|
$propertyNodes = $environmentNode->xpath("PROPERTY[@name='DIR']");
|
|
// Le noeud a été trouvé
|
|
if (count($propertyNodes) == 1) {
|
|
$environment->directory = trim($propertyNodes[0]['value']);
|
|
}
|
|
|
|
$propertyNodes = $environmentNode->xpath("PROPERTY[@name='PREFIX_CODE']");
|
|
// Le noeud a été trouvé
|
|
if (count($propertyNodes) == 1) {
|
|
$environment->code = strtoupper(trim($propertyNodes[0]['value']));
|
|
}
|
|
|
|
$propertyNodes = $environmentNode->xpath("PROPERTY[@name='PREFIX_OID']");
|
|
// Le noeud a été trouvé
|
|
if (count($propertyNodes) == 1) {
|
|
$environment->oid = trim($propertyNodes[0]['value']);
|
|
}
|
|
|
|
$environmentSettings = getChildEnvironmentSettings($environment);
|
|
if (! $environmentSettings) {
|
|
logErrorMsg("Environnement $environmentName sur $environment->directory inexistant ou mal configuré");
|
|
$environmentError = true;
|
|
} else {
|
|
$environment->name = $environmentSettings->name;
|
|
$environment->finess = $environmentSettings->finess;
|
|
}
|
|
|
|
$environmentDatabaseSettings = getChildEnvironmentDatabaseSettings($environment);
|
|
if (! $environmentDatabaseSettings) {
|
|
logErrorMsg("Environnement $environmentName sur $environment->directory inexistant ou mal configuré");
|
|
$environmentError = true;
|
|
} else {
|
|
$environment->host = $environmentDatabaseSettings->host;
|
|
$environment->port = $environmentDatabaseSettings->port;
|
|
$environment->dbname = $environmentDatabaseSettings->dbname;
|
|
$environment->username = $environmentDatabaseSettings->username;
|
|
$environment->password = $environmentDatabaseSettings->password;
|
|
}
|
|
|
|
if ($environment->code === '' || $environment->oid === 0) {
|
|
logErrorMsg("Préfixes obligatoires ($environment->code,$environment->oid) pour environnement $environmentName");
|
|
$environmentError = true;
|
|
}
|
|
|
|
// Ajout de l'environnement dont on a contrôlé les paramètres
|
|
if (!$environmentError) {
|
|
$childEnvironments[] = $environment;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if ($environmentError) {
|
|
logErrorMsg("Au moins un environnement de la consolidation ".ucfirst($moduleName)." n'est pas correct");
|
|
return false;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
return $childEnvironments;
|
|
}
|
|
|
|
/** Lit les informations d'un "environnement enfant" (qui consolide)
|
|
* On récupère le chemin de l'environnement via sa propriété 'directory'
|
|
* On retourne une stdClass avec les propriétés suivantes :
|
|
* - name
|
|
* - finess
|
|
* @param $environment
|
|
* @return stdClass|bool false en cas d'erreur
|
|
*/
|
|
function getChildEnvironmentSettings($environment) {
|
|
|
|
$settings = new stdClass();
|
|
|
|
$settings->name = '';
|
|
$settings->finess = '';
|
|
|
|
$file = $environment->directory."\settings\settings.XML";
|
|
if (file_exists($file)) {
|
|
$xml = simplexml_load_file($file);
|
|
if ($xml) {
|
|
$nodes = $xml->xpath("/SETTINGS/ENVIRONMENT/PROPERTY[@name='ENVIRONMENT']");
|
|
$settings->name = trim($nodes[0]['value']);
|
|
|
|
$nodes = $xml->xpath("/SETTINGS/ENVIRONMENT/PROPERTY[@name='FINESS']");
|
|
$settings->finess = trim($nodes[0]['value']);
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
/** Lit les informations de la base de données d'un "environnement enfant" (qui consolide)
|
|
* On récupère le chemin de l'environnement via sa propriété 'directory'
|
|
* On retourne une stdClass avec les propriétés suivantes :
|
|
* - host ('localhost' si non trouvé)
|
|
* - port ('5432' si non trouvé)
|
|
* - dbname
|
|
* - username
|
|
* - password
|
|
* @param $environment
|
|
* @return stdClass|bool false en cas d'erreur
|
|
*/
|
|
function getChildEnvironmentDatabaseSettings($environment) {
|
|
|
|
$dbSettings = new EnvironmentDatabaseSettings($environment->directory);
|
|
try {
|
|
$dbSettings->readSettings();
|
|
} catch (Exception $e) {
|
|
logErrorMsg($e->getMessage());
|
|
return false;
|
|
}
|
|
|
|
$database = new stdClass();
|
|
$database->host = $dbSettings->getHost();
|
|
$database->port = $dbSettings->getPort();
|
|
$database->dbname = $dbSettings->getName();
|
|
$database->username = $dbSettings->getUsername();
|
|
$database->password = $dbSettings->getPassword();
|
|
|
|
return $database;
|
|
}
|
|
|
|
/** Vérifie si le module dont on précise le nom est configuré pour la consolidation
|
|
* @param $moduleName
|
|
* @return bool
|
|
*/
|
|
function checkIsConsolidate($moduleName) {
|
|
$isConsolidate = false;
|
|
|
|
$file = Environnement::getInstance()->getRootPath()."/settings/settings.XML";
|
|
$xml = simplexml_load_file($file);
|
|
|
|
$nodes = $xml->xpath("/SETTINGS/MODULES/MODULE[@name='".$moduleName."' and @installed='true']");
|
|
|
|
// Le noeud a été trouvé
|
|
if (count($nodes) == 1) {
|
|
if (isset($nodes[0]['consolidate'])) {
|
|
$isConsolidate = (strtolower($nodes[0]['consolidate']) === 'true');
|
|
}
|
|
}
|
|
|
|
return $isConsolidate;
|
|
}
|
|
|
|
/** Connecte la base de données de l'environnement
|
|
* @return bool false en cas d'erreur
|
|
* @throws Exception
|
|
*/
|
|
function iCTI_connect() {
|
|
|
|
global $iCTI_connexion;
|
|
|
|
// Connexion BD
|
|
$iCTI_connexion = Database2Factory::getInstance(Environnement::PROVIDER_CTI);
|
|
$ret = $iCTI_connexion->connect();
|
|
|
|
if (!$ret) {
|
|
logErrorMsg("La connexion PostgreSQL iCTI a échoué");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function iCTI_disconnect() {
|
|
|
|
global $iCTI_connexion;
|
|
|
|
$iCTI_connexion->close();
|
|
}
|
|
|
|
?>
|