<?php

namespace App\Controller\Api;

use App\Entity\EmployeesEntity;
use App\Entity\PresenceCurrentmonthEntity;
use App\Entity\SitesEntity;
use App\Entity\UsersMobileEntity;
use App\Services\Formatter\Hours;
use App\Services\Response\JsonResponse;
use App\Table\BwDebugTable;
use App\Table\EmployeesTable;
use App\Table\EventsTable;
use App\Table\LogsTable;
use App\Table\PresenceCurrentmonthTable;
use App\Table\SitesTable;
use App\Table\UsersMobileTable;
use DateTime;
use Exception;

class PresenceController extends ApiController
{
    /**
     * Retourne les présences déjà présentes dans la Base de données pour les mettre en forme au chargement
     * de la page des présences sur la tablette.
     * @throws Exception
     */
    public function getPresenceBySiteIdAndWeek()
    {
        //On récupère les données request
        $post = $this->getDatas();

        if (!empty($post)) {
            if (isset($post["site_id"], $post["api_key"], $post["monday"], $post["sunday"]) &&
                !empty($post["site_id"]) &&
                !empty($post["api_key"]) &&
                !empty($post["monday"]) &&
                !empty($post["sunday"])
            ) {
                $siteId = $post["site_id"];
                $apiKey = $post["api_key"];
                $monday = $post["monday"];
                $sunday = $post["sunday"];

                //verification api_key
                $this->validateApiKey($apiKey);

                /**
                 * On récupère tous les salariés qui avaient un statut défini entre le lundi d'il y a 14 jours
                 * et le dimanche de la semaine actuelle.
                 */
                $previousMonday = new DateTime($monday);
                $previousSunday = new DateTime($sunday);
                $previousMonday = $previousMonday->modify("-14 days")->format("Y-m-d");
                $previousSunday = $previousSunday->format("Y-m-d");

                /**
                 * @var $presencesTable PresenceCurrentmonthTable
                 */
                $presencesTable = $this->getKernel()->getModelByKey(PresenceCurrentmonthTable::class);

                $presencesPrecedentes = $presencesTable->getAllOfTheYearBySiteId($siteId, $previousMonday, $previousSunday);

                $employes = [];

                if (!empty($presencesPrecedentes)) {
                    $datasPrevious = [];
                    /**
                     * @var $presencePrecedente PresenceCurrentmonthEntity
                     */
                    foreach ($presencesPrecedentes as $presencePrecedente) {
                        $user_key = $presencePrecedente->getReference() . "-" . $presencePrecedente->getCompany();
                        $datasPrevious["employes"][$user_key]["reference"] = $presencePrecedente->getReference();
                        $datasPrevious["employes"][$user_key]["company_id"] = $presencePrecedente->getCompany();
                        $datasPrevious["employes"][$user_key]["first_name"] = $presencePrecedente->getFirstname();
                        $datasPrevious["employes"][$user_key]["last_name"] = $presencePrecedente->getLastname();

                        if (date('w', strtotime($presencePrecedente->getDay())) === '1') {
                            $datasPrevious["employes"][$user_key]["lundi"] = $presencePrecedente->getEtat();
                        } elseif (date('w', strtotime($presencePrecedente->getDay())) === '2') {
                            $datasPrevious["employes"][$user_key]["mardi"] = $presencePrecedente->getEtat();
                        } elseif (date('w', strtotime($presencePrecedente->getDay())) === '3') {
                            $datasPrevious["employes"][$user_key]["mercredi"] = $presencePrecedente->getEtat();
                        } elseif (date('w', strtotime($presencePrecedente->getDay())) === '4') {
                            $datasPrevious["employes"][$user_key]["jeudi"] = $presencePrecedente->getEtat();
                        } elseif (date('w', strtotime($presencePrecedente->getDay())) === '5') {
                            $datasPrevious["employes"][$user_key]["vendredi"] = $presencePrecedente->getEtat();
                        } elseif (date('w', strtotime($presencePrecedente->getDay())) === '6') {
                            $datasPrevious["employes"][$user_key]["samedi"] = $presencePrecedente->getEtat();
                        }
                    }

                    foreach ($datasPrevious["employes"] as $key => $value) {
                        if (array_key_exists("lundi", $value)) {
                            $value["lundi"] = "";
                        }

                        if (array_key_exists("mardi", $value)) {
                            $value["mardi"] = "";
                        }

                        if (array_key_exists("mercredi", $value)) {
                            $value["mercredi"] = "";
                        }

                        if (array_key_exists("jeudi", $value)) {
                            $value["jeudi"] = "";
                        }

                        if (array_key_exists("vendredi", $value)) {
                            $value["vendredi"] = "";
                        }

                        if (array_key_exists("samedi", $value)) {
                            $value["samedi"] = "";
                        }

                        $employes[$key] = $value;
                    }
                }

                /**
                 * Prévisions pour la Semaine en cours
                 * @var $presencesTable PresenceCurrentmonthTable
                 */
                $presencesTable = $this->getKernel()->getModelByKey(PresenceCurrentmonthTable::class);

                $presences = $presencesTable->getAllOfTheYearBySiteId($siteId, $monday, $sunday);

                $datas = [];

                //gestion des horaires
                $horaires = [];
                if (!empty($presences)) {
                    /**
                     * @var $presence PresenceCurrentmonthEntity
                     */
                    foreach ($presences as $presence) {
                        $user_key = $presence->getReference() . "-" . $presence->getCompany();
                        $datas["employes"][$user_key]["reference"] = $presence->getReference();
                        $datas["employes"][$user_key]["company_id"] = $presence->getCompany();
                        $datas["employes"][$user_key]["first_name"] = $presence->getFirstname();
                        $datas["employes"][$user_key]["last_name"] = $presence->getLastname();

                        if (date('w', strtotime($presence->getDay())) === '1') {
                            $datas["employes"][$user_key]["lundi"] = $presence->getEtat();
                            if ((!isset($horaires["Lun"]) || empty($horaires["Lun"])) && !empty($presence->getBeginWork()) && !empty($presence->getEndWork()) && !empty($presence->getPauseWork()) && $presence->getPersonalHours() !== 1) {
                                $horaires["Lun"]["begin"] = $presence->getBeginWork();
                                $horaires["Lun"]["end"] = $presence->getEndWork();
                                $horaires["Lun"]["pause"] = $presence->getPauseWork();
                                $horaires["Lun"]["date"] = $presence->getDay();
                                $horaires["Lun"]["comment"] = $presence->getCommentWork();
                            }
                        } elseif (date('w', strtotime($presence->getDay())) === '2') {
                            $datas["employes"][$user_key]["mardi"] = $presence->getEtat();
                            if ((!isset($horaires["Mar"]) || empty($horaires["Mar"])) && !empty($presence->getBeginWork()) && !empty($presence->getEndWork()) && !empty($presence->getPauseWork()) && $presence->getPersonalHours() !== 1) {
                                $horaires["Mar"]["begin"] = $presence->getBeginWork();
                                $horaires["Mar"]["end"] = $presence->getEndWork();
                                $horaires["Mar"]["pause"] = $presence->getPauseWork();
                                $horaires["Mar"]["date"] = $presence->getDay();
                                $horaires["Mar"]["comment"] = $presence->getCommentWork();
                            }
                        } elseif (date('w', strtotime($presence->getDay())) === '3') {
                            $datas["employes"][$user_key]["mercredi"] = $presence->getEtat();
                            if ((!isset($horaires["Mer"]) || empty($horaires["Mer"])) && !empty($presence->getBeginWork()) && !empty($presence->getEndWork()) && !empty($presence->getPauseWork()) && $presence->getPersonalHours() !== 1) {
                                $horaires["Mer"]["begin"] = $presence->getBeginWork();
                                $horaires["Mer"]["end"] = $presence->getEndWork();
                                $horaires["Mer"]["pause"] = $presence->getPauseWork();
                                $horaires["Mer"]["date"] = $presence->getDay();
                                $horaires["Mer"]["comment"] = $presence->getCommentWork();
                            }
                        } elseif (date('w', strtotime($presence->getDay())) === '4') {
                            $datas["employes"][$user_key]["jeudi"] = $presence->getEtat();
                            if ((!isset($horaires["Jeu"]) || empty($horaires["Jeu"])) && !empty($presence->getBeginWork()) && !empty($presence->getEndWork()) && !empty($presence->getPauseWork()) && $presence->getPersonalHours() !== 1) {
                                $horaires["Jeu"]["begin"] = $presence->getBeginWork();
                                $horaires["Jeu"]["end"] = $presence->getEndWork();
                                $horaires["Jeu"]["pause"] = $presence->getPauseWork();
                                $horaires["Jeu"]["date"] = $presence->getDay();
                                $horaires["Jeu"]["comment"] = $presence->getCommentWork();
                            }
                        } elseif (date('w', strtotime($presence->getDay())) === '5') {
                            $datas["employes"][$user_key]["vendredi"] = $presence->getEtat();
                            if ((!isset($horaires["Ven"]) || empty($horaires["Ven"])) && !empty($presence->getBeginWork()) && !empty($presence->getEndWork()) && !empty($presence->getPauseWork()) && $presence->getPersonalHours() !== 1) {
                                $horaires["Ven"]["begin"] = $presence->getBeginWork();
                                $horaires["Ven"]["end"] = $presence->getEndWork();
                                $horaires["Ven"]["pause"] = $presence->getPauseWork();
                                $horaires["Ven"]["date"] = $presence->getDay();
                                $horaires["Ven"]["comment"] = $presence->getCommentWork();
                            }
                        } elseif (date('w', strtotime($presence->getDay())) === '6') {
                            $datas["employes"][$user_key]["samedi"] = $presence->getEtat();
                            if ((!isset($horaires["Sam"]) || empty($horaires["Sam"])) && !empty($presence->getBeginWork()) && !empty($presence->getEndWork()) && !empty($presence->getPauseWork()) && $presence->getPersonalHours() !== 1) {
                                $horaires["Sam"]["begin"] = $presence->getBeginWork();
                                $horaires["Sam"]["end"] = $presence->getEndWork();
                                $horaires["Sam"]["pause"] = $presence->getPauseWork();
                                $horaires["Sam"]["date"] = $presence->getDay();
                                $horaires["Sam"]["comment"] = $presence->getCommentWork();
                            }
                        }
                    }

                    $employes2 = [];

                    foreach ($datas["employes"] as $key => $value) {
                        if (!array_key_exists("lundi", $value)) {
                            $value["lundi"] = "";
                        }

                        if (!array_key_exists("mardi", $value)) {
                            $value["mardi"] = "";
                        }

                        if (!array_key_exists("mercredi", $value)) {
                            $value["mercredi"] = "";
                        }

                        if (!array_key_exists("jeudi", $value)) {
                            $value["jeudi"] = "";
                        }

                        if (!array_key_exists("vendredi", $value)) {
                            $value["vendredi"] = "";
                        }

                        if (!array_key_exists("samedi", $value)) {
                            $value["samedi"] = "";
                        }

                        $employes2[$key] = $value;
                    }

                    //On ajoute les attributs des jours en fonction des infos trouvées en DB
                    foreach ($employes2 as $key => $value) {
                        if (array_key_exists("lundi", $value) && $value["lundi"] !== "") {
                            $value["lundi"] = $datas["employes"][$value["reference"]."-".$value["company_id"]]["lundi"];
                        }

                        if (array_key_exists("mardi", $value) && $value["mardi"] !== "") {
                            $value["mardi"] = $datas["employes"][$value["reference"]."-".$value["company_id"]]["mardi"];
                        }

                        if (array_key_exists("mercredi", $value) && $value["mercredi"] !== "") {
                            $value["mercredi"] = $datas["employes"][$value["reference"]."-".$value["company_id"]]["mercredi"];
                        }

                        if (array_key_exists("jeudi", $value) && $value["jeudi"] !== "") {
                            $value["jeudi"] = $datas["employes"][$value["reference"]."-".$value["company_id"]]["jeudi"];
                        }

                        if (array_key_exists("vendredi", $value) && $value["vendredi"] !== "") {
                            $value["vendredi"] = $datas["employes"][$value["reference"]."-".$value["company_id"]]["vendredi"];
                        }

                        if (array_key_exists("samedi", $value) && $value["samedi"] !== "") {
                            $value["samedi"] = $datas["employes"][$value["reference"]."-".$value["company_id"]]["samedi"];
                        }

                        // On reconstruit le tableau final
                        foreach ($employes as $ke => $va) {
                            if (($va["reference"] === $value["reference"]) && ($va["company_id"] === $value["company_id"])) {
                                unset($va);
                            }
                        }

                        $employes[$key] = $value;
                    }
                }

                $infos = [];

                foreach ($employes as $k => $v) {
                    $infos[] = $v;
                }

                if (!empty($infos)) {
                    $this->response["horaires"] = $horaires;
                    $this->response["presences"] = $infos;
                    $this->response["success"] = true;
                    $this->response["message"] = "Voici les présences demandées.";
                } else {
                    $this->response["success"] = false;
                    $this->response["message"] = "Aucune donnée pour ce site !";
                }
            } else {
                $this->response["success"] = false;
                $this->response["message"] = "Vôtre requête est incomplète. Code erreur : api-presences-by-site-and-week-2";
            }
        } else {
            $this->response["success"] = false;
            $this->response["message"] = "Vôtre requête est incomplète. Code erreur : api-presences-by-site-and-week-1";
        }

        $jsonResponse = new JsonResponse($this->response);
        $jsonResponse->sendResponse();
    }

