<?php
$MODULE="Activité";
$TITLE="Consolidation COMPTA";
$DFTENDSTATUS="OK";
$ARGS="-raz";
/** 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
 */

global $ENDSTATUS;
global $iCTI_connexion;
global $cti_args;
global $ENV_TYPEETS;
global $nbE;

// IL S'AGIT D'UN MODELE A COPIER SUR SITE (pas encore de standard)
// Enregistrer la copie de ce fichier avec un nom explicite
// Ex : iCTI_import_compta_CONSOLIDATION_NUM/NOM_GROUPE.php ainsi que le programme .exe

require_once("../../../modules/base/php/lib/cti/Batch/iCTI_batch_common.php" );
require_once("../../../modules/base/php/lib/cti/Database/Database2Factory.class.php");
require_once("../../../modules/base/php/lib/cti/Environnement/Environnement.class.php");
require_once("../../../modules/base/php/classSimpleXML.php");
require_once("../../../modules/outils/php/httpService_cacheManager_cleanCache.php");

logStartMsg();

// Arguments transmis ?
$cti_args = new CTI_Args_class();

if (iCTI_connect()) {

	consolidation_environnement_initialize();
	consolidation_environnement("ISP", "I1", 1, "340000025", "icti_349_isp0");
	consolidation_environnement("ISP (Avant juin 2015)", "I2", 2, "340000025", "icti_349_isp1");
	consolidation_environnement("OMEM", "OM", 3, "340000025", "icti_349_omem");
	consolidation_environnement("AMETHYSTE (Avant juin 2015)", "AM", 4, "340000025", "icti_349_amet");
	consolidation_environnement("OCDF", "OC", 5, "340000025", "icti_349_ocdf");
	consolidation_environnement_finalize();
		
}

iCTI_disconnect();

// nettoyage du cache
logInfoMsg("Nettoyage cache");
cleanAllCache("iCTI_rh", TRUE);

logEndMsg();


