<?php

include ("config.php");

$highlight_menu = 8;

function getAnneeTrimestre()
{
	$q = "SELECT YEAR(date_saisie) as annee, QUARTER(date_saisie) as trimestre FROM consultation WHERE 1 GROUP BY annee ASC, trimestre ASC";
	$listMenu = queryList($q);

	return $listMenu;
}

function getTrimestre()
{
	$q = "SELECT QUARTER(NOW())";
	return queryOneField($q);
}

$fields = array (
	"anneeTrimestre" => array ("request", "string", date("Y") . "-" . getTrimestre()),
	"download"       => array ("request", "string", ""),
);
getFields($fields);

list($annee, $trimestre) = explode ("-", $anneeTrimestre);

$compta = new Compta($annee, $trimestre);
$listConsu = $compta->getConsu();
$nbConsu = count($listConsu);

$listModule = $compta->getModule();
$nbModule = count($listModule);

$listContrat = $compta->getFirstContrat();
$nbContrat = count($listContrat);

$consuArray = array (
	"1" => "&nbsp;",
	"2" => "DI",
	"3" => "&nbsp;",
	"4" => "DF",
);

for ($i = 0; $i < $nbContrat; $i++)
{
	if (!isset($comptaArray[$listContrat[$i]["date_saisie"]]))
		$comptaArray[$listContrat[$i]["date_saisie"]] = array();

	$firstConsultation = $consu->getFirstConsultation($listContrat[$i]["id_pat"]);
	$infoPro = $util->get_utilisateur($listContrat[$i]["id_user"]);
	$comptaArray[$listContrat[$i]["date_saisie"]][] = array (
		"id_consult" => 0,
		"pec"        => "CIN " . $listContrat[$i]["id_pat"],
		"nom_pro"    => $infoPro["user_nom"],
		"prenom_pro" => $infoPro["user_prenom"],
		"dossier"    => comptaDate($firstConsultation["consu_date"]) . "*<span class=\"bold\">" . substr($listContrat[$i]["pat_nom"], 0, 3) . "</span>*<span class=\"bold\">" . substr($listContrat[$i]["pat_prenom"], 0, 1) . "</span>*" . comptaDate($listContrat[$i]["pat_datenaiss"]),
		"contrat"    => "1. Evaluation",
		"montant"    => 20,
		"consu_type" => 0,
		"consu_date" => format_date($listContrat[$i]["pat_contrat"]),
		"date_saisie" => format_date($listContrat[$i]["date_saisie"]),
	);
}

for ($i = 0; $i < $nbModule; $i++)
{
	if (!isset($comptaArray[$listModule[$i]["date_saisie"]]))
		$comptaArray[$listModule[$i]["date_saisie"]] = array();

	$firstConsultation = $consu->getFirstConsultation($listModule[$i]["id_pat"]);
	$infoPro = $util->get_utilisateur($listModule[$i]["id_user"]);
	$comptaArray[$listModule[$i]["date_saisie"]][] = array (
		"id_consult" => 0,
		"pec"        => "PECI " . $listModule[$i]["numero_pec"],
		"nom_pro"    => $infoPro["user_nom"],
		"prenom_pro" => $infoPro["user_prenom"],
		"dossier"    => comptaDate($firstConsultation["consu_date"]) . "*<span class=\"bold\">" . substr($listModule[$i]["pat_nom"], 0, 3) . "</span>*<span class=\"bold\">" . substr($listModule[$i]["pat_prenom"], 0, 1) . "</span>*" . comptaDate($listModule[$i]["pat_datenaiss"]),
		"contrat"    => $listModule[$i]["mod_nom"],
		"montant"    => 20,
		"consu_type" => 0,
		"consu_date" => format_date($listModule[$i]["prop_contrat"]),
		"date_saisie" => format_date($listModule[$i]["date_saisie"]),
	);
}

for ($i = 0; $i < $nbConsu; $i++)
{
	if (!isset($comptaArray[$listConsu[$i]["date_saisie"]]))
		$comptaArray[$listConsu[$i]["date_saisie"]] = array();

	$firstConsultation = $consu->getFirstConsultation($listConsu[$i]["id_pat"]);

	$montant = 40;

	if ($listConsu[$i]["consu_type"] == 2 || $listConsu[$i]["consu_type"] == 4)
		$montant = 100;

	$currentProp = $prop->get_proposition($listConsu[$i]["id_prop"]);
	$currentMod = $menu->get_module($currentProp["id_mod"]);
	$contrat = $currentMod["mod_nom"];

	$comptaArray[$listConsu[$i]["date_saisie"]][] = array (
		"id_consult" => $listConsu[$i]["id_consult"],
		"pec"        => "PECI " . $listConsu[$i]["numero_pec"],
		"cs_numero"  => $listConsu[$i]["cs_numero"],
		"nom_pro"    => $listConsu[$i]["user_nom"],
		"prenom_pro" => $listConsu[$i]["user_prenom"],
		"dossier"    => comptaDate($firstConsultation["consu_date"]) . "*" . substr($listConsu[$i]["pat_nom"], 0, 3) . "*" . substr($listConsu[$i]["pat_prenom"], 0, 1) . "*" . comptaDate($listConsu[$i]["pat_datenaiss"]),
		"contrat"    => $contrat,
		"montant"    => $montant,
		"consu_type" => $listConsu[$i]["consu_type"],
		"consu_date" => format_date($listConsu[$i]["consu_date"]),
		"date_saisie" => format_date($listConsu[$i]["date_saisie"]),
	);
}

