<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Inventory_m extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }



    public function purchase_item_select_by_cat_id($id)
    {
        $this->db->select('*');
        $this->db->from('vendor_inventory_purchases_items');
        $this->db->where('cat_id', $id);
        $this->db->where('status', 1);
        $query = $this->db->get();
        return $query->result_array();
    }



    public function check_exits_purchase_item($item)
    {
        $shop_id = restaurant()->id;

        $this->db->select('u.id');
        $this->db->from('vendor_inventory_purchases_items u');
        $this->db->where('u.name', $item);
        $this->db->where('u.shop_id', $shop_id);
        $query = $this->db->get();
        $_item = $query->row();
        return $_item ? $_item->id : false;
    }


    public function purchases_item_select_by_item_id($id)
    {
        $this->db->select('u.*');
        $this->db->from('vendor_inventory_purchases u');
        $this->db->where('u.item_id', $id);
        $query = $this->db->get();
        $item = $query->row_array();
        return $item;
    }


    public function get_total_purchases_item()
    {
        $shop_id = restaurant()->id;

        $this->db->select('*');
        $this->db->from('vendor_inventory_purchases');
        $this->db->where('shop_id', $shop_id);
        $query = $this->db->get();
        $total_item = $query->num_rows();
        return $total_item;
    }


    public function get_inventory_logs_items()
    {
        $get = $this->input->get(null, true);

        $this->db->select('
        in.*, 
        in.id as inventory_logs_id, 
        item.id as purchases_items_id, 
        item.name as item_name, 
        item.min_stock as item_min_stock, 
        p.id as purchases_id, 
        p.stock as p_item_stock
    ');

        $this->db->from('vendor_inventory_logs in');
        $this->db->join('vendor_inventory_purchases_items item', 'item.id = in.item_id');
        $this->db->join('vendor_inventory_purchases p', 'p.item_id = item.id', 'left');

        if (!empty($get['q'])) {
            $this->db->like('item.name', $get['q']);
        }

        if (!empty($get['category'])) {
            $this->db->where('item.cat_id', $get['category']);
        }

        if (!empty($get['status'])) {
            $this->db->where('in.status', $get['status']);
        }

        if (!empty($get['priority'])) {
            $this->db->where('in.priority', $get['priority']);
        }

        $this->db->where('in.user_id', auth('id'));

        $this->db->order_by("
        CASE 
            WHEN in.status = 'pending' THEN 0
            ELSE 1
        END", '', false);

        $this->db->order_by("
        CASE in.priority
            WHEN 'urgent' THEN 1
            WHEN 'high' THEN 2
            WHEN 'normal' THEN 3
            ELSE 4
        END", '', false);

        return $this->db->get()->result_array();
    }


    public function get_recent_activities()
    {
        $get = $this->input->get(null, true);

        $this->db->select('
        in.*, 
        in.id as inventory_logs_id, 
        item.id as purchases_items_id, 
        item.name as item_name, 
        item.min_stock as item_min_stock, 
        p.id as purchases_id, 
        p.stock as p_item_stock
    ');

        $this->db->from('vendor_inventory_logs in');
        $this->db->join('vendor_inventory_purchases_items item', 'item.id = in.item_id');
        $this->db->join('vendor_inventory_purchases p', 'p.item_id = item.id', 'left');

        $this->db->where('in.status !=', 'cancelled');

        $this->db->where('in.user_id', auth('id'));

        $this->db->order_by("
        CASE 
            WHEN in.status = 'pending' THEN 0
            ELSE 1
        END", '', false);

        $this->db->order_by("
        CASE in.priority
            WHEN 'urgent' THEN 1
            WHEN 'high' THEN 2
            WHEN 'normal' THEN 3
            ELSE 4
        END", '', false);

        return $this->db->get()->result_array();
    }


    public function get_purchases_items()
    {
        $get = $this->input->get(null, true);
        $this->db->select('p.*, p.id as purchases_id, v.name as vendor_name, item.name as item_name, item.min_stock as item_min_stock, cat.category as category_name');
        $this->db->from('vendor_inventory_purchases p');
        $this->db->join('inventory_vendor_list v', 'v.id = p.vendor_id');
        $this->db->join('vendor_inventory_purchases_items item', 'item.id = p.item_id');
        $this->db->join('vendor_inventory_purchases_category cat', 'cat.id = p.cat_id');

        if (!empty($get['q'])) {
            $this->db->group_start();
            $this->db->like('item.name', $get['q']);
            $this->db->or_like('v.name', $get['q']);
            $this->db->or_like('p.invoice_number', $get['q']);
            $this->db->group_end();
        }

        if (!empty($get['vendor'])) {
            $this->db->where('p.vendor_id', $get['vendor']);
        }

        if (!empty($get['category'])) {
            $this->db->where('p.cat_id', $get['category']);
        }

        if (!empty($get['item'])) {
            $this->db->where('p.item_id', $get['item']);
        }

        $this->db->where('p.user_id', auth('id'));
        $this->db->order_by('p.id', 'DESC');
        $query = $this->db->get();
        return $query->result_array();
    }



    public function get_purchases_items_with_category()
    {
        $this->db->select('items.*, items.id as items_id, cat.category as category_name');
        $this->db->from('vendor_inventory_purchases_items items');
        $this->db->join('vendor_inventory_purchases_category cat', 'cat.id = items.cat_id');
        $this->db->where('items.user_id', auth('id'));
        $query = $this->db->get();
        return $query->result_array();
    }
}
