<?php

// كنترول الموظفين
//
//the global file operation
include("../public/impOpreation.php");

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

require_once('../models/dao/EmployeeDAO.class.php');
require_once('../models/dto/Employee.class.php');
require_once('../models/mysql/EmployeeMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeMySqlExtDAO.class.php');

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');


$do = $_GET['do'];

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

$smarty->display("header.html");


//create object  from employee
$employeeDAO = new EmployeeMySqlDAO();
$employee = new Employee();
$employeeEX = new EmployeeMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();


//create object from user
//dispaly  add form
if (empty($do)) {
    include_once("../public/authentication.php");

    $queryString = '';
    if ($_SESSION['branchId'] > 0)
        $queryString = ' AND  branchid = ' . $_SESSION['branchId'];

    $allemployee = $employeeEX->queryAllSimple($queryString);
    $smarty->assign('allemployee', $allemployee);


    $startDate = date('Y-m-d');
    $endDate = date('Y-m-d');

    $Programsetting = $ProgramsettingDAO->load(1);
    if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
        $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
        $endDate = date('Y-m-d', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
        $startDate = date('Y-m-d', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
    } else {
        $endDate = $endDate . ' 23:59:59';
        $startDate = $startDate . " 00:00:00";
    }

    $queryString = '';
    if ($_SESSION['branchId'] > 0)
        $queryString .= ' AND  branchid = ' . $_SESSION['branchId'];

    $employes = $employeeEX->queryAllString($startDate, $endDate,$queryString);
    $smarty->assign('employes', $employes);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("absentReportview/show.html"); //the template page
} //success
elseif ($do == "show") {

    $queryString = '';
    if ($_SESSION['branchId'] > 0)
        $queryString = ' AND  branchid = ' . $_SESSION['branchId'];

    $allemployee = $employeeEX->queryAllSimple($queryString);
    $smarty->assign('allemployee', $allemployee);

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $employeeId = $_REQUEST['chooseEmp'];
    $queryString = '';

    if (!empty($startDate) && !empty($endDate)) {

        $Programsetting = $ProgramsettingDAO->load(1);


        if (isset($Programsettingdata->reportsPlusHours) && !empty($Programsettingdata->reportsPlusHours)) {
            $reportsPlusHours = $Programsettingdata->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $endDate = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
            $startDate = date('Y-m-d H:i:s', strtotime('+' . $Programsettingdata->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
            //
        } else {
            $endDate = $endDate . ' 23:59:59';
            $startDate = $startDate . " 00:00:00";
            //
        }

        $queryString .= ' AND employeeDate >= "' . $startDate . '" AND employeeDate <= "' . $endDate . '" ';
    }


    if (!empty($employeeId)) {
        $queryString .= ' AND employee.employeeId =' . $employeeId;
    }

    if ($_SESSION['branchId'] > 0)
        $queryString .= ' AND  branchid = ' . $_SESSION['branchId'];

    $allemployee = $employeeEX->queryAllSimple($queryString);

    $employes = $employeeEX->queryAllReportAbsent($queryString);
    $smarty->assign('employes', $employes);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("absentReportview/show.html");
    $smarty->assign("customValidation", 1);
    //the template page
} elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}
$smarty->assign("customValidation", 1);
$smarty->assign("employeeendday", 1);
$smarty->display("footer.html");
