<?php

class bilanGlobal
{
	var $years;
	var $totalUsagersConcernes;
	var $listeUsagers;

	function bilanGlobal($currentYear = "")
	{
		if (empty($currentYear))
			$currentYear = date("Y");

		$this->years[] = ($currentYear - 1);
		$this->years[] = $currentYear;
		$this->years[] = ($currentYear + 1);

		echo "<tr>\n";
		echo "<td colspan=\"2\">&nbsp;</td>\n";

		foreach ($this->years AS $year)
			echo "<td class=\"center bold\">" . $year . "</td>\n";

		echo "</tr>\n";

		$q = "SELECT YEAR(consu_date) as annee, id_pat FROM consultation GROUP BY YEAR(consu_date), id_pat ORDER BY YEAR(consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
				{
					if (!isset($this->listeUsagers[$n["annee"]]))
						$this->listeUsagers[$n["annee"]] = array();

					$this->listeUsagers[$n["annee"]][] = $n["id_pat"];
				}
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function separation()
	{
		echo "<tr>\n";
		echo "<td colspan=\"5\" class=\"separation\">&nbsp;</td>\n";
		echo "</tr>\n";
	}

	// IMPACT population

	function impact_population()
	{
		$this->impact_total_usagers();
		$this->impact_fiche_contact();
		$this->impact_evaluation_directe();

		$this->separation();

		$this->impact_patient_sexe();

		$this->separation();

		$this->impact_moyenne_dage();

		$this->separation();

		$this->impact_moyenne_audit();

		$this->separation();

		$this->impact_qualite_vie();

		$this->separation();

		$this->impact_conso_alcool();
	}

	function impact_total_usagers()
	{
		$q = "SELECT YEAR(consu_date) as annee, count(distinct(id_pat)) as total FROM consultation GROUP BY YEAR(consu_date) ORDER BY YEAR(consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>IMPACT population</td>\n";
				echo "<td>Nombre total d'usagers concernés</td>\n";

				$data = array();

				while ($n = mysql_fetch_assoc($r))
					$data[$n["annee"]] = $n["total"];

				foreach ($this->years AS $year)
					echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

				echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$this->totalUsagersConcernes = $data;
	}

	function impact_fiche_contact()
	{
		$q = "SELECT YEAR(consu_date) as annee, count(distinct(id_pat)) as total FROM consultation WHERE consu_type = 1 GROUP BY YEAR(consu_date) ORDER BY YEAR(consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>Nombre de fiches contact remplies et retournées</td>\n";

				$data = array();

				while ($n = mysql_fetch_assoc($r))
					$data[$n["annee"]] = $n["total"];

				foreach ($this->years AS $year)
					echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

				echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function impact_evaluation_directe()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(c.id_pat)) as total FROM consultation AS c LEFT JOIN consultation AS d ON (c.id_pat = d.id_pat && d.consu_type = 1) WHERE d.consu_type IS NULL GROUP BY YEAR(c.consu_date) ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>Nombre d'évaluations directes</td>\n";

				while ($n = mysql_fetch_assoc($r))
					$data[$n["annee"]] = $n["total"];

				foreach ($this->years AS $year)
					echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

				echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function impact_patient_sexe()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(p.id_pat)) as total FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) WHERE p.pat_sexe = 1 GROUP BY YEAR(c.consu_date) ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>SEXE</td>\n";
				echo "<td>Pourcentage d'hommes concern&eacute;s</td>\n";

				while ($n = mysql_fetch_assoc($r))
					$data[$n["annee"]] = $n["total"];

				foreach ($this->years AS $year)
				{
					$totalUC = isset($this->totalUsagersConcernes[$year]) ? $this->totalUsagersConcernes[$year] : 0;
					echo "<td class=\"center\">" . (isset($data[$year]) ? round(($data[$year] / $totalUC) * 100) : 0) . "</td>\n";
				}

				echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(p.id_pat)) as total FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) WHERE p.pat_sexe = 2 GROUP BY YEAR(c.consu_date) ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>Pourcentage de femmes concern&eacute;es</td>\n";

				while ($n = mysql_fetch_assoc($r))
					$data[$n["annee"]] = $n["total"];

				foreach ($this->years AS $year)
				{
					$totalUC = isset($this->totalUsagersConcernes[$year]) ? $this->totalUsagersConcernes[$year] : 0;
					echo "<td class=\"center\">" . (isset($data[$year]) ? round(($data[$year] / $totalUC) * 100) : 0) . "</td>\n";
				}

				echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function impact_moyenne_dage()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, YEAR(NOW()) - AVG(YEAR(p.pat_datenaiss)) as total FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) WHERE 1 GROUP BY YEAR(c.consu_date) ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>AGE</td>\n";
				echo "<td>Moyenne d'age</td>\n";

				while ($n = mysql_fetch_assoc($r))
					$data[$n["annee"]] = $n["total"];

				foreach ($this->years AS $year)
					echo "<td class=\"center\">" . (isset($data[$year]) ? round($data[$year]) : 0) . "</td>\n";

				echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$tranche = array (
			"-15" => 0,
			"16 - 25" => 0,
			"26 - 35" => 0,
			"36 - 45" => 0,
			"46 - 55" => 0,
			"56+" => 0
		);

		$trancheTranslation = array (
			"-15" => "15 ans et moins",
			"16 - 25" => "Entre 16 et 25 ans",
			"26 - 35" => "Entre 26 et 35 ans",
			"36 - 45" => "Entre 36 et 45 ans",
			"46 - 55" => "Entre 46 et 55 ans",
			"56+" => "Plus de 56 ans"
		);

		$trancheAnnee = array();
		$alreadyCount = array();

		$q = "SELECT p.id_pat, YEAR(c.consu_date) as annee, YEAR(NOW()) - YEAR(p.pat_datenaiss) as age FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) WHERE 1 ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
				{
					if (empty($n["annee"]))
						continue;

					if (isset($alreadyCount[$n["annee"]][$n["id_pat"]]))
						continue;

					if (!isset($trancheAnnee[$n["annee"]]))
						$trancheAnnee[$n["annee"]] = $tranche;

					if (!isset($alreadyCount[$n["annee"]]))
						$alreadyCount[$n["annee"]] = array();

					$alreadyCount[$n["annee"]][$n["id_pat"]] = true;

					if ($n["age"] <= 15)
					{
						$trancheAnnee[$n["annee"]]["-15"]++;
					}
					elseif ($n["age"] >= 16 && $n["age"] <= 25)
					{
						$trancheAnnee[$n["annee"]]["16 - 25"]++;
					}
					elseif ($n["age"] >= 26 && $n["age"] <= 35)
					{
						$trancheAnnee[$n["annee"]]["26 - 35"]++;
					}
					elseif ($n["age"] >= 36 && $n["age"] <= 45)
					{
						$trancheAnnee[$n["annee"]]["36 - 45"]++;
					}
					elseif ($n["age"] >= 46 && $n["age"] <= 55)
					{
						$trancheAnnee[$n["annee"]]["46 - 55"]++;
					}
					elseif ($n["age"] >= 56)
					{
						$trancheAnnee[$n["annee"]]["56+"]++;
					}
				}

				foreach ($trancheTranslation as $key => $label)
				{
					echo "<tr>\n";
					echo "<td>&nbsp;</td>\n";
					echo "<td>" . $label . "</td>\n";

					foreach ($this->years AS $year)
					{
						echo "<td class=\"center\">" . (isset($trancheAnnee[$year][$key]) ? $trancheAnnee[$year][$key] : 0) . "</td>\n";
					}

					echo "</tr>\n";
				}
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function impact_moyenne_audit()
	{
// 		$q = "SELECT YEAR(c.consu_date) as annee, AVG(alc.alc_audit) as total FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) LEFT JOIN alctab AS alc ON (c.id_consult = alc.id_consult) WHERE c.consu_type = 2 GROUP BY YEAR(c.consu_date) ORDER BY YEAR(c.consu_date) ASC";

		$tranche = array (
			"-7" => 0,
			"7 - 12" => 0,
			"13+" => 0
		);

		$trancheTranslation = array (
			"-7" => "Moins de 7",
			"7 - 12" => "Entre 7 et 12",
			"13+" => "Plus de 13"
		);

		$trancheAnnee = array();
		$alreadyCount = array();

		foreach ($this->years as $year)
		{
			if (!isset($this->listeUsagers[$year]))
				continue;

			$q = "SELECT p.id_pat, alc.alc_audit as audit FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) LEFT JOIN alctab AS alc ON (c.id_consult = alc.id_consult) WHERE c.consu_type = 2 && p.id_pat IN (" . implode($this->listeUsagers[$year], ", ") . ") ORDER BY p.id_pat ASC";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					while ($n = mysql_fetch_assoc($r))
					{
						if (!isset($trancheAnnee[$year]))
							$trancheAnnee[$year] = $tranche;

						if (isset($alreadyCount[$year][$n["id_pat"]]))
							continue;

						if (!isset($alreadyCount[$year]))
							$alreadyCount[$year] = array();

						$alreadyCount[$year][$n["id_pat"]] = true;

						if ($n["audit"] <= 6)
						{
							$trancheAnnee[$year]["-7"]++;
						}
						elseif ($n["audit"] >= 7 && $n["audit"] <= 12)
						{
							$trancheAnnee[$year]["7 - 12"]++;
						}
						elseif ($n["audit"] >= 13)
						{
							$trancheAnnee[$year]["13+"]++;
						}
					}
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
		}

		$d = false;

		foreach ($trancheTranslation as $key => $label)
		{
			echo "<tr>\n";
			echo "<td>" . ($d == false ? "SCORE AUDIT" : "&nbsp;") . "</td>\n";
			echo "<td>" . $label . "</td>\n";

			$d = true;

			foreach ($this->years AS $year)
			{
				echo "<td class=\"center\">" . (isset($trancheAnnee[$year][$key]) ? $trancheAnnee[$year][$key] : 0) . "</td>\n";
			}

			echo "</tr>\n";
		}
// 		printVar($trancheAnnee);
	}

	function impact_qualite_vie()
	{
		$dataInit = array();
		$dataLast = array();
		$lastConsult = array();

		foreach ($this->years as $year)
		{
			if (!isset($this->listeUsagers[$year]))
				continue;

			if (!isset($dataInit[$year]))
				$dataInit[$year] = 0;

			if (!isset($dataLast[$year]))
				$dataLast[$year] = 0;

			if (!isset($lastConsult[$year]))
				$lastConsult[$year] = array();

			$q = "SELECT AVG(r.eval_qv) as qv FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) LEFT JOIN rosace AS r ON (c.id_consult = r.id_consult) WHERE c.consu_type = 2 && p.id_pat IN (" . implode($this->listeUsagers[$year], ", ") . ")";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					$n = mysql_fetch_assoc($r);
					$dataInit[$year] = $n["qv"];
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

			$q = "SELECT p.id_pat, MAX(c.id_consult) as lastconsult FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) WHERE YEAR(c.consu_date) = '" . $year . "' && p.id_pat IN (" . implode($this->listeUsagers[$year], ", ") . ") GROUP BY p.id_pat";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					while ($n = mysql_fetch_assoc($r))
						$lastConsult[$year][] = $n["lastconsult"];
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

			$q = "SELECT AVG(r.eval_qv) as qv FROM rosace AS r WHERE r.id_consult IN (" . implode($lastConsult[$year], ", ") . ")";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					$n = mysql_fetch_assoc($r);
					$dataLast[$year] = $n["qv"];
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
		}
// 		printVar($dataLast);

		echo "<tr>\n";
		echo "<td>EVALUATION QUALIT&Eacute; DE VIE</td>\n";
		echo "<td>Dossier initial</td>\n";

		foreach ($this->years AS $year)
		{
			echo "<td class=\"center\">" . (isset($dataInit[$year]) ? round($dataInit[$year], 1) : 0) . "</td>\n";
		}

		echo "</tr>\n";

		echo "<tr>\n";
		echo "<td>&nbsp;</td>\n";
		echo "<td>Derni&egrave;re consultation</td>\n";

		foreach ($this->years AS $year)
		{
			echo "<td class=\"center\">" . (isset($dataLast[$year]) ? round($dataLast[$year], 1) : 0) . "</td>\n";
		}

		echo "</tr>\n";
	}

	function impact_conso_alcool()
	{
		$dataInit = array();
		$dataLast = array();
		$lastConsult = array();

		foreach ($this->years as $year)
		{
			if (!isset($this->listeUsagers[$year]))
				continue;

			if (!isset($dataInit[$year]))
				$dataInit[$year] = 0;

			if (!isset($dataLast[$year]))
				$dataLast[$year] = 0;

			if (!isset($lastConsult[$year]))
				$lastConsult[$year] = array();

			$q = "SELECT AVG(alc.alc_conso_jour) as conso FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) LEFT JOIN alctab AS alc ON (c.id_consult = alc.id_consult) WHERE c.consu_type = 2 && p.id_pat IN (" . implode($this->listeUsagers[$year], ", ") . ")";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					$n = mysql_fetch_assoc($r);
					$dataInit[$year] = $n["conso"];
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

			$q = "SELECT p.id_pat, MAX(c.id_consult) as lastconsult FROM patient AS p LEFT JOIN consultation AS c ON (c.id_pat = p.id_pat) WHERE YEAR(c.consu_date) = '" . $year . "' && p.id_pat IN (" . implode($this->listeUsagers[$year], ", ") . ") GROUP BY p.id_pat";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					while ($n = mysql_fetch_assoc($r))
						$lastConsult[$year][] = $n["lastconsult"];
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

			$q = "SELECT AVG(alc.alc_4_5_jour + alc.alc_8_12_jour + alc.alc_16_20_jour + alc.alc_40_jour) as conso FROM alctab AS alc WHERE alc.id_consult IN (" . implode($lastConsult[$year], ", ") . ")";
			$r = mysql_query($q);

			if ($r)
			{
				if ($t = mysql_num_rows($r))
				{
					$n = mysql_fetch_assoc($r);
					$dataLast[$year] = $n["conso"];
				}
			}
			else
				die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
		}
// 		printVar($dataLast);

		echo "<tr>\n";
		echo "<td>CONSOMMATION D'ALCOOL (moyenne)</td>\n";
		echo "<td>Dossier initial</td>\n";

		foreach ($this->years AS $year)
		{
			echo "<td class=\"center\">" . (isset($dataInit[$year]) ? round($dataInit[$year], 1) : 0) . "</td>\n";
		}

		echo "</tr>\n";

		echo "<tr>\n";
		echo "<td>&nbsp;</td>\n";
		echo "<td>Derni&egrave;re consultation</td>\n";

		foreach ($this->years AS $year)
		{
			echo "<td class=\"center\">" . (isset($dataLast[$year]) ? round($dataLast[$year], 1) : 0) . "</td>\n";
		}

		echo "</tr>\n";
	}

	// Procédures

	function procedures()
	{
		$this->procedure_contrats_signe();
		$this->procedure_contrats_terme();
		$this->procedure_suite();
	}

	function procedure_contrats_signe()
	{
		$q = "SELECT YEAR(pat_contrat) as annee, COUNT(DISTINCT(id_pat))as total FROM patient WHERE pat_contrat != '0000-00-00' GROUP BY YEAR(pat_contrat) ASC";
		$listMod1 = queryList($q);

		$q = "SELECT mod_nom FROM module WHERE id_mod = 1";
		$mod1 = queryOneField($q);

		$data = array();
		foreach ($listMod1 as $mod)
		{
			$data[$mod1][$mod["annee"]] = $mod["total"];
		}

		$q = "SELECT YEAR(p.prop_contrat) as annee, COUNT(DISTINCT(p.id_pat)) as total, m.mod_nom as module FROM proposition p LEFT JOIN module m ON (p.id_mod = m.id_mod) WHERE p.id_mod != 1 GROUP BY YEAR(p.prop_contrat), m.mod_nom ORDER BY m.mod_nom ASC, YEAR(p.prop_contrat) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				echo "<tr>\n";
				echo "<td>PROCEDURES</td>\n";
				echo "<td>Nombres de contrats patients signés (attestations retournées)</td>\n";
				echo "<td colspan=\"3\">&nbsp;</td>\n";
				echo "</tr>\n";

				while ($n = mysql_fetch_assoc($r))
				{
					if ($n["module"])
						$data[$n["module"]][$n["annee"]] = $n["total"];
				}

				foreach ($data AS $module => $array)
				{
					echo "<tr>\n";
					echo "<td>&nbsp;</td>\n";
					echo "<td>&bull;&nbsp;" . $module . "</td>\n";

					foreach ($this->years AS $year)
						echo "<td class=\"center\">" . (isset($array[$year]) ? $array[$year] : 0) . "</td>\n";

					echo "</tr>\n";
				}
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function procedure_contrats_terme()
	{
		$q = "SELECT YEAR(p.prop_contrat) as annee, count(distinct(p.id_pat)) as total, m.mod_nom as module FROM proposition p LEFT JOIN module m ON (p.id_mod = m.id_mod) WHERE p.prop_fini = 1 GROUP BY YEAR(p.prop_contrat), m.mod_nom ORDER BY m.mod_nom ASC, YEAR(p.prop_contrat) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				$this->separation();

				echo "<tr>\n";
				echo "<td></td>\n";
				echo "<td>Nombres de contrats patients menés à terme</td>\n";
				echo "<td colspan=\"3\">&nbsp;</td>\n";
				echo "</tr>\n";

				$data = array();

				while ($n = mysql_fetch_assoc($r))
				{
					if ($n["module"])
						$data[$n["module"]][$n["annee"]] = $n["total"];
				}

				foreach ($data AS $module => $array)
				{
					echo "<tr>\n";
					echo "<td>&nbsp;</td>\n";
					echo "<td>&bull;&nbsp;" . $module . "</td>\n";

					foreach ($this->years AS $year)
						echo "<td class=\"center\">" . (isset($array[$year]) ? $array[$year] : 0) . "</td>\n";

					echo "</tr>\n";
				}
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function procedure_suite()
	{
		$q = "SELECT YEAR(c.consu_date) as Annee, count(distinct(c.id_pat)), m.mod_nom as Module FROM situpat s LEFT JOIN module m ON (s.id_mod = m.id_mod) LEFT JOIN consultation c ON (s.id_consult = c.id_consult) LEFT JOIN final f ON (s.id_consult = f.id_consult) WHERE f.fin_modfini = 1 AND c.consu_type = 4 GROUP BY YEAR(c.consu_date), m.mod_nom ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				$this->separation();

				echo "<tr>\n";
				echo "<td>PROCEDURES (suite)</td>\n";
				echo "<td>Nombres de contrats patients signés (attestations retournées)</td>\n";
				echo "<td colspan=\"3\">&nbsp;</td>\n";
				echo "</tr>\n";

				$data = array();

				while ($n = mysql_fetch_assoc($r))
				{
					if ($n["module"])
						$data[$n["module"]] = array ($n["annee"] => $n["total"]);
				}

				foreach ($data AS $module => $array)
				{
					echo "<tr>\n";
					echo "<td>&nbsp;</td>\n";
					echo "<td>" . $module . "</td>\n";

					foreach ($this->years AS $year)
						echo "<td class=\"center\">" . (isset($array[$year]) ? $array[$year] : 0) . "</td>\n";

					echo "</tr>\n";
				}
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	// Référentiel de qualité des soins

	function referentiel()
	{
		$this->ref_orientation_mod1();
		$this->ref_conseil_minimal_apres_contact();
		$this->ref_nouveau_rdv_apres_contact();
		$this->ref_ni_rdv_ni_conseil_apres_contact();

		$this->separation();

		$this->ref_patient_orientes_apres_eval();
		$this->ref_patient_sortant_apres_eval();
		$this->ref_conseil_minimal_patient_sortant();
		$this->ref_courrier_medecin_patient_sortant();
	}

	function ref_orientation_mod1()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(c.id_pat)) as total FROM consultation c WHERE c.consu_type = 1 AND c.consu_eval = 1  GROUP BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>REFERENTIEL DE QUALITE DE SOINS</td>\n";
			echo "<td>Orientation vers mod1 après contact</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function ref_conseil_minimal_apres_contact()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(c.id_pat)) as total FROM consultation c LEFT JOIN final f ON (c.id_consult = f.id_consult) WHERE c.consu_type = 1 AND f.fin_conseil = 1 GROUP BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Conseil minimal donné après contact</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function ref_nouveau_rdv_apres_contact()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(*) as total FROM consultation c LEFT JOIN rdv r ON (c.id_pat = r.id_pat) WHERE c.consu_type = 1 AND r.rdv_date >= c.consu_date GROUP BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Nouveau RDV donné après contact</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function ref_ni_rdv_ni_conseil_apres_contact()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(c.id_pat)) as total FROM consultation c LEFT JOIN rdv r ON (c.id_pat = r.id_pat && r.rdv_date >= c.consu_date) LEFT JOIN final f ON (c.id_consult = f.id_consult) WHERE c.consu_type = 1 AND r.rdv_date IS NULL AND f.fin_conseil = 0 GROUP BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Pas de nouveau RDV ni de conseil minimal donné après contact</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	// Référentiel de qualite des soins (suite)

	function ref_patient_orientes_apres_eval()
	{
		$q = "SELECT YEAR(p.prop_contrat) as annee, count(distinct(p.id_pat)) as total FROM proposition p WHERE p.prop_contrat != 0 GROUP BY YEAR(p.prop_contrat) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>REFERENTIEL DE QUALITE DES SOINS (SUITE)</td>\n";
			echo "<td>Patients orientés vers module après évaluation</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function ref_patient_sortant_apres_eval()
	{
		$q = "SELECT YEAR(p.prop_contrat) as annee, count(distinct(p.id_pat)) as total FROM proposition p WHERE p.prop_contrat != 0 AND p.prop_sortie = 1 GROUP BY YEAR(p.prop_contrat) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Patients sortant du réseau après évaluation</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function ref_conseil_minimal_patient_sortant()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(c.id_pat)) as total FROM consultation c LEFT JOIN proposition p ON (c.id_pat = p.id_pat) LEFT JOIN final f ON (c.id_consult = f.id_consult)  WHERE p.prop_sortie = 1 AND f.fin_conseil = 1  GROUP BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Conseil minimal donné aux patients sortant</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function ref_courrier_medecin_patient_sortant()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(c.id_pat)) as total FROM consultation c LEFT JOIN proposition p ON (c.id_pat = p.id_pat) LEFT JOIN final f ON (c.id_consult = f.id_consult)  WHERE p.prop_sortie = 1 AND f.fin_courrier = 1  GROUP BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Courrier au médecin traitant pour les patients sortant</td>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["annee"]] = $n["total"];
			}

			foreach ($this->years AS $year)
				echo "<td class=\"center\">" . (isset($data[$year]) ? $data[$year] : 0) . "</td>\n";

			echo "</tr>\n";
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	// Education des patients

	function education()
	{
		$this->nombre_atelier_realise();
		$this->separation();
		$this->nombre_atelier_inscrit();
	}

	function nombre_atelier_inscrit()
	{
		$q = "SELECT a.id_act, YEAR(c.consu_date) as annee, COUNT(DISTINCT c.id_pat) as total FROM activite a LEFT JOIN consultation c ON (c.id_consult = a.id_consult) WHERE a.act_prescrit != '0000-00-00' GROUP BY YEAR(c.consu_date), a.id_act ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>&nbsp;</td>\n";
			echo "<td>Nombre de patients inscrits</td>\n";
			echo "<td colspan=\"3\">&nbsp;</td>\n";
			echo "</tr>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["id_act"]][$n["annee"]] = $n["total"];
			}

			$id_act_tab = array (
				1 => "Di&eacute;teticienne",
				2 => "Psychologue",
				3 => "Infirmier",
				4 => "Relaxation",
				5 => "Information",
				6 => "Motivationnel",
				7 => "Maintien de l'abstinence ",
				8 => "Groupe du Mardi",
				9 => "D&eacute;tente et bien &ecirc;tre",
				10 => "Relaxation",
				11 => "Expression th&eacute;&acirc;trale et corporelle",
				12 => "Mouvement d'anciens buveurs",
			);

			foreach ($data AS $id_act => $years)
			{
				echo "<tr>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>&bull;&nbsp;" . $id_act_tab[$id_act] . "</td>\n";

				foreach ($this->years AS $year)
				{
					if (isset($years[$year]))
						echo "<td class=\"center\">" . $years[$year] . "</td>\n";
					else
						echo "<td class=\"center\">0</td>\n";
				}

					echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function nombre_atelier_realise()
	{
// 		$q = "SELECT a.id_act, YEAR(c.consu_date) as annee, count(a.act_effectue) as total FROM activite a LEFT JOIN consultation c ON (c.id_consult = a.id_consult) WHERE a.act_effectue != '0000-00-00' GROUP BY YEAR(c.consu_date), a.id_act ORDER BY YEAR(c.consu_date) ASC";
		$q = "SELECT YEAR(c.consu_date) as annee, a.id_act, COUNT(DISTINCT c.id_pat) as total FROM activite a INNER JOIN consultation c ON (c.id_consult = a.id_consult) WHERE a.act_effectue != '0000-00-00' GROUP BY YEAR(c.consu_date), a.id_act ORDER BY YEAR(c.consu_date) ASC";
		$r = mysql_query($q);

		if ($r)
		{
			echo "<tr>\n";
			echo "<td>EDUCATION DES PATIENTS</td>\n";
			echo "<td>Nombre d'ateliers r&eacute;alis&eacute;s</td>\n";
			echo "<td colspan=\"3\">&nbsp;</td>\n";
			echo "</tr>\n";

			$data = array();

			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
						$data[$n["id_act"]][$n["annee"]] = $n["total"];
			}

			$id_act_tab = array (
				1 => "Di&eacute;teticienne",
				2 => "Psychologue",
				3 => "Infirmier",
				4 => "Relaxation",
				5 => "Information",
				6 => "Motivationnel",
				7 => "Maintien de l'abstinence ",
				8 => "Groupe du Mardi",
				9 => "D&eacute;tente et bien &ecirc;tre",
				10 => "Relaxation (hors CCAA)",
				11 => "Expression th&eacute;&acirc;trale et corporelle",
				12 => "Mouvement d'anciens buveurs",
			);

			foreach ($data AS $id_act => $years)
			{
				echo "<tr>\n";
				echo "<td>&nbsp;</td>\n";
				echo "<td>&bull;&nbsp;" . $id_act_tab[$id_act] . "</td>\n";

				foreach ($this->years AS $year)
				{
					if (isset($years[$year]))
						echo "<td class=\"center\">" . $years[$year] . "</td>\n";
					else
						echo "<td class=\"center\">0</td>\n";
				}

					echo "</tr>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	// Autres

	function duree_moyenne_pec()
	{
		$q = "SELECT p.prop_contrat as Debut, c.consu_date as Fin, m.mod_nom as Module FROM consultation c LEFT JOIN proposition p ON (c.id_pat = p.id_pat) LEFT JOIN module m ON (p.id_mod = m.id_mod) WHERE c.consu_type = 4 AND p.prop_contrat != '0000-00-00' ORDER BY m.mod_nom";
		$r = mysql_query($q);

		if ($r)
		{

		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}

	function sortie_reseau_avant_fin_contrat()
	{
		$q = "SELECT YEAR(c.consu_date) as annee, count(distinct(p.id_pat)) as total, m.mod_nom as module FROM consultation c LEFT JOIN proposition p ON (c.id_pat = p.id_pat) LEFT JOIN module m ON (p.id_mod = m.id_mod) WHERE p.prop_sortie = 1 AND (c.consu_type = 3 OR c.consu_type = 4) AND p.prop_contrat <= c.consu_date GROUP BY YEAR(c.consu_date) ASC, m.mod_nom";
		$r = mysql_query($q);

		if ($r)
		{

		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
	}


}

?>