<?php

class bilan
{
	var $years;

	function bilan($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";
	}

	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();
	}

	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());
	}

	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());
	}

	// Procédures

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

	function procedure_contrats_signe()
	{
		$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) GROUP BY YEAR(p.prop_contrat), m.mod_nom ORDER BY 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";

				$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_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 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 YEAR(c.consu_date) as annee, count(a.id_act) as total, a.id_act FROM activite a LEFT JOIN consultation c ON (c.id_consult = a.id_consult) 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 YEAR(c.consu_date) as annee, count(a.act_faite) as total, a.act_faite FROM activite a LEFT JOIN consultation c ON (c.id_consult = a.id_consult) WHERE act_faite != 0 GROUP BY YEAR(c.consu_date), a.act_faite 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["act_faite"]][$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());
	}

	// 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());
	}


}

?>