<?php

class proposition
{
	function list_proposition()
	{
		$data = array();

		$q = "SELECT * FROM proposition";
		$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 get_last_proposition($id_pat)
	{
		$q = "SELECT * FROM proposition WHERE id_pat = '" . $id_pat . "' ORDER BY id_prop 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 get_proposition($id_prop)
	{
		$q = "SELECT * FROM proposition WHERE id_prop = '" . $id_prop . "'";
		$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 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_proposition($id_prop, $id_pat, $id_mod, $prop_date, $prop_contrat, $prop_objectif, $prop_orientation, $prop_interv, $prop_sortie, $prop_motif)
	{
		global $propPresc;

		$id_user = (!empty($propPresc) ? $propPresc : $_SESSION["Sid_user"]);

		if ($id_prop)
			$q = "UPDATE proposition SET id_pat = '" . $id_pat . "' , id_user = '" . $id_user . "', id_mod = '" . $id_mod . "' , prop_date = '" . $prop_date . "' , prop_contrat = '" . $prop_contrat . "' , prop_objectif = '" . sqldata($prop_objectif) . "' , prop_orientation = '" . $prop_orientation . "' , prop_interv = '" . $prop_interv . "' , prop_sortie = '" . $prop_sortie . "' , prop_motif = '" . sqldata($prop_motif) . "' WHERE id_prop = '" . $id_prop . "'";
		else
		{
			$pecNumero = $this->getPECNumero();
			$q = "INSERT INTO proposition (id_pat, id_user, id_mod, numero_pec, prop_date, prop_contrat, prop_objectif, prop_orientation, prop_interv, prop_sortie, prop_motif, date_saisie) VALUES ('" . $id_pat . "' , '" . $id_user . "', '" . $id_mod . "' , '" . $pecNumero . "', '" . $prop_date . "' , '" . $prop_contrat . "' , '" . sqldata($prop_objectif) . "' , '" . $prop_orientation . "' , '" . $prop_interv . "' , '" . $prop_sortie . "' , '" . sqldata($prop_motif) . "', NOW())";
		}
		$r = mysql_query($q);

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

		return false;
	}

	function close_proposition($id_prop)
	{
		$q = "UPDATE proposition SET prop_fini = '1' WHERE id_prop = '" . $id_prop . "'";
		$r = mysql_query($q);

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

		return false;
	}

	function del_proposition($id_prop)
	{
		$q = "DELETE FROM proposition WHERE id_prop = '" . $id_prop . "'";
		$r = mysql_query($q);

		if ($r)
		{
			$q = "OPTIMIZE TABLE proposition";
			$r = mysql_query($q);

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

		return false;
	}
}

?>