<?php

//Librairie de fonctions
require (__DIR__ . '/vendor/autoload.php');
require_once("lib/libconfig.inc.php");
require_once("lib/libsession.inc.php");
require_once("lib/libfonctions.inc.php");
require_once("lib/libmysql.inc.php");
require_once("lib/libencode.inc.php");
require_once("lib/libforms.inc.php");
require_once("lib/libcourriel.inc.php");
require_once("lib/libliberations.inc.php");
require_once("lib/lib2fa.inc.php");

session_start();
setConnectionSql();
if (isset($_POST['prm1']) && isset($_POST['prm2'])) {
    $rsearch1 = antiInjection($_POST['prm1']);
    $rsearch2 = antiInjection($_POST['prm2']);
    if (isset($_POST['prm3']) && !empty($_POST['prm3'])) {
        $rsearch3 = antiInjection($_POST['prm3']);
        if ($rsearch3 != "--------") {
            $prm3vide = false;
        } else {
            unset($rsearch3);
            $prm3vide = true;
        }
    } else {
        $prm3vide = true;
    }
    if (isset($_POST['prm4']) && !empty($_POST['prm4'])) {
        $rsearch4 = antiInjection($_POST['prm4']);
        if ($rsearch4 == "1" || $rsearch4 == "2") {
            $prm4vide = false;
        } else {
            unset($rsearch4);
            $prm4vide = true;
        }
    } else {
        $prm4vide = true;
    }
    if ($rsearch1 != "" && $rsearch2 != "") {
        unset($sqlparam);
        $sqlparam["table"][] = "liberations";
        $sqlparam["table"][] = "ligneliberations";
        //$sqlparam["table"][] = "ligneliberations as dtl2";
        $sqlparam["table"][] = "articles";
        $sqlparam["table"][] = "duree";
        $sqlparam["join"][] = "ligneliberations.refliberation = liberations.refliberation";       
        //$sqlparam["join"][] = array(0 => "left", 1 => "dtl2.demandeoriginale = ligneliberations.refligneliberation");
        $sqlparam["join"][] = "articles.refarticle=ligneliberations.refarticle";
        $sqlparam["join"][] = "duree.refduree=ligneliberations.duree";
        $sqlparam["champs"][] = "liberations.refemploye";
        $sqlparam["champs"][] = "articles.refarticle";
        $sqlparam["champs"][] = "unix_timestamp(ligneliberations.dateliberation) as dateliberation";
        $sqlparam["champs"][] = "SUM(If(liberations.typeoperation=1,duree.dureeNombre,0-dureeNombre)) AS calculduree";
        $sqlparam["groupe"][] = "liberations.refemploye";

        $sqlparam["groupe"][] = "ligneliberations.dateliberation";
        if (!$prm3vide) {
            $sqlparam["where"][] = sprintf("ligneliberations.refarticle=%d", $rsearch3);
            $sqlparam["groupe"][] = "articles.refarticle";
        }

        $sqlparam["where"][] = "DATE_FORMAT(ligneliberations.dateliberation,'%Y-%m-%d')='$rsearch2'";
        $sqlparam["where"][] = "liberations.refemploye=$rsearch1";
        if (!$prm4vide) {
            //$sqlparam["where"][] = sprintf("liberations.typeoperation<>%d", $rsearch4);
        }
        //$sqlparam["debug"] = 2;

        $resultat = executerRequeteSql($sqlparam);

        if (is_array($resultat) && count($resultat) > 0) {
            unset($tablovide);
            $tablovide = true;

            unset($somme_temps);
            $somme_temps = 0;

            foreach ($resultat as $clef => $valeur) {
                $somme_temps += floatval($valeur["calculduree"]);
            }
            //
            if ($somme_temps <= 0) {
                //S'il y a du temps à ajouter, donc une demande, c'est autoriser,
                //sinon pour une annulation, c'est refuser.
                if ($rsearch4 == "2") {
                    if (!$prm3vide) {
                        print "occupedatearticle_annulation";
                    } else {
                        print "occupedate_annulation";
                    }
                } else {
                    print "accepte";
                }
            } elseif ($somme_temps > 0) {
                //S'il y a du temps à retrancher, donc une annulation, c'est autoriser,
                //sinon pour une demande, c'est refuser.
                if (isset($rsearch4) && $rsearch4 == "1") {
                    if (!$prm3vide) {
                        print "occupedatearticle_demande";
                    } else {
                        print "occupedate_demande";
                    }
                } else {
                    print "accepte";
                }
            }
        } else {
            if ($rsearch4 == "2") {
                print "refus_annulation";
            } else {
                print "accepte";
            }
        }
    }
}