<?php

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

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//Restauranthall
require_once('../models/dao/RestauranthallDAO.class.php');
require_once('../models/dto/Restauranthall.class.php');
require_once('../models/mysql/RestauranthallMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranthallMySqlExtDAO.class.php');
//Restauranttable
require_once('../models/dao/RestauranttableDAO.class.php');
require_once('../models/dto/Restauranttable.class.php');
require_once('../models/mysql/RestauranttableMySqlDAO.class.php');
require_once('../models/mysql/ext/RestauranttableMySqlExtDAO.class.php');
//Restaurantorder
require_once('../models/dao/RestaurantorderDAO.class.php');
require_once('../models/dto/Restaurantorder.class.php');
require_once('../models/mysql/RestaurantorderMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantorderMySqlExtDAO.class.php');
//Restaurantorderdetail
require_once('../models/dao/RestaurantorderdetailsDAO.class.php');
require_once('../models/dto/Restaurantorderdetail.class.php');
require_once('../models/mysql/RestaurantorderdetailsMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantorderdetailsMySqlExtDAO.class.php');
////Programsetting
//require_once('../models/dao/ProgramsettingsDAO.class.php');
//require_once('../models/dto/Programsetting.class.php');
//require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
//require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');
//User
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');

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'];

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

  Controller Name :- storedetailCTRL تقرير مخزون اول مدة

  OPERTATION in Controller

  1-load save data
  2-display show form

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

//here the global templates
if (empty($do)) {
    $smarty->display("header.html");
}
//here goes the instances and general variables
//Restauranthall
$restaurantHall = new Restauranthall();
$restaurantHallDAO = new RestauranthallMySqlDAO();
$restaurantHallEX = new RestauranthallMySqlExtDAO();
//Restauranttable
$restaurantTable = new Restauranttable();
$restaurantTableDAO = new RestauranttableMySqlDAO();
$restaurantTableEX = new RestauranttableMySqlExtDAO();
//Restaurantorder
$restaurantOrder = new Restaurantorder();
$restaurantOrderDAO = new RestaurantorderMySqlDAO();
$restaurantOrderEX = new RestaurantorderMySqlExtDAO();
//Restaurantorderdetail
$restaurantOrderDetails = new Restaurantorderdetail();
$restaurantOrderDetailsDAO = new RestaurantorderdetailsMySqlDAO();
$restaurantOrderDetailsEX = new RestaurantorderdetailsMySqlExtDAO();
//$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
//
//User
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

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

