<?php

class patient
{
  function select_patient($selected)
  {
    $data = "<select name=\"id_pat\" id=\"id_pat\" size=\"1\">\n";
    $data .= "<option value=\"\">-- Choisir --</option>\n";

    $q = "SELECT p.* FROM patient AS p LEFT JOIN suivi AS s ON (s.id_pat = p.id_pat) WHERE s.id_pat IS NULL ORDER BY p.pat_nom ASC, p.pat_prenom ASC";
    $r = mysql_query($q);

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

    $data .= "</select>";

    return $data;
  }

  function list_patient()
  {
    $data = array();

    $q = "SELECT * FROM patient ORDER BY pat_nom ASC, pat_prenom ASC, id_pat ASC";
    $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_patient($id_pat)
  {
    $q = "SELECT * FROM patient WHERE id_pat = '" . $id_pat . "'";
    $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 edit_patient($id_pat, $id_zone, $pat_sexe, $pat_nom, $pat_prenom, $pat_datenaiss, $pat_nom_ep, $pat_adresse, $pat_code, $pat_ville, $pat_tel, $pat_mail, $pat_envoi, $pat_qui, $pat_hab, $pat_contrat)
	{
		global $suivi, $admin_user;

		if ($id_pat)
		{
			if (empty($pat_contrat) || $pat_contrat == "00/00/0000")
			{
				$q = "UPDATE patient SET id_zone = '" . $id_zone . "' , pat_sexe = '" . $pat_sexe . "' , pat_nom = '" . sqldata(strtoupper($pat_nom)) . "' , pat_prenom = '" . sqldata(ucwords(strtolower($pat_prenom))) . "' , pat_datenaiss = '" . $pat_datenaiss . "' , pat_nom_ep = '" . sqldata($pat_nom_ep) . "' , pat_adresse = '" . sqldata($pat_adresse) . "' , pat_code = '" . sqldata($pat_code) . "' , pat_ville = '" . sqldata($pat_ville) . "' , pat_tel = '" . sqldata($pat_tel) . "' , pat_mail = '" . sqldata($pat_mail) . "' , pat_envoi = '" . $pat_envoi . "' , pat_qui = '" . sqldata($pat_qui) . "' , pat_hab = '" . $pat_hab . "', pat_contrat = '" . sql_date($pat_contrat) . "', date_saisie = NOW(), id_user = '" . $_SESSION["Sid_user"] . "' WHERE id_pat = '" . $id_pat . "'";
			}
			else
			{
				$q = "UPDATE patient SET id_zone = '" . $id_zone . "' , pat_sexe = '" . $pat_sexe . "' , pat_nom = '" . sqldata(strtoupper($pat_nom)) . "' , pat_prenom = '" . sqldata(ucwords(strtolower($pat_prenom))) . "' , pat_datenaiss = '" . $pat_datenaiss . "' , pat_nom_ep = '" . sqldata($pat_nom_ep) . "' , pat_adresse = '" . sqldata($pat_adresse) . "' , pat_code = '" . sqldata($pat_code) . "' , pat_ville = '" . sqldata($pat_ville) . "' , pat_tel = '" . sqldata($pat_tel) . "' , pat_mail = '" . sqldata($pat_mail) . "' , pat_envoi = '" . $pat_envoi . "' , pat_qui = '" . sqldata($pat_qui) . "' , pat_hab = '" . $pat_hab . "', pat_contrat = '" . sql_date($pat_contrat) . "', date_saisie = NOW(), id_user = '" . $_SESSION["Sid_user"] . "' WHERE id_pat = '" . $id_pat . "'";
			}
		}
		else
		{
			$q = "INSERT INTO patient (pat_dossier, id_zone, pat_sexe, pat_nom, pat_prenom, pat_datenaiss, pat_nom_ep, pat_adresse, pat_code, pat_ville, pat_tel, pat_mail, pat_envoi, pat_qui, pat_hab, pat_contrat, date_saisie, id_user) VALUES (0, '" . $id_zone . "' , '" . $pat_sexe . "' , '" . sqldata(strtoupper($pat_nom)) . "' , '" . sqldata(ucwords(strtolower($pat_prenom))) . "' , '" . $pat_datenaiss . "' , '" . sqldata($pat_nom_ep) . "' , '" . sqldata($pat_adresse) . "' , '" . sqldata($pat_code) . "' , '" . sqldata($pat_ville) . "' , '" . sqldata($pat_tel) . "' , '" . sqldata($pat_mail) . "' , '" . $pat_envoi . "' , '" . sqldata($pat_qui) . "' , '" . $pat_hab . "', '" . sql_date($pat_contrat) . "', NOW(), '" . $_SESSION["Sid_user"] . "')";
		}
		$r = mysql_query($q);

		if ($r)
		{
			if (empty($id_pat))
			{
				$id = mysql_insert_id();
				if ($_SESSION["Sid_user"] != $admin_user)
					$suivi->edit_suivi($id, $_SESSION["Sid_user"]);
			}
			else
				$id = $id_pat;

			$this->edit_dossier();

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

		return false;
	}

	function getMaxDossier($patNom, $patPrenom)
	{
		$q = "SELECT MAX(pat_dossier) as max FROM patient WHERE LEFT(pat_nom, 3) = '" . $patNom . "' && LEFT(pat_prenom, 1) = '" . $patPrenom . "'";
		$r = mysql_query($q);
		if ($r === false)
			die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());

		$n = mysql_fetch_assoc($r);
		return $n["max"];
	}

	function edit_dossier()
	{
		$listPatient = $this->list_patient();
		$c = count($listPatient);

		for ($i = 0; $i < $c; $i++)
		{
			$idPat = $listPatient[$i]["id_pat"];
			$patNom = substr($listPatient[$i]["pat_nom"], 0, 3);
			$patPrenom = substr($listPatient[$i]["pat_prenom"], 0, 1);
			$maxDossier = $this->getMaxDossier($patNom, $patPrenom);
			$patDossier = ($listPatient[$i]["pat_dossier"] == 0 ? ($maxDossier + 1) : $listPatient[$i]["pat_dossier"]);

			if ($listPatient[$i]["pat_dossier"] != $patDossier)
			{
				$q = "UPDATE patient SET pat_dossier = '" . $patDossier . "' WHERE id_pat = '" . $idPat ."'";
				$r = mysql_query($q);

				if ($r === false)
					die("Erreur mécanique " . mysql_errno() . " : " . mysql_error());
			}
		}
	}

  function del_patient($id_pat)
  {
		if ($this->purge_patient($id_pat))
			return true;

		return false;

    $q = "DELETE patient, suivi, rdv FROM patient LEFT JOIN suivi ON (suivi.id_pat = patient.id_pat) LEFT JOIN rdv ON (rdv.id_pat = patient.id_pat) WHERE patient.id_pat = '" . $id_pat . "'";
    $r = mysql_query($q);

    if ($r)
    {
      $q = "OPTIMIZE TABLE patient, suivi, rdv";
      $r = mysql_query($q);

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

    return false;
  }

  function purge_patient($id_pat)
  {
    $q = "DELETE consultation, correspondants, situpat, structure, alctab, drogue, medicament, bio, rosace, final, activite, satisfaction FROM consultation LEFT JOIN correspondants ON (correspondants.id_consult = consultation.id_consult) LEFT JOIN situpat ON (situpat.id_consult = consultation.id_consult) LEFT JOIN structure ON (structure.id_consult = consultation.id_consult) LEFT JOIN alctab ON (alctab.id_consult = consultation.id_consult) LEFT JOIN drogue ON (drogue.id_consult = consultation.id_consult) LEFT JOIN medicament ON (medicament.id_consult = consultation.id_consult) LEFT JOIN bio ON (bio.id_consult = consultation.id_consult) LEFT JOIN rosace ON (rosace.id_consult = consultation.id_consult) LEFT JOIN final ON (final.id_consult = consultation.id_consult) LEFT JOIN activite ON (activite.id_consult = consultation.id_consult) LEFT JOIN satisfaction ON (satisfaction.id_consult = consultation.id_consult) WHERE consultation.id_pat = '" . $id_pat . "'";
    $r = mysql_query($q);

    if ($r)
    {
      $q = "DELETE patient, suivi, rdv, proposition, debut FROM patient LEFT JOIN suivi ON (suivi.id_pat = patient.id_pat) LEFT JOIN rdv ON (rdv.id_pat = patient.id_pat) LEFT JOIN proposition ON (proposition.id_pat = patient.id_pat) LEFT JOIN debut ON (debut.id_pat = patient.id_pat) WHERE patient.id_pat = '" . $id_pat . "'";
      $r = mysql_query($q);

      if ($r)
      {
        $q = "OPTIMIZE TABLE patient, suivi, rdv, proposition, debut, consultation, correspondants, situpat, structure, alctab, drogue, medicament, bio, rosace, final, activite, satisfaction";
        $r = mysql_query($q);

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

    return false;
  }

	function check_doublon($id_pat, $pat_nom, $pat_prenom, $pat_datenaiss)
	{
		if ($id_pat && $this->get_patient($id_pat))
			return false;

		$pat_nom = substr($pat_nom, 0, 3);
		$pat_prenom = substr($pat_prenom, 0, 1);
		$pat_datenaiss = sql_date($pat_datenaiss);

		$q = "SELECT * FROM patient WHERE pat_nom LIKE '" . sqldata($pat_nom) . "%' AND pat_prenom LIKE '" . sqldata($pat_prenom) . "%' AND pat_datenaiss = '" . $pat_datenaiss . "'";
		$r = mysql_query($q);

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

    return false;
	}
}

?>