// récupération historique
function consolidation_environnement_initialize() {

	global $iCTI_connexion;
	global $ENV_TYPEETS;

	global $cti_args;

	logInfoMsg("INITIALISATION CONSOLIDATION");
	
	// Chargement requetes SQL
	$environmentShareVars=array();
	$environmentShareVars["PX"] = "";
	
	$environmentVars=array();
	$environmentVars["PX"] = "";

	$environmentVars["IPX"] = "";
	$environmentVars["PPX"] = "";


	$environmentVars["TPX"] = "";
	$environmentVars["CPX"] = "";
	
	$environmentVars["FINESS"] = "";
	
	
	$sqlOptions=array();

	$cti_sqlStatements_share = new CTI_SqlStatements_class("iCTI_import_compta_SHARE.XML",$iCTI_connexion,$environmentShareVars,$sqlOptions);
	
	$cti_sqlStatements_provider = new CTI_SqlStatements_class("iCTI_import_compta_CONSOLIDATION.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	

	$cti_sqlStatements_provider->executeStatementsNode("DISABLE_INDEX");	
	$cti_sqlStatements_share->executeStatementsNode("RAZ");
	$cti_sqlStatements_share->executeStatementsNode("INIT");
	
	logInfoMsg("FIN INITIALISATION CONSOLIDATION");
	
}

function consolidation_environnement($COMPTA_ENVIRONMENT, $COMPTA_ENVIRONMENT_PREFIX, $COMPTA_ENVIRONMENT_OID, $FINESS, $DBNAME) {

	global $iCTI_connexion;
	global $iCTI_port;
	global $ENV_TYPEETS;

	global $cti_args;

	logInfoMsg("CONSOLIDATION ENVIRONNEMENT $COMPTA_ENVIRONMENT");

	// Chargement requetes SQL
	$environmentShareVars=array();
	$environmentShareVars["PX"] = "";
	
	$environmentVars=array();
	$environmentVars["PX"] = $COMPTA_ENVIRONMENT_PREFIX;

	$environmentVars["IPX"] = $COMPTA_ENVIRONMENT_OID . "000000000";
	$environmentVars["PPX"] = $COMPTA_ENVIRONMENT_OID;


	$environmentVars["TPX"] = $COMPTA_ENVIRONMENT_PREFIX . "_";
	$environmentVars["CPX"] = $COMPTA_ENVIRONMENT_PREFIX . "_";
	$environmentVars["SPX"] = "_" . $COMPTA_ENVIRONMENT_PREFIX;
	
	$environmentVars["FINESS"] = $FINESS ;
	$environmentVars["DBNAME"] = $DBNAME ;
	$environmentVars["DBPORT"] = $iCTI_port;
	
	
	$sqlOptions=array();

	$cti_sqlStatements_share = new CTI_SqlStatements_class("iCTI_import_compta_SHARE.XML",$iCTI_connexion,$environmentShareVars,$sqlOptions);
	
	$cti_sqlStatements_provider = new CTI_SqlStatements_class("iCTI_import_compta_CONSOLIDATION.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	
	// Raz forcé 
	$cti_args->args["-raz"] = "ONLYKEY";
	
	$cti_sqlStatements_provider->executeStatementsNode("PARAM");

	$cti_sqlStatements_provider->executeStatementsNode("COMPTA");
	
	logInfoMsg("FIN CONSOLIDATION ENVIRONNEMENT $COMPTA_ENVIRONMENT");

}

// récupération historique
function consolidation_environnement_finalize() {

	global $iCTI_connexion;
	global $ENV_TYPEETS;

	global $cti_args;

	logInfoMsg("FINALISATION CONSOLIDATION");
	
	// Chargement requetes SQL
	$environmentShareVars=array();
	$environmentShareVars["PX"] = "";
	
	$environmentVars=array();
	$environmentVars["PX"] = "";

	$environmentVars["IPX"] = "";
	$environmentVars["PPX"] = "";

	$environmentVars["TPX"] = "";
	$environmentVars["CPX"] = "";
	
	$environmentVars["FINESS"] = "";

	$sqlOptions=array();

	$cti_sqlStatements_share = new CTI_SqlStatements_class("iCTI_import_compta_SHARE.XML",$iCTI_connexion,$environmentShareVars,$sqlOptions);
	
	$cti_sqlStatements_provider = new CTI_SqlStatements_class("iCTI_import_compta_CONSOLIDATION.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	
	$cti_sqlStatements_provider->executeStatementsNode("POST");
	
	$cti_sqlStatements_provider->executeStatementsNode("ENABLE_INDEX");
		
	//$cti_sqlStatements_share->executeStatementsNode("POST");
		
	//$cti_sqlStatements_share->executeStatementsNode("VACUUM");
	
	logInfoMsg("FIN FINALISATION CONSOLIDATION");
	
}


function iCTI_connect() {


global $iCTI_connexion;
global $iCTI_cache;
global $iCTI_port;
global $ENV_TYPEETS;
global $ENV_CONSOLIDATION;


	$iCTI_connexion = Database2Factory::getInstance(Environnement::PROVIDER_CTI);
	$ret = $iCTI_connexion->connect();


	$file = "../../../settings/settings.XML";
	$settings_xml = simplexml_load_file($file);
	
	$ENV_CONSOLIDATION = "0";
	
	
	foreach ($settings_xml->ENVIRONMENT as $environmentNode) {
		foreach ($environmentNode->PROPERTY as $propertyNode) {
			if ($propertyNode['name'] == 'TYPEETS') {
				$ENV_TYPEETS = $propertyNode['value'];
			}
			if ($propertyNode['name'] == 'CONSOLIDATION') {
				$ENV_CONSOLIDATION = $propertyNode['value'];
			}
		}
	}

	
	if (!$ret) {
		logErrorMsg("La connexion PostgreSQL iCTI a échoué");
		return $ret;
	}
	
	return $iCTI_connexion;

}

function iCTI_disconnect() {

	global $iCTI_connexion;
	
	// Déconnexion BD
	$iCTI_connexion->close();
}
    
?>