<?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/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');

//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

//coment

require_once('../models/dao/CommentclientDAO.class.php');
require_once('../models/dto/Commentclient.class.php');
require_once('../models/mysql/CommentclientMySqlDAO.class.php');
require_once('../models/mysql/ext/CommentclientMySqlExtDAO.class.php');


//typeClient
require_once('../models/dao/TypeClientDAO.class.php');
require_once('../models/dto/TypeClient.class.php');
require_once('../models/mysql/TypeClientMySqlDAO.class.php');
require_once('../models/mysql/ext/TypeClientMySqlExtDAO.class.php');

//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.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 :- clientsAndProductsReportController تقرير عملاء ومنتجات

  OPERTATION in Controller

  1- show report
  2- search with clients and products and date

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

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

//here goes the instances and general variables



//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

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

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

//Client
$comment = new Commentclient();
$commentDAO = new CommentclientMySqlDAO();
$commentExt = new CommentclientMySqlExtDAO();

//typeClient
$type = new TypeClient();
$TypeClientDAO = new TypeClientMySqlDAO();
$TypeClientEX = new TypeClientMySqlExtDAO();


//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
//check and use the condetion that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");
    $clientsData = $clientDAO->queryAll();
    $smarty->assign("clientsData", $clientsData);

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

    $userid = $_REQUEST['userid'];
    $clientId = $_REQUEST['clientid'];
    $smarty->assign('client', $clientId);


    if ($clientId != '' && $clientId != '-1') {
        $clientData = $clientDAO->load($clientId);
        $userrs = $myUserRecord->load($clientData->userid);




        $types = explode(",",$clientData->typeclientid);
        $smarty->assign("types", $types);


        $typeClient = $TypeClientDAO->queryAll();
        $smarty->assign("typeClient", $typeClient);



        $smarty->assign('usernames', $userrs);
        $smarty->assign('clientData', $clientData);

        $clientdeptchange = $clientDeptChangeExt->clientdeptchangeandCommentClient($clientId);
        $smarty->assign('clientdeptchange', $clientdeptchange);

        $clientDatas = $commentExt->licommentclient($clientId);
        $smarty->assign('clientDatas', $clientDatas);

    }
    $smarty->display("comentclient/add.html"); //the template page
}elseif ($do == "change") {
    include_once("../public/authentication.php");

    $id = filter_input(INPUT_POST, 'id');
    $date = filter_input(INPUT_POST, 'date');

    $offerClient =$clientDAO->load($id);
    $offerClient->clientdetails = $date;
    $clientDAO->update($offerClient);
    echo 1;


}elseif ($do == "edit") {
    include_once("../public/authentication.php");

    $id = filter_input(INPUT_POST, 'id');
    $date = filter_input(INPUT_POST, 'date');

    $offerClient =$commentDAO->load($id);
    $offerClient->comments = $date;
    $offerClient->commentdate = date("Y-m-d");
    $commentDAO->update($offerClient);
    echo 1;

}elseif ($do == "del") {
    include_once("../public/authentication.php");

    $id = filter_input(INPUT_POST, 'id');
    $commentDAO->delete($id);
    echo 1;


}elseif ($do == "add") {

    $comment = filter_input(INPUT_POST, 'coment');
    $userid = filter_input(INPUT_POST, 'userid');
    $clientid = filter_input(INPUT_POST, 'clientid');


    $comments->comments = $comment;
    $comments->clientid = $clientid;
    $comments->commentdate = date("Y-m-d");
    $comments->userid = $_SESSION['userid'];

    try {
        $id = $commentDAO->insert($comments);

        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }

}elseif ($do == "show") {

    $datefrom = filter_input(INPUT_POST, 'from');
    $dateto = filter_input(INPUT_POST, 'to');

    if (isset($datefrom) && !empty($datefrom)) {
        $queryString1 = ' where date(commentclient.commentdate)  >= "' . $datefrom . '" ';


    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString1 .= ' and date(commentclient.commentdate)  <= "' . $dateto . '" ';

    }

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

    $comments = $commentExt->commentclient($queryString1);
//    var_dump($comments);
    $smarty->assign('comments', $comments);
    $smarty->display("comentclient/show.html"); //the template page
}elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}




$smarty->assign("offerclient", 1);
$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 = 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");


    $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);

    $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->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->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;



    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);
    }

    $employeeDAO->insert($employee);
}

function update() {

    global $employeeDAO;
    global $employee;

    $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 = 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");

    $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);

    $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->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->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;


    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);
    }

    $employeeDAO->update($employee);
}

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
}

?>