<?php

class ItemLedger extends CI_Model
{
    public $itemcode;
    public $brCode;
    public $beginningqty;
    public $inqty;
    public $outqty;
    public $sales;
    public $adjustment;
    public $endingqty;
    public $editDate;
    public $creationDate;
    public $remarks;

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

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

    public function getBranchesByItemcode($itemcode)
    {
        return $this->db->get_where("itemledger", array("itemcode"=>$itemcode));
    }

    public function getBranchesBySeriesCode($seriescode, $includeKWH)
    {
        $this->db->select("itemledger.*, items.*");
        $this->db->from("itemledger");
        $this->db->join("items", "itemledger.itemcode = items.itemcode", "left");
        $this->db->where("items.seriescode", $seriescode);
        if(!$includeKWH) $this->db->where("itemledger.brCode != 'KWH'"); // exclude KWH
        $this->db->where("itemledger.endingqty > 0");
        return $this->db->get();
    }

    public function getBranchesBySeriesCodeWithBranchExcempt($seriescode, $includeKWH)
    {
        $this->db->select("itemledger.*, items.*");
        $this->db->from("itemledger");
        $this->db->join("items", "itemledger.itemcode = items.itemcode", "left");
        $this->db->like("items.seriescode", $seriescode);
        $this->db->where("itemledger.endingqty > 0");
        if(!$includeKWH) $this->db->where("itemledger.brCode != 'KWH'"); // exclude KWH
        $this->db->where("itemledger.brCode NOT IN (SELECT brCode FROM branchexemption)");
        return $this->db->get();
    }

    public function getBranchesByDescWithBranchExcempt($itemdesc, $includeKWH)
    {
        $this->db->select("itemledger.*, items.*");
        $this->db->from("itemledger");
        $this->db->join("items", "itemledger.itemcode = items.itemcode", "left");
        $this->db->like("items.item_desc", $itemdesc);
        $this->db->where("itemledger.endingqty > 0");
        if(!$includeKWH) $this->db->where("itemledger.brCode != 'KWH'"); // exclude KWH
        $this->db->where("itemledger.brCode NOT IN (SELECT brCode FROM branchexemption)");
        return $this->db->get();
    }

    public function getItemLedgerOfKWHByItemcode($itemcode)
    {
        $result =  $this->db->get_where("itemledger", array("itemcode"=>$itemcode, "brCode"=>"KWH"));

        if($result->num_rows() > 0)
            return $result->row();
        else
        {
            $itemledger = new ItemLedger();

            $itemledger->itemcode = $itemcode;
            $itemledger->brCode = "KWH";
            $itemledger->beginningqty = 0;
            $itemledger->inqty = 0;
            $itemledger->outqty = 0;
            $itemledger->sales = 0;
            $itemledger->adjustment = 0;
            $itemledger->endingqty = 0;
            $itemledger->editDate = date('Y-m-d');
            $itemledger->creationDate = date('Y-m-d');
            $itemledger->remarks = "";

            return $itemledger;
        }
    }
}