<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Menu_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    function getcategories() {
        $this->db->select('NAME,ID');
        $where = 'CATEGORIES.ID != "-1"';
        $this->db->where($where);
        $q = $this->db->get('CATEGORIES');
        return $q->result_array();
    }

    function getproducts() {
        $this->db->select('P.NAME,P.ID,P.PRICESELL,P.CATEGORY');
        $q = $this->db->get('PRODUCTS P');
        return $q->result_array();
    }

    function addsharedticket($sharedticket) {
        $this->db->insert('SHAREDTICKETS', $sharedticket);
        if ($this->db->affected_rows() == '1') {
            return TRUE;
        }
        return FALSE;
    }

    function getsharedticket($tableid) {
        $this->db->select('NAME, CONTENT');
        $this->db->where('ID', $tableid);
        return $this->db->get('SHAREDTICKETS');
    }

    function updateitem($tableID, $updateticket) {
        $this->db->where('ID', $tableID);
        $this->db->update('SHAREDTICKETS', $updateticket);
        if ($this->db->affected_rows() == '1') {
            return TRUE;
        }
        return FALSE;
    }

    function getprodDetailsBid($pid) {
        $this->db->select('PRODUCTS.ID,PRODUCTS.NAME,PRODUCTS.PRICESELL,PRODUCTS.PRICEBUY,PRODUCTS.TAXCAT,PRODUCTS.ISCOM,PRODUCTS.CATEGORY');
        $this->db->where('PRODUCTS.ID', $pid);
        $q = $this->db->get('PRODUCTS');
        return $q->result_array();
    }

    function gettaxDetailsBid($taxid) {
        $this->db->select('TAXES.ID,TAXES.NAME,TAXES.RATE,TAXES.RATECASCADE,TAXES.CATEGORY,TAXES.RATEORDER');
        $this->db->where('TAXES.CATEGORY', $taxid);
        $q = $this->db->get('TAXES');
        return $q->result_array();
    }

    function gettableNameBid($pid) {
        $this->db->select('PLACES.NAME');
        $this->db->where('PLACES.ID', $pid);
        $q = $this->db->get('PLACES');
        return $q->result_array();
    }

    function getsharedticketName($tableid) {
        $this->db->select('NAME, CONTENT');
        $this->db->where('ID', $tableid);
        $q = $this->db->get('SHAREDTICKETS');
        return $q->result_array();
    }

    public function getticketItems($data) {
        $this->db->select('ST.NAME,ST.ID ,ST.CONTENT');
        $where = "ST.ID = '" . $data['tableID'] . "'";
        $this->db->where($where);
        $q = $this->db->get('SHAREDTICKETS ST');

        $results = $q->result_array();
        $ar = array();

//UPDATE QUANTITY IN XML       
        $xml = new DOMDocument('1.0');

        $xml->loadXml($results[0]['CONTENT']);
        $xml->saveXML();
//SELECT ROOT TAG TO UPDATE 
        $Xpath = new DOMXPath($xml);
        $result = $Xpath->query('/ticket/item');

        $baz_node_of_xml = $result->item(0);
        $root = $xml->documentElement;
        $i = 0;
        foreach ($result as $item) {
            $id = $item->getElementsByTagName('id')->item(0)->textContent;


            if ($id != null) {

                array_push($ar, $id);
            }
        }

        return $ar;
    }

}

