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

global $database;
global $recordNode;

$tableName = getPOST("tableName");
$enquete_id = floatval(getPOST("enquete_id"));

$database = new Database("iCTI");

$csvfile = "";

switch ($tableName) {
	case"p_fiche5_rss" :
		$csvfile = getExcel_fiche5_rss($enquete_id);
		break;
	
}

// Send Header
if ($csvfile != "") {
	if (file_exists($csvfile)) {
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
		header("Content-Type: application/csv;charset=iso-8859-1");
		header("Content-Disposition: attachment;filename=\"" . basename($csvfile) . "\""); 
		$fileLen = filesize($csvfile);
		$maxlen = 10000000;
		
		$i = 0;
		$p = 0;
		while ($p <= $fileLen) {
			$i++;
			echo file_get_contents($csvfile, FALSE, NULL, $p, $maxlen);
			$p = $i * $maxlen;
		}
	}
	else {
		echo "Erreur";
	}
}
else {
	echo "Pas de données pour $tableName $enquete_id";
}



function getExcel_fiche5_rss($enquete_id) {

	global $database;
	
	$tempfile = realpath("../../../temp")."/fiche5.csv";
	
	
	$sqlcmd = "COPY
					(
					SELECT 

						no_ogc AS \"No OGC\",
						t_fiche5_champ.code AS \"Champ\",
						no_rss AS \"No RSS\",
						no_sejour_administratif AS \"No sejour\",
						regime_code AS \"Regime\",
						CASE WHEN caisse_gestionnaire_code <> '' THEN caisse_gestionnaire_code ELSE caisse_gestionnaire_texte END AS \"Caisse\",
						duree_sejour AS \"Duree sejour\",
						taux_remboursement AS \"Taux\",
						
						initial_dp_code AS \"DP initial\",

						facture_ghm_code AS \"GHM Facture\",
						to_char(facture_ghs_montant_total+facture_exh_montant_total+facture_exb_montant_total,'FM9999999990D00') AS \"Total Facture\",
						scenar1_ghm_code AS \"GHM Optimiste\",
						to_char(scenar1_ghs_montant_total+scenar1_exh_montant_total+scenar1_exb_montant_total 
							-facture_ghs_montant_total-facture_exh_montant_total-facture_exb_montant_total,'FM9999999990D00') AS \"Ecart Optimiste\",
						scenar2_ghm_code AS \"GHM Pessimiste\",
						to_char(scenar2_ghs_montant_total+scenar2_exh_montant_total+scenar2_exb_montant_total 
							-facture_ghs_montant_total-facture_exh_montant_total-facture_exb_montant_total,'FM9999999990D00') AS \"Ecart Pessimiste\",
						control_ghm_code AS \"GHM Controleur\",
						to_char(control_ghs_montant_total+control_exh_montant_total+control_exb_montant_total 
							-facture_ghs_montant_total-facture_exh_montant_total-facture_exb_montant_total,'FM9999999990D00') AS \"Ecart Controleur\",
						retenu_ghm_code AS \"GHM Final\",
						to_char(retenu_ghs_montant_total+retenu_exh_montant_total+retenu_exb_montant_total 
							-facture_ghs_montant_total-facture_exh_montant_total-facture_exb_montant_total,'FM9999999990D00') AS \"Ecart Final\",
						CASE p_fiche5_rss.status
						WHEN 'P9' THEN 'Préparé'
						WHEN 'C8' THEN 'Contesté'
						WHEN 'C9' THEN 'Accepté'
						ELSE '' END AS \"Etat\"
					FROM pmsi.p_fiche5_rss
					JOIN pmsi.t_fiche5_champ ON champ_id = t_fiche5_champ.oid
					WHERE p_fiche5_rss.enquete_id = $enquete_id
					ORDER BY 1
					) TO '$tempfile' WITH CSV DELIMITER ';' HEADER
					";
	
	$result = $database->exec($sqlcmd);
	
	
	return "$tempfile";
	

}


?>