<?php

// كنترول الموظفين
//
//the global file operation
include("../public/impOpreation.php");
include_once("initiateStaticSessionCommingWithCurl.php");
include_once("dailyentryfun.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");
include("../library/uploadImages.php");

require_once('../models/sql/Connection.class.php');
require_once('../models/sql/ConnectionFactory.class.php');
require_once('../models/sql/ConnectionProperty.class.php');
require_once('../models/sql/QueryExecutor.class.php');
require_once('../models/sql/Transaction.class.php');
require_once('../models/sql/SqlQuery.class.php');
require_once('../models/core/ArrayList.class.php');
require_once('../models/dao/DAOFactory.class.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/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');

//Employeegroup
require_once('../models/dao/EmployeegroupDAO.class.php');
require_once('../models/dto/Employeegroup.class.php');
require_once('../models/mysql/EmployeegroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeegroupMySqlExtDAO.class.php');
//Employeesubgroup
require_once('../models/dao/EmployeesubgroupDAO.class.php');
require_once('../models/dto/Employeesubgroup.class.php');
require_once('../models/mysql/EmployeesubgroupMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeesubgroupMySqlExtDAO.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'];
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

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


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


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

$country = new Country();
$countryDAO = new CountryMySqlDAO();
$countryEX = new CountryMySqlExtDAO();

$religion = new Religion();
$religionDAO = new ReligionsMySqlDAO();
$religionEX = new ReligionsMySqlExtDAO();

//user
$myUser = new User();
$myUserRecord = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();

//Employeegroup
$employeeGroup = new Employeegroup();
$employeeGroupDAO = new EmployeegroupMySqlDAO();
$employeeGroupEX = new EmployeegroupMySqlExtDAO();
//Employeesubgroup
$employeeSubGroup = new Employeesubgroup();
$employeeSubGroupDAO = new EmployeesubgroupMySqlDAO();
$employeeSubGroupEX = new EmployeesubgroupMySqlExtDAO();

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

//Branch
$Branch = new Branch();
$BranchDAO = new BranchMySqlDAO();
$BranchEX = new BranchMySqlExtDAO();

$timeNow = date('H:i');
$smarty->assign('timeNow', $timeNow);
//create object from user
//dispaly  add form
if (!isset($_GET['do'])) {
    include_once("../public/authentication.php");

    $week = array("7" => "السبت", "1" => "الاحد", "2" => "الاثنين", "3" => "الثلاثاء", "4" => "الاربعاء", "5" => "الخميس", "6" => "الجمعه");
    $smarty->assign("week", $week);

    //    $country = $countryDAO->queryAll();
    //    $religion = $religionDAO->queryAll();

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

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

    $employeeGroup = $employeeGroupDAO->queryByDel(0);
    $smarty->assign('employeeGroup', $employeeGroup);
    //    $smarty->assign('country', $country);
    //    $smarty->assign('religion', $religion);

    $allbranchdata = $BranchDAO->queryAll();
    $smarty->assign("allbranchdata", $allbranchdata);

    $smarty->display("employeeview/add.html"); //the template page
}

//save employee in db
elseif ($_GET['do'] == "add") {
    include_once("../public/authentication.php");
    try {
        $empid = add();
        if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {
            echo $empid;
        } else {
            header("location:?do=sucess");
        }
    } catch (Exception $e) {
        print_r('<pre>');
        print_r($e);
        print_r('<pre>');

        header("location:?do=error");
    }
}

