<?php

class consultation
{
	function select_consultation($id_user, $id_pat, $selected)
	{
		$data = "<select name=\"id_consult\" id=\"id_consult\" size=\"1\">\n";
		$data .= "<option value=\"\">-- Choisir --</option>\n";

		$q = "SELECT * FROM consultation WHERE id_user = '" . $id_user . "' && id_pat = '" . $id_pat . "' ORDER BY consu_date DESC";
		$r = mysql_query($q);

		if ($r)
		{
			if ($t = mysql_num_rows($r))
			{
				while ($n = mysql_fetch_assoc($r))
					$data .= "<option value=\"" . $n["id_consult"] . "\"" . ($n["id_consult"] == $selected ? " selected" : "") . ">" . format_date($n["consu_date"]) . "</option>\n";
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$data .= "</select>";

		return $data;
	}

	function list_consultation($id_pat, $id_user)
	{
		$data = array();

		$q = "SELECT * FROM consultation AS c LEFT JOIN utilisateur AS u ON (u.id_user = c.id_user) WHERE c.id_pat = '" . $id_pat . "' ORDER BY c.consu_date DESC, c.id_consult DESC, c.consu_type DESC";
//		$q = "SELECT * FROM consultation AS c LEFT JOIN utilisateur AS u ON (u.id_user = c.id_user) WHERE c.id_pat = '" . $id_pat . "' && c.id_user = '" . $id_user . "' ORDER BY id_consult DESC";
		$r = mysql_query($q);

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

		return $data;
	}

	function regenerateCSNumero($id_pat, $id_prop)
	{
		$q = "UPDATE consultation SET cs_numero = 0 WHERE id_pat = '" . $id_pat . "' && id_prop = '" . $id_prop . "' && consu_type = 3";
		query($q);

		$q = "SELECT id_consult FROM consultation WHERE id_pat = '" . $id_pat . "' && id_prop = '" . $id_prop . "' && consu_type = 3 ORDER BY consu_date ASC";
		$consuList = queryListOne($q);

		if (empty($consuList))
			return false;

		foreach ($consuList as $id_consult)
		{
			$this->updateConsuNumero($id_pat, $id_consult, $id_prop);
		}
	}

	function get_last_consultation($id_user, $id_pat)
	{
		$q = "SELECT * FROM consultation WHERE id_pat = '" . $id_pat . "' ORDER BY id_consult DESC LIMIT 1";
		$r = mysql_query($q);

		if ($r)
		{
			$n = mysql_fetch_assoc($r);
			return $n;
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}

	function isLastAndEditable($id_consult, $id_user, $id_pat)
	{
		if ($consu = $this->get_last_consultation($id_user, $id_pat))
		{
			if ($id_consult != $consu["id_consult"])
				return false;

			if ($consu["consu_ferme"] == 0)
				return false;

			return true;
		}

		return false;
	}

	function get_consultation($id_consult)
	{
		$q = "SELECT * FROM consultation WHERE id_consult = '" . $id_consult . "'";
		$r = mysql_query($q);

		if ($r)
		{
			$n = mysql_fetch_assoc($r);
			return $n;
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}

	function getFirstConsultation($id_pat)
	{
		$q = "SELECT * FROM consultation WHERE id_pat = '" . $id_pat . "' ORDER BY id_consult ASC LIMIT 1";
		$r = mysql_query($q);

		if ($r)
		{
			$n = mysql_fetch_assoc($r);
			return $n;
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}

	function getCSNumero($id_pat)
	{
		$q = "SELECT MAX(cs_numero) FROM consultation WHERE id_pat = '" . $id_pat . "'";
		$csNumero = queryOneField($q) + 1;

		return $csNumero;
	}

	function getPECNumero()
	{
		$q = "SELECT MAX(numero_pec) FROM proposition";
		$propPEC = queryOneField($q);

		$q = "SELECT MAX(numero_pec) FROM consultation";
		$consuPEC = queryOneField($q);

		$maxPEC = max($propPEC, $consuPEC);
		$nextPEC = $maxPEC + 1;

		return $nextPEC;
	}

	function edit_consultation($id_consult, $id_pat, $id_user, $consu_type, $consu_date, $consu_comment, $consu_eval = 0)
	{
		if ($id_consult && $this->get_consultation($id_consult))
		{
			if (isset($_SESSION["Sadmin"]))
				$q = "UPDATE consultation SET id_pat = '" . $id_pat . "', consu_type = '" . $consu_type . "', consu_date = '" . $consu_date . "', consu_comment = '" . sqldata($consu_comment) . "', consu_eval = '" . $consu_eval . "' WHERE id_consult = '" . $id_consult . "'";
			else
				$q = "UPDATE consultation SET id_pat = '" . $id_pat . "', id_user = '" . $id_user . "', consu_type = '" . $consu_type . "', consu_date = '" . $consu_date . "', consu_comment = '" . sqldata($consu_comment) . "', consu_eval = '" . $consu_eval . "' WHERE id_consult = '" . $id_consult . "'";
		}
		else
		{
			$pecNumero = $this->getPECNumero();
			$q = "INSERT INTO consultation (id_pat, id_user, numero_pec, consu_type, cs_numero, consu_date, consu_comment, consu_eval, date_saisie) VALUES ('" . $id_pat . "' , '" . $id_user . "' , '" . $pecNumero . "', '" . $consu_type . "', 0, '" . $consu_date . "' , '" . sqldata($consu_comment) . "', '" . $consu_eval . "', NOW())";
		}
		$r = mysql_query($q);

		if ($r)
		{
			if ($id_consult)
				$id = $id_consult;
			else
				$id = mysql_insert_id();

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

		return false;
	}

	function editPropTera($id_consult, $prop_tera)
	{
		$q = "UPDATE situpat SET prop_tera = '" . $prop_tera . "' WHERE id_consult = '" . $id_consult . "'";
		query($q);

		return true;
	}

	function updateConsuNumero($id_pat, $id_consult, $id_prop)
	{
		$q = "SELECT MAX(cs_numero) FROM consultation WHERE id_pat = '" . $id_pat . "' && id_prop = '" . $id_prop . "' && consu_type = 3";
		$csNumero = queryOneField($q) + 1;

		$q = "UPDATE consultation SET cs_numero = '" . $csNumero . "' WHERE id_consult = '" . $id_consult . "'";
		query($q);

		return true;
	}

	function updatePropConsultation($id_consult, $id_prop)
	{
		$q = "UPDATE consultation SET id_prop = '" . $id_prop . "' WHERE id_consult = '" . $id_consult . "'";
		query($q);

		return true;
	}

	function close_consultation($id_consult)
	{
		$q = "UPDATE consultation SET consu_ferme = '1' WHERE id_consult = '" . $id_consult . "'";
		$r = mysql_query($q);

		if ($r)
			return true;
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		return false;
	}

	function del_consultation($id_consult)
	{
		global $prop;

		$infoConsu = $this->get_consultation($id_consult);
		$lastConsu = $this->get_last_consultation(0, $infoConsu["id_pat"]);

		if ($infoConsu["id_consult"] == $lastConsu["id_consult"])
		{
			if ($infoConsu["consu_type"] == 2)
			{
				$prop->del_proposition($infoConsu["id_prop"]);
			}

			if ($infoConsu["consu_type"] == 4)
			{
				$lastProp = $prop->get_last_proposition($infoConsu["id_pat"]);
				$prop->del_proposition($lastProp["id_prop"]);
			}
		}

		$q = "DELETE FROM consultation WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM activite WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM alctab WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM bio WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM correspondants WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM drogue WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM final WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM medicament WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM rosace WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM satisfaction WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM situpat WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "DELETE FROM structure WHERE id_consult = '" . $id_consult . "'";
		query($q);

		$q = "OPTIMIZE TABLE consultation, activite, alctab, bio, correspondants, drogue, final, medicament, rosace, satisfaction, situpat, structure";
		query($q);

		return true;
	}

	function check_consultation($id_consult, $id_pat, $id_user)
	{
		global $suivi;

		if (isset($_SESSION["Sadmin"]))
			return;

		if ($id_consult == 0)
		{
			if ($suivi->check_suivi($id_pat, $id_user))
				return;
			else
			{
				header("Location: suivi.php");
				exit();
			}
		}

		$q = "SELECT * FROM consultation WHERE id_consult = '" . $id_consult . "' && id_pat = '" . $id_pat . "'";
		$r = mysql_query($q);

		if ($r)
		{
			if (mysql_num_rows($r))
			{
				if ($suivi->check_suivi($id_pat, $id_user))
					return;
			}
		}
		else
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		header("Location: suivi.php");
		exit();
	}
}

?>