<?php

require_once("libliberations.inc.php");

$sqlWhereDateDemande = "";
$sqlWhereEmploye = "";
$sqlWhereEmployeur = "";
$sqlWhereArticle = "";

// ****************************************************
// Sélection de date
// Toujours déterminer la date de début et de fin de la période courante.
// Pour celles des BER (si nécessaire)
// ****************************************************
$datetemp = getPeriodeCourante();
$datedebut1 = $datetemp["datedebut"];
$datefin1 = $datetemp["datefin"];

//Date de début indiquée?
$operation = "";
if (filter_input(INPUT_GET, "datedebut") != "") {
    $datedebut = antiInjection(filter_input(INPUT_GET, "datedebut"));
    if (!strtotime($datedebut)) {
        $datedebut = date("Y-M-d");
    }

    //Date de fin indiquée?
    if (filter_input(INPUT_GET, "datefin") != "") {
        $datedebut1 = $datedebut;
        $datedebut2 = $datedebut;
        $datefin = antiInjection(filter_input(INPUT_GET, "datefin"));
        if (!strtotime($datefin)) {
            $datefin = date("Y-M-d");
        }
        $datefin1 = $datefin;
        $datefin2 = $datefin;
    } else {
        if (filter_input(INPUT_GET, "optiondate") !== "") {
            $optiondate = sprintf("%d", filter_input(INPUT_GET, "optiondate"));
            switch ($optiondate) {
                case "1":
                    $operation = "=";
                    $datedebut1 = $datedebut;
                    $datedebut2 = $datedebut;
                    break;
                case "2":
                    $operation = "<";
                    $datefin1 = $datedebut;
                    $datefin2 = $datedebut;
                    break;
                case "3":
                    $operation = ">";
                    $datedebut1 = $datedebut;
                    $datedebut2 = $datedebut;
                    break;
                default:
                    $operation = "=";
                    $datedebut1 = $datedebut;
                    $datedebut2 = $datedebut;
                    break;
            }
        } else {
            $operation = "=";
            $datedebut1 = $datedebut;
            $datedebut2 = $datedebut;
        }
    }
}

if (isset($rapport_source) && $rapport_source !== "rapport_employes_employeur") {
    if ($operation != "=") {
        $sqlWhereDateDemande = "DATE_FORMAT(ligneliberations.dateliberation,'%Y-%m-%d') BETWEEN '$datedebut1' And '$datefin1'";
    } else {
        $sqlWhereDateDemande = "DATE_FORMAT(ligneliberations.dateliberation,'%Y-%m-%d') = '$datedebut1' ";
    }
    $sqlparam["where"]["datedemande"] = $sqlWhereDateDemande;
}

// ****************************************************
// Employeur ou employé
// ****************************************************
if (intval(filter_input(INPUT_GET, "ListeEmployes")) != -1) {

    if (filter_input(INPUT_GET, "ListeEmployes") != "") {
        $listeEmployes = antiInjection(sprintf("%d", filter_input(INPUT_GET, "ListeEmployes")));

        $sqlparam["where"]["employe"] = "(employes.refemploye = " . $listeEmployes . ")";
        $sqlGroupEmployeur = "";
        $bHavEmploye = True;
    }
}

if (intval(filter_input(INPUT_GET, "ListeEmployeurs")) != -1 || $_SESSION['gestion_site'] == "Non") {
    unset($listeEmployeurs);
    if ($_SESSION['gestion_site'] == "Non") {
        $listeEmployeurs = $_SESSION['employeur'];
    } else {
        $listeEmployeurs = intval(antiInjection(filter_input(INPUT_GET, "ListeEmployeurs")));
    }
    $sqlWhereEmployeur = sprintf("(employes.refemployeur = %d)", $listeEmployeurs);
    $sqlparam["where"]["employeur"] = sprintf("employes.refemployeur = %d", $listeEmployeurs);
    $bHavEmployeur = True;
} else {
    $sqlWhereEmployeur = "";
    $sqlGroupEmployeur = "";
}

// ****************************************************
// Articles de la convention
// ****************************************************
if (filter_input(INPUT_GET, "ListeArticles") != "") {
    $listeArticles = antiInjection(filter_input(INPUT_GET, "ListeArticles"));
    $listeArticles2 = explode(",", $listeArticles);
    $nombreligne = sizeof($listeArticles2) - 1;
    $listeArticles3 = "";
    for ($v = 0; $v < $nombreligne; $v++) {
        $listeArticles3 .= "\"" . sprintf("%d", $listeArticles2[$v]) . "\"";
        if ($v < $nombreligne - 1) {
            $listeArticles3 .= ",";
        }
    }
    $sqlparam["where"]["article"] = "articles.refarticle IN (" . $listeArticles3 . ")";
}

$sqlGroup = "";
$sqlGroupEmployeur = "";

// ****************************************************
// Commentaire
// ****************************************************
$sqlWhereCommentaire = "";
if (filter_input(INPUT_GET, "Commentaire") != "") {
    $sqlparam["where"]["commentaire"] = "ligneliberations.commentaire LIKE \"%" . antiInjection(sprintf("%s", filter_input(INPUT_GET, "Commentaire"))) . "%\" ";
}

// ****************************************************
// Traitement par rapport
// ****************************************************
if (isset($mode)) {

    //Est-ce le rapport des employés par employeur?
    if ($mode == "Rapport des employés par employeur") {
        unset($sqlparam["where"]["datedemande"]);
        unset($sqlparam["where"]["employe"]);
        unset($sqlparam["where"]["article"]);
        unset($sqlparam["where"]["commentaire"]);
    }
    //Est-ce le rapport des libérations selon le paiement?
    elseif ($mode == "Rapport des libérations selon le paiement") {

        if (filter_input(INPUT_GET, "Assumerpar") != "") {
            if (filter_input(INPUT_GET, "Assumerpar") == "1") {
                $sqlparam["where"]["quipaye"] = "ligneliberations.quipaye = 'Syndicat' ";
            } elseif (filter_input(INPUT_GET, "Assumerpar") == "2") {
                $sqlparam["where"]["quipaye"] = "ligneliberations.quipaye = 'Employeur' ";
            }
        }
        if (filter_input(INPUT_GET, "Paiement") != "") {
            $var1 = antiInjection(filter_input(INPUT_GET, "Paiement"));
            $sqlparam["where"]["paiement"] = ($var1 == "1") ? "ligneliberations.paiement=2" : "ligneliberations.paiement<2";
        }
    }
    if (isset($sqlWhereEmployeur) && !empty($sqlWhereEmployeur)) {
        $sqlparam["where"]["employeur"] = $sqlWhereEmployeur;
    }
}

if (isset($sqlSelect) && isset($sqlselect2) && isset($sqlFrom) && isset($sqlGroup) && isset($sqlGroupEmployeur) && isset($sqlWhere) && isset($sqlOrder)) {
    $sqlprinc = $sqlSelect . $sqlselect2 . $sqlFrom . $sqlGroup . $sqlGroupEmployeur . $sqlWhere . $sqlOrder;
    print $sqlprinc;
}
