<?php
$MODULE="Activité";
$TITLE="Récupération activité";
$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
 */

if (!defined('__ROOT__') ) {
    list($environmentPath) = explode(DIRECTORY_SEPARATOR.'modules', dirname(__FILE__));
    $environmentPath .= DIRECTORY_SEPARATOR;
    define('__ROOT__', $environmentPath);
}

require_once __ROOT__ . '/modules/base/php/lib/cti/Batch/iCTI_batch_common.php';
require_once __ROOT__ . '/modules/base/php/classSimpleXML.php';
require_once __ROOT__ . '/modules/outils/php/httpService_cacheManager_cleanCache.php';
require_once __ROOT__ . '/modules/base/php/classDatabase.php';
require_once __ROOT__ . '/modules/activite/php/expert/ControleExpertManager.class.php';
require_once __ROOT__ . '/modules/activite/php/expert/ControleExpert.class.php';
require_once __ROOT__ . '/modules/activite/php/expert/RegleExpert.class.php';
require_once __ROOT__ . '/modules/base/php/lib/cti/Environnement/Environnement.class.php';
require_once __ROOT__ . '/modules/base/php/lib/cti/Database/Database2Factory.class.php';

global $ENDSTATUS;
global $iCTI_connexion;
global $cti_args;
global $ADMPROVIDER;
global $ADMPROVIDER_PREFIX;
global $ADMPROVIDER2;
global $ADMPROVIDER2_PREFIX;
global $ADMPROVIDER2_ASPISTATUS;
global $ADMPROVIDER2_SYNCSTATUS;
global $ADMPROVIDER3;
global $ADMPROVIDER3_PREFIX;
global $ADMPROVIDER3_ASPISTATUS;
global $ADMPROVIDER3_SYNCSTATUS;
global $ENV_FINESS;
global $ENV_TYPEETS;
global $ENV_ADM_ANNEEDEBUT;
global $nbE;

// Temps d'exécution ++ pour éviter que les imports sur les gros environnements ne plantent par timeout
set_time_limit(36000);

logStartMsg();

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

if (iCTI_connect()) {

	// 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');
	}

	// Extraction données aspirateur
	$exitCode1 = 0;
	$exitCode2 = 0;
	$exitCode3 = 0;

	if (!isset($cti_args->args["-f"])) {
		if (!isset($cti_args->args["-f1"])) {
			$exitCode1 = extraction_prod($ADMPROVIDER);
		}
		else {
			logInfoMsg("La vérification des données est ignoree pour $ADMPROVIDER");
		}			
		if ($ADMPROVIDER2 != "") {
			if ($ADMPROVIDER2_ASPISTATUS != 'disabled' && !isset($cti_args->args["-f2"])) {
				$exitCode2 = extraction_prod($ADMPROVIDER2);
			}
			else {
				logInfoMsg("La vérification des données est ignoree pour $ADMPROVIDER2");
			}			
			if ($ADMPROVIDER3 != "") {
				if ($ADMPROVIDER3_ASPISTATUS != 'disabled' && !isset($cti_args->args["-f3"])) {
					$exitcode3 = extraction_prod($ADMPROVIDER3);
				}
				else {
					logInfoMsg("La vérification des données est ignoree pour $ADMPROVIDER3");
				}
			}
		}
	}

	// Synchronisation schéma activite
	if (!isset($cti_args->args["--fusion_only"])) {
		
		if (($exitCode1 == 0 || $cti_args->args["-f"] || $cti_args->args["-f1"])
			&& (!(isset($cti_args->args["-p2"]) || isset($cti_args->args["-p3"]))
					|| isset($cti_args->args["-p1"]))) {
			extraction_environnement($ADMPROVIDER, $ADMPROVIDER_PREFIX, 0);
		}
		if ($ADMPROVIDER2 != "" && $ADMPROVIDER2_SYNCSTATUS != 'disabled' 
				&& ($exitCode2 == 0 || $cti_args->args["-f2"] || $cti_args->args["-f"])
				&& (!(isset($cti_args->args["-p1"]) || isset($cti_args->args["-p3"]))
					|| isset($cti_args->args["-p2"]))) {
			extraction_environnement($ADMPROVIDER2, $ADMPROVIDER2_PREFIX, 2);
			if ($ADMPROVIDER3 != "" && $ADMPROVIDER3_SYNCSTATUS != 'disabled'  
				&& ($exitCode3 == 0 || $cti_args->args["-f3"] || $cti_args->args["-f"])
				&& (!(isset($cti_args->args["-p2"]) || isset($cti_args->args["-p1"]))
					|| isset($cti_args->args["-p3"]))) {
				extraction_environnement($ADMPROVIDER3, $ADMPROVIDER3_PREFIX, 3);
			}
		}
	}
	
	if (!isset($cti_args->args["--no_fusion"])) {
		// Fusion si plusieurs sources
		if ($ADMPROVIDER2 != "") {
			fusion_environnement($ADMPROVIDER, $ADMPROVIDER_PREFIX, 0);
			if ($ADMPROVIDER3 != "") {
				fusion_environnement($ADMPROVIDER3, $ADMPROVIDER3_PREFIX, 3);
			}
			fusion_environnement($ADMPROVIDER2, $ADMPROVIDER2_PREFIX, 2);
		}
		
		run_expert_controls();
	}
		
}

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

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

