<?php

class StockIn extends CI_Model
{
    public $recordID;
    public $transID;
    public $dr;
    public $catCode;
    public $modelno;
    public $qty;
    public $transDate;
    public $validated;

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

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

    public function getStockInByTransID($transID)
    {
        return $this->db->get_where("stockin", array("transID"=>$transID));
    }

    public function getStockInByRecordID($recordID)
    {
        $result = $this->db->get_where("stockin", array("recordID"=>$recordID));
        return $result->row();
    }

    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("stockin", array("transid"=>$transID));

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

        return $nextID;
    }


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

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

    public function deleteStockIn($recordID)
    {
        return $this->db->delete("stockin", array("recordID"=>$recordID));
    }
}