<?php

// get the config file
include_once("../public/config.php");
include_once("dailyentryfun.php");
//here the db files that include in the file
include("../public/include_dao.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');
//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');
//Restaurantplaystation
require_once('../models/dao/RestaurantplaystationDAO.class.php');
require_once('../models/dto/Restaurantplaystation.class.php');
require_once('../models/mysql/RestaurantplaystationMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantplaystationMySqlExtDAO.class.php');
//Restaurantplaystationplay
require_once('../models/dao/RestaurantplaystationplayDAO.class.php');
require_once('../models/dto/Restaurantplaystationplay.class.php');
require_once('../models/mysql/RestaurantplaystationplayMySqlDAO.class.php');
require_once('../models/mysql/ext/RestaurantplaystationplayMySqlExtDAO.class.php');
//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.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');

//get the do the action
$do = $_GET['do'];

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

  Controller Name :- clientTypeCTRL

  OPERTATION in Controller

  1-
  2-
  3-
  4-

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


//here goes the instances and general variables
//Restauranttable
$restaurantTable = new Restauranttable();
$restaurantTableDAO = new RestauranttableMySqlDAO();
$restaurantTableEX = new RestauranttableMySqlExtDAO();
//Restauranthall
$restaurantHall = new Restauranthall();
$restaurantHallDAO = new RestauranthallMySqlDAO();
$restaurantHallEX = new RestauranthallMySqlExtDAO();
//Restaurantplaystation
$restaurantPlaystation = new Restaurantplaystation();
$restaurantPlaystationDAO = new RestaurantplaystationMySqlDAO();
$restaurantPlaystationEX = new RestaurantplaystationMySqlExtDAO();
//Restaurantplaystationplay
$restaurantPlaystationPlay = new Restaurantplaystationplay();
$restaurantPlaystationPlayDAO = new RestaurantplaystationplayMySqlDAO();
$restaurantPlaystationPlayEX = new RestaurantplaystationplayMySqlExtDAO();
//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingEX = new ProgramsettingsMySqlExtDAO();

//check and use the condetion that suite this action
if ($do == "openPlayStation") {
    try {
        $id = (int) filter_input(INPUT_POST, "id");
        $priceIs = (int) filter_input(INPUT_POST, "priceIs");
        $theType = (int) filter_input(INPUT_POST, "theType");
        $hoursResrvation = (int) filter_input(INPUT_POST, "hoursResrvation");
        $minutesResrvation = (int) filter_input(INPUT_POST, "minutesResrvation");

        //save new status
        $restaurantPlaystation = $restaurantPlaystationDAO->load($id);
        $restaurantPlaystation->beingUsed = 1;
        $restaurantPlaystationDAO->update($restaurantPlaystation);
        //make new play
        $restaurantPlaystationPlay = $restaurantPlaystationPlayEX->getUnFinishedPlaystationPlay($id);
        $restaurantPlaystationPlay->playstationid = $restaurantPlaystation->id;
        $restaurantPlaystationPlay->hourPrice = $restaurantPlaystation->hourPrice;
        $restaurantPlaystationPlay->hourPriceMulti = $restaurantPlaystation->hourPriceMulti;
        $restaurantPlaystationPlay->startsysDate = date('Y-m-d H:i:s');
        $restaurantPlaystationPlay->endsysDate = "";
        $restaurantPlaystationPlay->duration = 0;
        $restaurantPlaystationPlay->note = "";
        $restaurantPlaystationPlay->totalPrice = 0;
        $restaurantPlaystationPlay->discount = 0;
        $restaurantPlaystationPlay->tax = 0;
        $restaurantPlaystationPlay->netPrice = 0;
        $restaurantPlaystationPlay->payed = 0;
        $restaurantPlaystationPlay->finished = 0;
        $restaurantPlaystationPlay->sellBillId = 0;
        $restaurantPlaystationPlay->userId = $_SESSION['userid'];
        $restaurantPlaystationPlay->sysdate = date('Y-m-d H:i:s');
        $restaurantPlaystationPlay->del = 0;
        $restaurantPlaystationPlay->reservationType = $theType;
        $restaurantPlaystationPlay->reservationHours = $hoursResrvation;
        $restaurantPlaystationPlay->reservationMinutes = $minutesResrvation;
        $restaurantPlaystationPlay->priceIs = $priceIs;
        $endTime = strtotime($restaurantPlaystationPlay->startsysDate) + 60 * 60 * $hoursResrvation + 60 * $minutesResrvation;
        $endTime = date('Y-m-d H:i:s', $endTime);
        $restaurantPlaystationPlay->reservationEndDateTime = $endTime;

        if (isset($restaurantPlaystationPlay->id) && $restaurantPlaystationPlay->id > 0) {
            $restaurantPlaystationPlayDAO->update($restaurantPlaystationPlay);
        } else {
            $restaurantPlaystationPlay->oldPsPlayid = 0;
            $restaurantPlaystationPlay->startPrice = 0;
            $restaurantPlaystationPlay->moveStartSysDate = $restaurantPlaystationPlay->startsysDate;
            $restaurantPlaystationPlayDAO->insert($restaurantPlaystationPlay);
        }


        $hourPrice = $restaurantPlaystationPlay->hourPrice . "/h";
        if ($restaurantPlaystationPlay->priceIs == 1) {
            $hourPrice = $restaurantPlaystationPlay->hourPriceMulti . "/h";
        }


        $retData = array('start' => date("H:i", strtotime($restaurantPlaystationPlay->startsysDate)),
            'end' => $restaurantPlaystationPlay->reservationEndDateTime, 'hourPrice' => $hourPrice);
        echo json_encode($retData);
    } catch (Exception $ex) {
        //echo $exc->getTraceAsString();
        echo "حدث خطأ";
    }
} elseif ($do == "getCloseData") {
    $id = (int) filter_input(INPUT_POST, "id");
    $json_ret = (int) filter_input(INPUT_POST, "json_ret");

    if (isset($_SESSION['userid']) && $_SESSION['userid'] > 0) {
        //make new play
        $restaurantPlaystationPlay = $restaurantPlaystationPlayEX->getUnFinishedPlaystationPlay($id);
        if (isset($restaurantPlaystationPlay->id) && $restaurantPlaystationPlay->id > 0) {
            $programsetting = $ProgramsettingDAO->load(1);
            $noOfDecimalPlaces = ($programsetting->roundnumbers == 0) ? 0 : (int) $programsetting->noOfDecimalPlaces;


            //get end play time
            $restaurantPlaystationPlay->endsysDate = date('Y-m-d H:i:s');
            //getstart , end time in form H:i
            $restaurantPlaystationPlay->startTime = date('H:i', strtotime($restaurantPlaystationPlay->startsysDate));
            $restaurantPlaystationPlay->endTime = date('H:i', strtotime($restaurantPlaystationPlay->endsysDate));


            //calc duration for all time
            $datetime1 = new DateTime($restaurantPlaystationPlay->startsysDate);
            $datetime2 = new DateTime($restaurantPlaystationPlay->endsysDate);
            $interval = $datetime1->diff($datetime2);
            $restaurantPlaystationPlay->durationInText = $interval->format('%h') . " ساعة - " . $interval->format('%i') . " دقيقة";
            $restaurantPlaystationPlay->duration = $interval->format('%h') . ":" . $interval->format('%i') . ":" . $interval->format('%s');
            $restaurantPlaystationPlay->durationInMin = round((abs(strtotime($restaurantPlaystationPlay->endsysDate) - strtotime($restaurantPlaystationPlay->startsysDate)) / 60), 2);



            //calc duration for move time
            $datetime1 = new DateTime($restaurantPlaystationPlay->moveStartSysDate);
            $datetime2 = new DateTime($restaurantPlaystationPlay->endsysDate);
            $interval = $datetime1->diff($datetime2);
            $restaurantPlaystationPlay->moveDuration = $interval->format('%h') . ":" . $interval->format('%i') . ":" . $interval->format('%s');
            $restaurantPlaystationPlay->moveDurationInMin = round((abs(strtotime($restaurantPlaystationPlay->endsysDate) - strtotime($restaurantPlaystationPlay->moveStartSysDate)) / 60), 2);
            #
            $durationSplitted = explode(":", $restaurantPlaystationPlay->moveDuration); //duration
            $restaurantPlaystationPlay->hourNum = $durationSplitted[0] + ($durationSplitted[1] / 60) + (($durationSplitted[2] / 60) / 60);

            //calc total
            $hourPrice = $restaurantPlaystationPlay->hourPrice;
            if ($restaurantPlaystationPlay->priceIs == 1) {
                $hourPrice = $restaurantPlaystationPlay->hourPriceMulti;
            }
            //$restaurantPlaystationPlay->totalPrice = $restaurantPlaystationPlay->startPrice + ($restaurantPlaystationPlay->moveDurationInMin / 60) * $hourPrice;

            $restaurantPlaystationPlay->totalPrice = $restaurantPlaystationPlay->startPrice + $restaurantPlaystationPlay->hourNum * $hourPrice;
            //$restaurantPlaystationPlay->totalPrice = round($restaurantPlaystationPlay->totalPrice, $noOfDecimalPlaces);
            $restaurantPlaystationPlay->totalPrice = round($restaurantPlaystationPlay->totalPrice); //round to the nearest integer
            $restaurantPlaystationPlayDAO->update($restaurantPlaystationPlay);




            //
            if ($json_ret == 0) {
                $Usergroupdata = $UsergroupDAO->load($_SESSION['usergroupid']);
                $smarty->assign("Usergroupdata", $Usergroupdata);
                $smarty->assign("restaurantPlaystationPlay", $restaurantPlaystationPlay);
                $smarty->display("restaurantplaystationplay/endplay.html");
            } else {
                $PSData = $restaurantPlaystationDAO->load($restaurantPlaystationPlay->playstationid);
                $restaurantPlaystationPlay->PSName = $PSData->name;
                echo json_encode($restaurantPlaystationPlay);
            }
        }
    } else {
        echo "sessionEnded!";
    }
} elseif ($do == "getdevicesfrom") {
    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = intval($_GET['page_limit']);
    $limit = 15; //intval($_GET['page_limit']);

    $queryString = ' and restaurantplaystation.del = 0 and restaurantplaystation.beingUsed = 1 and restaurantplaystation.name LIKE "%' . $name . '%" ';
    $playstations = $restaurantPlaystationEX->queryAllWithQueryString($queryString);
    if (count($playstations) > 0) {
        foreach ($playstations as $ps) {
            $row_array['id'] = $ps->id;
            $row_array['text'] = $ps->name;
            array_push($return_arr, $row_array);
        }
    }
//print_r($clients);
    echo json_encode($return_arr);
} elseif ($do == "getdevicesto") {
    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = intval($_GET['page_limit']);
    $limit = 15; //intval($_GET['page_limit']);

    $queryString = ' and restaurantplaystation.del = 0 and restaurantplaystation.beingUsed = 0 and restaurantplaystation.name LIKE "%' . $name . '%" ';
    $playstations = $restaurantPlaystationEX->queryAllWithQueryString($queryString);
    if (count($playstations) > 0) {
        foreach ($playstations as $ps) {
            $row_array['id'] = $ps->id;
            $row_array['text'] = $ps->name;
            array_push($return_arr, $row_array);
        }
    }
//print_r($clients);
    echo json_encode($return_arr);
}

//******************    Functions    ************************//
?>