logEndMsg();

/**
 * ******************************************************
 * FONCTIONS *
 * ******************************************************
 */

 function run_expert_controls() {

	logInfoMsg("CONTROLES EXPERT");
	$exp_manager = new ControleExpertManager('activite');
	if ($exp_manager->run_controls()) {
		logInfoMsg($exp_manager->msg);
	}
	else {
		$error_msg = $exp_manager->error_msg;
		if (strpos($error_msg, PHP_EOL) !== false) {
			$items = explode(PHP_EOL, $error_msg);
			foreach($items as $item) {
				if ($item != '') {
					logWarnMsg($item);
				}
			}
		} else {
			logWarnMsg($error_msg);
		}
	}

}

function extraction_prod($ADMPROVIDER_check) {
	global $cti_args;
	global $ADMPROVIDER;
	
	echo PHP_EOL;
	// mode chainé
	if ($cti_args->args["-u"] || $cti_args->args["--prod-update"]) {
		if ($ADMPROVIDER_check == "CEGI") {
			$ADMPROVIDER_check = "CEGILOGHOS";
		}		
		if ($cti_args->args["-noactualizeprod"]) {
			logInfoMsg("CONTROLE REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
			$prodhm_exe = realpath("../../../prodsgbd/iCTI_synchronize_prodsgbd_check.exe");
			$extractCmd = "$prodhm_exe -p $ADMPROVIDER_check -m activite";
			$return = exec($extractCmd, $output_array);
			foreach ($output_array as $output_line) {
				if (trim($output_line) != "") {
					if($ADMPROVIDER_check == $ADMPROVIDER) echo PHP_EOL . $output_line;
					else echo PHP_EOL . str_replace("ERROR_R", "WARNING", $output_line);
				}
			}
			logInfoMsg("FIN CONTROLE REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
		}
		else {
			logInfoMsg("REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
			$prodhm_exe = realpath("../../../prodsgbd/iCTI_synchronize_prodsgbd_put.exe");
			$extractCmd = "$prodhm_exe -p $ADMPROVIDER_check -m activite";
			$return = exec($extractCmd, $output_array);
			foreach ($output_array as $output_line) {
				if (trim($output_line) != "") {
					if($ADMPROVIDER_check == $ADMPROVIDER) echo PHP_EOL . $output_line;
					else echo PHP_EOL . str_replace("ERROR_R", "WARNING", $output_line);

				}
			}
			logInfoMsg("FIN REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
		}
		// Si CEGI, Il faut LOGHOS et CEGI
		if ($ADMPROVIDER_check == "CEGILOGHOS") {
			$ADMPROVIDER_check = "CEGI";
			if ($cti_args->args["-u"] || $cti_args->args["--prod-update"]) {
				if ($cti_args->args["-noactualizeprod"]) {
					logInfoMsg("CONTROLE REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
					$prodhm_exe = realpath("../../../prodsgbd/iCTI_synchronize_prodsgbd_check.exe");
					$extractCmd = "$prodhm_exe -p $ADMPROVIDER_check -m activitef";
					$return = exec($extractCmd, $output_array);
					foreach ($output_array as $output_line) {
						if (trim($output_line) != "") {
						if($ADMPROVIDER_check == $ADMPROVIDER) echo PHP_EOL . $output_line;
						else echo PHP_EOL . str_replace("ERROR_R", "WARNING", $output_line);

						}
					}
					logInfoMsg("FIN CONTROLE REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
				}
				else {
					logInfoMsg("REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
					$prodhm_exe = realpath("../../../prodsgbd/iCTI_synchronize_prodsgbd_put.exe");
					$extractCmd = "$prodhm_exe -p $ADMPROVIDER_check -m activitef";
					$return = exec($extractCmd, $output_array);
					foreach ($output_array as $output_line) {
						if (trim($output_line) != "") {
						if($ADMPROVIDER_check == $ADMPROVIDER) echo PHP_EOL . $output_line;
						else echo PHP_EOL . str_replace("ERROR_R", "WARNING", $output_line);

						}
					}
					logInfoMsg("FIN REPLICATION BASE DE DONNEES PRODUCTION $ADMPROVIDER_check");
				}
			}
		}
	}
	// mode débrayé (défaut)
	else {
		if ($ADMPROVIDER_check != "CEGI") {
			$return = checkLoadedData($ADMPROVIDER_check,'activite',($ADMPROVIDER_check == $ADMPROVIDER));
		}
		else {
			// Emplacement prodsgbd
            $DB_settings_file = "../../../settings/databases/cegi.XML";
            $DB_settings_xml = simplexml_load_file($DB_settings_file);

            $prodsgbd_dir = "";
            if ($DB_settings_xml) {
                $prodsgbd_dir = trim($DB_settings_xml['prodsgbd_dir']);
            }
            if ($prodsgbd_dir == "") {
                $prodsgbd_dir = "../../../prodsgbd";
            }
			if (!file_exists($prodsgbd_dir . "/CEGI/data/CEGIdata_CJ_EFS.CSV.GZ")) {
				$return = checkLoadedData('CEGILOGHOS','activite',($ADMPROVIDER_check == $ADMPROVIDER));
				$return += checkLoadedData($ADMPROVIDER_check,'activite',($ADMPROVIDER_check == $ADMPROVIDER));
			}
			else {
				$return = checkLoadedData($ADMPROVIDER_check,'activitef',($ADMPROVIDER_check == $ADMPROVIDER));
			}
		}
	}
	return $return;
}

// récupération historique
function extraction_environnement($ADMPROVIDER, $ADMPROVIDER_PREFIX, $ADMPROVIDER_OID) {

	global $iCTI_connexion;
	global $ENV_FINESS;
	global $ENV_TYPEETS;
	global $ENV_ADM_ANNEEDEBUT;
	global $cti_args;

	logInfoMsg("RECUPERATION DE LA PRODUCTION $ADMPROVIDER");

	// Paramètres base de données
	if ($ADMPROVIDER != "CHATAIGNERAIE" || $ADMPROVIDER2 != "CHATAIGNERAIE") {
		$file = "../../../settings/databases/$ADMPROVIDER.XML";
	}
	else {
		$file = "../../../settings/databases/CHATAIGNERAIE_AS400.XML";
	}
	$xml = simplexml_load_file($file);

	$DB_pmsischema = trim($xml['pmsilib']);
	$DB_activiteschema = trim($xml['admlib']);
	$DB_comptaschema = trim($xml['comptalib']);

	$idEtablissement = trim($xml['idEtablissement']);
	if ($idEtablissement == "") {
		$idEtablissement = "-1";
	}

	// Chargement requetes SQL
	$environmentVars=array();

	$environmentVars["TYPE_TRAITEMENT"] = "E";
	$environmentVars["PX"] = $ADMPROVIDER_PREFIX;
	if ($ADMPROVIDER_OID < 2) {
		$environmentVars["IPX"] = 0;
		$environmentVars["PPX"] = 0;
	}
	else {
		$environmentVars["IPX"] = $ADMPROVIDER_OID * 100000000000;
		$environmentVars["PPX"] = $ADMPROVIDER_OID;
	}
	$environmentVars["TPX"] = substr($ADMPROVIDER_PREFIX,1,2) . "_";
	$environmentVars["CPX"] = substr($ADMPROVIDER_PREFIX,1,2) . "_";
	if ($ADMPROVIDER_OID < 2) {
		$environmentVars["CPX"] = "";
	}

	$environmentVars["PMSISCHEMA"] = $DB_pmsischema;
	$environmentVars["ACTIVITESCHEMA"] = $DB_activiteschema;
	$environmentVars["COMPTASCHEMA"] = $DB_comptaschema;

	$environmentVars["ID_ETAB"] = $idEtablissement;
	$environmentVars["ENV_TYPEETS"] = $ENV_TYPEETS;
	$environmentVars["ENV_FINESS"] = $ENV_FINESS;

	$environmentVars["ENV_ADM_ANNEEDEBUT"] = $ENV_ADM_ANNEEDEBUT;
	
	$sqlOptions=array();
	
	foreach ($cti_args->args as $key => $value) {
		$sqlOptions[] = $key;
	}
	
	$cti_sqlStatements_share = new CTI_SqlStatements_class("iCTI_import_activite_SHARE.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	$cti_sqlStatements_provider = new CTI_SqlStatements_class("iCTI_import_activite_$ADMPROVIDER.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	$cti_sqlStatements_fusion = new CTI_SqlStatements_class("iCTI_import_activite_FUSION.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	
	$cti_sqlStatements_share->executeStatementsNode("RAZ");
	
	if ($ADMPROVIDER_PREFIX != "") {
		$cti_sqlStatements_fusion->executeStatementsNode("INIT_SI_FUSION");
	}
	
	$cti_sqlStatements_provider->executeStatementsNode("PARAM");
	$cti_sqlStatements_provider->executeStatementsNode("INIT");
	$cti_sqlStatements_provider->executeStatementsNode("SEJOUR");
	$cti_sqlStatements_provider->executeStatementsNode("FACTURE");
	$cti_sqlStatements_provider->executeStatementsNode("ENCOURS");
	$cti_sqlStatements_provider->executeStatementsNode("CHIFFRIER");
	$cti_sqlStatements_provider->executeStatementsNode("POST");
	
	$cti_sqlStatements_share->executeStatementsNode("POST");
	$cti_sqlStatements_share->executeStatementsNode("VACUUM");
	
	logInfoMsg("FIN RECUPERATION DE LA PRODUCTION $ADMPROVIDER");

}

// récupération historique
function fusion_environnement($ADMPROVIDER, $ADMPROVIDER_PREFIX, $ADMPROVIDER_OID) {

	global $iCTI_connexion;
	global $ENV_TYPEETS;
	global $ENV_FINESS;
	global $ENV_ADM_ANNEEDEBUT;

	global $cti_args;

	logInfoMsg("FUSION ENVIRONNEMENT $ADMPROVIDER");

	// Chargement requetes SQL
	$environmentShareVars=array();
	$environmentShareVars["PX"] = "";
	
	$environmentVars=array();
	$environmentVars["PX"] = $ADMPROVIDER_PREFIX;
	if ($ADMPROVIDER_OID < 2) {
		$environmentVars["IPX"] = 0;
		$environmentVars["PPX"] = 0;
	}
	else {
		$environmentVars["IPX"] = $ADMPROVIDER_OID * 100000000000;
		$environmentVars["PPX"] = $ADMPROVIDER_OID;
	}

	$environmentVars["TPX"] = substr($ADMPROVIDER_PREFIX,1,2) . "_";
	$environmentVars["CPX"] = substr($ADMPROVIDER_PREFIX,1,2) . "_";
	if ($ADMPROVIDER_OID < 2) {
		$environmentVars["CPX"] = "";
	}
	
	$sqlOptions=array();

	$cti_sqlStatements_share = new CTI_SqlStatements_class("iCTI_import_activite_SHARE.XML",$iCTI_connexion,$environmentShareVars,$sqlOptions);
	$cti_sqlStatements_provider = new CTI_SqlStatements_class("iCTI_import_activite_FUSION.XML",$iCTI_connexion,$environmentVars,$sqlOptions);
	
	if ($ADMPROVIDER_OID == 0) {
		$cti_sqlStatements_provider->executeStatementsNode("DISABLE_INDEX");
		$cti_sqlStatements_share->executeStatementsNode("RAZ");
	}
	
	$cti_sqlStatements_provider->executeStatementsNode("PARAM");
	$cti_sqlStatements_provider->executeStatementsNode("INIT");
	$cti_sqlStatements_provider->executeStatementsNode("SEJOUR");
	$cti_sqlStatements_provider->executeStatementsNode("FACTURE");
	$cti_sqlStatements_provider->executeStatementsNode("CHIFFRIER");
	
	if ($ADMPROVIDER_OID == 2) {
		$cti_sqlStatements_provider->executeStatementsNode("ENABLE_INDEX");
		$cti_sqlStatements_provider->executeStatementsNode("POST");
		
		$cti_sqlStatements_share->executeStatementsNode("POST");
		$cti_sqlStatements_share->executeStatementsNode("VACUUM");
	}
	logInfoMsg("FUSION ENVIRONNEMENT $ADMPROVIDER");

}


function iCTI_connect() {

global $iCTI_connexion;
global $iCTI_cache;
global $cti_args;
global $ENV_TYPEETS;
global $ENV_FINESS;
global $ENV_ADM_ANNEEDEBUT;
global $ADMPROVIDER;
global $ADMPROVIDER_PREFIX;
global $ADMPROVIDER2;
global $ADMPROVIDER2_PREFIX;
global $ADMPROVIDER2_ASPISTATUS;
global $ADMPROVIDER2_SYNCSTATUS;
global $ADMPROVIDER3;
global $ADMPROVIDER3_PREFIX;
global $ADMPROVIDER3_ASPISTATUS;
global $ADMPROVIDER3_SYNCSTATUS;

	$iCTI_connexion = FALSE;

	$file = "../../../settings/settings.XML";
	$settings_xml = simplexml_load_file($file);
	
	$ADMPROVIDER = "";
	$wADMPROVIDER = "";
	$ADMPROVIDER_PREFIX = "";
	$ADMPROVIDER2 = "";
	$wADMPROVIDER2 = "";
	$ADMPROVIDER2_PREFIX = "";
	$ADMPROVIDER2_ASPISTATUS = "";
	$ADMPROVIDER2_SYNCSTATUS = "";
	$ADMPROVIDER3 = "";
	$wADMPROVIDER3 = "";
	$ADMPROVIDER3_PREFIX = "";
	$ADMPROVIDER3_ASPISTATUS = "";
	$ADMPROVIDER3_SYNCSTATUS = "";
	//Initialisation de l'année de début à année en cours -5
	$ENV_ADM_ANNEEDEBUT = date("Y")-5;
	
	foreach ($settings_xml->ENVIRONMENT as $environmentNode) {
		foreach ($environmentNode->PROPERTY as $propertyNode) {
			if ($propertyNode['name'] == 'FINESS') {
				$ENV_FINESS = (string)$propertyNode['value'];
			}
			if ($propertyNode['name'] == 'TYPEETS') {
				$ENV_TYPEETS = (string)$propertyNode['value'];
			}
			if ($propertyNode['name'] == 'ADMPROVIDER') {
				$wADMPROVIDER = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER_PREFIX') {
				$ADMPROVIDER_PREFIX = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER2') {
				$wADMPROVIDER2 = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER2_PREFIX') {
				$ADMPROVIDER2_PREFIX = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER2_ASPISTATUS') {
				$ADMPROVIDER2_ASPISTATUS = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER2_SYNCSTATUS') {
				$ADMPROVIDER2_SYNCSTATUS = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER3') {
				$wADMPROVIDER3 = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER3_PREFIX') {
				$ADMPROVIDER3_PREFIX = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER3_ASPISTATUS') {
				$ADMPROVIDER3_ASPISTATUS = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'ADMPROVIDER3_SYNCSTATUS') {
				$ADMPROVIDER3_SYNCSTATUS = trim((string)$propertyNode['value']);
			}
			if ($propertyNode['name'] == 'OPTADM_ANNEEDEBUT') {
				//Soit on utilise dans le setting une année en dur soit un nombre d'année(s) précédé de la lettre A
				if (substr($propertyNode['value'],0,1)==='A'){
					//année en cours moins nombre années demandées
					$nbAnnee = substr($propertyNode['value'],1);
					$ENV_ADM_ANNEEDEBUT = date("Y")-intval($nbAnnee);
				} else if ($propertyNode['value']!="" && strlen($propertyNode['value']) === 4) {
					// si années écrit en dur
					$ENV_ADM_ANNEEDEBUT = $propertyNode['value'];
				}
			}
		}
	}
    
	logInfoMsg("DEPUIS $ENV_ADM_ANNEEDEBUT");

	if ($wADMPROVIDER == "" && $cti_args->args["-p"]) {
		$wADMPROVIDER = strtolower($cti_args->args["-p"]);
	}

	$ADMPROVIDER = uppercase_provider($wADMPROVIDER);
	
	if ($wADMPROVIDER2 != "") {
		$ADMPROVIDER2 = uppercase_provider($wADMPROVIDER2);
	}
	
	if ($wADMPROVIDER3 != "") {
		$ADMPROVIDER3 = uppercase_provider($wADMPROVIDER3);
	}
	
	if ($ADMPROVIDER == "") {
		logErrorMsg("Prestataire $wADMPROVIDER non traité actuellement");
		return FALSE;
	}
	
	if ($ADMPROVIDER2 == "") {
		$ADMPROVIDER_PREFIX = "";
		$ADMPROVIDER2_PREFIX = "";
		$ADMPROVIDER3_PREFIX = "";
		$ADMPROVIDER_PREFIX = "";
		$ADMPROVIDER2_PREFIX = "";
		$ADMPROVIDER3_PREFIX = "";
	}
	else {
		if ($ADMPROVIDER_PREFIX == "") {
			$ADMPROVIDER_PREFIX = substr($ADMPROVIDER,0,2);
		}
		$ADMPROVIDER_PREFIX = "_" . $ADMPROVIDER_PREFIX;
		
		if ($ADMPROVIDER2_PREFIX == "") {
			$ADMPROVIDER2_PREFIX = substr($ADMPROVIDER2,0,2);
		}
		$ADMPROVIDER2_PREFIX = "_" . $ADMPROVIDER2_PREFIX;
		
		if ($ADMPROVIDER3_PREFIX == "") {
			$ADMPROVIDER3_PREFIX = substr($ADMPROVIDER3,0,2);
		}
		$ADMPROVIDER3_PREFIX = "_" . $ADMPROVIDER3_PREFIX;
	}
	
	$iCTI_connexion = Database2Factory::getInstance(Environnement::PROVIDER_CTI);
	$ret = $iCTI_connexion->connect();

	return $iCTI_connexion;

}

function uppercase_provider($_provider) {
	$toRet = "";

	if ($_provider == "r2i" || $_provider == "hm") {
		$toRet = "HM";
	} else {
		$toRet = strtoupper($_provider);
	}

	return $toRet;
}

function copy_schema($toSuffixe) {

}
  
?>