<?php
$do = isset($_GET['do']) ? $_GET['do'] : '';
$ajaxDoArr = array('addTime');
if (!in_array($do, $ajaxDoArr)) {
    //the global file operation
    include("../public/impOpreation.php");
}
include_once("../library/num_to_ar.php");
// get the config file
include_once("../public/config.php");
include_once("dailyentryfun.php");

//here the db files that include in the file
include("../public/include_dao.php");
include("../library/uploadImages.php");
include '../library/Classes/PHPExcel/IOFactory.php';

//Employeeclosedayhistory
require_once('../models/dao/EmployeeclosedayhistoryDAO.class.php');
require_once('../models/dto/Employeeclosedayhistory.class.php');
require_once('../models/mysql/EmployeeclosedayhistoryMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeclosedayhistoryMySqlExtDAO.class.php');

require_once('../models/dao/EmployeeattendanceDAO.class.php');
require_once('../models/dto/Employeeattendance.class.php');
require_once('../models/mysql/EmployeeattendanceMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeattendanceMySqlExtDAO.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');

//get the do the action
$do = isset($_GET['do']) ? $_GET['do'] : '';

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

/* ======================

  Controller Name :- EmployeeAttendanceExcelController

  OPERTATION in Controller

  1- display add form
  2- add employee attendance in employee personnel tbl
  ======================== */

//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    $smarty->display("header.html");
}
//here goes the instances and general 
$employee = new Employee();
$employeeDAO = new EmployeeMySqlDAO();
$employeeEX = new EmployeeMySqlExtDAO();
//creat object from employeePersonal
$EmployeepersonnelDAO = new EmployeepersonnelMySqlDAO();
$Employeepersonnel = new Employeepersonnel();
$EmployeepersonnelEX = new EmployeepersonnelMySqlExtDAO();

//Employeeclosedayhistory
$employeeCloseDayHistory = new Employeeclosedayhistory();
$employeeCloseDayHistoryDAO = new EmployeeclosedayhistoryMySqlDAO();
$employeeCloseDayHistoryEX = new EmployeeclosedayhistoryMySqlExtDAO();

$employeeAttendance = new Employeeattendance();
$employeeAttendanceDAO = new EmployeeattendanceMySqlDAO();
$employeeAttendanceEX = new EmployeeattendanceMySqlExtDAO();

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

$myUserRecord = new UserMySqlDAO();

$userr = $myUserRecord->load($_SESSION['userid']);
$username = $userr->username;

if (empty($do) || $do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");
    $date = filter_input(INPUT_POST, 'date');
    if (!isset($date))
        $date = date('Y-m-d');

    $smarty->assign('date', $date);

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

    // $employees = $employeeDAO->queryByConditions(0);

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

    $employees = $employeeEX->queryAllemployeebycondation(0, $queryStringAll);

    foreach ($employees as $emp) {

        if ($emp->employeeId > 0) {
            $queryString = '';

            if ($_SESSION['branchId'] > 0)
                $queryString .= ' AND  branchid = ' . $_SESSION['branchId'];
            if ($date != '') {
                $queryString .= " and date(employeeattendance.sysdate) = '" . $date . "' ";
            }

            if ($queryString == '') {
                $queryString = " and date(sysdate) = '" . date('Y-m-d') . "' ";
            }

            $queryString .= " and empid = $emp->employeeId ";
        }
        $employeeAttendanceData = $employeeAttendanceEX->queryByQueryString($queryString);
        if ($employeeAttendanceData) {
            $employeeClosedHistoryData = $employeeCloseDayHistoryEX->getEmployeeHistoryByQueryString(" and employeeid = " . $emp->employeeId . " and date(day) = '" . $date . "'");
            if (count($employeeAttendanceData) < 2) {
                if ($employeeClosedHistoryData[0]->attendanceTime == "00:00:00")
                    $employeeClosedHistoryData[0]->attendanceTime = '';
                if ($employeeClosedHistoryData[0]->departureTime == "00:00:00")
                    $employeeClosedHistoryData[0]->departureTime = '';
            }
            $emp->attendanceData = $employeeClosedHistoryData;
        }
    }
    // $smarty->assign('employeeAttendanceData', $employeeAttendanceData);
    $smarty->assign('employees', $employees);
    $timeNow = date('H:i:s');
    $smarty->assign('timeNow', $timeNow);

    //here the smarty templates
    $smarty->display("employee_attendance_view/add.html");
} elseif ($do == "addTime") {
    //Transaction
    try {
        $empid = (int) filter_input(INPUT_POST, 'empid');
        $day = filter_input(INPUT_POST, 'date');
        $time = filter_input(INPUT_POST, 'value');
        $type = filter_input(INPUT_POST, 'type');
        $attend_time = $depart_time = '';

        //with this step make sure any employee exist and not deleted will be in table
        if ((int) $employeeCloseDayHistoryEX->dayAttendanceCount($day) == 0) {
            $employeeCloseDayHistoryEX->beginDayAttendance($day, date('Y-m-d H:i:s'), $_SESSION['userid']);
        }

        $employeeAttendanceData = $employeeAttendanceEX->queryByQueryString(" and empid = " . $empid . " and date(sysdate) = '" . $day . "'");
        if (count($employeeAttendanceData) < 2) {
            $employeeAttendance->empid = $empid;
            $employeeAttendance->theImage = '';
            $employeeAttendance->userid = $_SESSION['userid'];
            $employeeAttendance->sysdate = date_format(date_create($day . ' ' . $time), 'Y-m-d H:i:s'); // date('Y-m-d H:i:s');
            $employeeAttendance->fingerid = '';
            $employeeAttendance->rfid = '';
            $employeeAttendance->accessType = 0;
            $employeeAttendance->syncToServer = 0;
            $id = $employeeAttendanceDAO->insert($employeeAttendance);
        }

        $employee = $employeeDAO->load($empid);
        $row = $employeeCloseDayHistoryEX->getEmployeeHistoryByQueryString(" and employeeid=$empid and day='" . $day . "' and del = 0");
        if (count($row) > 0) {
            //2- update dataof attendance or departure in employeeCloseDayHistory table
            $employeeCloseDayHistory = $row[0];
            if ($type == "attend") {
                $employeeCloseDayHistory->attendanceTime = $time;
            } else {
                $employeeCloseDayHistory->departureTime = $time;
            }
            $employeeCloseDayHistory->isAbsent = 0;
            $employeeCloseDayHistory->absentHasPermission = 0;
            $employeeCloseDayHistory->absentDiscount = 0;
            $employeeCloseDayHistory->userid = $_SESSION['userid'];
            $employeeCloseDayHistory->sysdate = date('Y-m-d H:i:s');
            $employeeCloseDayHistoryDAO->update($employeeCloseDayHistory);
        }
        // send data
        echo 1;
    } catch (Exception $e) {
        echo -1;
    }
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
} ##
$smarty->assign("customValidation", 1);
//here the global templates
if (!in_array($do, $ajaxDoArr)) {
    $smarty->display("footer.html");
}
/* ===============================
  function in this CONTROLLER
  ================================ */
