<?php
if (!defined('BASEPATH'))  exit('No direct script access allowed');
class DashboardKDS_model extends CI_Model {
    public function __construct() {
        parent::__construct();
    }
    
    function getdataboxes() {
        $this->db->select('CONTENT');
        $this->db->where('NAME', 'Webin.DashboardKDS.Databoxes');
        $q = $this->db->get('RESOURCES')->result();
        if($q!=null){
            return $q[0]->CONTENT;
        }else {
            return null;   
        }
    }
    
    function kitchenticketexists($displayid='') {
        $sql    =   "SELECT COUNT(*) CNT FROM KDSTICKETS WHERE DISPLAYID='".$displayid."'";
        $query = $this->db->query($sql);
        $arrec  =   $query->row();
        return $arrec->CNT;
    }
    
    function kitchenordersdetails($displayid='') {
        $sql    =   "SELECT SUM(ORDERSEND) AS ORDERSEND,SUM(ORDERATTENDED) AS ORDERATTENDED,SUM(ORDERCLOSED) AS ORDERCLOSED,SUM(ORDERTOTAL) AS ORDERTOTAL FROM (
SELECT COUNT(*) AS ORDERSEND,0 AS ORDERATTENDED,0 AS ORDERCLOSED,0 AS ORDERTOTAL FROM KDSTICKETS WHERE STATUS=0 AND DISPLAYID='".$displayid."'
 UNION SELECT 0 AS ORDERSEND,COUNT(*) AS ORDERATTENDED,0 AS ORDERCLOSED,0 AS ORDERTOTAL FROM KDSTICKETS WHERE STATUS=2 AND DISPLAYID='".$displayid."'
 UNION SELECT 0 AS ORDERSEND,0 AS ORDERATTENDED,COUNT(*) AS ORDERCLOSED,0 AS ORDERTOTAL FROM KDSTICKETS WHERE STATUS=3 AND DISPLAYID='".$displayid."'
  UNION SELECT 0 AS ORDERSEND,0 AS ORDERATTENDED,0 AS ORDERCLOSED,COUNT(*) AS ORDERTOTAL FROM KDSTICKETS WHERE DISPLAYID='".$displayid."') A";
        $query = $this->db->query($sql);
        if($query->num_rows()>0) {
            return $query->row(); 
        } else {
            return null;
        }
    }
    
    function kitchenorderprepdetails($displayid='') {
        $sql    =   "SELECT MIN(TIMESTAMPDIFF(MINUTE, DATEATTENDED, DATECLOSED)) AS MINIMUMPREPTIME, 
            MAX(TIMESTAMPDIFF(MINUTE, DATEATTENDED, DATECLOSED)) AS MAXIMUMPREPTIME,
            ((MIN(TIMESTAMPDIFF(MINUTE, DATEATTENDED, DATECLOSED))+MAX(TIMESTAMPDIFF(MINUTE, DATEATTENDED, DATECLOSED)))/2) AS AVERAGEPREPTIME
            FROM KDSTICKETS WHERE DISPLAYID='".$displayid."'";
        $query = $this->db->query($sql);
        if($query->num_rows()>0) {
            return $query->row(); 
        } else {
            return null;
        }
    }
    
    function kitchentoporderitems($displayid='', $limit='') {
        $sql    =   "SELECT P.NAME,SUM(KL.UNITS) AS TOTALUNITS FROM KDSLINES KL"
            . " JOIN KDSTICKETS KT ON KT.ID=KL.KDSID JOIN PRODUCTS P ON P.CODE=KL.PRODUCT WHERE 1";
        if($displayid)
            $sql =  $sql." AND KT.DISPLAYID='".$displayid."'";
        $sql =  $sql." GROUP BY PRODUCT ORDER BY SUM(KL.UNITS) DESC";
        if($limit)
            $sql =  $sql." LIMIT ".$limit;
        $query = $this->db->query($sql);
        if($query->num_rows()>0) {
            return $query->result(); 
        } else {
            return null;
        }
    }
    
    function checkkdslinesexisttable() {
        $totalCount = $this->common->customquery("SHOW TABLES LIKE 'KDSLINES'");
        if($totalCount[0]){
            return 1;
        } else {
            return 0;
        }
    }
    function checkkdsticketsexisttable() {
        $totalCount = $this->common->customquery("SHOW TABLES LIKE 'KDSTICKETS'");
        if($totalCount[0]){
            return 1;
        } else {
            return 0;
        }
    }
}