//$myUserGroupRecord = new UsergroupMySqlDAO();
//$userGroupId = $_SESSION['usergroupid'];
//$loadData = $myUserGroupRecord->load($userGroupId);
//$smarty->assign("loadData", $loadData);
//check and use the condition that suite this action
if (empty($do)) {
    $usergroupid = $_SESSION['usergroupid'];
    $usergroup = R::getRow("SELECT usergroup.* FROM `usergroup` WHERE usergroupid = $usergroupid ");
    $_SESSION['passworduse'] = $usergroup['billDeletePassword'];
   $smarty->assign("usergroup", $usergroup); 
    //here the permission check
    //include_once("../public/authentication.php");
    //search
    $orderId = filter_input(INPUT_POST, 'orderId');
    $sellBillId = filter_input(INPUT_POST, 'sellBillId');
    $hallId = filter_input(INPUT_POST, 'hallId');
    $tableId = filter_input(INPUT_POST, 'tableId');
    $userId = filter_input(INPUT_POST, 'userId');
    $orderType = (int) filter_input(INPUT_POST, 'orderType');
    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');
    $queryString = " where 1 ";
    ##orderid
    if (isset($orderId) && !empty($orderId) && $orderId != -1) {
        $queryString .= 'and restaurantorder.id  = ' . $orderId . ' ';
    } else {
        ##sellBillId
        if (isset($sellBillId) && !empty($sellBillId) && $sellBillId != -1) {
            $queryString .= 'and restaurantorder.sellBillId  = ' . $sellBillId . ' ';
        }
        ##hallId
        if (isset($hallId) && !empty($hallId) && $hallId != -1) {
            $queryString .= 'and restauranthall.id  = ' . $hallId . ' ';
        }
        ##table
        if (isset($tableId) && !empty($tableId) && $tableId != -1) {
            $queryString .= 'and restaurantorder.tableId  = ' . $tableId . ' ';
        }
        ##user
        if (isset($userId) && !empty($userId) && $userId != -1) {
            $queryString .= 'and restaurantorder.userId  = ' . $userId . ' ';
        }
        ##orderType
        if (isset($orderType) && !empty($orderType)) {
            if ($orderType == -1 || $orderType == -2) {//delivery , take away
                $queryString .= 'and restaurantorder.tableId  = ' . $orderType . ' ';
            } else if ($orderType == 1) {//table
                $queryString .= 'and restaurantorder.tableId  > 0 ';
            }
        }
        ##date
        if (isset($datefrom) && !empty($datefrom)) {
            $queryString .= 'and  restaurantorder.sysdate  >= "' . $datefrom . '" ';
        }
        if (isset($dateto) && !empty($dateto)) {
            $queryString .= 'and  restaurantorder.sysdate  <= "' . $dateto . '" ';
        }
    }
    if ($queryString == ' where 1 ') {
        $queryString .= 'and  restaurantorder.sysdate  >= "' . date('Y-m-d') . ' 00:00:00" ';
        $queryString .= 'and  restaurantorder.sysdate  <= "' . date('Y-m-d') . ' 23:59:59" ';
    }
    $queryString .=' and restaurantorder.finished=1 order by restaurantorder.id desc, restaurantorder.daySreial desc';
    $ordersData = $restaurantOrderEX->queryOrdersQueryString($queryString);
    $smarty->assign("ordersData", $ordersData);

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

    //here the smarty templates
    $smarty->display("ordersreport/show.html");
    $smarty->assign("ordersreport", 1);
 } else if ($do == "changetype") {
  $orderId = filter_input(INPUT_POST, 'orderid');
    $tableid = filter_input(INPUT_POST, 'tableid');
if($tableid == -1){
$tableid = -2;
}else{
$tableid = -1;
}
R::exec("UPDATE restaurantorder SET `tableId`= $tableid WHERE id = $orderId ");

 
  
}
if (empty($do)) {
//here the global templates
    $smarty->display("footer.html");
}
## select 2 ajax
elseif ($do == "getOrders") {
    $row_array = array();
    $return_arr = array();
    $search = $_POST["searchTerms"];
    $results = $restaurantOrderEX->getOrdersIdSimple($search);
    if (count($results) > 0) {
        foreach ($results as $data) {
            $row_array['id'] = $data->id;
            $row_array['text'] = $data->id;
            array_push($return_arr, $row_array);
        }
    }
    echo json_encode($return_arr);
} elseif ($do == "getSellBillIds") {
    $row_array = array();
    $return_arr = array();
    $search = $_POST["searchTerms"];
    $results = $restaurantOrderEX->getSellBillIdsSimple($search);
    if (count($results) > 0) {
        foreach ($results as $data) {
            $row_array['id'] = $data->sellBillId;
            $row_array['text'] = $data->sellBillId;
            array_push($return_arr, $row_array);
        }
    }
    echo json_encode($return_arr);
} elseif ($do == "getHalls") {
    $row_array = array();
    $return_arr = array();
    $search = $_POST["searchTerms"];
    $results = $restaurantHallEX->getHallsIds($search);
    if (count($results) > 0) {
        foreach ($results as $data) {
            $row_array['id'] = $data->id;
            $row_array['text'] = $data->name;
            array_push($return_arr, $row_array);
        }
    }
    echo json_encode($return_arr);
} elseif ($do == "getTables") {
    $row_array = array();
    $return_arr = array();
    $search = $_POST["searchTerms"];
    $hallId = (int) $_POST["hallId"];
    $results = $restaurantTableEX->gettableIds($search, $hallId);
    if (count($results) > 0) {
        foreach ($results as $data) {
            $row_array['id'] = $data->id;
            $row_array['text'] = $data->name;
            array_push($return_arr, $row_array);
        }
    }
    echo json_encode($return_arr);
} elseif ($do == "getUsers") {
    $row_array = array();
    $return_arr = array();
    $search = $_POST["searchTerms"];
    $results = $userEX->getUsers($search);
    if (count($results) > 0) {
        foreach ($results as $data) {
            $row_array['id'] = $data->userid;
            $row_array['text'] = $data->employeename;
            array_push($return_arr, $row_array);
        }
    }
    echo json_encode($return_arr);
}

/* ===============================
  function in this CONTROLLER
  ================================ */
?>