<?php

include ("dbconfig.php");
include ("functions.php");

if (!mysql_connect($dbconfig["server"], $dbconfig["login"], $dbconfig["pass"]) || !mysql_select_db($dbconfig["database"]))
  die ("Erreur m&eacute;canique " . mysql_errno() . " : " . mysql_error());

function regenerateCSNumero()
{
	$q = "UPDATE consultation SET cs_numero = 0 WHERE consu_type = 3";
	query($q);

	// Regenerate missing CS number
	$q = "SELECT id_pat FROM consultation WHERE consu_type = 3 GROUP BY id_pat ASC";
	$listPat = queryListOne($q);

	foreach ($listPat as $patID)
	{
		$currentPropID = 0;
		$cs = 0;
// 		$q = "SELECT MAX(cs_numero) FROM consultation WHERE consu_type = 3 && id_pat = '" . $patID . "'";
// 		$max = queryOneField($q);

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

		foreach ($listConsu as $consuID)
		{
			$q = "SELECT id_prop FROM consultation WHERE id_consult = '" . $consuID . "'";
			$propID = queryOneField($q);

			if ($currentPropID != $propID)
			{
				$currentPropID = $propID;
				$cs = 1;
			}
			else
				$cs++;

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

// Delete orphan
function deleteOrphan()
{
	$q = "SELECT id_pat FROM patient";
	$listPat = implode(", ", queryListOne($q));

	if (!empty($listConsu))
	{
		$q = "SELECT id_consult FROM consultation WHERE id_pat NOT IN (" . $listPat . ")";
		$listConsu = implode(", ", queryListOne($q));

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

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

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

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

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

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

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

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

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

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

		$q = "DELETE FROM structure WHERE id_consult IN (" . $listConsu . ")";
		query($q);
	}

	if (!empty($listPat))
	{
		$q = "DELETE FROM consultation WHERE id_pat NOT IN (" . $listPat . ")";
		query($q);

		$q = "DELETE FROM debut WHERE id_pat NOT IN (" . $listPat . ")";
		query($q);

		$q = "DELETE FROM proposition WHERE id_pat NOT IN (" . $listPat . ")";
		query($q);

		$q = "DELETE FROM rdv WHERE id_pat NOT IN (" . $listPat . ")";
		query($q);

		$q = "DELETE FROM suivi WHERE id_pat NOT IN (" . $listPat . ")";
		query($q);
	}

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

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 regeneratePECNumero()
{
	$q = "SELECT * FROM consultation WHERE numero_pec = 0 ORDER BY consu_date ASC";
	$listConsu = queryList($q);

	$q = "SELECT * FROM proposition WHERE numero_pec = 0 ORDER BY prop_date ASC";
	$listProp = queryList($q);

	foreach ($listConsu as $consu)
	{
		$pecNumero = getPECNumero();
		$q = "UPDATE consultation SET numero_pec = '" . $pecNumero . "' WHERE id_consult = '" . $consu["id_consult"] . "'";
		query($q);
	}

	foreach ($listProp as $prop)
	{
		$pecNumero = getPECNumero();
		$q = "UPDATE proposition SET numero_pec = '" . $pecNumero . "' WHERE id_prop = '" . $prop["id_prop"] . "'";
		query($q);
	}
}

function regeneratePropAssoc()
{
	$q = "SELECT * FROM consultation WHERE id_prop = 0 && consu_type > 1 ORDER BY id_pat";
	$listConsu = queryList($q);

	foreach ($listConsu as $consu)
	{
		$q = "SELECT * FROM patient WHERE id_pat = '" . $consu["id_pat"] . "'";
		$pat = queryOne($q);

		if ($consu["consu_type"] == 2)
		{
			$q = "SELECT * FROM proposition WHERE id_pat = '" . $consu["id_pat"] . "' && prop_contrat != '0000-00-00' && prop_contrat <= '" . $consu["consu_date"] . "' ORDER BY prop_contrat DESC LIMIT 1";
			$prop = queryOne($q);

			if (empty($prop))
			{
				$q = "SELECT * FROM proposition WHERE id_pat = '" . $consu["id_pat"] . "' && prop_contrat != '0000-00-00' && prop_contrat >= '" . $consu["consu_date"] . "' ORDER BY prop_contrat ASC LIMIT 1";
				$prop = queryOne($q);
			}
		}

		if ($consu["consu_type"] == 3)
		{
			$q = "SELECT * FROM proposition WHERE id_pat = '" . $consu["id_pat"] . "' && prop_contrat != '0000-00-00' && prop_contrat <= '" . $consu["consu_date"] . "' ORDER BY prop_contrat DESC LIMIT 1";
			$prop = queryOne($q);
		}

		if ($consu["consu_type"] == 4)
		{
			$q = "SELECT * FROM proposition WHERE id_pat = '" . $consu["id_pat"] . "' && prop_contrat != '0000-00-00' && prop_contrat < '" . $consu["consu_date"] . "' ORDER BY prop_contrat DESC LIMIT 1";
			$prop = queryOne($q);
		}

		$q = "UPDATE consultation SET id_prop = '" . $prop["id_prop"] . "' WHERE id_consult = '" . $consu["id_consult"] . "'";
		query($q);
	}

// 	printVar($listConsu);
}

function regenerateDateSaisie()
{
	$q = "UPDATE proposition SET date_saisie = prop_date WHERE date_saisie = '0000-00-00'";
	query($q);

	$q = "UPDATE consultation SET date_saisie = consu_date WHERE date_saisie = '0000-00-00'";
	query($q);

	$q = "UPDATE patient SET date_saisie = pat_contrat WHERE date_saisie = '0000-00-00'";
	query($q);
}

// regenerateCSNumero();
// deleteOrphan();
// regeneratePECNumero();
// regeneratePropAssoc();
// regenerateDateSaisie();

?>