<?php

//the global file operation
include("../public/impOpreation.php");

//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
// GOES HERE ....................
include("../public/include_dao.php");

//Employeeattendancesystem
require_once('../models/dao/EmployeeattendancesystemDAO.class.php');
require_once('../models/dto/Employeeattendancesystem.class.php');
require_once('../models/mysql/EmployeeattendancesystemMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeattendancesystemMySqlExtDAO.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 = $_GET['do'];

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

/* ======================
  Controller Name :-storeCTRL
  ======================== */


//here the global templates
$smarty->display("header.html");



//Employeeattendancesystem
$employeeAttendanceSystem = new Employeeattendancesystem();
$employeeAttendanceSystemDAO = new EmployeeattendancesystemMySqlDAO();
$employeeAttendanceSystemEX = new EmployeeattendancesystemMySqlExtDAO();

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

$timeNow = date('H:i:s');
$timeNowsum8 = date("H:i:s", strtotime('+8 hours'));

$smarty->assign('timeNow', "08:00:00");
$smarty->assign('timeNowsum8', "16:00:00");

//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");
  $week = array("7"=>"السبت","1"=>"الاحد", "2"=>"الاثنين", "3"=>"الثلاثاء", "4"=>"الاربعاء", "5"=>"الخميس", "6"=>"الجمعه");
  $smarty->assign("week", $week);
    //here the smarty templates
    $smarty->display("employeeAttendanceSystemsview/add.html");
} elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");

    $showData = $employeeAttendanceSystemDAO->queryByDel(0);
    $smarty->assign("showData", $showData);

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

    $smarty->display("employeeAttendanceSystemsview/show.html");

    $smarty->assign("customCheck", 1);
} elseif ($do == "delete") {
    include_once("../public/authentication.php");

    // here the function that do the action

    try {
        $id = (int) $_GET['id'];
        $employeeAttendanceSystemDAO->delete($id);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    //here the permssion check
    include_once("../public/authentication.php");
    $id = (int) $_GET['id'];
    $loadData = $employeeAttendanceSystemDAO->load($id);
    $smarty->assign("loadData", $loadData);

   $employeeattendancesystemweek = R::findAll('employeeattendancesystemweek','employeeattendancesystem_id = ? and employee_id = 0',[$id]);
   $smarty->assign("employeeattendancesystemweek", $employeeattendancesystemweek);

    $smarty->display("employeeAttendanceSystemsview/edit.html");
}



//edit  update
elseif ($do == "update") {

    //here the permssion check
    include_once("../public/authentication.php");
    //
    try {
        // here the function that do the action
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} 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
$smarty->display("footer.html");

/* ===============================
  function in this CONTROLLER
  ================================ */

// add in Storerawmaterial tbl
function add() {
    //to use the variable out side the funcion
    global $employeeAttendanceSystem;
    global $employeeAttendanceSystemDAO;

    //add in tbl
    $employeeAttendanceSystem->name = filter_input(INPUT_POST, 'name');
    $employeeAttendanceSystem->numberOfMinutesOfDelayAllowed = (int) filter_input(INPUT_POST, 'numberOfMinutesOfDelayAllowed');
    $employeeAttendanceSystem->takeVacationDays = (int) filter_input(INPUT_POST, 'takeVacationDays');
    $employeeAttendanceSystem->halfHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithPermissionDisount');
    $employeeAttendanceSystem->hourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithPermissionDisount');
    $employeeAttendanceSystem->hourAndHalfLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithPermissionDisount');
    $employeeAttendanceSystem->twoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithPermissionDisount');
    $employeeAttendanceSystem->moreThanTwoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithPermissionDisount');
    $employeeAttendanceSystem->halfHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithoutPermissionDisount');
    $employeeAttendanceSystem->hourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithoutPermissionDisount');
    $employeeAttendanceSystem->hourAndHalfLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithoutPermissionDisount');
    $employeeAttendanceSystem->twoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithoutPermissionDisount');
    $employeeAttendanceSystem->moreThanTwoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithoutPermissionDisount');
    $employeeAttendanceSystem->halfHourPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourPermissionDisount');
    $employeeAttendanceSystem->hourPermissionDisount = (float) filter_input(INPUT_POST, 'hourPermissionDisount');
    $employeeAttendanceSystem->hourAndHalfPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfPermissionDisount');
    $employeeAttendanceSystem->twoHoursPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursPermissionDisount');
    $employeeAttendanceSystem->dayAbsenceWithPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithPermissionDisount');
    $employeeAttendanceSystem->dayAbsenceWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithoutPermissionDisount');
    $employeeAttendanceSystem->quarterHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithoutPermissionDisount');
    $employeeAttendanceSystem->quarterHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithPermissionDisount');
    $employeeAttendanceSystem->numberOfCountsOfDelayAllowed = filter_input(INPUT_POST, 'numberOfCountsOfDelayAllowed');


    $employeeAttendanceSystem->userid = $_SESSION["userid"];
    $employeeAttendanceSystem->del = 0;
    $employeeAttendanceSystem->sysdate = date("Y-m-d H:i:s");
    $employeeAttendanceid = $employeeAttendanceSystemDAO->insert($employeeAttendanceSystem);
    for ($i = 1; $i < 8; $i++) {
        $attendancedaynum = (int)filter_input(INPUT_POST, 'attendancedaynum_' . $i);
        $attendancedayar = filter_input(INPUT_POST, 'attendancedayar_' . $i);
        $attendancetime = filter_input(INPUT_POST, 'attendancetime_' . $i);
        $departuretime = filter_input(INPUT_POST, 'departuretime_' . $i);
        $chosseday = filter_input(INPUT_POST, 'chosseday_' . $i);
        $week = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");


       $employeeattendancesystemweek = R::dispense('employeeattendancesystemweek');
       $employeeattendancesystemweek->attendancedaynum = $attendancedaynum;
       $employeeattendancesystemweek->attendancedayar = $attendancedayar;
       $employeeattendancesystemweek->attendancedayen = $week[$attendancedaynum - 1];
       $employeeattendancesystemweek->attendancetime = $attendancetime;
       $employeeattendancesystemweek->departuretime = $departuretime;
       $employeeattendancesystemweek->employeeattendancesystem_id = $employeeAttendanceid;
       $employeeattendancesystemweek->chosseday = $chosseday;

       $employeeattendancesystemweek->employee_id = 0;
        R::store($employeeattendancesystemweek);
    }


}

function update() {
    //to use the variable out side the funcion
    global $employeeAttendanceSystem;
    global $employeeAttendanceSystemDAO;

    $id = (int) filter_input(INPUT_POST, 'id');
    $employeeAttendanceSystem = $employeeAttendanceSystemDAO->load($id);
    $employeeAttendanceSystem->numberOfMinutesOfDelayAllowed = (int) filter_input(INPUT_POST, 'numberOfMinutesOfDelayAllowed');
    $employeeAttendanceSystem->takeVacationDays = (int) filter_input(INPUT_POST, 'takeVacationDays');
    $employeeAttendanceSystem->halfHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithPermissionDisount');
    $employeeAttendanceSystem->hourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithPermissionDisount');
    $employeeAttendanceSystem->hourAndHalfLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithPermissionDisount');
    $employeeAttendanceSystem->twoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithPermissionDisount');
    $employeeAttendanceSystem->moreThanTwoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithPermissionDisount');
    $employeeAttendanceSystem->halfHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithoutPermissionDisount');
    $employeeAttendanceSystem->hourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithoutPermissionDisount');
    $employeeAttendanceSystem->hourAndHalfLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithoutPermissionDisount');
    $employeeAttendanceSystem->twoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithoutPermissionDisount');
    $employeeAttendanceSystem->moreThanTwoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithoutPermissionDisount');
    $employeeAttendanceSystem->halfHourPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourPermissionDisount');
    $employeeAttendanceSystem->hourPermissionDisount = (float) filter_input(INPUT_POST, 'hourPermissionDisount');
    $employeeAttendanceSystem->hourAndHalfPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfPermissionDisount');
    $employeeAttendanceSystem->twoHoursPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursPermissionDisount');
    $employeeAttendanceSystem->dayAbsenceWithPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithPermissionDisount');
    $employeeAttendanceSystem->dayAbsenceWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithoutPermissionDisount');

      $employeeAttendanceSystem->quarterHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithoutPermissionDisount');


      $employeeAttendanceSystem->quarterHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithPermissionDisount');
 $employeeAttendanceSystem->numberOfCountsOfDelayAllowed = filter_input(INPUT_POST, 'numberOfCountsOfDelayAllowed');

    $employeeAttendanceSystem->userid = $_SESSION["userid"];
    $employeeAttendanceSystem->del = 0;
    $employeeAttendanceSystem->sysdate = date("Y-m-d H:i:s");

    $employeeAttendanceSystemDAO->update($employeeAttendanceSystem);


     for ($i = 1; $i < 8; $i++) {
        $employeeattendancesystemweekid = (int)filter_input(INPUT_POST, 'employeeattendancesystemweekid_' . $i);
        $attendancetime = filter_input(INPUT_POST, 'attendancetime_' . $i);
        $departuretime = filter_input(INPUT_POST, 'departuretime_' . $i);
        $chosseday = filter_input(INPUT_POST, 'chosseday_' . $i);

       $employeeattendancesystemweek = R::load('employeeattendancesystemweek',$employeeattendancesystemweekid);
       $employeeattendancesystemweek->attendancetime = $attendancetime;
       $employeeattendancesystemweek->departuretime = $departuretime;
       $employeeattendancesystemweek->chosseday = $chosseday;
       R::store($employeeattendancesystemweek);
    }
}

?>