<?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
$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();
//Restaurantorderdeliverydetail
$restaurantOrderDeliveryDetail = new Restaurantorderdeliverydetail();
$restaurantOrderDeliveryDetailDAO = new RestaurantorderdeliverydetailsMySqlDAO();
$restaurantOrderDeliveryDetailEX = new RestaurantorderdeliverydetailsMySqlExtDAO();
//Restaurantorderdetail
$restaurantOrderDetails = new Restaurantorderdetail();
$restaurantOrderDetailsDAO = new RestaurantorderdetailsMySqlDAO();
$restaurantOrderDetailsEX = new RestaurantorderdetailsMySqlExtDAO();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//
//User
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//$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) || $do == "show") {
    //here the permission check
    //include_once("../public/authentication.php");
    //search
    $orderId = filter_input(INPUT_POST, 'orderId');
    $sellBillId = filter_input(INPUT_POST, 'sellBillId');
    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');
    $driver = (int) filter_input(INPUT_POST, 'driver');
    $clientId = (int) filter_input(INPUT_POST, 'clientId');
    $queryString = " where 1 ";
    if (empty($datefrom) && empty($dateto) && empty($sellBillId) && empty($orderId) && empty($driver) && empty($clientId)) {
        $Programsetting = $ProgramsettingDAO->load(1);
        $datefrom = $dateto = date('Y-m-d');
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $dateto = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($dateto)));
            $datefrom = date('Y-m-d H:i:s', strtotime('-' . $Programsetting->reportsPlusHours . ' hour', strtotime($datefrom))); 

            //$datefrom = date('Y-m-d H:i:s', strtotime('-' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($datefrom)));
        } else {
            $dateto = $dateto . ' 23:59:59';
            $datefrom = $datefrom . " 00:00:00";
        }
    }
    ##orderid
    if (isset($orderId) && !empty($orderId) && $orderId != -1) {
        $queryString .= 'and restaurantorder.id  = ' . $orderId . ' ';
    }
    ##sellBillId
    if (isset($sellBillId) && !empty($sellBillId) && $sellBillId != -1) {
        $queryString .= 'and restaurantorder.sellBillId  = ' . $sellBillId . ' ';
    }
    ##date
    if (isset($datefrom) && !empty($datefrom)) {
        $queryString .= 'and  restaurantorder.sysdate  >= "' . $datefrom . '" ';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString .= 'and  restaurantorder.sysdate  <= "' . $dateto . '" ';
    }
    if (isset($driver) && !empty($driver)) {
        $queryString .= 'and  restaurantorderdeliverydetails.driverId  = ' . $driver . ' ';
    }
    if (isset($clientId) && !empty($clientId)) {
        $queryString .= 'and  client.clientid  = ' . $clientId . ' ';
    }
    $queryString .=' and restaurantorder.finished=1 and restaurantorder.tableId =-2 and restaurantorder.del = 0 ';
    $ordersData = $restaurantOrderEX->queryOrdersQueryStringNoHallNoTable($queryString);
    $now = new DateTime(date('Y-m-d H:i:s'));
    foreach ($ordersData as $value) {
        if ($value->deliveryStatus == 1) {
            //calc time diff using "date_interval_format() Function" or "DateInterval::format"
            $recDate = new DateTime($value->driverReciveDate);
            $interval = $now->diff($recDate);
            $value->diff = $interval->format('%h') . " ساعة و " . $interval->format('%i') . " دقيقة";
        }
    }
    $smarty->assign("ordersData", $ordersData);

    //السائقين
    $drivers = $userEX->queryWithGroupName("سائقين");
    $smarty->assign("drivers", $drivers);
    $clients = $clientExt->queryAllClientsEX();
    $smarty->assign("clients", $clients);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    //here the smarty templates
    $smarty->display("orderstatusreport/show.html");
    //$smarty->assign("ordersreport", 1);

} elseif ($do == "changeDriver") {
  $orderId = filter_input(INPUT_POST, 'orderid');
    $driver = filter_input(INPUT_POST, 'driver');
 $status = filter_input(INPUT_POST, 'status');
R::exec("UPDATE restaurantorderdeliverydetails SET `driverId`= '".$driver."', `deliveryStatus`= '".$status."' WHERE orderId = '" . $orderId . "' ");
 


} elseif ($do == "saveDriver") {
    foreach ($_POST as $key => $value) {
        //read post that start with driver_
        if (strpos($key, 'driver_') === 0) {
            $orderId = explode("_", $key)[1];
            $driver = (int) $_POST["driver_" . $orderId];
            if ($orderId > 0 && $driver > 0) {
                $restaurantOrderDeliveryDetail = $restaurantOrderDeliveryDetailDAO->queryByOrderId($orderId);
                $deliveryData = clone $restaurantOrderDeliveryDetail[0];
                $deliveryData->driverId = $driver;
                $deliveryData->driverReciveDate = date('Y-m-d H:i:s');
                $deliveryData->deliveryStatus = 1;
                $restaurantOrderDeliveryDetailDAO->update($deliveryData);
            }
        }
    }
    header("location:?do=show");
} elseif ($do == "markAsDelivered") {
    foreach ($_POST["choosedItem"] as $orderId) {
        if ((int) $orderId > 0) {
            $restaurantOrderDeliveryDetailEX->markOrderAsDelivered($orderId);
        }
    }
    header("location:?do=show");
}
//here the global templates
$smarty->display("footer.html");

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