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

set_time_limit(3600);

$import_id = @$_POST["import_id"];
if ($import_id == "") {
	$import_id = @$_GET["import_id"];
}
$import_id = $import_id + 0;

$option = @$_POST["option"];
if ($option == "") {
	$option = @$_GET["option"];
}

$option_expert = @$_POST["option_expert"];
if ($option_expert == "") {
	$option_expert = @$_GET["option_expert"];
}

$option_essentiels = @$_POST["option_essentiels"];
if ($option_essentiels == "") {
	$option_essentiels = @$_GET["option_essentiels"];
}

$option_complements = @$_POST["option_complements"];
if ($option_complements == "") {
	$option_complements = @$_GET["option_complements"];
}


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

// Imports fichiers 
if ($option != "encours") {


    $database = new Database("iCTI");

	$OK = FALSE;
	$result  =  $database->exec("SELECT oid FROM pmsi.p_imports WHERE oid = $import_id ");
	if ($result != false) {
		$record = $database->nextRecordInto();
		if ($record != FALSE) {
			$OK = TRUE;
		}
		
	}
	
	if ($OK == TRUE) {
	
		// Controle en interactif
		if ($option == "C") {
			$sqlcmd = "";
			$sqlcmd = "UPDATE pmsi.p_imports SET traitement_a_faire = 'C' WHERE oid = $import_id ";
			$result  =  $database->exec($sqlcmd);
			
			$submitCmd = "..\import_rss_rsf\iCTI_import_rss_rsf.exe";
			$return = exec($submitCmd);
			
			$result  =  $database->exec("SELECT nb_erreurs, nb_avertissements FROM pmsi.p_imports WHERE oid = $import_id ");
			if ($result != false) {
				$record = $database->nextRecordInto();
				if ($record != FALSE) {
					$nb_erreurs = $record[0] + 0;
					$nb_avertissements = $record[1] + 0;
					
					if ($nb_erreurs > 0) {
						$returnCode = "KO";
						$returnTexte = "$nb_erreurs erreur(s) grave(s)";
						if ($nb_avertissements > 0) {
							$returnTexte = $returnTexte . ", $nb_avertissements avertissement(s)";
						}
					}
					else {
						if ($nb_avertissements > 0) {
							$returnCode = "ATT";
							$returnTexte = "$nb_avertissements avertissement(s)";
						}
					}
				}
			}
		}
		
		// Génération en batch
		if ($option == "G") {
			// Sauf si erreurs blocantes
			$nb_erreurs_blocantes = 0;
			$traitement_en_cours = 0;
			
			$sqlcmd = "SELECT 
					SUM(CASE WHEN import_id = $import_id AND gravite >= 9 THEN 1 ELSE 0 END) as count_gravite9,
					count(DISTINCT CASE WHEN etat_en_cours = 'G' THEN import_id ELSE NULL END) as count_generationencours
				FROM pmsi.p_imports
				LEFT JOIN pmsi.p_imports_controles ON import_id = p_imports.oid";

			$result = $database->exec($sqlcmd);
			if ($result != false) {
				$record = $database->nextRecordInto();
				if ($record != FALSE) {
					$OK = TRUE;
					$nb_erreurs_blocantes = (int)$record[0];
					$traitement_en_cours = (int)$record[1];
				}
				else {
				
				}
			}
			else {
				$returnCode = "KO";
				$returnTexte = "Accès à la base iCTI en erreur";
			}
	
			if ($returnCode != "KO") {
				if ($nb_erreurs_blocantes > 0 || $traitement_en_cours > 0) {
					$returnCode = "KO";
					if ($nb_erreurs_blocantes > 0) {
						if ($nb_erreurs_blocantes == 1) {
							$returnTexte = "1 erreur blocante empêche la génération";
						}
						else {
							$returnTexte = "$nb_erreurs_blocantes erreurs blocantes empêchent la génération";
						}
					}
					else {
						$returnTexte = "Au moins un traitement de génération est actif actuellement";
					}
				}
				else {
					$sqlcmd = "";

					$sqlcmd = "UPDATE pmsi.p_imports SET traitement_a_faire = 'G' WHERE oid = $import_id ";
					$result  =  $database->exec($sqlcmd);
					
					$exe = rootDir() . "\modules\pmsi\import_rss_rsf\iCTI_import_rss_rsf.exe";
                    $args = "";
					if ($option_expert == "false") {$args .= " -xx";}
					if ($option_essentiels == "false") {$args .= " -xe";}
					if ($option_complements == "false") {$args .= " -xc";}
                    if (strlen($args) > 0) { $args = " \"" . $args . "\"";}
					$submitCmd = "..\..\outils\exe\iCTI_submitJob.exe \"$exe\"$args";
                    $return = exec($submitCmd);
				}
			}
		}
		
		// suppression
		if ($option == "D") {
			$sqlcmd = "";
			$sqlcmd = "DELETE FROM pmsi.p_imports WHERE oid = $import_id; ";
			$sqlcmd = $sqlcmd . "DELETE FROM pmsi.p_imports_data WHERE import_id = $import_id; ";
			$sqlcmd = $sqlcmd . "DELETE FROM pmsi.p_imports_controles WHERE import_id = $import_id; ";
			$result = $database->exec($sqlcmd);
		}
	
	}
	
	else {
		$returnCode = "KO";
		$returnTexte = "Import n° $import_id non trouvé";
	}
}

else {
	$exe = rootDir() . "\modules\pmsi\import_rss_rsf\iCTI_import_rss_rsf_en_cours.exe";
					
	$submitCmd = "..\..\outils\exe\iCTI_submitJob.exe \"$exe\"";
	$return = exec($submitCmd);

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

 

$httpString = compress64($httpString);
echo "<CTICONTENT>$httpString</CTICONTENT>";



    
?>