<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Items extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("MStocks");
        checkLogin();
    }
    public function _remap($alias = "", $params = array())
    {

        $data['dataValue'] = $this->session;

        $data['pageTitle'] = "Inventory Items";
        $TableData = $this->db->where('status', 1);
        $TableData = $this->db->get('tbl_items')->result();
        foreach ($TableData as $TableRow) {
            $TableRow->Stock = $this->MStocks->getOpeningStock($TableRow->item_id);
        }
        $data['TableData'] = $TableData;
        switch ($alias) {
            case 'add':
                if (isset($_POST['submit'])) {
                    $title = filter_var($_POST['title']);
                    $item_code = filter_var($_POST['item_code']);
                    $this->db->where('title',$title);
                    $this->db->or_where('item_code',$item_code);
                    $query = $this->db->get('tbl_items');
                    if($query->num_rows() > 0){
                        echo "Title or code already exists.";die;
                    }else{
                        $TableData = array(
                            'itemcategories_id' => filter_var($_POST['itemcategories_id']),
                            'item_code' => filter_var($_POST['item_code']),
                            'title' => filter_var($_POST['title']),
                            'description' => filter_var($_POST['description']),
                            'units_id' => filter_var($_POST['units_id']),
                            'status' => 1,
                            'created_on' => date('Y-m-d H:i:s'),
                            'created_by' => 'admin',
                        );
                        $this->db->insert('tbl_items', $TableData);
                        $item_id = $this->db->insert_id();
                        $id = $item_id;
                        $qty = $_POST['opening_stock'];
                        $rate = $_POST['opening_stock_rate'];
                        $this->MStocks->addOpeningStock($id, $qty, $rate);
                        redirect("inventory/items/list");
                    }
                }
                loadView("inventory/items/add", $data);
                break;
            case 'edit':
                if (isset($_POST['submit'])) {
                    $id = $this->uri->segment(4);
                    
                    $title = filter_var($_POST['title']);
                    $item_code = filter_var($_POST['item_code']);
                    $this->db->where('status',1);
                    $this->db->where('item_id !=', $id);
                    $this->db->group_start();
                    $this->db->where('title', $title);
                    $this->db->or_where('item_code', $item_code);
                    $this->db->group_end();
                    
                    $query = $this->db->get('tbl_items');  
                    if($query->num_rows() > 0){
                        echo "Title or description already exists.";die;
                    }else{   
                        $TableData = array(
                            'itemcategories_id' => filter_var($_POST['itemcategories_id']),
                            'item_code' => filter_var($_POST['item_code']),
                            'title' => filter_var($_POST['title']),
                            'description' => filter_var($_POST['description']),
                            'units_id' => filter_var($_POST['units_id']),
                            'status' => 1,
                            'created_on' => date('Y-m-d H:i:s'),
                            'created_by' => 'admin',
                        );
                        $this->db->where('item_id', $id);
                        $this->db->update('tbl_items', $TableData);
                        $qty = $_POST['opening_stock'];
                        $rate = $_POST['opening_stock_rate'];
                        $this->MStocks->updateOpeningStock($id, $qty, $rate);
                        redirect("inventory/items/list");
                    }
                }
                $id = $this->uri->segment(4);
                $this->db->where('item_id', $id);
                $item = $this->db->get("tbl_items")->row();
                $Stock = new stdClass;
                $stockVal = $this->MStocks->getOpeningStock($id);
                $Stock->opening_stock = $stockVal->qty;
                $Stock->opening_stock_rate = $stockVal->rate;
                $Stock->opening_stock_amount = $stockVal->amount;

                $item->Stock = $Stock;
                $data['item'] = $item;
                loadView("inventory/items/list", $data);
                break;
            case 'delete':
                $id = $this->uri->segment(4);
                if (!$this->myaccounts->hasTransaction("items", $id)) {
                    if ($this->MStocks->isDeleteable($id)) {
                        $this->db->where('item_id', $id);
                        $this->db->delete('tbl_items');
                        $this->MStocks->deleteOpeningStock($id);
                    }
                } else {
                    echo "Transaction Exists!!! Can't Delete";
                    die;
                }

                redirect("inventory/items/list");
                break;
            default:

                loadView("inventory/items/list", $data);
        }
    }
}