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


	$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,
		  date_validite_debut text,
		  date_validite_fin text,
		  is_hide text,
		  is_global text,
		  gravite_id text,
		  prestataire_code text
		);";
						
	$sqlcmd = "SELECT 
			'INSERT INTO w_dbsetup VALUES(' ||
				array_to_string(ARRAY[
				quote_literal(t_expert_controle.code), 
				quote_literal(t_expert_controle.texte), 
				quote_literal(COALESCE(t_expert_controle.texte_court, t_expert_controle.texte)), 
				quote_literal(COALESCE(t_expert_controle.texte_abrege, t_expert_controle.texte_court, t_expert_controle.texte)), 
				quote_literal(t_expert_controle.description), 
				quote_literal(COALESCE(t_expert_controle.is_cti, '0')), 
				quote_literal(COALESCE(t_expert_controle.date_validite_debut, '20100101'::date)::text), 
				quote_literal(COALESCE(t_expert_controle.date_validite_fin, '20991231'::date)::text), 
				quote_literal(COALESCE(t_expert_controle.is_hide, '0')), 
				quote_literal(COALESCE(t_expert_controle.is_global, '0')), 
				quote_literal(COALESCE(t_expert_controle.gravite_id, 5)::text),
				quote_literal(COALESCE(t_prestataires.code,'ALL'))
			],',')
			|| ');' AS cmd
		FROM rh.t_expert_controle 
		LEFT JOIN base.t_prestataires ON t_prestataires.oid = t_expert_controle.prestataire_id
		WHERE t_expert_controle.code <> '' AND t_expert_controle.oid IN ($oids)
		ORDER BY t_expert_controle.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 rh.t_expert_controle SET 
			texte = w_dbsetup.texte, 
			texte_court = w_dbsetup.texte_court, 
			texte_abrege = w_dbsetup.texte_abrege, 
			description = w_dbsetup.description, 
			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,
			prestataire_id = COALESCE(t_prestataires.oid, -1)
		FROM w_dbsetup
			LEFT JOIN base.t_prestataires ON t_prestataires.code = w_dbsetup.prestataire_code
		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.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 OR
			t_expert_controle.prestataire_id IS DISTINCT FROM t_prestataires.oid
		);
			
			
			
		INSERT INTO rh.t_expert_controle(
			code,
			texte,
			texte_court,
			date_validite_debut,
			date_validite_fin,
			is_global,
			texte_abrege,
			description,
			is_cti,
			gravite_id,
			is_hide,
			prestataire_id)
		SELECT 
			w_dbsetup.code,
			w_dbsetup.texte,
			w_dbsetup.texte_court,
			w_dbsetup.date_validite_debut::date,
			w_dbsetup.date_validite_fin::date,
			w_dbsetup.is_global,
			w_dbsetup.texte_abrege,
			w_dbsetup.description,
			w_dbsetup.is_cti,
			5,
			'0',
			COALESCE(t_prestataires.oid, -1)
		FROM w_dbsetup 
			LEFT JOIN base.t_prestataires ON t_prestataires.code = w_dbsetup.prestataire_code
		WHERE w_dbsetup.code NOT IN (SELECT code FROM rh.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,
		  table_id 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, '')),
				quote_literal(COALESCE(t_expert_controle_rule.table_id, 0)::text)
			],',')
			|| ');' AS cmd
		FROM rh.t_expert_controle_rule
		JOIN rh.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 rh.t_expert_controle_rule
		SET numero = 1
		WHERE numero IS NULL or NUMERO NOT IN (1,2,3);
			
		UPDATE rh.t_expert_controle_rule SET
			sqlcmd_where = w_dbsetup.sqlcmd_where,
			sqlcmd_justificatif = w_dbsetup.sqlcmd_justificatif,
			table_id = w_dbsetup.table_id::bigint
		FROM w_dbsetup
			JOIN rh.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 OR 
			t_expert_controle_rule.table_id IS DISTINCT FROM w_dbsetup.table_id::bigint 
			);
			
		INSERT INTO rh.t_expert_controle_rule (
			controle_id,
			numero, 
			sqlcmd_where, 
			sqlcmd_justificatif,
			table_id
		)
		SELECT
			t_expert_controle.oid,
			w_dbsetup.numero::numeric, 
			w_dbsetup.sqlcmd_where, 
			w_dbsetup.sqlcmd_justificatif,
			w_dbsetup.table_id::bigint
		FROM w_dbsetup
			JOIN rh.t_expert_controle ON t_expert_controle.code = w_dbsetup.controle_code
			LEFT JOIN rh.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;
	
	

}
    
?>