if ($download == "csv" && !empty($comptaArray))
{
	if (strstr(getenv("HTTP_USER_AGENT"), "MSIE"))
	{
		header ("Content-Disposition: attachment; filename=compta.csv");
		header ("Content-type: text/csv");
	}
	else
	{
		if (strstr(getenv("HTTP_USER_AGENT"), "Mozilla"))
		{
			if (strstr(getenv("HTTP_USER_AGENT"), "Windows"))
				header ("Content-type: text/csv");
			else
				header ("Content-type: application/force-download");

			header ("Content-Disposition: attachment; filename=compta.csv");
		}
	}

	header ("Pragma: no-cache");
	header ("Expires: 0");

	ksort($comptaArray);
	$sep = ",";

	echo "\"NOM\"" . $sep;
	echo "\"PRENOM\"" . $sep;
	echo "\"N° DOSSIER\"" . $sep;
	echo "\"CONTRAT\"" . $sep;
	echo "\"DOSSIER\"" . $sep;
	echo "\"CS\"" . $sep;
	echo "\"MONTANT\"" . $sep;
	echo "\"N°\"" . $sep;
	echo "\"DATE\"" . $sep;
	echo "\"DATE SAISIE\"";
	echo "\n";

	foreach ($comptaArray as $date => $rows)
	{
		foreach ($rows as $row)
		{
			echo "\"" . trim($row["nom_pro"]) . "\"" . $sep;
			echo "\"" . trim($row["prenom_pro"]) . "\"" . $sep;
			echo "\"" . trim(strip_tags($row["dossier"])) . "\"" . $sep;
			echo "\"" . trim($row["contrat"]) . "\"" . $sep;
			echo "\"" . trim(($row["consu_type"] == 2 || $row["consu_type"] == 4 ? $consuArray[$row["consu_type"]] : "")) . "\"" . $sep;
			echo "\"" . trim(($row["consu_type"] == 3 ? "CS " . $row["cs_numero"] : "")) . "\"" . $sep;
			echo "\"" . trim($row["montant"]) . "\"" . $sep;
			echo "\"" . trim($row["pec"]) . "\"" . $sep;
			echo "\"" . trim($row["consu_date"]) . "\"" . $sep;
			echo "\"" . trim($row["date_saisie"]) . "\"";
			echo "\n";
		}
	}

	exit;
}

include ("header.php");

echo "<p>";
echo "<form action=\"compta.php\" method=\"get\">";
echo "<select name=\"anneeTrimestre\" size=\"1\" onchange=\"this.form.submit();\">";
foreach (getAnneeTrimestre() as $item)
{
	list($annee, $trimestre) = $item;
	echo "<option value=\"" . $annee . "-" . $trimestre . "\"" . ($anneeTrimestre == ($annee . "-" . $trimestre) ? " selected=\"selected\"" : "") . ">Année " . $annee . ", trimestre " . $trimestre . "</option>";
}
echo "</select>";
echo "</form>";
echo "</p>";

if (!empty($comptaArray))
{
	echo "<table id=\"compta\" class=\"center\" style=\" width: 100%; font-size: 80%\">";
	echo "<tr>";
	echo "<th>NOM</th>";
	echo "<th>Prénom</th>";
	echo "<th>N° dossier</th>";
	echo "<th>Contrat</th>";
	echo "<th>Dossier</th>";
	echo "<th>CS</th>";
	echo "<th>Montant</th>";
	echo "<th>N°</th>";
	echo "<th>Date</th>";
	echo "<th>Date saisie</th>";
	echo "</tr>";

	ksort($comptaArray);
	foreach ($comptaArray as $date => $rows)
	{
		foreach ($rows as $row)
		{
			if ($row["montant"] == 20)
				echo "<tr class=\"sign\">";
			else
				echo "<tr>";
			echo "<td>" . $row["nom_pro"] . "</td>";
			echo "<td>" . $row["prenom_pro"] . "</td>";
			echo "<td>" . $row["dossier"] . "</td>";
			echo "<td>" . $row["contrat"] . "</td>";
			echo "<td class=\"center\">" . ($row["consu_type"] == 2 || $row["consu_type"] == 4 ? $consuArray[$row["consu_type"]] : "&nbsp;") . "</td>";
			echo "<td>" . ($row["consu_type"] == 3 ? "CS " . $row["cs_numero"] : "&nbsp;") . "</td>";
			echo "<td class=\"center\">" . $row["montant"] . "</td>";
			echo "<td>" . $row["pec"] . "</td>";
			echo "<td class=\"center\">" . $row["consu_date"] . "</td>";
			echo "<td class=\"center\">" . $row["date_saisie"] . "</td>";
			echo "</tr>";
		}
	}
	echo "</table>";

	echo "<p><a class=\"slink\" href=\"compta.php?anneeTrimestre=" . $anneeTrimestre . "&amp;download=csv\">Télécharger le tableau au format CSV</a></p>";
}

include ("footer.php");

?>