<?php 
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");
require_once("../../base/php/Functions.php");
require_once("../../base/php/WebAppLog.php");

global $database;
global $finess;
global $typeEts;
global $tmp_path;

set_time_limit(3600);
ini_set('memory_limit','256M');


initializeDir();


$tmp_path = "../../../temp";


$returnCode = "OK";
$returnTexte = "";


$httpString = "";
$httpString = $httpString . "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$httpString = $httpString . "\n<HTTP_SERVICE returnCode=\"$returnCode\" returnTexte=\"$returnTexte\" >";
$httpString = $httpString . get_files();
$httpString = $httpString . "\n</HTTP_SERVICE>";
 

$httpString = gzcompress($httpString,9);
$httpString = "_c_" . base64_encode($httpString); 

echo "<CTICONTENT>$httpString</CTICONTENT>";

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

	global $tmp_path;
	global $typeEts;
	global $ENV_TYPEETS;
	global $FILES_array;
	
	$FILES_array = array();
	$httpString = "";
	
	$path = $tmp_path;
	$rep=@opendir($path);
	if ($rep > 0) {
		while ($FILE_fullname = readdir($rep)){
			if($FILE_fullname != '..' && $FILE_fullname !='.' && $FILE_fullname !='' && substr($FILE_fullname,0,10) == "PMSIMPORT."  ){
				$longFile = $path . '/' . $FILE_fullname;
			
				$fileType = "";
				$path_parts = pathinfo($longFile);

				$fileType =  strtoupper($path_parts['extension']);
				if (is_dir($longFile)) {
					$fileType = "DIR";
				}
				
				$array = explode(".", $FILE_fullname);
				$FILE_index = 0;
				$FILE_pmsitype = $array[1];

				// On ne récupère pas les fichiers originaux de druides du dossier temp
				$druide_types = array("DRUIDE_SEJ", "DRUIDE_ACE", "DRUIDE_OQN");
				if (in_array($FILE_pmsitype, $druide_types, true)){
					continue;
				}

				$FILE_finess = $array[2];
				$FILE_mois = $array[3];
				if ($FILE_mois == "0000") {
					$FILE_mois = "";
				}
				$FILE_date = date("d/m/Y H:i", $array[4]);
				array_shift($array);
				array_shift($array);
				array_shift($array);
				array_shift($array);
				array_shift($array);
				$FILE_name = implode(".", $array);
				$FILE_size = filesize($longFile);
				$FILE_pmsitypeTexte = $FILE_pmsitype;
				switch ($FILE_pmsitype) {
                    case "RSS" :
                        $FILE_pmsitypeTexte = "RSS.TXT (ex OQN)";
                        $FILE_index = 11;
                        break;
                    case "RSF" :
                        $FILE_pmsitypeTexte = "RSF.TXT (ex OQN)";
                        $FILE_index = 12;
                        break;
                    case "ZIN" :
                        $FILE_pmsitypeTexte = "RSS.IN.ZIP";
                        $FILE_index = 1;
                        break;
                    case "ZOU" :
                        $FILE_pmsitypeTexte = "RSS.OUT.ZIP";
                        $FILE_index = 2;
                        break;
                    case "ZIF" :
                        $FILE_pmsitypeTexte = "RSF.IN.ZIP (ex DG)";
                        $FILE_index = 3;
                        break;
                    case "MED" :
                        $FILE_pmsitypeTexte = "Compléments : Médecins";
                        $FILE_index = 21;
                        break;
                    case "RSM" :
                        $FILE_pmsitypeTexte = "Compléments : Médecins par RSS";
                        $FILE_index = 22;
                        break;
                    case "RSI" :
                        $FILE_pmsitypeTexte = "Compléments : Identités des RSS";
                        $FILE_index = 23;
                        break;
                    case "ACM" :
                        $FILE_pmsitypeTexte = "Compléments : Exécutants actes";
                        $FILE_index = 24;
                        break;
                    case "VSJ" :
                        $FILE_pmsitypeTexte = "Valorisation des séjours";
                        $FILE_index = 25;
                        break;
                    case "VSE" :
                        $FILE_pmsitypeTexte = "Valorisation des ACE";
                        $FILE_index = 26;
						break;
                    case "TSP" :
                        $FILE_pmsitypeTexte = "FICHSUP transports";
                        $FILE_index = 27;
                        break;
                }
				
				$FILE_object["fullname"] = $FILE_fullname;
				$FILE_object["name"] = $FILE_name;
				$FILE_object["fullpath"] = $longFile;
				$FILE_object["pmsitype"] = $FILE_pmsitype;
				$FILE_object["pmsitypeTexte"] = $FILE_pmsitypeTexte;
				$FILE_object["finess"] = $FILE_finess;
				$FILE_object["mois"] = $FILE_mois;
				$FILE_object["date"] = $FILE_date;
				$FILE_object["size"] = $FILE_size;
				
				$FILES_array[$FILE_index] = $FILE_object;
			}
		}
	}
	@closedir($rep);
	
	$ZIN_mois = "";
	$ZIN_name = "";
	$ZOU_mois = "";
	$ZOU_name = "";
	$ZIF_mois = "";
	$ZIF_name = "";
	$RSS_mois = "";
	$RSS_name = "";
	$RSF_mois = "";
	$RSF_name = "";
	$VSJ_mois = "";
	$VSJ_name = "";
	$VSJ_size = 0;
	$VSE_mois = "";
	$VSE_name = "";
	$VSE_size = 0;
	

	ksort($FILES_array);
	foreach ($FILES_array as &$FILE_object) {
		$FILE_name = $FILE_object["name"];
		$FILE_fullname = $FILE_object["fullname"];
		$FILE_fullpath = $FILE_object["fullpath"];
		$FILE_pmsitype = $FILE_object["pmsitype"];
		$FILE_pmsitypeTexte = $FILE_object["pmsitypeTexte"];
		$FILE_finess = $FILE_object["finess"];
		$FILE_mois = $FILE_object["mois"];
		$FILE_date = $FILE_object["date"];
		$FILE_size = $FILE_object["size"];
		
		if ($FILE_pmsitype == "ZIN") {
			$ZIN_mois = $FILE_mois;
			$ZIN_name = $FILE_name;
		}
		if ($FILE_pmsitype == "ZOU") {
			$ZOU_mois = $FILE_mois;
			$ZOU_name = $FILE_name;
		}
		if ($FILE_pmsitype == "ZIF") {
			$ZIF_mois = $FILE_mois;
			$ZIF_name = $FILE_name;
		}
		if ($FILE_pmsitype == "VSJ") {
			$VSJ_mois = $FILE_mois;
			$VSJ_name = $FILE_name;
			$VSJ_size = $FILE_size;
		}
		if ($FILE_pmsitype == "VSE") {
			$VSE_mois = $FILE_mois;
			$VSE_name = $FILE_name;
			$VSE_size = $FILE_size;
		}
		if ($FILE_pmsitype == "RSS") {
			$RSS_mois = $FILE_mois;
			$RSS_name = $FILE_name;
		}
		if ($FILE_pmsitype == "RSF") {
			$RSF_mois = $FILE_mois;
			$RSF_name = $FILE_name;
		}
				
		$httpString = $httpString . "\r\n<FILE name=\"$FILE_name\" type=\"$FILE_pmsitype\" typeTexte=\"$FILE_pmsitypeTexte\" fullname=\"$FILE_fullname\" fullpath=\"$FILE_fullpath\" mois=\"$FILE_mois\" date=\"$FILE_date\" size=\"$FILE_size\" />";
	}
	
	$statusCode = "OK";
	$statusText = "";
	
	if ($statusCode == "OK") {
		if ($ZIN_name == "" && $RSS_name == "") {
			$statusCode = "KO";
			$statusText = "Le fichier RSS est obligatoire";
		}
	}
	
	if ($statusCode == "OK") {
		if ($ZIN_name != "" && $ZOU_name == "") {
			$statusCode = "KO";
			$statusText = "Le fichier OUT des RSS est obligatoire";
		}
	}
	
	if ($statusCode == "OK") {
		if ($RSS_name != "" && $RSF_name != "" && $RSS_mois != $RSF_mois) {
			$statusCode = "KO";
			$statusText = "Les fichiers RSS et RSF doivent être au même mois";
		}
	}
	
	if ($statusCode == "OK") {
		if ($RSS_name != "" && $RSF_name == "") {
			$statusCode = "ATT";
			$statusText = "Le fichier RSF est manquant";
		}
	}
	
	
	if ($statusCode == "OK") {
		if ($ZIN_name != "" && $ZOU_name != "" && $ZIN_mois != $ZOU_mois) {
			$statusCode = "KO";
			$statusText = "Les fichiers IN et OUT doivent être au même mois";
		}
	}
	
	if ($statusCode == "OK") {
		if ($ZIN_name != "" && $ZOU_name != "" && $ZIN_name != str_replace(".out.",".in.",$ZOU_name)) {
			$statusCode = "KO";
			$statusText = "Les fichiers IN et OUT doivent avoir le même horodatage";
		}
	}
	
	if ($statusCode == "OK") {
		if ($typeEts == "2" || $typeEts == "3") {
			if ($ZIN_name != "" && $VSJ_name == "") {
				$statusCode = "KO";
				$statusText = "Le fichier VALOSEJ est obligatoire";
			}
		}
	}
	
	if ($statusCode == "OK") {
		if ($typeEts == "2" || $typeEts == "3") {
			if ($ZIN_name != "" && $VSJ_name != "" && $VSJ_size < 1000) {
				$statusCode = "KO";
				$statusText = "Le fichier VALOSEJ est vide";
			}
		}
	}
	
	if ($statusCode == "OK") {
		if ($typeEts == "2" || $typeEts == "3") {
			if ($ZIF_name != "" && $VSE_name != "" && $VSE_size < 1000) {
				$statusCode = "KO";
				$statusText = "Le fichier VALOACE est vide";
			}
		}
	}
	
	if ($statusCode == "OK") {
		if ($ZIN_name != "" && $ZIF_name != "" && $ZIN_mois != $ZIF_mois) {
			$statusCode = "KO";
			$statusText = "Les fichiers RSS et ACE doivent être au même mois";
		}
	}
	if ($statusCode == "OK") {
		if ($ZIN_name != "" && $VSJ_name != "" && $ZIN_mois != $VSJ_mois) {
			$statusCode = "KO";
			$statusText = "Les fichiers RSS IN et VALOSEJ doivent être au même mois";
		}
	}
	if ($statusCode == "OK") {
		if ($ZIF_name != "" && $VSE_name != "" && $ZIF_mois != $VSE_mois) {
			$statusCode = "KO";
			$statusText = "Les fichiers RSF ACE et VALOACE doivent être au même mois";
		}
	}
	
	if ($statusCode == "OK") {
		if ($typeEts == "2" || $typeEts == "3") {
			if ($ZIF_name != "" && $VSE_name == "") {
				$statusCode = "ATT";
				$statusText = "Le fichier VALOACE est indispensable à une valorisation correcte. L'import est malgré tout possible";
			}
		}
	}
	
	$httpString = $httpString . "\r\n<STATUS statusCode=\"$statusCode\" statusText=\"$statusText\" />";

	return $httpString;
}


function initializeDir() {
	
	if (! file_exists("../../../temp")) {
		@mkdir("../../../temp");
	}

}
?> 