    /**
     * Route qui retourne les informations horaires d'un salarié.
     */
    public function getPersonal()
    {
        //On récupère les données request
        $post = $this->getDatas();
        if (isset($post["api_key"], $post["date"], $post["reference"], $post["company"], $post["site"]) &&
            !empty($post["date"]) &&
            !empty($post["api_key"]) &&
            !empty($post["reference"]) &&
            !empty($post["company"]) &&
            !empty($post["site"]))
        {
            $apiKey = $post["api_key"];
            $this->validateApiKey($apiKey);

            $date = $post["date"];
            $reference = $post["reference"];
            $company = $post["company"];
            $site = $post["site"];

            /**
             * @var $presencesTable PresenceCurrentmonthTable
             */
            $presencesTable = $this->getKernel()->getModelByKey(PresenceCurrentmonthTable::class);
            /**
             * @var $personal PresenceCurrentmonthEntity
             */
            $personal = $presencesTable->getSalaryPersonalPresence($site, $date, $reference, $company);

            if ($personal) {
                $this->response["success"] = true;
                $this->response["personal_horaires"] = [
                    "begin" => $personal->getBeginWork(),
                    "end" => $personal->getEndWork(),
                    "pause" => $personal->getPauseWork(),
                    "date" => $personal->getDay(),
                    "comment" => (!empty($personal->getCommentWork()) ? $personal->getCommentWork() : ""),
                ];
                $this->response["message"] = "Voici les horaires personnalisés du salarié.";
            } else {
                $this->response["success"] = true;
                $this->response["personal_horaires"] = [];
                $this->response["message"] = "Aucun information concernant les horaires personnalisés.";
            }
        } else {
            $this->response["success"] = false;
            $this->response["message"] = "Votre requête est incomplète. Code erreur : api-presences-personal-1";
        }

        $jsonResponse = new JsonResponse($this->response);
        $jsonResponse->sendResponse();
    }

