<?php

class BranchItemLedger 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 getDataByBrCode($brCode)
    {
        return $this->db->get_where("branchitemledger", array("brCode"=>$brCode));
    }

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

    public function getDataByItemcodeWithBranchExcempt($itemcode)
    {
        $this->db->where("itemcode=".$itemcode." AND brCode NOT IN (SELECT brCode FROM branchexemption)");
        return $this->db->get_where("branchitemledger");
    }

    public function getDataByBrCodeItemcode($brCode, $itemcode)
    {
        $result = $this->db->get_where("branchitemledger", array("brCode"=>$brCode, "itemcode"=>$itemcode));
        return $result->row();
    }

    public function brCodeAndItemcodeExist($itemcode, $brCode)
    {
        $result = $this->db->get_where("branchitemledger", array("itemcode"=>$itemcode, "brCode"=>$brCode));

        if($result->num_rows() > 0)
            return true;
        else
            return false;
    }

    public function getDataViewByBrCode($brCode)
    {
        $sql = "SELECT branchitemledger.itemcode, items.modelno, items.seriescode, items.item_desc, items.karat, items.size, items.grams, items.cts, items.mrp, items.srp, items.manref, items.catCode, items.goldID, items.supCode, items.modifieddate, items.sourceDate, branchitemledger.brCode, branchitemledger.beginningqty, branchitemledger.inqty, branchitemledger.outqty, branchitemledger.sales, branchitemledger.adjustment, branchitemledger.endingqty, branchitemledger.editDate, branchitemledger.creationDate, branchitemledger.remarks FROM branchitemledger LEFT JOIN items ON branchitemledger.itemcode=items.itemcode WHERE branchitemledger.brCode=? ORDER BY items.catCode, items.modelno";
        return $this->db->query($sql, array($brCode));
    }

    public function getItemTransactionHistory($modelno, $brCode)
    {
        $sql = "SELECT transID, modelno, (CASE WHEN transType='STOCKIN' THEN qty ELSE 0 END) as InQty, (CASE WHEN transType='STOCKOUT' THEN qty ELSE 0 END) as OutQty, (CASE WHEN transType='SALES' THEN qty ELSE 0 END) AS SalesQty, transType, transDate FROM (SELECT transID, modelno, qty, transDate, 'STOCKIN' AS transType FROM stockin WHERE modelno='".$this->db->escape_str($modelno)."' AND substring(transID, 1, (CHAR_LENGTH(transID)-8))='".$brCode."' UNION SELECT transID, modelno, qty, transDate, 'STOCKOUT' AS transType FROM stockout WHERE modelno='".$this->db->escape_str($modelno)."' AND substring(transID, 1, (CHAR_LENGTH(transID)-8))='".$brCode."' UNION SELECT transID, modelno, qty, transDate, 'SALES' AS transType FROM sales WHERE modelno='".$this->db->escape_str($modelno)."' AND substring(transID, 1, (CHAR_LENGTH(transID)-8))='".$brCode."') AS ItemMovement ORDER BY transDate";
        return $this->db->query($sql);
    }



    public function addNewData()
    {
        $data = array(
            "itemcode" => $this->itemcode,
            "brCode" => $this->brCode,
            "beginningqty" => $this->beginningqty,
            "inqty" => $this->inqty,
            "outqty" => $this->outqty,
            "sales" => $this->sales,
            "adjustment" => $this->adjustment,
            "endingqty" => $this->endingqty,
            "remarks" => $this->remarks
        );

        $this->db->set("editDate", "NOW()", false);
        $this->db->set("creationDate", "NOW()", false);

        return $this->db->insert("branchitemledger", $data);
    }

    public function addNewMultiData($data)
    {
        return $this->db->insert_batch("branchitemledger", $data);
    }

    public function prepareNewInventory($brCode, $data)
    {
        $result = FALSE;

        if($this->deleteByBrCode($brCode) !== FALSE)
            $result = $this->addNewMultiData($data);

        return $result;
    }



    public function updateInQty($itemcode, $brCode, $qty, $isIncrementQty=TRUE)
    {
        if($isIncrementQty)
        {
            $this->db->set("inqty", "inqty+".$qty, FALSE);
            $this->db->set("endingqty", "endingqty+".$qty, FALSE);
        }
        else
        {
            $this->db->set("inqty", "inqty-".$qty, FALSE);
            $this->db->set("endingqty", "endingqty-".$qty, FALSE);
        }

        $this->db->set("editDate", "NOW()", FALSE);
        $this->db->where(array("itemcode"=>$itemcode, "brCode"=>$brCode));

        return $this->db->update("branchitemledger");
    }

    public function updateOutQty($itemcode, $brCode, $qty, $isIncrementQty=TRUE)
    {
        if($isIncrementQty)
        {
            $this->db->set("outqty", "outqty+".$qty, FALSE);
            $this->db->set("endingqty", "endingqty-".$qty, FALSE);
        }
        else
        {
            $this->db->set("outqty", "outqty-".$qty, FALSE);
            $this->db->set("endingqty", "endingqty+".$qty, FALSE);
        }

        $this->db->set("editDate", "NOW()", FALSE);
        $this->db->where(array("itemcode"=>$itemcode, "brCode"=>$brCode));

        return $this->db->update("branchitemledger");
    }

    public function updateSalesQty($itemcode, $brCode, $qty, $isIncrementQty=TRUE)
    {
        if($isIncrementQty)
        {
            $this->db->set("sales", "sales+".$qty, FALSE);
            $this->db->set("endingqty", "endingqty-".$qty, FALSE);
        }
        else
        {
            $this->db->set("sales", "sales-".$qty, FALSE);
            $this->db->set("endingqty", "endingqty+".$qty, FALSE);
        }

        $this->db->set("editDate", "NOW()", FALSE);
        $this->db->where(array("itemcode"=>$itemcode, "brCode"=>$brCode));

        return $this->db->update("branchitemledger");
    }

    public function updateAdjustmentQty($itemcode, $brCode, $qty, $remarks, $isIncrementQty=true)
    {
        if($isIncrementQty)
        {
            $this->db->set("adjustment", "adjustment+".$qty, FALSE);
            $this->db->set("endingqty", "endingqty+".$qty, FALSE);
        }
        else
        {
            $this->db->set("adjustment", "adjustment-".$qty, FALSE);
            $this->db->set("endingqty", "endingqty-".$qty, FALSE);
        }

        $this->db->set("editDate", "NOW()", FALSE);
        $this->db->set("remarks", "CONCAT(remarks, '|', '".$remarks."')", FALSE);
        $this->db->where(array("itemcode"=>$itemcode, "brCode"=>$brCode));

        return $this->db->update("branchitemledger");
    }

    public function updateAllQtyAndRemarks()
    {
        $data = array(
            "beginningqty" => $this->beginningqty,
            "inqty" => $this->inqty,
            "outqty" => $this->outqty,
            "sales" => $this->sales,
            "adjustment" => $this->adjustment,
            "endingqty" => $this->endingqty,
            "remarks" => $this->remarks
        );

        $where = array(
            "itemcode" => $this->itemcode,
            "brCode" => $this->brCode
        );

        $this->db->set("editDate", "NOW()", false);

        return $this->db->update("branchitemledger", $data, $where);
    }



    public function deleteByBrCode($brCode)
    {
        return $this->db->delete("branchitemledger", array("brCode"=>$brCode));
    }
}