<?php

class StoreInventory extends CI_Model
{
    public $recordID;
    public $transID;
    public $catCode;
    public $bQty;
    public $eQty;
    public $transDate;
    public $sortOrder;

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

        $this->load->database();
        $this->db->reconnect();
    }

    public function getStoreInventoryByTransID($transID)
    {
        return $this->db->order_by('sortOrder', 'ASC')->get_where("storeinventory", array("transID"=>$transID));
    }

    public function getNextID($transID)
    {
        $nextID = 0;

        $this->db->select("IFNULL(MAX(CAST(replace(recordID, transID, '') AS UNSIGNED)), 0) + 1 as NextID");
        $result = $this->db->get_where("storeinventory", array("transid"=>$transID));

        foreach($result->result() as $row)
        $nextID = $row->NextID;

        return $nextID;
    }

    public function addStoreInventory()
    {
        $dateFormatedtransDate = DateTime::createFromFormat('m/d/Y', $this->transDate);
		$this->transDate = $dateFormatedtransDate->format('Y-m-d');

		return $this->db->insert('storeinventory', $this);
    }

    public function addStoreInventoryBatch($storeInventory)
    {
        return $this->db->insert_batch('storeinventory', $storeInventory);
    }

    public function updateStoreInventory($recordID, $bQty, $eQty)
    {
        $this->db->where(array("recordID"=>$recordID));
        
        return $this->db->update("storeinventory", array("bQty"=>$bQty, "eQty"=>$eQty));
    }

    public function deleteStoreInventoryByTransId($transid)
    {
        $this->db->where('transID', $transid);
        return $this->db->delete("storeinventory");
    }
}