//show all client type
elseif ($_GET['do'] == "show") {
    include_once("../public/authentication.php");

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

    $name = $_REQUEST['txtSearch'];

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

    if ($name != '-1' && isset($name)) {

        //get the selected data with limitation
        // $allemployee = $employeeEX->queryAllwithtname($name);
        $allemployee = $employeeEX->queryAllwithtnameAndQuerystring($name, $queryString);
        // assign your db results to the template

        $smarty->assign('allemployee', $allemployee);
        // assign {$paginate} var


        $smarty->display('employeeview/show.html');
    } else {
        /* ====================================== */
        $queryString1 = ' where 1 ' . $queryString;
        $allemployee = $employeeEX->queryAllemployee($queryString1);

        $smarty->assign('allemployee', $allemployee);
        // assign {$paginate} var


        $smarty->display('employeeview/show.html');
    }
}
//delete store
elseif ($_GET['do'] == "delete") {



    include_once("../public/authentication.php");

    try {
        delete();

        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
} elseif ($_GET['do'] == "edit") {
    $id = $_GET["id"];

    $employeeData = $employeeDAO->load($id);

    //employee doesn't have the same branch as user
    if ($_SESSION['branchId'] != 0 && $_SESSION['branchId'] != $employeeData->branchid)
        header("location:?do=error");

    $smarty->assign("employeeData", $employeeData);
    $employeeattendancesystemweek = R::findAll('employeeattendancesystemweek', 'employee_id = ?', [$id]);
    $smarty->assign("employeeattendancesystemweek", $employeeattendancesystemweek);
    $country = $countryDAO->queryAll();
    $religion = $religionDAO->queryAll();

    $smarty->assign('country', $country);
    $smarty->assign('religion', $religion);

    $employeeGroup = $employeeGroupDAO->queryByDel(0);
    $smarty->assign('employeeGroup', $employeeGroup);
    $employeeSubGroup = $employeeSubGroupDAO->load($employeeData->employeesubgroupid);
    $smarty->assign('employeeSubGroup', $employeeSubGroup);
    //print_r($employeeData);

    $allbranchdata = $BranchDAO->queryAll();
    $smarty->assign("allbranchdata", $allbranchdata);

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

//save edit data
elseif ($_GET['do'] == "update") {

    include_once("../public/authentication.php");
    try {
        update();

        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
} elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}
$smarty->assign("customValidation", 1);
if (isset($_POST['curlpost']) && $_POST['curlpost'] == 1) {

} else {
    $smarty->display("footer.html");
}

function add() {
    global $employeeDAO;
    global $employee;

    $username = filter_input(INPUT_POST, "username");
    $employeeName = filter_input(INPUT_POST, "empName");
    $employeeBOD = filter_input(INPUT_POST, "empDOF");
    $employeeBouns = filter_input(INPUT_POST, "empBouns");
    $employeeSalary = filter_input(INPUT_POST, "empSalary");
    $employeePhone = filter_input(INPUT_POST, "empTel");
    $employeeAddress = filter_input(INPUT_POST, "empAddress");
    $isworker = (int) filter_input(INPUT_POST, "isworker");
    $Hourlyrate = filter_input(INPUT_POST, "Hourlyrate");

    $nationality = filter_input(INPUT_POST, "nationality");
    $religion = filter_input(INPUT_POST, "religion");

    $houseAllowance = filter_input(INPUT_POST, "houseAllowance");
    $transportationAllowance = filter_input(INPUT_POST, "transportationAllowance");
    $otherAllowance = filter_input(INPUT_POST, "otherAllowance");

    $noOfWorkDaysPerMonth = filter_input(INPUT_POST, "noOfWorkDaysPerMonth");
    $noOfWorkHoursDaysPerDay = filter_input(INPUT_POST, "noOfWorkHoursDaysPerDay");
    $noOfDistountDaysWhenAbsences = filter_input(INPUT_POST, "noOfDistountDaysWhenAbsences");
    $socialInsurance = (float) filter_input(INPUT_POST, "socialInsurance");
    $workTax = (float) filter_input(INPUT_POST, "workTax");
    $empFingerId = filter_input(INPUT_POST, "empFingerId");
    $empFingerId2 = filter_input(INPUT_POST, "empFingerId2");
    $empFingerId3 = filter_input(INPUT_POST, "empFingerId3");
    $empFingerId4 = filter_input(INPUT_POST, "empFingerId4");
    $empFingerId5 = filter_input(INPUT_POST, "empFingerId5");
    $empRFID = filter_input(INPUT_POST, "empRFID");

    $empCode = filter_input(INPUT_POST, "empCode");
    $worthExtra = filter_input(INPUT_POST, "worthExtra");
    $administration = filter_input(INPUT_POST, "administration");
    $subManagement = filter_input(INPUT_POST, "subManagement");
    $directManager = filter_input(INPUT_POST, "directManager");
    $occupation = filter_input(INPUT_POST, "occupation");
    $dateBeing = filter_input(INPUT_POST, "dateBeing");
    $gender = filter_input(INPUT_POST, "gender");
    $socialSituation = filter_input(INPUT_POST, "socialSituation");
    $bankAccount = filter_input(INPUT_POST, "bankAccount");
    $dcholarship = filter_input(INPUT_POST, "dcholarship");
    $branchid = filter_input(INPUT_POST, "branchid");

    $today = date("Y-m-d h:i:s A");


    $handle = new upload($_FILES['contract']);
    $contract = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['residence']);
    $residence = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['personalPhoto']);
    $personalPhoto = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['healthCard']);
    $healthCard = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['visa']);
    $visa = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['passport']);
    $passport = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['graduate']);
    $graduate = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['expert1']);
    $expert1 = uploadImages($handle, "../views/default/images/employee", 300, 300);


    $handle = new upload($_FILES['expert2']);
    $expert2 = uploadImages($handle, "../views/default/images/employee", 300, 300);

    $handle = new upload($_FILES['expert3']);
    $expert3 = uploadImages($handle, "../views/default/images/employee", 300, 300);



    $handle = new upload($_FILES['birthCertificate']);
    $birthCertificate = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->birthCertificate = $birthCertificate;

    $handle = new upload($_FILES['armyPhoto']);
    $armyPhoto = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->armyPhoto = $armyPhoto;

    $handle = new upload($_FILES['idCard']);
    $idCard = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->idCard = $idCard;

    $handle = new upload($_FILES['driverLicense']);
    $driverLicense = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->driverLicense = $driverLicense;

    $handle = new upload($_FILES['endorsementPeace']);
    $endorsementPeace = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->endorsementPeace = $endorsementPeace;

    $handle = new upload($_FILES['confidentialityDeclaration']);
    $confidentialityDeclaration = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->confidentialityDeclaration = $confidentialityDeclaration;

    $handle = new upload($_FILES['criminalCamouflage']);
    $criminalCamouflage = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->criminalCamouflage = $criminalCamouflage;

    $handle = new upload($_FILES['practiceProfession']);
    $practiceProfession = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->practiceProfession = $practiceProfession;

    $handle = new upload($_FILES['socialStatus']);
    $socialStatus = uploadImages($handle, "../views/default/images/employee", 300, 300);
    $employee->socialStatus = $socialStatus;


    $employee->Hourlyrate = $Hourlyrate;
    $employee->isworker = $isworker;

    $employee->sysdate = $today;
    $employee->employeeDate = $today;
    $employee->conditions = 0;
    $employee->employeeAddress = $employeeAddress;
    $employee->employeeBOD = $employeeBOD;
    $employee->employeeBouns = $employeeBouns;
    $employee->employeeName = $employeeName;
    $employee->employeeSalary = $employeeSalary;
    $employee->employeePhone = $employeePhone;
    $employee->empFingerId = $empFingerId;
    $employee->empFingerId2 = $empFingerId2;
    $employee->empFingerId3 = $empFingerId3;
    $employee->empFingerId4 = $empFingerId4;
    $employee->empFingerId5 = $empFingerId5;
    $employee->empRFID = $empRFID;

    $employee->userId = $_SESSION['userid'];
    $employee->username = $username;


    $employee->hourlyrate = $Hourlyrate;

    $employee->nationality = $nationality;
    $employee->religion = $religion;

    $employee->houseAllowance = $houseAllowance;
    $employee->transportationAllowance = $transportationAllowance;
    $employee->otherAllowance = $otherAllowance;

    $employee->noOfWorkDaysPerMonth = $noOfWorkDaysPerMonth;
    $employee->noOfWorkHoursDaysPerDay = $noOfWorkHoursDaysPerDay;
    $employee->noOfDistountDaysWhenAbsences = $noOfDistountDaysWhenAbsences;
    $employee->socialInsurance = $socialInsurance;
    $employee->workTax = $workTax;

    $employee->contract = $contract;
    $employee->residence = $residence;
    $employee->personalPhoto = $personalPhoto;
    $employee->healthCard = $healthCard;
    $employee->visa = $visa;
    $employee->passport = $passport;
    $employee->graduate = $graduate;
    $employee->expert1 = $expert1;
    $employee->expert2 = $expert2;
    $employee->expert3 = $expert3;

    $employee->empCode = $empCode;
    $employee->worthExtra = $worthExtra;
    $employee->administration = $administration;
    $employee->subManagement = $subManagement;
    $employee->directManager = $directManager;
    $employee->occupation = $occupation;
    $employee->dateBeing = $dateBeing;
    $employee->gender = $gender;
    $employee->socialSituation = $socialSituation;
    $employee->bankAccount = $bankAccount;
    $employee->dcholarship = $dcholarship;
    $employee->branchid = $branchid;

    if (empty($employee->employeeSalary)) {
        $employee->employeeSalary = 0;
    }
    if (empty($employee->houseAllowance)) {
        $employee->houseAllowance = 0;
    }
    if (empty($employee->transportationAllowance)) {
        $employee->transportationAllowance = 0;
    }
    if (empty($employee->noOfWorkDaysPerMonth)) {
        $employee->noOfWorkDaysPerMonth = 0;
    }
    if (empty($employee->noOfWorkHoursDaysPerDay)) {
        $employee->noOfWorkHoursDaysPerDay = 0;
    }
    if (empty($employee->noOfDistountDaysWhenAbsences)) {
        $employee->noOfDistountDaysWhenAbsences = 0;
    }

    if (empty($employee->nationality)) {
        $employee->nationality = 0;
    }
    if (empty($employee->religion)) {
        $employee->religion = 0;
    }

    $bast = $employee->employeeSalary + $employee->houseAllowance + $employee->transportationAllowance;
    $makam = $employee->noOfWorkDaysPerMonth * $employee->noOfWorkHoursDaysPerDay * 60;
    if ($makam != 0) {
        $employee->minuteLateCost = round(($bast / $makam), 3);
    }

    $makam = $employee->noOfWorkDaysPerMonth * $employee->noOfDistountDaysWhenAbsences;
    if ($makam != 0) {
        $employee->dayAbsencesCost = round(($bast / $makam), 3);
    }

    $employee->employeegroupid = (float) filter_input(INPUT_POST, 'employeegroupid');
    $employee->employeesubgroupid = (float) filter_input(INPUT_POST, 'employeesubgroupid');
    $employee->numberOfMinutesOfDelayAllowed = (float) filter_input(INPUT_POST, 'numberOfMinutesOfDelayAllowed');
    $employee->takeVacationDays = (float) filter_input(INPUT_POST, 'takeVacationDays');
    $employee->halfHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithPermissionDisount');
    $employee->quarterHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithPermissionDisount');


    $employee->hourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithPermissionDisount');
    $employee->hourAndHalfLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithPermissionDisount');
    $employee->twoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithPermissionDisount');
    $employee->moreThanTwoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithPermissionDisount');
    $employee->halfHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithoutPermissionDisount');
    $employee->quarterHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithoutPermissionDisount');

    $employee->hourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithoutPermissionDisount');
    $employee->hourAndHalfLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithoutPermissionDisount');
    $employee->twoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithoutPermissionDisount');
    $employee->moreThanTwoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithoutPermissionDisount');
    $employee->halfHourPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourPermissionDisount');
    $employee->hourPermissionDisount = (float) filter_input(INPUT_POST, 'hourPermissionDisount');
    $employee->hourAndHalfPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfPermissionDisount');
    $employee->twoHoursPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursPermissionDisount');
    $employee->dayAbsenceWithPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithPermissionDisount');
    $employee->dayAbsenceWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithoutPermissionDisount');
    $employee->numberOfCountsOfDelayAllowed = filter_input(INPUT_POST, 'numberOfCountsOfDelayAllowed');

    $treeId1 = addTreeElement("سلفة $employee->employeeName", 63, 1, 0, 1);
    $treeId2 = addTreeElement($employee->employeeName, 97, 1, 0, 1);
    $employee->treeId = $treeId1;
    $employee->treeId2 = $treeId2;
    $id = $employeeDAO->insert($employee);



    for ($i = 1; $i < 8; $i++) {
        $employeeattendancesystemweek_id = (int) filter_input(INPUT_POST, 'employeeattendancesystemweek_id_' . $i);
        $attendancedaynum = (int) filter_input(INPUT_POST, 'attendancedaynum_' . $i);
        $attendancedayar = filter_input(INPUT_POST, 'attendancedayar_' . $i);
        $attendancedayen = filter_input(INPUT_POST, 'attendancedayen_' . $i);
        $attendancetime = filter_input(INPUT_POST, 'attendancetime_' . $i);
        $departuretime = filter_input(INPUT_POST, 'departuretime_' . $i);
        $employeeattendancesystem_id = filter_input(INPUT_POST, 'employeeattendancesystem_id_' . $i);
        $chosseday = filter_input(INPUT_POST, 'chosseday_' . $i);



        $employeeattendancesystemweek = R::dispense('employeeattendancesystemweek');
        $employeeattendancesystemweek->employeeattendancesystemweek_id = $employeeattendancesystemweek_id;
        $employeeattendancesystemweek->attendancedaynum = $attendancedaynum;
        $employeeattendancesystemweek->attendancedayar = $attendancedayar;
        $employeeattendancesystemweek->attendancedayen = $attendancedayen;
        $employeeattendancesystemweek->attendancetime = $attendancetime;
        $employeeattendancesystemweek->departuretime = $departuretime;
        $employeeattendancesystemweek->employeeattendancesystem_id = $employeeattendancesystem_id;
        $employeeattendancesystemweek->chosseday = $chosseday;
        $employeeattendancesystemweek->employee_id = $id;
        R::store($employeeattendancesystemweek);
    }
    return $id;
}

