<?php

// Librairie de fonction MYSQL
// Fonctions spécifiques à l'application de Libérations
// Date de début et de fin de la période courante
$datedebut = null;
$datefin = null;

//Liste des employeurs
unset($donneeslisteemployeurs);

//Liste des employes
unset($donneeslisteemployes);

function getPeriodeCourante($param = '') {
    //TODO remplacer les variables globales par des paramètres dans la fonction
    global $datedebut;
    global $datefin;

    if (!isset($datedebut) || !isset($datefin)) {
        $sqlparam["type"] = "SELECT";
        $sqlparam["table"][] = "compteurautre";
        $sqlparam["champs"][] = "renouvellement";
        $sqlparam["limit"][] = 1;

        $result = executerRequeteSql($sqlparam);
        $datefin = date("Y-m-d", strtotime($result[0]["renouvellement"]));
        $datedebut = date("Y-m-d", strtotime("-1 year", strtotime($datefin)));
    }
    if ($param == "début") {
        return $datedebut;
    } elseif ($param == "fin") {
        return $datefin;
    } else {
        return array("datedebut" => $datedebut, "datefin" => $datefin);
    }
}

function getEmployeurs($droit = '') {
    //TODO Enlever la variable $donneeslisteemployeurs
    global $donneeslisteemployeurs;
    if (!isset($donneeslisteemployeurs)) {
        $sqlparam["type"] = "SELECT";
        $sqlparam["table"][] = "employeurs";
        $sqlparam["champs"][] = "refemployeur as refid";
        $sqlparam["champs"][] = "nomemployeur as valeur";
        $sqlparam["ordre"][] = "nomemployeur ASC";

        if (isset($_SESSION[$droit]) && $_SESSION[$droit] != 'Oui') {
            $sqlparam["where"][] = sprintf("refemployeur=%d", $_SESSION["employeur"]);
        }
        $donneeslisteemployeurs = executerRequeteSql($sqlparam);
    }
    return $donneeslisteemployeurs;
}

function getEmployes($employeur = '', $droit = '', $tous_les_employes = false) {
    //TODO Enlever la variable $donneeslisteemployes
    global $donneeslisteemployes;
    if (($employeur != '' && is_numeric($employeur)) || ($droit != '' && $droit == 'gestion_site' && $_SESSION[$droit] != 'Oui')) {
        $sqlparam["where"][] = sprintf("employes.refemployeur = %d", $employeur);
    }
    $sqlparam["type"] = "SELECT";
    $sqlparam["table"][] = "employes";
    $sqlparam["champs"][] = "refemploye as refid";
    $sqlparam["champs"][] = "CONCAT(IFNULL(employes.nomfamille,''), ', ', IFNULL(employes.prenom,' '),',"
            . " ', IFNULL(employes.adresse1,' '),', ', IFNULL(employes.ville,' ')) as valeur";
    $sqlparam["ordre"][] = "CONCAT(employes.nomfamille,employes.prenom)";
    // Tenir compte des employés actif seulement
    $sqlparam["where"][] = "archive=0";
    return executerRequeteSql($sqlparam);
}

function getLieuxTravail($droit = '') {
    $sqlparam["type"] = "SELECT";
    $sqlparam["table"][] = "lieuxtravail";
    $sqlparam["table"][] = "employeurs";
    $sqlparam["join"][] = "employeurs.refemployeur = lieuxtravail.refemployeur";
    $sqlparam["champs"][] = "lieuxtravail.reflieutravail as refid";
    $sqlparam["champs"][] = "lieuxtravail.nomlieutravail as valeur";
    $sqlparam["champs"][] = "employeurs.nomemployeur as nomemployeur";
    $sqlparam["ordre"][] = "lieuxtravail.reflieutravail ASC";

    if (isset($_SESSION[$droit]) && $_SESSION[$droit] != 'Oui') {
        $sqlparam["where"][] = sprintf("reflieutravail=%d", $_SESSION["lieutravail"]);
    }

    return executerRequeteSql($sqlparam);
}

function getFournisseursCellulaire() {
    $sqlparam["connexion"] = "maitre";
    $sqlparam["type"] = "SELECT";
    $sqlparam["table"][] = "fournisseurs_cellulaire";
    $sqlparam["champs"][] = "format as refid";
    $sqlparam["champs"][] = "nom as valeur";
    $sqlparam["ordre"][] = "nom ASC";

    return executerRequeteSql($sqlparam);
}

function compteLigneDetails($noliberation) {
    if ($noliberation > 0) {
        $sqlparam1 = array();
        $sqlparam1["table"][] = "liberations";
        $sqlparam1["table"][] = "ligneliberations";
        $sqlparam1["join"][] = "ligneliberations.refliberation=liberations.refliberation";
        $sqlparam1["champs"][] = "count(liberations.refliberation) as compte";
        $sqlparam1["where"][] = sprintf("liberations.refliberation=%d", $noliberation);
        $resultCheck1 = executerRequeteSql($sqlparam1);

//        $sqlparam2 = array();
//        $sqlparam2["table"][] = "liberations";
//        $sqlparam2["table"][] = "ligneliberations";
//        $sqlparam2["join"][] = "ligneliberations.refliberation=liberations.refliberation";
//        $sqlparam2["champs"][] = "count(liberations.refliberation) as compte";
//        $sqlparam2["where"][] = sprintf("ligneliberations.demandeOriginale=%d", $noliberation);
//        $resultCheck2 = executerRequeteSql($sqlparam2);

        $sqlparam2 = array();
        $sqlparam2["mode"] = "direct";
        $sqlparam2["type"] = "SELECT";
        $sqla = "select count(dtl2.refligneliberation) as compte from ligneliberations as dtl inner join ligneliberations as dtl2 ";
        $sqlb = "on dtl2.demandeoriginale =dtl.refligneliberation ";
        $sqlc = "inner join liberations as lib on lib.refliberation=dtl.refliberation ";
        $sqld = sprintf("where lib.refliberation=%d", $noliberation);
        $sqlparam2["sql"] = $sqla . $sqlb . $sqlc . $sqld;
        $resultCheck2 = executerRequeteSql($sqlparam2);

        return array("comptetot" => $resultCheck1[0]["compte"], "compteanl" => $resultCheck2[0]["compte"]);
    } else {
        return null;
    }
}

/**
 * Return true si la libération a été complètement annulée
 * Return falsi si elle est en partie annulée
 * Peut retourner null
 */
function isLiberationCanceledCompletely($noliberation) {
    $result = compteLigneDetails($noliberation);
    if (!is_null($result)) {
        return ($result["comptetot"] == $result["compteanl"] ? true : false);
    } else {
        return null;
    }
}

/**
 * Return true si la libération a été complètement annulée
 */
function isLiberationHasSomeCanceledDetails($noliberation) {
    $result = compteLigneDetails($noliberation);
    if (!is_null($result)) {
        return ($result["comptetot"] < $result["compteanl"] ? true : false);
    } else {
        return null;
    }
}
