<?php

include ("config.php");

$highlight_menu = 6;

_get("bilanType");
_get("annee");

include ("header.php");

$bilanTypeArray = array (
	0  => "-- Choisir --",
	1  => "Global",
	7  => "Module 1",
	6  => "Module 2",
	2  => "Module 3",
	3  => "Module 4",
	4  => "Module 5",
	5  => "Module 6",
);

?>
<form action="bilan.php" method="get">
<p>
<select name="bilanType" size="1" onchange="this.form.submit()">
<?php

foreach ($bilanTypeArray as $bilanTypeKey => $bilanTypeItem)
	echo "<option value=\"" . $bilanTypeKey . "\"" . ($bilanType == $bilanTypeKey ? " selected=\"selected\"" : "") . ">" . $bilanTypeItem . "</option>\n";

?>
</select>&nbsp;<input class="inb" type="submit" name="edit_submit" value="Envoyer" />
</p>
</form>
<?php

if ($bilanType > 1 && $bilanType != 7)
{
	$q = "SELECT c1.id_pat, c1.id_prop, c1.id_consult as c1_id_consult, p.id_mod FROM consultation c1 INNER JOIN proposition p ON (p.id_prop = c1.id_prop) WHERE c1.consu_type = 2 && p.id_mod = " . $bilanType;

	$listConsu = queryList($q);

	$module = $menu->get_module($bilanType);
	echo "<br /><p>Module: <span class=\"bold\">" . $module["mod_nom"] . "</span></p><br />";

	$listPat = array();
	$listConsuInit = array();
	$listConsuFinal = array();
	$listProp = array();

	$notFinish = 0;

	foreach ($listConsu as $consuItem)
	{
		$q = "SELECT id_consult FROM consultation WHERE id_prop = '" . $consuItem["id_prop"] . "' && consu_type = 3";
		$checkConsuSuite = queryList($q);

		if (empty($checkConsuSuite))
			continue;

		$q = "SELECT id_consult FROM consultation WHERE id_prop = '" . $consuItem["id_prop"] . "' ORDER BY consu_date DESC LIMIT 1";
		$idConsuFinal = queryOneField($q);

		$q = "SELECT consu_type FROM consultation WHERE id_consult = '" . $idConsuFinal . "'";
		$consuTypeFinal = queryOneField($q);

		if ($consuTypeFinal != 4)
			$notFinish++;

		$listPat[] = $consuItem["id_pat"];
		$listConsuInit[] = $consuItem["c1_id_consult"];
		$listConsuFinal[] = $idConsuFinal;
		$listProp[] = $consuItem["id_prop"];
	}

	if ($notFinish)
	{
		if ($notFinish == 1)
			echo $notFinish . " patient n'a pas terminé ce module.<br />";
		else
			echo $notFinish . " patient n'ont pas terminés ce module.<br />";
	}

	/*
	echo "<form action=\"bilan.php\" method=\"get\"><p>\n";

	$q = "SELECT DISTINCT(YEAR(consu_date)) AS annee FROM consultation WHERE 1 GROUP BY consu_date ASC";
	$r = mysql_query($q);

	echo "<select name=\"annee\" size=\"1\" onchange=\"this.form.submit()\">\n";
	echo "<option value=\"\">-- Choisir --</option>\n";

	if ($r)
	{
		if ($t = mysql_num_rows($r))
		{
			while ($n = mysql_fetch_assoc($r))
				echo "<option value=\"" . $n["annee"] . "\"" . ($annee == $n["annee"] ? " selected=\"selected\"" : "") . ">" . $n["annee"] . "</option>\n";
		}
	}

	echo "</select>\n";

	echo "<input class=\"inb\" type=\"submit\" name=\"edit_submit\" value=\"Envoyer\" />\n";
	echo "</p></form>\n";
	*/

// 	printVar($listConsuInit);
// 	printVar($listConsuFinal);

	echo "<table id=\"bilan\">\n";

	$bilan = new bilan($listPat, $listConsuInit, $listConsuFinal);

	// $bilan->getSexe();
	// $bilan->getAge();
	// $bilan->getFicheContact();
	// $bilan->getEtatCivil();
	$bilan->getMulti("Etat civil", $sit_mat_tab, "situpat", "sit_mat", "sit_mat != 0");
	$bilan->getMulti("Situation familliale", $sit_fam_tab, "situpat", "sit_fam", "sit_fam != 0");
	$bilan->getMulti("Logement", $sit_log_tab, "situpat", "sit_log", "sit_log != 0");
	$bilan->getMulti("Ressources", $sit_ress_tab, "situpat", "sit_ress", "sit_ress != 0");
	$bilan->getMulti("Couverture sociale", $couv_tab, "situpat", "id_couv", "id_couv != 0");
	$bilan->getMulti("Niveau d'études", $sit_etude_tab, "situpat", "sit_etude", "sit_etude != 0");
	$bilan->getMulti("Catégorie socio professionnelle", $sit_csp_tab, "situpat", "sit_csp", "sit_csp != 0");
	$bilan->getMulti("Prise en charge", $sit_pec_tab, "situpat", "sit_pec", "sit_pec IN (1, 2, 3)");
// 	$bilan->getMultiTableDebut("Consommateur d'alcool", $alc_conso_tab, "alc_conso", "alc_conso > 1");
// 	$bilan->getMultiTableDebut("Usage", $alc_usage_tab, "alc_usage", "alc_usage > 1");

// 	$bilan->getMoyenne("Age de début", "debut", "alc_debut", 2, "alc_debut != 0");
// 	$bilan->getMoyenne("Nombre d'ivresses au cours du mois passé", "debut", "alc_ivresse", 1);
// 	$bilan->getMoyenne("Nombre d'abstinence de plus de 3 mois", "debut", "alc_abst", 1);
// 	$bilan->getMoyenne("Durée en mois de la période d'abstinence la plus longue", "debut", "alc_abst_duree", 1, "alc_abst_duree > 0");

// 	$bilan->getMoyenne("Score AUDIT", "alctab", "alc_audit", 1, "alc_audit > 0");

	$bilan->getSingle("Non fumeur", "alctab", "tab_conso", "tab_conso = 0");
	$bilan->getSingle("Fumeur", "alctab", "tab_conso", "tab_conso = 1");
	$bilan->getSingle("Ancien fumeur", "alctab", "tab_conso", "tab_conso = 2");

	$bilan->titre("Usage de produits illicites");

// 	$bilan->getSingle("<ul><li>Global</li></ul>", "drogue", "*", "1 IN (drog_canabis, drog_heroine, drog_codeine, drog_opium, drog_lsd, drog_hallu, drog_colle, drog_amphe, drog_mdma, drog_stimul, drog_coke, drog_crack, drog_autre)");

	$prodIllicites = array (
		"Cannabis et dérivés" => "drog_canabis",
		"Héroine" => "drog_heroine",
		"Codéine" => "drog_codeine",
		"Autres opiacés et dérivés" => "drog_opium",
		"LSD + autres acides" => "drog_lsd",
		"Autres hallucinogènes" => "drog_hallu",
		"Solvants et colles" => "drog_colle",
		"Amphétamines" => "drog_amphe",
		"Ecstasy et dérivés" => "drog_mdma",
		"Autres stimulants" => "drog_stimul",
		"Cocaïne et coca (feuilles)" => "drog_coke",
		"Crack" => "drog_crack",
		"Autres produits illicites" => "drog_autre",
	);

	foreach ($prodIllicites as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "drogue", $field, $field . " = 1");

	$bilan->titre("Ancien usage de produits illicites");

	foreach ($prodIllicites as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "drogue", $field, $field . " = 2");

	$medicPsy = array (
		"Benzodiazépines" => "medic_benzo",
		"Autres anxiolytiques" => "medic_anxio",
		"Hypnotiques" => "medic_hypno",
		"Antidépresseurs" => "medic_antidepr",
		"Neuroleptiques" => "medic_neuro",
		"Barbituriques" => "medic_barbi",
		"Autres" => "medic_autres",
	);

	$bilan->titre("Usage de psychotropes selon prescription");

	foreach ($medicPsy as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field, $field . " = 1");

	$bilan->titre("Mésuage de psychotropes");

	foreach ($medicPsy as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field . "_mesu", $field . "_mesu = 2");

	$substitutions = array (
		"Méthadone" => "medic_methadone",
		"Subutex" => "medic_subutex",
		"Autres" => "medic_subst_autres",
	);

	$substitutionsMesu = array (
		"Méthadone" => "medic_metha_mesu",
		"Subutex" => "medic_subu_mesu",
		"Autres" => "medic_subst_autres_mesu",
	);

	$bilan->titre("Substitutions");

	foreach ($substitutions as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field, $field . " = 1");

	$bilan->titre("Mésusage de substitution");

	foreach ($substitutionsMesu as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field, $field . " = 1");

// 	$bilan->getSingle("Ancien usage de produits illicites", "drogue", "*", "2 IN (drog_canabis, drog_heroine, drog_codeine, drog_opium, drog_lsd, drog_hallu, drog_colle, drog_amphe, drog_mdma, drog_stimul, drog_coke, drog_crack, drog_autre)");

// 	$bilan->getSingle("Usage de psychotropes selon prescription", "medicament", "*", "1 IN (medic_benzo, medic_anxio, medic_hypno, medic_antidepr, medic_neuro, medic_barbi, medic_autres)");

// 	$bilan->getSingle("Mésuage de psychotropes", "medicament", "*", "1 IN (medic_benzo_mesu, medic_anxio_mesu, medic_hypno_mesu, medic_antidepr_mesu, medic_neuro_mesu, medic_barbi_mesu, medic_autres_mesu)");

// 	$bilan->getSingle("Substitution", "medicament", "*", "1 IN (medic_methadone, medic_subutex, medic_subst_autres)");

// 	$bilan->getSingle("Mésusage de substitution", "medicament", "*", "1 IN (medic_metha_mesu, medic_subu_mesu, medic_subst_autres_mesu)");

	$bilan->getSingle("Anorexie", "medicament", "*", "medic_anorexie = 1");
	$bilan->getSingle("Boulimie", "medicament", "*", "medic_boulimie = 1");
	$bilan->getSingle("Jeu pathologique", "medicament", "*", "medic_jeupath = 1");

	$bilan->getMulti("Evaluation anxiét&eacute;", $medic_anxiete_tab, "medicament", "medic_anxiete", "medic_anxiete > 0");
	$bilan->getMulti("Evaluation d&eacute;pression", $medic_depression_tab, "medicament", "medic_depression", "medic_depression > 0");
	$bilan->getMulti("Troubles psychiatriques", $medic_trouble_psy_tab, "medicament", "medic_trouble_psy", "medic_trouble_psy > 1");
// 	$bilan->getMoyenne("Evaluation anxiété" , "medicament", "medic_anxiete", 1);
// 	$bilan->getMoyenne("Evaluation dépression" , "medicament", "medic_depression", 1);
// 	$bilan->getMoyenne("Troubles psychiatriques" , "medicament", "medic_trouble_psy", 1, "medic_trouble_psy > 1");

	$bilan->titre("Evaluation");

	$bilan->getMoyenne("Score alcool", "rosace", "eval_alcool", 1);
	$bilan->getMoyenne("Score tabac", "rosace", "eval_tabac", 1);
	$bilan->getMoyenne("Score cannabis", "rosace", "eval_canabis", 1);
	$bilan->getMoyenne("Score mésusage de médicaments", "rosace", "eval_mesusmedi", 1);
	$bilan->getMoyenne("Score produits illicites", "rosace", "eval_prodill", 1);
	$bilan->getMoyenne("Score troubles des conduites alimentaires", "rosace", "eval_troublcond", 1);
	$bilan->getMoyenne("Score problème judiciaire", "rosace", "eval_probjudi", 1);
	$bilan->getMoyenne("Score problème professionnel", "rosace", "eval_probpro", 1);
	$bilan->getMoyenne("Score problème socio financier", "rosace", "eval_probsoc", 1);
	$bilan->getMoyenne("Score familial", "rosace", "eval_probfam", 1);
	$bilan->getMoyenne("Score état psychique", "rosace", "eval_etatpsy", 1);
	$bilan->getMoyenne("Score état somatique", "rosace", "eval_etatsoma", 1);

	$bilan->getMulti("Score motivation", $eval_motiv_tab, "rosace", "eval_motiv", "eval_motiv > 0");

	echo "</table>\n";
}
elseif ($bilanType == 7)
{
	$q = "SELECT id_pat FROM patient WHERE pat_contrat != '0000-00-00'";
	$listPat = queryListOne($q);

	$module = $menu->get_module("1");
	echo "<br /><p>Module: <span class=\"bold\">" . $module["mod_nom"] . "</span></p><br />";

	$q = "SELECT id_consult FROM consultation WHERE id_pat IN (" . implode(", ", $listPat) . ") && consu_type = 2";
	$listConsu = queryListOne($q);

	echo "<table id=\"bilan\">\n";

	$bilan = new bilanMod1($listPat, $listConsu);

	$bilan->getMulti("Etat civil", $sit_mat_tab, "situpat", "sit_mat", "sit_mat != 0");
	$bilan->getMulti("Situation familliale", $sit_fam_tab, "situpat", "sit_fam", "sit_fam != 0");
	$bilan->getMulti("Logement", $sit_log_tab, "situpat", "sit_log", "sit_log != 0");
	$bilan->getMulti("Ressources", $sit_ress_tab, "situpat", "sit_ress", "sit_ress != 0");
	$bilan->getMulti("Couverture sociale", $couv_tab, "situpat", "id_couv", "id_couv != 0");
	$bilan->getMulti("Niveau d'études", $sit_etude_tab, "situpat", "sit_etude", "sit_etude != 0");
	$bilan->getMulti("Catégorie socio professionnelle", $sit_csp_tab, "situpat", "sit_csp", "sit_csp != 0");
	$bilan->getMulti("Prise en charge", $sit_pec_tab, "situpat", "sit_pec", "sit_pec IN (1, 2, 3)");

	$bilan->getSingle("Non fumeur", "alctab", "tab_conso", "tab_conso = 0");
	$bilan->getSingle("Fumeur", "alctab", "tab_conso", "tab_conso = 1");
	$bilan->getSingle("Ancien fumeur", "alctab", "tab_conso", "tab_conso = 2");

	$bilan->titre("Usage de produits illicites");

	$prodIllicites = array (
		"Cannabis et dérivés" => "drog_canabis",
		"Héroine" => "drog_heroine",
		"Codéine" => "drog_codeine",
		"Autres opiacés et dérivés" => "drog_opium",
		"LSD + autres acides" => "drog_lsd",
		"Autres hallucinogènes" => "drog_hallu",
		"Solvants et colles" => "drog_colle",
		"Amphétamines" => "drog_amphe",
		"Ecstasy et dérivés" => "drog_mdma",
		"Autres stimulants" => "drog_stimul",
		"Cocaïne et coca (feuilles)" => "drog_coke",
		"Crack" => "drog_crack",
		"Autres produits illicites" => "drog_autre",
	);

	foreach ($prodIllicites as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "drogue", $field, $field . " = 1");

	$bilan->titre("Ancien usage de produits illicites");

	foreach ($prodIllicites as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "drogue", $field, $field . " = 2");

	$medicPsy = array (
		"Benzodiazépines" => "medic_benzo",
		"Autres anxiolytiques" => "medic_anxio",
		"Hypnotiques" => "medic_hypno",
		"Antidépresseurs" => "medic_antidepr",
		"Neuroleptiques" => "medic_neuro",
		"Barbituriques" => "medic_barbi",
		"Autres" => "medic_autres",
	);

	$bilan->titre("Usage de psychotropes selon prescription");

	foreach ($medicPsy as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field, $field . " = 1");

	$bilan->titre("Mésuage de psychotropes");

	foreach ($medicPsy as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field . "_mesu", $field . "_mesu = 2");

	$substitutions = array (
		"Méthadone" => "medic_methadone",
		"Subutex" => "medic_subutex",
		"Autres" => "medic_subst_autres",
	);

	$substitutionsMesu = array (
		"Méthadone" => "medic_metha_mesu",
		"Subutex" => "medic_subu_mesu",
		"Autres" => "medic_subst_autres_mesu",
	);

	$bilan->titre("Substitutions");

	foreach ($substitutions as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field, $field . " = 1");

	$bilan->titre("Mésusage de substitution");

	foreach ($substitutionsMesu as $titre => $field)
		$bilan->getSingle("<ul><li>" . $titre . "</li></ul>", "medicament", $field, $field . " = 1");

	$bilan->getSingle("Anorexie", "medicament", "*", "medic_anorexie = 1");
	$bilan->getSingle("Boulimie", "medicament", "*", "medic_boulimie = 1");
	$bilan->getSingle("Jeu pathologique", "medicament", "*", "medic_jeupath = 1");

	$bilan->getMulti("Evaluation anxiét&eacute;", $medic_anxiete_tab, "medicament", "medic_anxiete", "medic_anxiete > 0");
	$bilan->getMulti("Evaluation d&eacute;pression", $medic_depression_tab, "medicament", "medic_depression", "medic_depression > 0");
	$bilan->getMulti("Troubles psychiatriques", $medic_trouble_psy_tab, "medicament", "medic_trouble_psy", "medic_trouble_psy > 1");

// 	$bilan->getMoyenne("Evaluation anxiété" , "medicament", "medic_anxiete", 1);
// 	$bilan->getMoyenne("Evaluation dépression" , "medicament", "medic_depression", 1);
// 	$bilan->getMoyenne("Troubles psychiatriques" , "medicament", "medic_trouble_psy", 1, "medic_trouble_psy > 1");

	$bilan->titre("Evaluation");

	$bilan->getMoyenne("Score alcool", "rosace", "eval_alcool", 1);
	$bilan->getMoyenne("Score tabac", "rosace", "eval_tabac", 1);
	$bilan->getMoyenne("Score cannabis", "rosace", "eval_canabis", 1);
	$bilan->getMoyenne("Score mésusage de médicaments", "rosace", "eval_mesusmedi", 1);
	$bilan->getMoyenne("Score produits illicites", "rosace", "eval_prodill", 1);
	$bilan->getMoyenne("Score troubles des conduites alimentaires", "rosace", "eval_troublcond", 1);
	$bilan->getMoyenne("Score problème judiciaire", "rosace", "eval_probjudi", 1);
	$bilan->getMoyenne("Score problème professionnel", "rosace", "eval_probpro", 1);
	$bilan->getMoyenne("Score problème socio financier", "rosace", "eval_probsoc", 1);
	$bilan->getMoyenne("Score familial", "rosace", "eval_probfam", 1);
	$bilan->getMoyenne("Score état psychique", "rosace", "eval_etatpsy", 1);
	$bilan->getMoyenne("Score état somatique", "rosace", "eval_etatsoma", 1);

	$bilan->getMulti("Score motivation", $eval_motiv_tab, "rosace", "eval_motiv", "eval_motiv > 0");

	echo "</table>\n";
}
elseif ($bilanType == 1)
{
	echo "<form action=\"bilan.php\" method=\"get\">\n";

	$q = "SELECT DISTINCT(YEAR(consu_date)) AS annee FROM consultation WHERE 1 GROUP BY consu_date ASC";
	$r = mysql_query($q);

	if (empty($annee))
		$annee = date("Y");

	echo "<select name=\"annee\" size=\"1\" onchange=\"this.form.submit()\">\n";

	if ($r)
	{
		if ($t = mysql_num_rows($r))
		{
			while ($n = mysql_fetch_assoc($r))
				echo "<option value=\"" . $n["annee"] . "\"" . ($annee == $n["annee"] ? " selected=\"selected\"" : "") . ">" . $n["annee"] . "</option>\n";
		}
	}

	echo "</select>\n";
	echo "<input type=\"hidden\" name=\"bilanType\" value=\"1\" />";
	echo "<input class=\"inb\" type=\"submit\" name=\"edit_submit\" value=\"Envoyer\" />\n";
	echo "</form>\n";

	echo "<br />\n";

	echo "<table id=\"bilan\">\n";

	$bilanGlobal = new bilanGlobal($annee);

	$bilanGlobal->impact_population();

	$bilanGlobal->separation();

	$bilanGlobal->procedures();

	$bilanGlobal->separation();

	$bilanGlobal->referentiel();

	$bilanGlobal->separation();

	$bilanGlobal->education();

	echo "</table>\n";
}

include ("footer.php");

?>