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

global $database;
global $action;
global $importString;
global $recordNode;

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

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

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


$recordXML = @$_POST["recordNode"];
if ($recordXML == "") {
	$recordXML = @$_GET["recordNode"];
}
$recordNode = @simplexml_load_string($recordXML);

$database = new Database("iCTI");

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

switch ($tableName) {
	case"t_fiche5_enquete" :
		$httpString .= setRecords_fiche5_enquete();
		break;
	case"p_fiche5_rss" :
		$httpString .= setRecords_fiche5_rss();
		break;
	
}


erase_cache();

$httpString .= "\n</HTTP_SERVICE>";
 
$httpString = compress64($httpString);
echo "<CTICONTENT>$httpString</CTICONTENT>";



function toSQLnum($val) {
	$num = str_replace(",", ".", $val) + 0;
	
	return $num;
}

function setRecords_fiche5_enquete() {

	global $database;
	global $action;
	global $recordNode;
	
	$oid = floatval($recordNode["oid"]);
	$code = utf8_decode($recordNode["code"]);
	$texte = utf8_decode($recordNode["texte"]);
	$annee = utf8_decode($recordNode["annee"]);
	$data = utf8_decode($recordNode["data"]);
	$fg_dir = realpath("../../../../iCTI_system\FG\MCO\FG$annee");
	
	

	if ($action != "delete" && $action != "copy") {
		$code = toSQL($code);
		$texte = toSQL($texte);
		
		// creation ou mise à  jour de la liste
		if ($oid == -1) {
		
			$sqlcmd = "INSERT INTO pmsi.t_fiche5_enquete
						(
						code,
						texte,
						annee,
						data,
						fg_dir
						)
						VALUES(
						'$code',
						'$texte',
						$annee,
						'$data',
						'$fg_dir'
						)";
			$result  =  $database->exec($sqlcmd);
			if ($result == FALSE) {
				
			}
			else {
				$result = $database->exec("SELECT MAX(oid) as oid FROM pmsi.t_fiche5_enquete");
				if ($result != false) {
					$record = $database->nextRecordInto();
					if ($record != FALSE) {
						$oid = floatval($record[0]);
					}
				}
			}
		}
		else {
			
			$sqlcmd = "UPDATE pmsi.t_fiche5_enquete SET 
						code = '$code', 
						texte = '$texte',
						annee = $annee,
						data = '$data',
						fg_dir = '$fg_dir'
						WHERE oid = $oid";
			$result  =  $database->exec($sqlcmd);
		}
		
		if ($oid > 0) {
			$sqlcmd = "SELECT pmsi.cti_fiche5_extract($oid)";
			$result  =  $database->exec($sqlcmd);
			
			extract_rss_ng($oid);
			
			$sqlcmd = "SELECT pmsi.cti_fiche5_groupage($oid,0)";
			$result  =  $database->exec($sqlcmd);
		}
				

	}

	if ($action == "copy") {
	

	}


	if ($action == "delete") {
		$sqlcmd = "DELETE FROM pmsi.t_fiche5_enquete WHERE oid = $oid";
		$result  =  $database->exec($sqlcmd);
		$sqlcmd = "DELETE FROM pmsi.p_fiche5_rss WHERE enquete_id = $oid";
		$result  =  $database->exec($sqlcmd);
	}
	
	
}


