<?php

//the global file operation
include("../public/impOpreation.php");
include_once("../library/uploadImages.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");

//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");
//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//user
$userGroup = new Usergroup();
$userGroupDAO = new UsergroupMySqlDAO();
$userGroupEX = new UsergroupMySqlExtDAO();

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

//other request
$otherRequest = new Otherrequest();
$otherRequestDAO = new OtherrequestMySqlDAO();
$otherRequestEX = new OtherrequestMySqlExtDAO();

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

$userdata = $userDAO->load($_SESSION['userid']);

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


//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    //الفروع
    $branches = $BranchDAO->queryAll();
    $smarty->assign('branches', $branches);

    //المجموعات
    $userGroup = $userGroupDAO->queryAll();
    $smarty->assign('userGroups', $userGroup);


    //المستخدم
    $user = $userDAO->queryAll();
    $smarty->assign('users', $user);



    //here the smarty templates
    $smarty->display("otherrequestsview/add.html");
}

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

####
elseif ($do == "edit") {//edit  client
    include_once("../public/authentication.php");
    $requestId = filter_input(INPUT_GET, "id");

    $otherRequest = $otherRequestDAO->load($requestId);
    $smarty->assign('otherRequest', $otherRequest);

    //الفروع
    $branches = $BranchDAO->queryAll();
    $smarty->assign('branches', $branches);

    //المجموعات
    $userGroup = $userGroupDAO->queryAll();
    $smarty->assign('userGroups', $userGroup);


    //المستخدم
    $user = $userDAO->queryAll();
    $smarty->assign('users', $user);


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


####
elseif ($do == "update") {
    include_once("../public/authentication.php");
    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

####
elseif ($do == "details") {//edit  client
    include_once("../public/authentication.php");
    $requestId = filter_input(INPUT_GET, "id");

    $otherRequest = $otherRequestDAO->load($requestId);
    $otherRequest->seen = 1;
    $otherRequestDAO->update($otherRequest);

    ## user name
    $user = $userDAO->load($otherRequest->userId);
    $otherRequest->userName = $user->username;

    ## branch name
    if ($otherRequest->branchId == 0) {
        $otherRequest->branchName = 'كل الفروع ';
    } else {
        $Branch = $BranchDAO->load($otherRequest->branchId);
        $otherRequest->branchName = $Branch->branchName;
    }


    ## request To name
    if ($otherRequest->requestTo == 0) {
        $Branch = $BranchDAO->load($otherRequest->requestToId);
        $otherRequest->requestToName = $Branch->branchName;
    }
    ##
    elseif ($otherRequest->requestTo == 1) {
        $userGroup = $userGroupDAO->load($otherRequest->requestToId);
        $otherRequest->requestToName = $userGroup->usergroupname;
    }
    ##
    elseif ($otherRequest->requestTo == 2) {
        $user = $userDAO->load($otherRequest->requestToId);
        $otherRequest->requestToName = $user->username;
    }

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

    $smarty->display("otherrequestsview/details.html");
}

####
elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");
    $branchId = $_SESSION['branchId'];         ## 0 all branches    , else see its branch only
    $userGroupId = $userdata->usergroupid;     ## 1 admin   see every thing   , else see its group only
    $userId = $_SESSION['userid'];

##########################################################################################################################################

    if ($branchId == 0) {

        ##  my branch requests
        $string1 = 'and o.branchId  =  ' . $branchId . ' ';
        $myBranchRequests = $otherRequestEX->getRequestsWithStringEx($string1, ' 0 , 1 ', 0);


        ##  requests come to all branchs
        $string2 = 'and o.requestTo = 0  ';
        $toBranchRequests = $otherRequestEX->getRequestsWithStringEx($string2, ' 0 , 1 ', 0);


        ###################################################to user group  requests #####################################################################

        if ($userGroupId == 1) {
            ##  requests come to all user groups
            $string3 = 'and o.requestTo = 1  ';
            $toGroupRequests = $otherRequestEX->getRequestsWithStringEx($string3, ' 0 , 1 ', 0);


            ##  requests come to all users
            $string4 = 'and o.requestTo = 2   ';
            $toUserRequests = $otherRequestEX->getRequestsWithStringEx($string4, ' 0 , 1 ', 0);
        }
        ##
        else {
            ##  requests come to my user group
            $string3 = 'and o.requestTo = 1   and o.requestToId  =  ' . $userGroupId . ' ';
            $toGroupRequests = $otherRequestEX->getRequestsWithStringEx($string3, ' 0 , 1 ', 0);

            ##  requests come to my user
            $string4 = 'and o.requestTo = 2   and o.requestToId  =  ' . $userId . ' ';
            $toUserRequests = $otherRequestEX->getRequestsWithStringEx($string4, ' 0 , 1 ', 0);
        }
    }
    ##
    else {
        ##  my branch requests
        $string1 = 'and o.branchId  =  ' . $branchId . ' ';
        $myBranchRequests = $otherRequestEX->getRequestsWithString($string1, ' 0 , 1 ', 0);

        ##  requests come to my branch
        $string2 = 'and o.requestTo = 0   and o.requestToId  =  ' . $branchId . ' ';
        $toBranchRequests = $otherRequestEX->getRequestsWithString($string2, ' 0 , 1 ', 0);

        ###################################################to user group  requests #####################################################################

        if ($userGroupId == 1) {
            ##  requests come to all user groups
            $string3 = 'and o.requestTo = 1  ';
            $toGroupRequests = $otherRequestEX->getRequestsWithString($string3, ' 0 , 1 ', 0);


            ##  requests come to all users
            $string4 = 'and o.requestTo = 2   ';
            $toUserRequests = $otherRequestEX->getRequestsWithString($string4, ' 0 , 1 ', 0);
        }
        ##
        else {
            ##  requests come to my user group
            $string3 = 'and o.requestTo = 1   and o.requestToId  =  ' . $userGroupId . ' ';
            $toGroupRequests = $otherRequestEX->getRequestsWithString($string3, ' 0 , 1 ', 0);

            ##  requests come to my user
            $string4 = 'and o.requestTo = 2   and o.requestToId  =  ' . $userId . ' ';
            $toUserRequests = $otherRequestEX->getRequestsWithString($string4, ' 0 , 1 ', 0);
        }
    }

    $smarty->assign('branchId', $branchId);
    $smarty->assign('myBranchRequests', $myBranchRequests);
    $smarty->assign('toBranchRequests', $toBranchRequests);



    $smarty->assign('userGroupId', $userGroupId);
    $smarty->assign('toUserRequests', $toUserRequests);
    $smarty->assign('toGroupRequests', $toGroupRequests);

##################################################################################################################################################################

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


####
elseif ($do == "delete") {
    include_once("../public/authentication.php");

    $otherRequestId = filter_input(INPUT_GET, "id");
    $otherRequest = $otherRequestDAO->load($otherRequestId);
    $otherRequest->deleted = 1;
    try {
        $otherRequestDAO->update($otherRequest);
        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("productrequest", 1);

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

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

// add in client tbl
function add() {
    global $otherRequest;
    global $otherRequestDAO;


    $requestTo = filter_input(INPUT_POST, "requestTo");
    if ($requestTo == 2) {
        $requestToSelectName = 'userId';
    } elseif ($requestTo == 1) {
        $requestToSelectName = 'userGroupId';
    } else {
        $requestToSelectName = 'branchId';
    }

    $requestToId = filter_input(INPUT_POST, $requestToSelectName);
    $requestTitle = filter_input(INPUT_POST, "requestTitle");
    $comment = filter_input(INPUT_POST, "comment");


    $otherRequest->comment = $comment;
    $otherRequest->requestDate = date('Y-m-d');
    $otherRequest->requestTitle = $requestTitle;
    $otherRequest->requestTo = $requestTo;
    $otherRequest->requestToId = $requestToId;
    $otherRequest->userId = $_SESSION['userid'];
    $otherRequest->branchId = $_SESSION['branchId'];
    $otherRequest->seen = 0;
    $otherRequest->deleted = 0;

    $otherRequestDAO->insert($otherRequest);
}

function update() {
    global $otherRequest;
    global $otherRequestDAO;

    $requestId = filter_input(INPUT_POST, "requestId");
    $requestTo = filter_input(INPUT_POST, "requestTo");
    if ($requestTo == 2) {
        $requestToSelectName = 'userId';
    } elseif ($requestTo == 1) {
        $requestToSelectName = 'userGroupId';
    } else {
        $requestToSelectName = 'branchId';
    }

    $requestToId = filter_input(INPUT_POST, $requestToSelectName);
    $requestTitle = filter_input(INPUT_POST, "requestTitle");
    $comment = filter_input(INPUT_POST, "comment");


    $otherRequest = $otherRequestDAO->load($requestId);
    $otherRequest->comment = $comment;
    //$otherRequest->requestDate = date('Y-m-d');
    $otherRequest->requestTitle = $requestTitle;
    $otherRequest->requestTo = $requestTo;
    $otherRequest->requestToId = $requestToId;
    $otherRequest->userId = $_SESSION['userid'];
    $otherRequest->seen = 0;

    $otherRequestDAO->update($otherRequest);
}

?>