<?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 $oids;

$oids = getPOST("oids");
$tableName = getPOST("tableName");

$sqlScript = "";

$database = new Database("iCTI");

switch($tableName) {
	case "t_expert_controle" :
		$sqlScript = getSqlScript_expert_controle();
		break;
}

header("Content-type: text/plain");
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: inline; filename=\"expert_controle.sql\"");
echo "$sqlScript";



// recherche des variables de session
function getSqlScript_expert_controle() {

global $database;
global $oids;

    // Si un préfixe a été donné, recherche des oid correspondant
    if (substr($oids,-1,1) == "%") {
		$sqlcmd = "SELECT base.cti_group_concat(oid) AS oids FROM pmsi.t_expert_controle WHERE code LIKE '$oids' AND code <> '';";
		$oids = "";
		$result  =  $database->exec($sqlcmd);
		if ($result != false) {
			$record = $database->nextRecordInto();
			if ($record != FALSE) {
				$oids = trim($record[0]);
			}
		}
		
		if ($oids == "") {
			$oids = "0";
		}
	}


	$sqlScript = "";

	
	// Entetes des controles
	
	$sqlScript = $sqlScript . "
		DROP TABLE IF EXISTS w_dbsetup;"; 
	$sqlScript = $sqlScript . "
		CREATE TEMP TABLE w_dbsetup
		(
		  code text,
		  texte text,
		  texte_court text,
		  texte_abrege text,
		  description text,
		  is_cti text,
		  is_datim text,
		  code_datim text,
		  date_validite_debut text,
		  date_validite_fin text,
		  is_hide text,
		  is_global text,
		  gravite_id text
		);";
						
	$sqlcmd = "SELECT 
			'INSERT INTO w_dbsetup VALUES(' ||
				array_to_string(ARRAY[
				quote_literal(code), 
				quote_literal(texte), 
				quote_literal(COALESCE(texte_court,texte)), 
				quote_literal(COALESCE(texte_abrege,texte_court,texte)), 
				quote_literal(description), 
				quote_literal(COALESCE(is_cti,'0')), 
				quote_literal(COALESCE(is_datim,'0')), 
				quote_literal(COALESCE(code_datim,0)::text), 
				quote_literal(COALESCE(date_validite_debut,'20100101'::date)::text), 
				quote_literal(COALESCE(date_validite_fin,'20991231'::date)::text), 
				quote_literal(COALESCE(is_hide,'0')), 
				quote_literal(COALESCE(is_global,'0')), 
				quote_literal(COALESCE(gravite_id,5)::text)
			],',')
			|| ');' AS cmd
		FROM pmsi.t_expert_controle
		WHERE code <> '' AND oid IN ($oids)
		ORDER BY code
		;";
			
				
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;
				
				$r++;
				
				$cmd = $record[0];

				$sqlScript = $sqlScript . "\r\n\t\t$cmd";
			}
		}

	}
	
	if ($cmd == "") {
		return "ERREUR. Cette classe ne peux être choisie (Vérifiez le code)";
	}
	
	$sqlScript = $sqlScript . "
		UPDATE pmsi.t_expert_controle SET 
			texte = w_dbsetup.texte, 
			texte_court = w_dbsetup.texte_court, 
			texte_abrege = w_dbsetup.texte_abrege, 
			description = w_dbsetup.description, 
			code_datim = w_dbsetup.code_datim::numeric, 
			is_datim = w_dbsetup.is_datim, 
			is_cti = w_dbsetup.is_cti, 
			is_global = w_dbsetup.is_global, 
			date_validite_debut = w_dbsetup.date_validite_debut::date, 
			date_validite_fin = w_dbsetup.date_validite_fin::date
		FROM w_dbsetup
		WHERE t_expert_controle.code = w_dbsetup.code
		AND (
			t_expert_controle.texte IS DISTINCT FROM w_dbsetup.texte OR
			t_expert_controle.texte_court IS DISTINCT FROM w_dbsetup.texte_court OR
			t_expert_controle.texte_abrege IS DISTINCT FROM w_dbsetup.texte_abrege OR
			t_expert_controle.description IS DISTINCT FROM w_dbsetup.description OR
			t_expert_controle.code_datim IS DISTINCT FROM w_dbsetup.code_datim::numeric OR
			t_expert_controle.is_datim IS DISTINCT FROM w_dbsetup.is_datim OR 
			t_expert_controle.is_cti IS DISTINCT FROM w_dbsetup.is_cti OR
			t_expert_controle.is_global IS DISTINCT FROM w_dbsetup.is_global OR
			t_expert_controle.date_validite_debut IS DISTINCT FROM w_dbsetup.date_validite_debut::date OR
			t_expert_controle.date_validite_fin IS DISTINCT FROM w_dbsetup.date_validite_fin::date
		);
			
			
			
		INSERT INTO pmsi.t_expert_controle(
			code,
			texte,
			texte_court,
			is_datim,
			code_datim,
			date_validite_debut,
			date_validite_fin,
			is_global,
			texte_abrege,
			description,
			is_cti,
			gravite_id,
			is_hide)
		SELECT 
			code,
			texte,
			texte_court,
			is_datim,
			code_datim::numeric,
			date_validite_debut::date,
			date_validite_fin::date,
			is_global,
			texte_abrege,
			description,
			is_cti,
			5,
			'0'
		FROM w_dbsetup 
		WHERE code NOT IN (SELECT code FROM pmsi.t_expert_controle WHERE code IS NOT NULL);	
	";
	
	
	
	
	
	// Règles SQL
	
	$sqlScript = $sqlScript . "
		DROP TABLE IF EXISTS w_dbsetup;"; 
	$sqlScript = $sqlScript . "
		CREATE TEMP TABLE w_dbsetup
		(
		  controle_code text,
		  numero text,
		  sqlcmd_where text,
		  sqlcmd_justificatif text
		);";
						
	$sqlcmd = "SELECT 
			'INSERT INTO w_dbsetup VALUES(' ||
				array_to_string(ARRAY[
				quote_literal(t_expert_controle.code), 
				quote_literal(COALESCE(t_expert_controle_rule.numero,1)::text), 
				quote_literal(COALESCE(t_expert_controle_rule.sqlcmd_where,'')), 
				quote_literal(COALESCE(t_expert_controle_rule.sqlcmd_justificatif,''))
			],',')
			|| ');' AS cmd
		FROM pmsi.t_expert_controle_rule
		JOIN pmsi.t_expert_controle ON controle_id = t_expert_controle.oid
		WHERE t_expert_controle.code <> '' AND t_expert_controle.oid IN ($oids)
		ORDER BY t_expert_controle.code, t_expert_controle_rule.numero
		;";
			
				
	$result = $database->exec($sqlcmd);
				
	if ($result != false) {
	
		
		// lignes 
		$ok = TRUE;

		while ($ok == TRUE) {
			$ok = FALSE;
		
			$record = $database->nextRecordInto();
										

			if ($record != FALSE) {
				$ok = TRUE;
				
				$r++;
				
				$cmd = $record[0];

				$sqlScript = $sqlScript . "\r\n\t\t$cmd";
			}
		}

	}
	
	
	$sqlScript = $sqlScript . "
		UPDATE pmsi.t_expert_controle_rule
		SET numero = 1
		WHERE numero IS NULL or NUMERO NOT IN (1,2,3);
			
		UPDATE pmsi.t_expert_controle_rule SET
			sqlcmd_where = w_dbsetup.sqlcmd_where,
			sqlcmd_justificatif = w_dbsetup.sqlcmd_justificatif
		FROM w_dbsetup
			JOIN pmsi.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
		WHERE t_expert_controle_rule.controle_id = t_expert_controle.oid AND
			t_expert_controle_rule.numero = w_dbsetup.numero::numeric AND
			(
			t_expert_controle_rule.sqlcmd_where IS DISTINCT FROM w_dbsetup.sqlcmd_where OR
			t_expert_controle_rule.sqlcmd_justificatif IS DISTINCT FROM w_dbsetup.sqlcmd_justificatif
			);
			
		INSERT INTO pmsi.t_expert_controle_rule (
			controle_id,
			numero, 
			sqlcmd_where, 
			sqlcmd_justificatif
		)
		SELECT
			t_expert_controle.oid,
			w_dbsetup.numero::numeric, 
			w_dbsetup.sqlcmd_where, 
			w_dbsetup.sqlcmd_justificatif
		FROM w_dbsetup
			JOIN pmsi.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
			LEFT JOIN pmsi.t_expert_controle_rule ON
				t_expert_controle_rule.controle_id = t_expert_controle.oid AND
				t_expert_controle_rule.numero = w_dbsetup.numero::numeric
		WHERE t_expert_controle_rule.oid IS NULL;	
	";
		
		


			
	return $sqlScript;
	
	

}
    
?>