function setRecords_fiche5_rss() {

	global $database;
	global $action;
	global $recordNode;
	
	$httpString = "";
	
	$oid = floatval($recordNode["oid"]);
	
	$status = 							trim(utf8_decode($recordNode["status"]));
	
	$scenar1_rule = 					trim(utf8_decode($recordNode["scenar1_rule"]));
	$scenar1_comment = 					trim(utf8_decode($recordNode["scenar1_comment"]));
	$scenar1_rum_dp_code_01 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_01"]));
	$scenar1_rum_dp_code_02 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_02"]));
	$scenar1_rum_dp_code_03 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_03"]));
	$scenar1_rum_dp_code_04 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_04"]));
	$scenar1_rum_dp_code_05 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_05"]));
	$scenar1_rum_dp_code_06 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_06"]));
	$scenar1_rum_dp_code_07 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_07"]));
	$scenar1_rum_dp_code_08 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_08"]));
	$scenar1_rum_dp_code_09 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_09"]));
	$scenar1_rum_dp_code_10 = 			trim(utf8_decode($recordNode["scenar1_rum_dp_code_10"]));
	$scenar1_rum_dr_code_01 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_01"]));
	$scenar1_rum_dr_code_02 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_02"]));
	$scenar1_rum_dr_code_03 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_03"]));
	$scenar1_rum_dr_code_04 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_04"]));
	$scenar1_rum_dr_code_05 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_05"]));
	$scenar1_rum_dr_code_06 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_06"]));
	$scenar1_rum_dr_code_07 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_07"]));
	$scenar1_rum_dr_code_08 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_08"]));
	$scenar1_rum_dr_code_09 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_09"]));
	$scenar1_rum_dr_code_10 = 			trim(utf8_decode($recordNode["scenar1_rum_dr_code_10"]));
	$scenar1_rum_da_code_01 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_01"]));
	$scenar1_rum_da_code_02 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_02"]));
	$scenar1_rum_da_code_03 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_03"]));
	$scenar1_rum_da_code_04 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_04"]));
	$scenar1_rum_da_code_05 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_05"]));
	$scenar1_rum_da_code_06 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_06"]));
	$scenar1_rum_da_code_07 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_07"]));
	$scenar1_rum_da_code_08 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_08"]));
	$scenar1_rum_da_code_09 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_09"]));
	$scenar1_rum_da_code_10 = 			trim(utf8_decode($recordNode["scenar1_rum_da_code_10"]));
	$scenar1_rum_ccam_code_01 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_01"]));
	$scenar1_rum_ccam_code_02 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_02"]));
	$scenar1_rum_ccam_code_03 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_03"]));
	$scenar1_rum_ccam_code_04 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_04"]));
	$scenar1_rum_ccam_code_05 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_05"]));
	$scenar1_rum_ccam_code_06 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_06"]));
	$scenar1_rum_ccam_code_07 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_07"]));
	$scenar1_rum_ccam_code_08 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_08"]));
	$scenar1_rum_ccam_code_09 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_09"]));
	$scenar1_rum_ccam_code_10 = 		trim(utf8_decode($recordNode["scenar1_rum_ccam_code_10"]));
	
	$scenar2_rule = 					trim(utf8_decode($recordNode["scenar2_rule"]));
	$scenar2_comment = 					trim(utf8_decode($recordNode["scenar2_comment"]));
	$scenar2_rum_dp_code_01 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_01"]));
	$scenar2_rum_dp_code_02 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_02"]));
	$scenar2_rum_dp_code_03 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_03"]));
	$scenar2_rum_dp_code_04 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_04"]));
	$scenar2_rum_dp_code_05 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_05"]));
	$scenar2_rum_dp_code_06 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_06"]));
	$scenar2_rum_dp_code_07 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_07"]));
	$scenar2_rum_dp_code_08 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_08"]));
	$scenar2_rum_dp_code_09 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_09"]));
	$scenar2_rum_dp_code_10 = 			trim(utf8_decode($recordNode["scenar2_rum_dp_code_10"]));
	$scenar2_rum_dr_code_01 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_01"]));
	$scenar2_rum_dr_code_02 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_02"]));
	$scenar2_rum_dr_code_03 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_03"]));
	$scenar2_rum_dr_code_04 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_04"]));
	$scenar2_rum_dr_code_05 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_05"]));
	$scenar2_rum_dr_code_06 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_06"]));
	$scenar2_rum_dr_code_07 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_07"]));
	$scenar2_rum_dr_code_08 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_08"]));
	$scenar2_rum_dr_code_09 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_09"]));
	$scenar2_rum_dr_code_10 = 			trim(utf8_decode($recordNode["scenar2_rum_dr_code_10"]));
	$scenar2_rum_da_code_01 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_01"]));
	$scenar2_rum_da_code_02 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_02"]));
	$scenar2_rum_da_code_03 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_03"]));
	$scenar2_rum_da_code_04 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_04"]));
	$scenar2_rum_da_code_05 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_05"]));
	$scenar2_rum_da_code_06 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_06"]));
	$scenar2_rum_da_code_07 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_07"]));
	$scenar2_rum_da_code_08 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_08"]));
	$scenar2_rum_da_code_09 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_09"]));
	$scenar2_rum_da_code_10 = 			trim(utf8_decode($recordNode["scenar2_rum_da_code_10"]));
	$scenar2_rum_ccam_code_01 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_01"]));
	$scenar2_rum_ccam_code_02 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_02"]));
	$scenar2_rum_ccam_code_03 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_03"]));
	$scenar2_rum_ccam_code_04 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_04"]));
	$scenar2_rum_ccam_code_05 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_05"]));
	$scenar2_rum_ccam_code_06 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_06"]));
	$scenar2_rum_ccam_code_07 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_07"]));
	$scenar2_rum_ccam_code_08 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_08"]));
	$scenar2_rum_ccam_code_09 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_09"]));
	$scenar2_rum_ccam_code_10 = 		trim(utf8_decode($recordNode["scenar2_rum_ccam_code_10"]));
	
	$control_rule = 					trim(utf8_decode($recordNode["control_rule"]));
	$control_comment = 					trim(utf8_decode($recordNode["control_comment"]));
	$control_rum_dp_code_01 = 			trim(utf8_decode($recordNode["control_rum_dp_code_01"]));
	$control_rum_dp_code_02 = 			trim(utf8_decode($recordNode["control_rum_dp_code_02"]));
	$control_rum_dp_code_03 = 			trim(utf8_decode($recordNode["control_rum_dp_code_03"]));
	$control_rum_dp_code_04 = 			trim(utf8_decode($recordNode["control_rum_dp_code_04"]));
	$control_rum_dp_code_05 = 			trim(utf8_decode($recordNode["control_rum_dp_code_05"]));
	$control_rum_dp_code_06 = 			trim(utf8_decode($recordNode["control_rum_dp_code_06"]));
	$control_rum_dp_code_07 = 			trim(utf8_decode($recordNode["control_rum_dp_code_07"]));
	$control_rum_dp_code_08 = 			trim(utf8_decode($recordNode["control_rum_dp_code_08"]));
	$control_rum_dp_code_09 = 			trim(utf8_decode($recordNode["control_rum_dp_code_09"]));
	$control_rum_dp_code_10 = 			trim(utf8_decode($recordNode["control_rum_dp_code_10"]));
	$control_rum_dr_code_01 = 			trim(utf8_decode($recordNode["control_rum_dr_code_01"]));
	$control_rum_dr_code_02 = 			trim(utf8_decode($recordNode["control_rum_dr_code_02"]));
	$control_rum_dr_code_03 = 			trim(utf8_decode($recordNode["control_rum_dr_code_03"]));
	$control_rum_dr_code_04 = 			trim(utf8_decode($recordNode["control_rum_dr_code_04"]));
	$control_rum_dr_code_05 = 			trim(utf8_decode($recordNode["control_rum_dr_code_05"]));
	$control_rum_dr_code_06 = 			trim(utf8_decode($recordNode["control_rum_dr_code_06"]));
	$control_rum_dr_code_07 = 			trim(utf8_decode($recordNode["control_rum_dr_code_07"]));
	$control_rum_dr_code_08 = 			trim(utf8_decode($recordNode["control_rum_dr_code_08"]));
	$control_rum_dr_code_09 = 			trim(utf8_decode($recordNode["control_rum_dr_code_09"]));
	$control_rum_dr_code_10 = 			trim(utf8_decode($recordNode["control_rum_dr_code_10"]));
	$control_rum_da_code_01 = 			trim(utf8_decode($recordNode["control_rum_da_code_01"]));
	$control_rum_da_code_02 = 			trim(utf8_decode($recordNode["control_rum_da_code_02"]));
	$control_rum_da_code_03 = 			trim(utf8_decode($recordNode["control_rum_da_code_03"]));
	$control_rum_da_code_04 = 			trim(utf8_decode($recordNode["control_rum_da_code_04"]));
	$control_rum_da_code_05 = 			trim(utf8_decode($recordNode["control_rum_da_code_05"]));
	$control_rum_da_code_06 = 			trim(utf8_decode($recordNode["control_rum_da_code_06"]));
	$control_rum_da_code_07 = 			trim(utf8_decode($recordNode["control_rum_da_code_07"]));
	$control_rum_da_code_08 = 			trim(utf8_decode($recordNode["control_rum_da_code_08"]));
	$control_rum_da_code_09 = 			trim(utf8_decode($recordNode["control_rum_da_code_09"]));
	$control_rum_da_code_10 = 			trim(utf8_decode($recordNode["control_rum_da_code_10"]));
	$control_rum_ccam_code_01 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_01"]));
	$control_rum_ccam_code_02 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_02"]));
	$control_rum_ccam_code_03 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_03"]));
	$control_rum_ccam_code_04 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_04"]));
	$control_rum_ccam_code_05 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_05"]));
	$control_rum_ccam_code_06 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_06"]));
	$control_rum_ccam_code_07 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_07"]));
	$control_rum_ccam_code_08 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_08"]));
	$control_rum_ccam_code_09 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_09"]));
	$control_rum_ccam_code_10 = 		trim(utf8_decode($recordNode["control_rum_ccam_code_10"]));
	
	$retenu_rule = 						trim(utf8_decode($recordNode["retenu_rule"]));
	$retenu_comment = 					trim(utf8_decode($recordNode["retenu_comment"]));
	$retenu_rum_dp_code_01 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_01"]));
	$retenu_rum_dp_code_02 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_02"]));
	$retenu_rum_dp_code_03 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_03"]));
	$retenu_rum_dp_code_04 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_04"]));
	$retenu_rum_dp_code_05 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_05"]));
	$retenu_rum_dp_code_06 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_06"]));
	$retenu_rum_dp_code_07 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_07"]));
	$retenu_rum_dp_code_08 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_08"]));
	$retenu_rum_dp_code_09 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_09"]));
	$retenu_rum_dp_code_10 = 			trim(utf8_decode($recordNode["retenu_rum_dp_code_10"]));
	$retenu_rum_dr_code_01 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_01"]));
	$retenu_rum_dr_code_02 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_02"]));
	$retenu_rum_dr_code_03 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_03"]));
	$retenu_rum_dr_code_04 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_04"]));
	$retenu_rum_dr_code_05 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_05"]));
	$retenu_rum_dr_code_06 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_06"]));
	$retenu_rum_dr_code_07 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_07"]));
	$retenu_rum_dr_code_08 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_08"]));
	$retenu_rum_dr_code_09 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_09"]));
	$retenu_rum_dr_code_10 = 			trim(utf8_decode($recordNode["retenu_rum_dr_code_10"]));
	$retenu_rum_da_code_01 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_01"]));
	$retenu_rum_da_code_02 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_02"]));
	$retenu_rum_da_code_03 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_03"]));
	$retenu_rum_da_code_04 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_04"]));
	$retenu_rum_da_code_05 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_05"]));
	$retenu_rum_da_code_06 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_06"]));
	$retenu_rum_da_code_07 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_07"]));
	$retenu_rum_da_code_08 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_08"]));
	$retenu_rum_da_code_09 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_09"]));
	$retenu_rum_da_code_10 = 			trim(utf8_decode($recordNode["retenu_rum_da_code_10"]));
	$retenu_rum_ccam_code_01 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_01"]));
	$retenu_rum_ccam_code_02 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_02"]));
	$retenu_rum_ccam_code_03 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_03"]));
	$retenu_rum_ccam_code_04 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_04"]));
	$retenu_rum_ccam_code_05 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_05"]));
	$retenu_rum_ccam_code_06 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_06"]));
	$retenu_rum_ccam_code_07 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_07"]));
	$retenu_rum_ccam_code_08 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_08"]));
	$retenu_rum_ccam_code_09 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_09"]));
	$retenu_rum_ccam_code_10 = 			trim(utf8_decode($recordNode["retenu_rum_ccam_code_10"]));

	$code = toSQL($code);
	$texte = toSQL($texte);
	$scenar1_comment = toSQL($scenar1_comment);
	$scenar2_comment = toSQL($scenar2_comment);
	$control_comment = toSQL($control_comment);
	$retenu_comment = toSQL($retenu_comment);


	$sqlcmd = "UPDATE pmsi.p_fiche5_rss SET 
				status =				'$status',
				
				scenar1_rule =			'$scenar1_rule',
				scenar1_comment =		'$scenar1_comment',
				scenar1_rum_dp_code = ARRAY[
										'$scenar1_rum_dp_code_01',
										'$scenar1_rum_dp_code_02',
										'$scenar1_rum_dp_code_03',
										'$scenar1_rum_dp_code_04',
										'$scenar1_rum_dp_code_05',
										'$scenar1_rum_dp_code_06',
										'$scenar1_rum_dp_code_07',
										'$scenar1_rum_dp_code_08',
										'$scenar1_rum_dp_code_09',
										'$scenar1_rum_dp_code_10'
										],
				scenar1_rum_dr_code = ARRAY[
										'$scenar1_rum_dr_code_01',
										'$scenar1_rum_dr_code_02',
										'$scenar1_rum_dr_code_03',
										'$scenar1_rum_dr_code_04',
										'$scenar1_rum_dr_code_05',
										'$scenar1_rum_dr_code_06',
										'$scenar1_rum_dr_code_07',
										'$scenar1_rum_dr_code_08',
										'$scenar1_rum_dr_code_09',
										'$scenar1_rum_dr_code_10'
										],
				scenar1_rum_da_code = ARRAY[
										'$scenar1_rum_da_code_01',
										'$scenar1_rum_da_code_02',
										'$scenar1_rum_da_code_03',
										'$scenar1_rum_da_code_04',
										'$scenar1_rum_da_code_05',
										'$scenar1_rum_da_code_06',
										'$scenar1_rum_da_code_07',
										'$scenar1_rum_da_code_08',
										'$scenar1_rum_da_code_09',
										'$scenar1_rum_da_code_10'
										],
				scenar1_rum_ccam_code = ARRAY[
										'$scenar1_rum_ccam_code_01',
										'$scenar1_rum_ccam_code_02',
										'$scenar1_rum_ccam_code_03',
										'$scenar1_rum_ccam_code_04',
										'$scenar1_rum_ccam_code_05',
										'$scenar1_rum_ccam_code_06',
										'$scenar1_rum_ccam_code_07',
										'$scenar1_rum_ccam_code_08',
										'$scenar1_rum_ccam_code_09',
										'$scenar1_rum_ccam_code_10'
										],
				
				scenar2_rule =			'$scenar2_rule',
				scenar2_comment =		'$scenar2_comment',				
				scenar2_rum_dp_code = ARRAY[
										'$scenar2_rum_dp_code_01',
										'$scenar2_rum_dp_code_02',
										'$scenar2_rum_dp_code_03',
										'$scenar2_rum_dp_code_04',
										'$scenar2_rum_dp_code_05',
										'$scenar2_rum_dp_code_06',
										'$scenar2_rum_dp_code_07',
										'$scenar2_rum_dp_code_08',
										'$scenar2_rum_dp_code_09',
										'$scenar2_rum_dp_code_10'
										],
				scenar2_rum_dr_code = ARRAY[
										'$scenar2_rum_dr_code_01',
										'$scenar2_rum_dr_code_02',
										'$scenar2_rum_dr_code_03',
										'$scenar2_rum_dr_code_04',
										'$scenar2_rum_dr_code_05',
										'$scenar2_rum_dr_code_06',
										'$scenar2_rum_dr_code_07',
										'$scenar2_rum_dr_code_08',
										'$scenar2_rum_dr_code_09',
										'$scenar2_rum_dr_code_10'
										],
				scenar2_rum_da_code = ARRAY[
										'$scenar2_rum_da_code_01',
										'$scenar2_rum_da_code_02',
										'$scenar2_rum_da_code_03',
										'$scenar2_rum_da_code_04',
										'$scenar2_rum_da_code_05',
										'$scenar2_rum_da_code_06',
										'$scenar2_rum_da_code_07',
										'$scenar2_rum_da_code_08',
										'$scenar2_rum_da_code_09',
										'$scenar2_rum_da_code_10'
										],
				scenar2_rum_ccam_code = ARRAY[
										'$scenar2_rum_ccam_code_01',
										'$scenar2_rum_ccam_code_02',
										'$scenar2_rum_ccam_code_03',
										'$scenar2_rum_ccam_code_04',
										'$scenar2_rum_ccam_code_05',
										'$scenar2_rum_ccam_code_06',
										'$scenar2_rum_ccam_code_07',
										'$scenar2_rum_ccam_code_08',
										'$scenar2_rum_ccam_code_09',
										'$scenar2_rum_ccam_code_10'
										],
				
				control_rule =			'$control_rule',		
				control_comment =		'$control_comment',	
				control_rum_dp_code = ARRAY[
										'$control_rum_dp_code_01',
										'$control_rum_dp_code_02',
										'$control_rum_dp_code_03',
										'$control_rum_dp_code_04',
										'$control_rum_dp_code_05',
										'$control_rum_dp_code_06',
										'$control_rum_dp_code_07',
										'$control_rum_dp_code_08',
										'$control_rum_dp_code_09',
										'$control_rum_dp_code_10'
										],
				control_rum_dr_code = ARRAY[
										'$control_rum_dr_code_01',
										'$control_rum_dr_code_02',
										'$control_rum_dr_code_03',
										'$control_rum_dr_code_04',
										'$control_rum_dr_code_05',
										'$control_rum_dr_code_06',
										'$control_rum_dr_code_07',
										'$control_rum_dr_code_08',
										'$control_rum_dr_code_09',
										'$control_rum_dr_code_10'
										],
				control_rum_da_code = ARRAY[
										'$control_rum_da_code_01',
										'$control_rum_da_code_02',
										'$control_rum_da_code_03',
										'$control_rum_da_code_04',
										'$control_rum_da_code_05',
										'$control_rum_da_code_06',
										'$control_rum_da_code_07',
										'$control_rum_da_code_08',
										'$control_rum_da_code_09',
										'$control_rum_da_code_10'
										],
				control_rum_ccam_code = ARRAY[
										'$control_rum_ccam_code_01',
										'$control_rum_ccam_code_02',
										'$control_rum_ccam_code_03',
										'$control_rum_ccam_code_04',
										'$control_rum_ccam_code_05',
										'$control_rum_ccam_code_06',
										'$control_rum_ccam_code_07',
										'$control_rum_ccam_code_08',
										'$control_rum_ccam_code_09',
										'$control_rum_ccam_code_10'
										],
				
				retenu_rule =			'$retenu_rule',		
				retenu_comment =			'$retenu_comment',					
				retenu_rum_dp_code = ARRAY[
										'$retenu_rum_dp_code_01',
										'$retenu_rum_dp_code_02',
										'$retenu_rum_dp_code_03',
										'$retenu_rum_dp_code_04',
										'$retenu_rum_dp_code_05',
										'$retenu_rum_dp_code_06',
										'$retenu_rum_dp_code_07',
										'$retenu_rum_dp_code_08',
										'$retenu_rum_dp_code_09',
										'$retenu_rum_dp_code_10'
										],
				retenu_rum_dr_code = ARRAY[
										'$retenu_rum_dr_code_01',
										'$retenu_rum_dr_code_02',
										'$retenu_rum_dr_code_03',
										'$retenu_rum_dr_code_04',
										'$retenu_rum_dr_code_05',
										'$retenu_rum_dr_code_06',
										'$retenu_rum_dr_code_07',
										'$retenu_rum_dr_code_08',
										'$retenu_rum_dr_code_09',
										'$retenu_rum_dr_code_10'
										],
				retenu_rum_da_code = ARRAY[
										'$retenu_rum_da_code_01',
										'$retenu_rum_da_code_02',
										'$retenu_rum_da_code_03',
										'$retenu_rum_da_code_04',
										'$retenu_rum_da_code_05',
										'$retenu_rum_da_code_06',
										'$retenu_rum_da_code_07',
										'$retenu_rum_da_code_08',
										'$retenu_rum_da_code_09',
										'$retenu_rum_da_code_10'
										],
				retenu_rum_ccam_code = ARRAY[
										'$retenu_rum_ccam_code_01',
										'$retenu_rum_ccam_code_02',
										'$retenu_rum_ccam_code_03',
										'$retenu_rum_ccam_code_04',
										'$retenu_rum_ccam_code_05',
										'$retenu_rum_ccam_code_06',
										'$retenu_rum_ccam_code_07',
										'$retenu_rum_ccam_code_08',
										'$retenu_rum_ccam_code_09',
										'$retenu_rum_ccam_code_10'
										]
				WHERE oid = $oid";
	$result  =  $database->exec($sqlcmd);


	$sqlcmd = "SELECT pmsi.cti_fiche5_groupage(0,$oid)";
	$result  =  $database->exec($sqlcmd);

			
	$sqlcmd = "SELECT 
				
				p_fiche5_rss.scenar1_dp_code,
				p_fiche5_rss.scenar1_dp_code || ' ' || COALESCE(scenar1_t_dp.texte,'') AS scenar1_dp_texte,
				p_fiche5_rss.scenar1_ghs_code,
				p_fiche5_rss.scenar1_ghm_code,
				p_fiche5_rss.scenar1_ghm_code || ' ' || COALESCE(scenar1_t_ghm.texte,'') AS scenar1_ghm_texte,
				p_fiche5_rss.scenar1_ghs_montant_total,
				p_fiche5_rss.scenar1_exb_montant_total,
				p_fiche5_rss.scenar1_exh_montant_total,
				
				p_fiche5_rss.scenar2_dp_code,
				p_fiche5_rss.scenar2_dp_code || ' ' || COALESCE(scenar2_t_dp.texte,'') AS scenar2_dp_texte,
				p_fiche5_rss.scenar2_ghs_code,
				p_fiche5_rss.scenar2_ghm_code,
				p_fiche5_rss.scenar2_ghm_code || ' ' || COALESCE(scenar2_t_ghm.texte,'') AS scenar2_ghm_texte,
				p_fiche5_rss.scenar2_ghs_montant_total,
				p_fiche5_rss.scenar2_exb_montant_total,
				p_fiche5_rss.scenar2_exh_montant_total,
				
				p_fiche5_rss.control_dp_code,
				p_fiche5_rss.control_dp_code || ' ' || COALESCE(control_t_dp.texte,'') AS control_dp_texte,
				p_fiche5_rss.control_ghs_code,
				p_fiche5_rss.control_ghm_code,
				p_fiche5_rss.control_ghm_code || ' ' || COALESCE(control_t_ghm.texte,'') AS control_ghm_texte,
				p_fiche5_rss.control_ghs_montant_total,
				p_fiche5_rss.control_exb_montant_total,
				p_fiche5_rss.control_exh_montant_total,
				
				p_fiche5_rss.retenu_dp_code,
				p_fiche5_rss.retenu_dp_code || ' ' || COALESCE(retenu_t_dp.texte,'') AS retenu_dp_texte,
				p_fiche5_rss.retenu_ghs_code,
				p_fiche5_rss.retenu_ghm_code,
				p_fiche5_rss.retenu_ghm_code || ' ' || COALESCE(retenu_t_ghm.texte,'') AS retenu_ghm_texte,
				p_fiche5_rss.retenu_ghs_montant_total,
				p_fiche5_rss.retenu_exb_montant_total,
				p_fiche5_rss.retenu_exh_montant_total
				
				
			FROM pmsi.p_fiche5_rss
			LEFT JOIN pmsi.t_diagnostics scenar1_t_dp ON scenar1_dp_id = scenar1_t_dp.oid AND scenar1_t_dp.oid <> 0
			LEFT JOIN pmsi.t_diagnostics scenar2_t_dp ON scenar2_dp_id = scenar2_t_dp.oid AND scenar2_t_dp.oid <> 0
			LEFT JOIN pmsi.t_diagnostics control_t_dp ON control_dp_id = control_t_dp.oid AND control_t_dp.oid <> 0
			LEFT JOIN pmsi.t_diagnostics  retenu_t_dp ON  retenu_dp_id =  retenu_t_dp.oid AND  retenu_t_dp.oid <> 0
			LEFT JOIN pmsi.t_ghm scenar1_t_ghm ON scenar1_ghm_id = scenar1_t_ghm.oid AND scenar1_t_ghm.oid <> 0
			LEFT JOIN pmsi.t_ghm scenar2_t_ghm ON scenar2_ghm_id = scenar2_t_ghm.oid AND scenar2_t_ghm.oid <> 0
			LEFT JOIN pmsi.t_ghm control_t_ghm ON control_ghm_id = control_t_ghm.oid AND control_t_ghm.oid <> 0
			LEFT JOIN pmsi.t_ghm  retenu_t_ghm ON  retenu_ghm_id =  retenu_t_ghm.oid AND  retenu_t_ghm.oid <> 0
			WHERE p_fiche5_rss.oid = '$oid'";
	
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordAssoc();
									
			if ($record != FALSE) {
				$ok = TRUE;

				$scenar1_dp_code          			= toHTML(trim($record['scenar1_dp_code']));
				$scenar1_dp_texte          			= toHTML(trim($record['scenar1_dp_texte']));
				$scenar1_ghs_code                   = toHTML(trim($record['scenar1_ghs_code']));
				$scenar1_ghm_code                   = toHTML(trim($record['scenar1_ghm_code']));
				$scenar1_ghm_texte                  = toHTML(trim($record['scenar1_ghm_texte']));
				$scenar1_ghs_montant_total 			= floatval($record['scenar1_ghs_montant_total']);
				$scenar1_exb_montant_total          = floatval($record['scenar1_exb_montant_total']);
				$scenar1_exh_montant_total          = floatval($record['scenar1_exh_montant_total']);
				
				$scenar2_dp_code          			= toHTML(trim($record['scenar2_dp_code']));
				$scenar2_dp_texte          			= toHTML(trim($record['scenar2_dp_texte']));
				$scenar2_ghs_code                   = toHTML(trim($record['scenar2_ghs_code']));
				$scenar2_ghm_code                   = toHTML(trim($record['scenar2_ghm_code']));
				$scenar2_ghm_texte                  = toHTML(trim($record['scenar2_ghm_texte']));
				$scenar2_ghs_montant_total 			= floatval($record['scenar2_ghs_montant_total']);
				$scenar2_exb_montant_total          = floatval($record['scenar2_exb_montant_total']);
				$scenar2_exh_montant_total          = floatval($record['scenar2_exh_montant_total']);
				
				$control_dp_code          			= toHTML(trim($record['control_dp_code']));
				$control_dp_texte          			= toHTML(trim($record['control_dp_texte']));
				$control_ghs_code                   = toHTML(trim($record['control_ghs_code']));
				$control_ghm_code                   = toHTML(trim($record['control_ghm_code']));
				$control_ghm_texte                  = toHTML(trim($record['control_ghm_texte']));
				$control_ghs_montant_total 			= floatval($record['control_ghs_montant_total']);
				$control_exb_montant_total          = floatval($record['control_exb_montant_total']);
				$control_exh_montant_total          = floatval($record['control_exh_montant_total']);
				
				$retenu_dp_code          			= toHTML(trim($record['retenu_dp_code']));
				$retenu_dp_texte          			= toHTML(trim($record['retenu_dp_texte']));
				$retenu_ghs_code                    = toHTML(trim($record['retenu_ghs_code']));
				$retenu_ghm_code                    = toHTML(trim($record['retenu_ghm_code']));
				$retenu_ghm_texte                   = toHTML(trim($record['retenu_ghm_texte']));
				$retenu_ghs_montant_total 			= floatval($record['retenu_ghs_montant_total']);
				$retenu_exb_montant_total           = floatval($record['retenu_exb_montant_total']);
				$retenu_exh_montant_total           = floatval($record['retenu_exh_montant_total']);
				

				$httpString = $httpString . 
					"\r\n<RECORD
						scenar1_dp_code=\"$scenar1_dp_code\"
						scenar1_dp_texte=\"$scenar1_dp_texte\"
						scenar1_ghs_code=\"$scenar1_ghs_code\"
						scenar1_ghm_code=\"$scenar1_ghm_code\"
						scenar1_ghm_texte=\"$scenar1_ghm_texte\"
						scenar1_ghs_montant_total=\"$scenar1_ghs_montant_total\"
						scenar1_exb_montant_total=\"$scenar1_exb_montant_total\"
						scenar1_exh_montant_total=\"$scenar1_exh_montant_total\"
						
						scenar2_dp_code=\"$scenar2_dp_code\"
						scenar2_ghs_code=\"$scenar2_ghs_code\"
						scenar2_ghm_code=\"$scenar2_ghm_code\"
						scenar2_ghm_texte=\"$scenar2_ghm_texte\"
						scenar2_ghs_montant_total=\"$scenar2_ghs_montant_total\"
						scenar2_exb_montant_total=\"$scenar2_exb_montant_total\"
						scenar2_exh_montant_total=\"$scenar2_exh_montant_total\"
						
						control_dp_code=\"$control_dp_code\"
						control_ghs_code=\"$control_ghs_code\"
						control_ghm_code=\"$control_ghm_code\"
						control_ghm_texte=\"$control_ghm_texte\"
						control_ghs_montant_total=\"$control_ghs_montant_total\"
						control_exb_montant_total=\"$control_exb_montant_total\"
						control_exh_montant_total=\"$control_exh_montant_total\"
						
						retenu_dp_code=\"$retenu_dp_code\"
						retenu_ghs_code=\"$retenu_ghs_code\"
						retenu_ghm_code=\"$retenu_ghm_code\"
						retenu_ghm_texte=\"$retenu_ghm_texte\"
						retenu_ghs_montant_total=\"$retenu_ghs_montant_total\"
						retenu_exb_montant_total=\"$retenu_exb_montant_total\"
						retenu_exh_montant_total=\"$retenu_exh_montant_total\"
					/>";
			}
		}

	
	}
	
	return $httpString;
	

}

