<?php

/**
 * Class that operate on table 'restaurantorder'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2018-05-16 11:38
 */
class RestaurantorderMySqlExtDAO extends RestaurantorderMySqlDAO {

    public function getlastrestaurantorder($sellbillid) {
       $sql = 'SELECT * FROM `restaurantorder` 
                WHERE sellbillid = '.$sellbillid.' order by restaurantorder.id desc ';
        $sqlQuery = new SqlQuery($sql);
        return $this->getRow($sqlQuery);
    }

    public function queryByTableIdNotFinishedEX($tableId) {
        $sql = 'SELECT * FROM restaurantorder WHERE finished =0 and tableId = ' . $tableId;
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }


    public function queryOrderGetByTableEX($tableId) {
        $sql = 'SELECT * FROM restaurantorder WHERE tableId = ' . $tableId;
        $sqlQuery = new SqlQuery($sql);
        return $this->getRow($sqlQuery);
    }

    public function queryOrderGetByTableEX2($tableId) {
        $sql = 'SELECT * FROM restaurantorder WHERE tableId = ' . $tableId . 'order by id desc';
        $sqlQuery = new SqlQuery($sql);
        return $this->getRow($sqlQuery);
    }

    public function countorder($searchQuery) {
        $sql = "SELECT * FROM restaurantorder WHERE del = 0 and $searchQuery ";
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function getOrdersIdSimple($search) {
        $sql = 'SELECT id FROM restaurantorder where id like "%' . $search . '%"';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function getSellBillIdsSimple($search) {
        $sql = 'SELECT id,sellBillId FROM restaurantorder where sellBillId like "%' . $search . '%" and sellBillId !=0';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryOrdersQueryString($queryString) {
        $sql = 'SELECT restaurantorder.*,restaurantorderdetails.productId,restaurantorderdetails.quantity,restaurantorderdetails.price,restaurantorderdetails.total
                ,product.productName,restauranttable.name as tableName,restauranthall.name as hallName
                FROM restaurantorder
                join restaurantorderdetails on restaurantorderdetails.orderId = restaurantorder.id
                join product on product.productId = restaurantorderdetails.productId
                left join restauranttable on restauranttable.id = restaurantorder.tableId
                left join restauranthall on restauranthall.id = restauranttable.hallid
                ' . $queryString . ' ';
        // echo $sql;
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function queryOrdersQueryStringNoHallNoTable($queryString) {
        $sql = 'SELECT restaurantorder.*,client.clientname,client.clientaddress,client.clientdetails,client.clientmobile,client.clientphone,user.employeename,driverReciveDate,deliveryStatus,driverId
                ,restaurantorderdeliverydetails.clientaddress as showAddress,restaurantorderdeliverydetails.clientdetails as showDetails
                ,restaurantorderdeliverydetails.clientphone as showPhone,restaurantorderdeliverydetails.clientmobile as showMobile
                FROM restaurantorder
                join restaurantorderdeliverydetails on restaurantorderdeliverydetails.orderId = restaurantorder.id
                left join client on client.clientid = restaurantorderdeliverydetails.clientId
                left join user on user.userid = restaurantorderdeliverydetails.driverId
                ' . $queryString . ' ';
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function getMaxDaySreial($sqlQuery) {
        $sql = 'SELECT max(daySreial)
		 FROM restaurantorder
		 where finished = 1 and del = 0 ' . $sqlQuery;
        $sqlQuery = new SqlQuery($sql);
        return $this->querySingleResult($sqlQuery);
    }

    public function updateBillComment($id, $comment) {
        $sql = 'UPDATE restaurantorder SET billComment = "' . $comment . '" WHERE id = ' . $id;
        $sqlQuery = new SqlQuery($sql);

        return $this->executeUpdate($sqlQuery);
    }
    
    public function updateRestaurantPlaystationplayId($id, $restaurantplaystationplayid) {
        $sql = 'UPDATE restaurantorder SET restaurantplaystationplayid = ' . $restaurantplaystationplayid . ' WHERE id = ' . $id;
        $sqlQuery = new SqlQuery($sql);
        return $this->executeUpdate($sqlQuery);
    }


    public function sumTotalBill($startDate,$endDate) {
        $sql = 'SELECT sum(totalBill) as SumTotal
		FROM restaurantorder
		where restaurantorder.sysdate >= "' . $startDate . '" and restaurantorder.sysdate <=  "' . $endDate . '"
		 and restaurantorder.finished= 1 ';
//        echo $sql;
        $sqlQuery = new SqlQuery($sql);
        return $this->querySingleResult($sqlQuery);
    }

}

?>