function update() {

    global $employeeDAO;
    global $employee;
    global $accountsTreeDAO;

    $conditions = filter_input(INPUT_POST, "conditions");
    $employeeId = filter_input(INPUT_POST, "employeeId");
    $username = filter_input(INPUT_POST, "username");
    $employeeName = filter_input(INPUT_POST, "empName");
    $employeeBOD = filter_input(INPUT_POST, "empDOF");
    $employeeBouns = filter_input(INPUT_POST, "empBouns");
    $employeeSalary = filter_input(INPUT_POST, "empSalary");
    $employeePhone = filter_input(INPUT_POST, "empTel");
    $employeeAddress = filter_input(INPUT_POST, "empAddress");

    $isworker = (int) filter_input(INPUT_POST, "isworker");
    $Hourlyrate = filter_input(INPUT_POST, "Hourlyrate");

    $nationality = filter_input(INPUT_POST, "nationality");
    $religion = filter_input(INPUT_POST, "religion");

    $houseAllowance = filter_input(INPUT_POST, "houseAllowance");
    $transportationAllowance = filter_input(INPUT_POST, "transportationAllowance");
    $otherAllowance = filter_input(INPUT_POST, "otherAllowance");

    $noOfWorkDaysPerMonth = filter_input(INPUT_POST, "noOfWorkDaysPerMonth");
    $noOfWorkHoursDaysPerDay = filter_input(INPUT_POST, "noOfWorkHoursDaysPerDay");
    $noOfDistountDaysWhenAbsences = filter_input(INPUT_POST, "noOfDistountDaysWhenAbsences");
    $socialInsurance = (float) filter_input(INPUT_POST, "socialInsurance");
    $workTax = (float) filter_input(INPUT_POST, "workTax");
    $empFingerId = filter_input(INPUT_POST, "empFingerId");
    $empFingerId2 = filter_input(INPUT_POST, "empFingerId2");
    $empFingerId3 = filter_input(INPUT_POST, "empFingerId3");
    $empFingerId4 = filter_input(INPUT_POST, "empFingerId4");
    $empFingerId5 = filter_input(INPUT_POST, "empFingerId5");
    $empRFID = filter_input(INPUT_POST, "empRFID");



    $empCode = filter_input(INPUT_POST, "empCode");
    $worthExtra = filter_input(INPUT_POST, "worthExtra");
    $administration = filter_input(INPUT_POST, "administration");
    $subManagement = filter_input(INPUT_POST, "subManagement");
    $directManager = filter_input(INPUT_POST, "directManager");
    $occupation = filter_input(INPUT_POST, "occupation");
    $dateBeing = filter_input(INPUT_POST, "dateBeing");
    $gender = filter_input(INPUT_POST, "gender");
    $socialSituation = filter_input(INPUT_POST, "socialSituation");
    $bankAccount = filter_input(INPUT_POST, "bankAccount");
    $dcholarship = filter_input(INPUT_POST, "dcholarship");
    $branchid = filter_input(INPUT_POST, "branchid");


    $today = date("Y-m-d");

    $employee = $employeeDAO->load($employeeId);

    $handle = new upload($_FILES['contract']);

    $contract = updateImages($handle, "oldcontract", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->contract);

    $handle = new upload($_FILES['residence']);
    $residence = updateImages($handle, "oldresidence", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->residence);

    $handle = new upload($_FILES['personalPhoto']);
    $personalPhoto = updateImages($handle, "oldpersonalPhoto", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->personalPhoto);

    $handle = new upload($_FILES['healthCard']);
    $healthCard = updateImages($handle, "oldhealthCard", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->healthCard);

    $handle = new upload($_FILES['visa']);
    $visa = updateImages($handle, "oldvisa", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->visa);

    $handle = new upload($_FILES['passport']);
    $passport = updateImages($handle, "oldpassport", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->passport);


    $handle = new upload($_FILES['graduate']);
    $graduate = updateImages($handle, "oldgraduate", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->graduate);

    $handle = new upload($_FILES['expert1']);
    $expert1 = updateImages($handle, "oldexpert1", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->expert1);

    $handle = new upload($_FILES['expert2']);
    $expert2 = updateImages($handle, "oldexpert2", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->expert2);

    $handle = new upload($_FILES['expert3']);
    $expert3 = updateImages($handle, "oldexpert3", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->expert3);







    $handle = new upload($_FILES['birthCertificate']);
    $birthCertificate = updateImages($handle, "oldbirthCertificate", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->birthCertificate);

    $handle = new upload($_FILES['armyPhoto']);
    $armyPhoto = updateImages($handle, "oldarmyPhoto", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->armyPhoto);


    $handle = new upload($_FILES['armyPhoto']);
    $armyPhoto = updateImages($handle, "oldarmyPhoto", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->armyPhoto);


    $handle = new upload($_FILES['idCard']);
    $idCard = updateImages($handle, "oldidCard", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->idCard);

    $handle = new upload($_FILES['driverLicense']);
    $driverLicense = updateImages($handle, "olddriverLicense", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->driverLicense);

    $handle = new upload($_FILES['endorsementPeace']);
    $endorsementPeace = updateImages($handle, "oldendorsementPeace", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->endorsementPeace);

    $handle = new upload($_FILES['confidentialityDeclaration']);
    $confidentialityDeclaration = updateImages($handle, "oldconfidentialityDeclaration", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->confidentialityDeclaration);


    $handle = new upload($_FILES['criminalCamouflage']);
    $criminalCamouflage = updateImages($handle, "oldcriminalCamouflage", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->criminalCamouflage);

    $handle = new upload($_FILES['practiceProfession']);
    $practiceProfession = updateImages($handle, "oldpracticeProfession", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->practiceProfession);


    $handle = new upload($_FILES['socialStatus']);
    $socialStatus = updateImages($handle, "oldsocialStatus", "../views/default/images/employee", 300, 300);
    unlink("../views/default/images/employee" . $employee->socialStatus);







    $employee->Hourlyrate = $Hourlyrate;
    $employee->isworker = $isworker;

    $employee->sysdate = date("Y-m-d H:i:s");
    $employee->employeeDate = $employee->employeeDate;
    $employee->employeeId = $employeeId;
    $employee->conditions = $conditions;
    $employee->employeeAddress = $employeeAddress;
    $employee->employeeBOD = $employeeBOD;
    $employee->employeeBouns = $employeeBouns;
    $employee->employeeName = $employeeName;
    $employee->employeeSalary = $employeeSalary;
    $employee->employeePhone = $employeePhone;
    $employee->empFingerId = $empFingerId;
    $employee->empFingerId2 = $empFingerId2;
    $employee->empFingerId3 = $empFingerId3;
    $employee->empFingerId4 = $empFingerId4;
    $employee->empFingerId5 = $empFingerId5;
    $employee->empRFID = $empRFID;



    $employee->userId = $_SESSION['userid'];
    $employee->username = $username;

    $employee->hourlyrate = $Hourlyrate;

    $employee->nationality = $nationality;
    $employee->religion = $religion;

    $employee->houseAllowance = $houseAllowance;
    $employee->transportationAllowance = $transportationAllowance;
    $employee->otherAllowance = $otherAllowance;

    $employee->noOfWorkDaysPerMonth = $noOfWorkDaysPerMonth;
    $employee->noOfWorkHoursDaysPerDay = $noOfWorkHoursDaysPerDay;
    $employee->noOfDistountDaysWhenAbsences = $noOfDistountDaysWhenAbsences;
    $employee->socialInsurance = $socialInsurance;
    $employee->workTax = $workTax;

    $employee->contract = $contract;
    $employee->residence = $residence;
    $employee->personalPhoto = $personalPhoto;
    $employee->healthCard = $healthCard;
    $employee->visa = $visa;
    $employee->passport = $passport;
    $employee->graduate = $graduate;
    $employee->expert1 = $expert1;
    $employee->expert2 = $expert2;
    $employee->expert3 = $expert3;

    $employee->birthCertificate = $birthCertificate;
    $employee->armyPhoto = $armyPhoto;
    $employee->idCard = $idCard;
    $employee->driverLicense = $driverLicense;
    $employee->endorsementPeace = $endorsementPeace;
    $employee->confidentialityDeclaration = $confidentialityDeclaration;
    $employee->criminalCamouflage = $criminalCamouflage;
    $employee->practiceProfession = $practiceProfession;
    $employee->socialStatus = $socialStatus;




    $employee->empCode = $empCode;
    $employee->worthExtra = $worthExtra;
    $employee->administration = $administration;
    $employee->subManagement = $subManagement;
    $employee->directManager = $directManager;
    $employee->occupation = $occupation;
    $employee->dateBeing = $dateBeing;
    $employee->gender = $gender;
    $employee->socialSituation = $socialSituation;
    $employee->bankAccount = $bankAccount;
    $employee->dcholarship = $dcholarship;
    $employee->branchid = $branchid;



    if (empty($employee->employeeSalary)) {
        $employee->employeeSalary = 0;
    }
    if (empty($employee->houseAllowance)) {
        $employee->houseAllowance = 0;
    }
    if (empty($employee->transportationAllowance)) {
        $employee->transportationAllowance = 0;
    }
    if (empty($employee->noOfWorkDaysPerMonth)) {
        $employee->noOfWorkDaysPerMonth = 0;
    }
    if (empty($employee->noOfWorkHoursDaysPerDay)) {
        $employee->noOfWorkHoursDaysPerDay = 0;
    }
    if (empty($employee->noOfDistountDaysWhenAbsences)) {
        $employee->noOfDistountDaysWhenAbsences = 0;
    }
    if (empty($employee->nationality)) {
        $employee->nationality = 0;
    }
    if (empty($employee->religion)) {
        $employee->religion = 0;
    }

    $bast = $employee->employeeSalary + $employee->houseAllowance + $employee->transportationAllowance;
    $makam = $employee->noOfWorkDaysPerMonth * $employee->noOfWorkHoursDaysPerDay * 60;
    if ($makam != 0) {
        $employee->minuteLateCost = round(($bast / $makam), 3);
    }

    $makam = $employee->noOfWorkDaysPerMonth * $employee->noOfDistountDaysWhenAbsences;
    if ($makam != 0) {
        $employee->dayAbsencesCost = round(($bast / $makam), 3);
    }
    $employee->employeegroupid = (float) filter_input(INPUT_POST, 'employeegroupid');
    $employee->employeesubgroupid = (float) filter_input(INPUT_POST, 'employeesubgroupid');
    $employee->numberOfMinutesOfDelayAllowed = (float) filter_input(INPUT_POST, 'numberOfMinutesOfDelayAllowed');
    $employee->takeVacationDays = (float) filter_input(INPUT_POST, 'takeVacationDays');
    $employee->halfHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithPermissionDisount');
    $employee->quarterHourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithPermissionDisount');

    $employee->hourLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithPermissionDisount');
    $employee->hourAndHalfLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithPermissionDisount');
    $employee->twoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithPermissionDisount');
    $employee->moreThanTwoHoursLateWithPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithPermissionDisount');
    $employee->halfHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourLateWithoutPermissionDisount');

    $employee->quarterHourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'quarterHourLateWithoutPermissionDisount');
    $employee->hourLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourLateWithoutPermissionDisount');
    $employee->hourAndHalfLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfLateWithoutPermissionDisount');
    $employee->twoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursLateWithoutPermissionDisount');
    $employee->moreThanTwoHoursLateWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'moreThanTwoHoursLateWithoutPermissionDisount');
    $employee->halfHourPermissionDisount = (float) filter_input(INPUT_POST, 'halfHourPermissionDisount');
    $employee->hourPermissionDisount = (float) filter_input(INPUT_POST, 'hourPermissionDisount');
    $employee->hourAndHalfPermissionDisount = (float) filter_input(INPUT_POST, 'hourAndHalfPermissionDisount');
    $employee->twoHoursPermissionDisount = (float) filter_input(INPUT_POST, 'twoHoursPermissionDisount');
    $employee->dayAbsenceWithPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithPermissionDisount');
    $employee->dayAbsenceWithoutPermissionDisount = (float) filter_input(INPUT_POST, 'dayAbsenceWithoutPermissionDisount');
    $employee->numberOfCountsOfDelayAllowed = filter_input(INPUT_POST, 'numberOfCountsOfDelayAllowed');

    $employeeDAO->update($employee);

    $oldTree = $accountsTreeDAO->load($employee->treeId);
    $oldTree->customName = $employee->employeeName;
    $oldTree->name = $employee->employeeName;
    editTreeElement($oldTree);

    for ($i = 1; $i < 8; $i++) {
        $employeeattendancesystemweek_id = (int) filter_input(INPUT_POST, 'employeeattendancesystemweek_id_' . $i);
        $attendancedaynum = (int) filter_input(INPUT_POST, 'attendancedaynum_' . $i);
        $attendancedayar = filter_input(INPUT_POST, 'attendancedayar_' . $i);
        $attendancedayen = filter_input(INPUT_POST, 'attendancedayen_' . $i);
        $attendancetime = filter_input(INPUT_POST, 'attendancetime_' . $i);
        $departuretime = filter_input(INPUT_POST, 'departuretime_' . $i);
        $employeeattendancesystem_id = filter_input(INPUT_POST, 'employeeattendancesystem_id_' . $i);
        $employeeattendancesysteml_id = filter_input(INPUT_POST, 'employeeattendancesysteml_id_' . $i);
        $chosseday = filter_input(INPUT_POST, 'chosseday_' . $i);




        $employeeattendancesystemweek = R::load('employeeattendancesystemweek', $employeeattendancesysteml_id);
        $employeeattendancesystemweek->employeeattendancesystemweek_id = $employeeattendancesystemweek_id;
        $employeeattendancesystemweek->attendancedaynum = $attendancedaynum;
        $employeeattendancesystemweek->attendancedayar = $attendancedayar;
        $employeeattendancesystemweek->attendancedayen = $attendancedayen;
        $employeeattendancesystemweek->attendancetime = $attendancetime;
        $employeeattendancesystemweek->departuretime = $departuretime;
        $employeeattendancesystemweek->employeeattendancesystem_id = $employeeattendancesystem_id;
        $employeeattendancesystemweek->chosseday = $chosseday;
        $employeeattendancesystemweek->employee_id = $employeeId;
        R::store($employeeattendancesystemweek);
    }
}

function delete() {
    global $employeeDAO;
    global $employee;
    global $employeeEX;

    $id = $_GET["id"];
    $conditions = $_GET["conditions"];
    //print_r($id);
    $today = date("Y-m-d H:i:s");
    $employee->sysdate = $today;
    // $employee->employeeDate = $today;
    $employee->conditions = $conditions;
    $employee->userId = $userid;
    $employee->employeeId = $id;

    print_r($employee);
    //Check if there is any data associated with this employeeId
    //$empIdInEmpPersonnel = $employeeEX->queryEmpIdInEmployeePersonnel($id);



    $employeeEX->updateEmployee($employee); //Update delete
    $empData = $employeeDAO->load($id);
    delTreeElementById($empData->treeId);
}