function erase_cache() {

	global $database;
	
	// Effacer cache ancienne version
	if ($database->cticache == "true") {
		$sqlcmd = "SELECT cti_cache_erase as result from cache.cti_cache_erase('iCTI_pmsi')";
		$result = $database->exec($sqlcmd);
	}
	
	// Effacer cache nouvelle version
	cleanAllCache("iCTI_pmsi");


}


function extract_rss_ng($enquete_id) {

	global $database;
	
	$tmp_path		 = rootDir() . "temp";
	

	// Restauration fichier 
	$tmp_RSSinfile = $tmp_path . "/fiche5_rss_" . $enquete_id . "_in.txt";
	if (file_exists($tmp_RSSinfile)) {
		unlink($tmp_RSSinfile);
	}
	$tmp_IUMinfile = $tmp_path . "/fiche5_ium_" . $enquete_id . "_in.txt";
	if (file_exists($tmp_IUMinfile)) {
		unlink($tmp_IUMinfile);
	}
	$tmp_RSSinfileZip = $tmp_path . "/fiche5_rss_" . $enquete_id . "_in.zip";
	if (file_exists($tmp_RSSinfileZip)) {
		unlink($tmp_RSSinfileZip);
	}

	// Aller dans IN.ZIP
	$sqlcmd	 = "
			SELECT 
				p_imports_data.data 
			FROM pmsi.p_imports_data 
			JOIN (
				SELECT MAX(p_rss.import_id) AS import_id
				FROM pmsi.p_fiche5_rss
				JOIN pmsi.p_rss ON p_fiche5_rss.rss_id = p_rss.oid
				WHERE p_fiche5_rss.enquete_id = $enquete_id
			) subview ON p_imports_data.import_id = subview.import_id
			WHERE data_type = 'ZIN' 
			ORDER BY data_num 
			";
	$result  =  $database->exec($sqlcmd);
	$ok		 = TRUE;
	while ($ok == TRUE) {
		$ok		 = FALSE;
		$record	 = pg_fetch_array($result);
		if ($record != FALSE) {
			$ok		 = TRUE;
			$data	 = $record[0];

			$handle = fopen($tmp_RSSinfileZip, 'a');
			fwrite($handle, uncompress(pg_unescape_bytea($data)));
			fclose($handle);
		}
	}

	// Décompression rss et ium
	if (file_exists($tmp_RSSinfileZip)) {
		$zip = zip_open(realpath($tmp_RSSinfileZip));
		if ($zip) {
			while ($zip_entry = zip_read($zip)) {
				if (zip_entry_open($zip, $zip_entry)) {
					$filename	 = trim(zip_entry_name($zip_entry));
					$filesuffix	 = substr($filename, strlen($filename) - 7, 7);
					if ($filesuffix == "rss.txt") {
						$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
						file_put_contents($tmp_RSSinfile, $buf);
					}
					if ($filesuffix == "ium.txt") {
						$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
						file_put_contents($tmp_IUMinfile, $buf);
					}
					zip_entry_close($zip_entry);
				}
			}
			zip_close($zip);
		}
		unlink($tmp_RSSinfileZip);
	}
	
	// Extraction UM
	$sqlcmd = 
		"DROP TABLE IF EXISTS w_rss_ng_data;
		CREATE TEMP TABLE w_rss_ng_data (data text)
		;

		COPY w_rss_ng_data (data) FROM '$tmp_IUMinfile'
		;

		UPDATE pmsi.t_fiche5_enquete
		SET initial_um_in = (SELECT base.cti_array_accum(data) FROM w_rss_ng_data)
		"
		;
	$result  =  $database->exec($sqlcmd);
	
	
	
	// Extraction RSS
	$sqlcmd = 
		"DROP SEQUENCE IF EXISTS w_rss_ng_sequence;
		CREATE TEMP SEQUENCE w_rss_ng_sequence;
		DROP TABLE IF EXISTS w_rss_ng_data;
		CREATE TEMP TABLE w_rss_ng_data (line numeric default nextval('w_rss_ng_sequence'::regclass), data text)
		;

		COPY w_rss_ng_data (data) FROM '$tmp_RSSinfile'
		;
		
		UPDATE pmsi.p_fiche5_rss
		SET initial_rum_in = subview.initial_rum_in
		FROM
		(
		SELECT
			oid,
			ARRAY[
				MAX(CASE WHEN no_rum = 01 THEN data else '' END),
				MAX(CASE WHEN no_rum = 02 THEN data else '' END),
				MAX(CASE WHEN no_rum = 03 THEN data else '' END),
				MAX(CASE WHEN no_rum = 04 THEN data else '' END),
				MAX(CASE WHEN no_rum = 05 THEN data else '' END),
				MAX(CASE WHEN no_rum = 06 THEN data else '' END),
				MAX(CASE WHEN no_rum = 07 THEN data else '' END),
				MAX(CASE WHEN no_rum = 08 THEN data else '' END),
				MAX(CASE WHEN no_rum = 09 THEN data else '' END),
				MAX(CASE WHEN no_rum = 10 THEN data else '' END)
			] AS initial_rum_in
		FROM
		(
		SELECT 
			p_fiche5_rss.oid,
			ROW_NUMBER() OVER (PARTITION BY substr(data,28,20) ORDER BY line) AS no_rum,
			data
		FROM w_rss_ng_data
		JOIN pmsi.p_fiche5_rss ON 
			lpad(trim(substr(data,28,20)),20,'0') = to_char(p_fiche5_rss.no_rss,'FM00000000000000000000')
		WHERE p_fiche5_rss.enquete_id = $enquete_id
		) subview
		GROUP BY 1
		) subview
		WHERE p_fiche5_rss.oid = subview.oid
		;
		
		SELECT pmsi.cti_fiche5_extract_rum($enquete_id)
		;
		
		
		";
		
		$result  =  $database->exec($sqlcmd);
	

	return $tmp_RSSinfile;
}

?>