    /**
     * Enregistrement des présences
     */
    public function setPresence()
    {
        //On récupère les données request
        $post = $this->getDatas();
        $result = false;

        if (!empty($post)) {

            /**
             * @var $debugTable BwDebugTable
             */
            $debugTable = $this->getKernel()->getModelByKey(BwDebugTable::class);

            //Sauvegarde debug
            $debugTable->create([
                "data" => json_encode($post),
                "item_id" => null,
                "date" => date('Y-m-d H:i:s'),
                "type" => "Nouvelles données (présences)",
            ]);

            if (isset($post["api_key"], $post["presences"], $post["horaires"]) && !empty($post["api_key"])) {
                $apiKey = $post["api_key"];
                //verification api_key
                $this->validateApiKey($apiKey);

                /**
                 * @var $presenceCurrentTable PresenceCurrentmonthTable
                 */
                $presenceCurrentTable = $this->getKernel()->getModelByKey(PresenceCurrentmonthTable::class);

                /**
                 * @var $userMobileTable UsersMobileTable
                 */
                $userMobileTable = $this->getKernel()->getModelByKey(UsersMobileTable::class);

                /**
                 * @var $employeesTable EmployeesTable
                 */
                $employeesTable = $this->getKernel()->getModelByKey(EmployeesTable::class);

                /**
                 * @var $logTable LogsTable
                 */
                $logTable = $this->getKernel()->getModelByKey(LogsTable::class);

                /**
                 * @var $siteTable SitesTable
                 */
                $siteTable = $this->getKernel()->getModelByKey(SitesTable::class);

                /**
                 * @var $eventTable EventsTable
                 */
                $eventTable = $this->getKernel()->getModelByKey(EventsTable::class);

                /**
                 * on initialise le formateur de données (heures)
                 * @var $formatter Hours
                 */
                $formatter = $this->getKernel()->getModelByKey(Hours::class);

                if (!empty($post["presences"])) {
                    //Bool pour set une seule fois la sauvegarde du message dans la table evenements (1 enregistrement par appel à la fonction)
                    $messageSaved = false;

                    $who = "Tablette non à jour/Tâche cron";
                    if (isset($post["rsp_id"]) && !empty($post["rsp_id"])) {
                        /**
                         * @var $rsp UsersMobileEntity
                         */
                        $rsp = $userMobileTable->findById($post["rsp_id"]);

                        if ($rsp) {
                            $who = $rsp->getLastname(). " ".$rsp->getFirstname();
                        }
                    }

                    foreach ($post["presences"] as $p) {
                        $siteId = $p["site_id"];
                        $reference = $p["reference"];
                        $company = $p["company_id"];
                        $day = $p["date"];
                        $etat = $p["status"];

                        $createdAt = null; // retourne un int timestamp
                        $created_at = null; // retourne une string de la date
                        $createDate = null;

                        if (isset($p["created_at"]) && !empty($p["created_at"])) {
                            $createdAt = strtotime($p["created_at"]);
                            $created_at = $p["created_at"];
                        }

                        if (!empty($siteId) && !empty($day) && !empty($etat)) {

                            /**
                             * @var $employe EmployeesEntity
                             */
                            $employe = $employeesTable->getSalarie($reference, $company);

                            if (!empty($employe)) {
                                if (!$messageSaved) {
                                    $tab = explode("-", $day);
                                    $date = $tab[2]."/".$tab[1]."/".$tab[0];

                                    /**
                                     * @var $site SitesEntity
                                     */
                                    $site = $siteTable->getSiteById($siteId);
                                    //on genere le message à sauvegarder dans la table evenements
                                    if ($site) {
                                        $message = "Les présences du site " . $site->getName() . " viennent d'arriver pour la journée du " . $date;
                                    } else {
                                        $message = "Les présences pour un site non identifié (id: ".$siteId.") viennent d'arriver pour la journée du " . $date;
                                    }

                                    //on sauvegarde le message dans la table evenements
                                    $eventTable->savePresencesEventMessage($message, $siteId);
                                    $messageSaved = true;
                                }

                                /**
                                 * @var $isFound PresenceCurrentmonthEntity
                                 */
                                $isFound = $presenceCurrentTable->checkIfFoundSalary($reference, $company, $siteId, date('Y-m-d', strtotime($day)));
                                if ($isFound) {
                                    if (!empty($isFound->getCreatedAt())) {
                                        $createdAt = date_create($isFound->getCreatedAt())->format("Y-m-d H:i:s");
                                        $createDate = date_create()->format("Y-m-d H:i:s");
                                    }

                                    if ($isFound->getEtat() !== $etat && ($createDate === null || $createDate >= $createdAt)) {
                                        $query = "Mise à jour de la référence: " . $isFound->getReference() . " sur le site " . $isFound->getSite() . " , compagnie " . $isFound->getCompany() . ". Le statut passe de " . $isFound->getEtat() . " à $etat pour la date du " . $isFound->getDay();
                                        //Sauvegarde des logs dans la DB

                                        $logs = $logTable->create([
                                            "datetime" => date('Y-m-d H:i:s'),
                                            "query" => $query,
                                            "who" => $who,
                                            "ihm" => "tablette",
                                            "json_datas" => json_encode($post)
                                        ]);

                                        if ($logs) {
                                            $dateAt = null;

                                            //pas de date en db ni en tablette. On en ajoute une nouvelle
                                            if (!$isFound->getCreatedAt() && !$created_at) {
                                                $dateAt = date('Y-m-d H:i:s');
                                            } else if (!$isFound->getCreatedAt() && $created_at) { // pas de date en db, on ajoute celle de la tablette
                                                $dateAt = $created_at;
                                            } else if ($isFound->getCreatedAt() && $created_at && $createDate >= $createdAt) { // deux dates, tablette sup à db
                                                $dateAt = $created_at;
                                            }

                                            //si on a une nouvelle date on update la db, sinon on laisse
                                            if ($dateAt) {
                                                $result = $presenceCurrentTable->update("id", $isFound->getId(), [
                                                    "etat" => $etat,
                                                    "created_at" => $dateAt,
                                                ]);
                                            } else {
                                                //pas de changement sur la date donc on update que le statut
                                                //On ne passera normalement jamais ici, mais mieux vaut prévoir...
                                                $result = $presenceCurrentTable->update("id", $isFound->getId(), [
                                                    "etat" => $etat,
                                                ]);
                                            }
                                        }
                                    }
                                } else {
                                    //variable query dans les logs
                                    $query = "Ajout d'une nouvelle présence pour la référence $reference, sur le site $siteId / compagnie $company. Le statut est $etat pour la date du " . date('Y-m-d', strtotime($day));

                                    //Sauvegarde des logs dans la DB
                                    $logs = $logTable->create([
                                        "datetime" => date('Y-m-d H:i:s'),
                                        "query" => $query,
                                        "who" => $who,
                                        "ihm" => "tablette",
                                        "json_datas" => json_encode($post)
                                    ]);

                                    if ($logs) {
                                        $result = $presenceCurrentTable->create([
                                            "reference" => (string) $reference,
                                            "company" => (int) $company,
                                            "site" => (int) $siteId,
                                            "day" => date('Y-m-d', strtotime($day)),
                                            "etat" => $etat,
                                            "created_at" => date('Y-m-d H:i:s')
                                        ]);
                                    }
                                }

                                if ($result) {
                                    $this->response["message"] = "La présence de " . $employe->getFirstName() . " " . $employe->getLastName() . " a été enregistré";
                                    $this->response["success"] = true;
                                } else {
                                    $this->response["message"] = "Enregistrement des présences impossibles.";
                                    $this->response["success"] = false;
                                }
                            } else {
                                $this->response["success"] = false;
                                $this->response["message"] = "Aucun résultat avec cet employé " . $reference . "-" . $company;
                            }
                        } else {
                            $this->response["success"] = false;
                            $this->response["message"] = "Veuillez renseigner site_id, date, status";
                        }
                    }
                }

                //Enregistrement des horaires s'ils sont définis
                if (!empty($post["horaires"])) {
                    foreach ($post["horaires"] as $h) {
                        $siteId = $h["site_id"];
                        $d = $h["date"];
                        $begin = $formatter->formatHour($h["begin_work"]);
                        $end = $formatter->formatHour($h["end_work"]);
                        $pause = $formatter->formatHour($h["pause_work"]);
                        $comment = $h["comment_work"];
                        //enregistrement des horaires si un enregistrement est trouvé
                        $presenceCurrentTable->updateHoursBySiteAndDate($begin, $end, $pause, $d, $siteId, $comment);
                    }

                    if ((isset($this->response["success"]) && $this->response["success"]) || !isset($this->response["success"])) {
                        $this->response["message"] = "Les plages horaires ont bien été enregistrées.";
                        $this->response["success"] = true;
                    } else {
                        $this->response["message"] = "Une erreur est survenue lors de l'enregistrement des présences et/ou horaires.";
                        $this->response["success"] = false;
                    }
                }

                //Enregistrement des horaires personnels s'ils sont définis
                if (!empty($post["horaires_personnels"])) {
                    foreach ($post["horaires_personnels"] as $h) {
                        $siteId = $h["site_id"];
                        $d = $h["date"];
                        $begin = $formatter->formatHour($h["begin_work"]);
                        $end = $formatter->formatHour($h["end_work"]);
                        $pause = $formatter->formatHour($h["pause_work"]);
                        $comment = $h["comment_work"];
                        $reference = $h["reference"];
                        $company = $h["company"];
                        //enregistrement des horaires si un enregistrement est trouvé
                        $presenceCurrentTable->updateHoursPerSalary($begin, $end, $pause, $d, $siteId, $reference, $company, $comment);
                    }

                    if ((isset($this->response["success"]) && $this->response["success"]) || !isset($this->response["success"])) {
                        $this->response["message"] = "Les plages horaires personnalisées ont bien été enregistrées.";
                        $this->response["success"] = true;
                    } else {
                        $this->response["message"] = "Une erreur est survenue lors de l'enregistrement des présences et/ou horaires. Code erreur: api-presences-personals";
                        $this->response["success"] = false;
                    }
                }
            } else {
                $this->response["success"] = false;
                $this->response["message"] = "Vôtre requête est incomplète. Code erreur: api-presence-2";
            }
        } else {
            $this->response["success"] = false;
            $this->response["message"] = "Vôtre requête est incomplète. Code erreur: api-presence-1";
        }

        $jsonResponse = new JsonResponse($this->response);
        $jsonResponse->sendResponse();
    }
}
