<?php

class ItemInquiryLog extends CI_Model
{
    public $logid;
    public $itemcode;
    public $modelno;
    public $catCode;
    public $brCode;
    public $searchedBy;
    public $dateSearched;

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

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


    public function getDataByBranch($brCode)
    {
        return $this->db->get_where("iteminquirylog", array("brCode"=>$brCode));
    }

    public function getDataByBranchAndDateRange($brCode, $dateFrom, $dateTo)
    {
        $this->db->where("brCode=".$brCode." AND DATE(dateSearched) BETWEEN '".date('Y-m-d', strtotime($dateFrom))."' AND '".date('Y-m-d', strtotime($dateTo))."'");
        return $this->db->get("iteminquirylog");
    }

    public function getSummaryPerCategory($brCode, $dateFrom, $dateTo)
    {
        $sql = "SELECT catCode, COUNT(itemcode) AS SearchCount FROM iteminquirylog WHERE brCode=? AND DATE(dateSearched) BETWEEN ? AND ? GROUP BY catCode";
        return $this->db->query($sql, array($brCode, date('Y-m-d', strtotime($dateFrom)), date('Y-m-d', strtotime($dateTo))));
    }

    public function getSummaryPerItemcodeCategory($brCode, $catCode, $dateFrom, $dateTo)
    {
        $sql = "SELECT catCode, itemcode, modelno, COUNT(itemcode) AS SearchCount FROM iteminquirylog WHERE brCode=? AND catCode=? AND DATE(dateSearched) BETWEEN ? AND ? GROUP BY catCode,itemcode,modelno";
        return $this->db->query($sql, array($brCode, $catCode, date('Y-m-d', strtotime($dateFrom)), date('Y-m-d', strtotime($dateTo))));
    }


    public function addNewData()
    {
        $data = array(
            "itemcode" => $this->itemcode,
            "modelno" => $this->modelno,
            "catCode" => $this->catCode,
            "brCode" => $this->brCode,
            "searchedBy" => $this->searchedBy,
            "dateSearched" => $this->dateSearched
        );

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