<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Stocks 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 Stock";
        switch ($alias) {
            case 'add':
                if (isset($_POST['submit'])) {
                    $TableData = array(
                        'stocklocations_id' => filter_var($_POST['stocklocations_id']),
                        'title' => filter_var($_POST['title']),
                        'items_id' => filter_var($_POST['items_id']),
                        'qty' => filter_var($_POST['qty']),
                        'price' => filter_var($_POST['price']),
                        'display_order' => filter_var($_POST['display_order']),
                        'status' => 1,
                        'remarks' => filter_var($_POST['remarks']),
                        'created_on' => date('Y-m-d H:i:s'),
                        'created_by' => 'admin',
                    );
                    $this->db->insert('tbl_stocks', $TableData);
                    redirect("inventory/stocks/list");
                }
                loadView("inventory/stocks/add", $data);
                break;
            case 'edit':
                if (isset($_POST['submit'])) {
                    $id = $this->uri->segment(4);
                    $TableData = array(
                        'stocklocations_id' => filter_var($_POST['stocklocations_id']),
                        'title' => filter_var($_POST['title']),
                        'items_id' => filter_var($_POST['items_id']),
                        'qty' => filter_var($_POST['qty']),
                        'price' => filter_var($_POST['price']),
                        'display_order' => filter_var($_POST['display_order']),
                        'status' => 1,
                        'remarks' => filter_var($_POST['remarks']),
                        'created_on' => date('Y-m-d H:i:s'),
                        'created_by' => 'admin',
                    );
                    $this->db->where('stock_id', $id);
                    $this->db->update('tbl_stocks', $TableData);
                    redirect("inventory/stocks/list");
                }
                $id = $this->uri->segment(4);
                $this->db->where('stock_id', $id);
                $data['stock'] = $this->db->get("tbl_stocks")->row();
                loadView("inventory/stocks/add", $data);
                break;
            case 'summary':
                $data['StockRecords'] = $this->MStocks->getStockSummary();
                // pre($data['StockRecords']);
                loadView("inventory/stocks/summary", $data);
                break;
            case 'delete':
                $id = $this->uri->segment(4);
                $this->db->where('stock_id', $id);
                $this->db->delete('tbl_stocks');
                redirect("inventory/stocks/list");
                break;
            default:
                $data['StockRecords'] = $this->MStocks->getStockRecords();
                loadView("inventory/stocks/list", $data);
        }
    }
}