<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Model_delivery extends CI_Model {

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

	public function getDeliveryList($ID = null, $requester_id=null, $year = null)
	{
		$query = $this->db->select('a.*, a.department_id as requester_department_id, b.shortName as department_short_name, b.name as requester_dept, c.name as requester_branch')
				->select('e.name as created_name, e.email as created_email, f.department_id as created_department_id, g.name as created_dept_name, h.name as created_branchName')
				->select('d.detail_name, d.detail_address, d.detail_item, d.detail_remark')
				->select('(select status from sv__approval where table_name="sv__delivery" and id_table=a.ID and is_delete=0 order by ID desc limit 1) as status_app')
				->select('(select reason from sv__approval where table_name="sv__delivery" and id_table=a.ID and is_delete=0 order by ID desc limit 1) as status_reason')
                ->join('company__department b', 'b.id = a.department_id', 'LEFT')
                ->join('company__branch c', 'c.id = b.branch_id','LEFT')
                ->join('sv__delivery_detail d', 'd.delivery_id = a.ID and d.is_delete = 0','LEFT')
                ->join('user e', 'e.id = a.created_by','LEFT')
                ->join('user__employment_detail f', 'f.id = a.created_by', 'LEFT')
                ->join('company__department g', 'g.id = f.department_id', 'LEFT')
                ->join('company__branch h', 'h.id = g.branch_id','LEFT')
				->where('a.is_delete',0)
				->group_by('a.ID')
				->order_by('a.ID desc');

        if($ID)
		{
			$this->db->where('a.ID',$ID);
		}
        
        if(!empty($requester_id))
		{
			$this->db->where('(a.requester_id = '.$requester_id.' OR a.created_by = '.$requester_id.')');
		}

		if($year)
		{
			$this->db->where('year(a.created_ts)',$year);
		}

        $query = $this->db->get('sv__delivery a')
				->result_array();

		// debug($query);
		// die(debug($this->db->last_query()));
		return (!empty($query))?$query:null;
	}

	public function getDeliveryByWeek()
	{
		$query = $this->db->select('a.*, d.name, b.shortName, b.name as dept_name, c.name as branchName')
				->join('user__employment_detail d', 'd.id = a.requester_id', 'LEFT')
				->join('company__department b', 'b.id = d.department_id', 'left')
				->join('company__branch c', 'c.id = b.branch_id','left')
				->where('DATE(a.date) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 WEEK)')
				->where('a.is_delete',0)
				->order_by('a.ID asc')
				->get('sv__delivery a')
				->result_array();
				
		// die(debug($this->db->last_query()));
		return (!empty($query))?$query:null;
	}

	public function update_delivery($sets = array(), $where = array())
	{
		foreach ($sets as $key => $set) 
		{
			$this->db->set($key,$set);
		}
		foreach ($where as $key => $wh) 
		{
			$this->db->where($key,$wh);
		}
		$this->db->update('sv__delivery');
		return $this->db->affected_rows();
	}

	public function getRequestByCompany($year = null)
	{
		$query = $this->db->select('a.name as dept_name, a.shortName, b.name as branchName, count(d.id) as total_request')
							->join('company__branch b','b.id = a.branch_id','LEFT')
							->join('user__employment_detail c','c.department_id = a.id','LEFT')
							->join('sv__delivery d','d.requester_id = c.id AND d.is_delete = 0 and YEAR(d.created_ts) = '.$year.'','LEFT')
							->where('a.is_delete',0)
							->group_by('a.id')
							->get('company__department a');

		// die(debug($this->db->last_query()));
		return (!empty($query))?$query->result_array():null;
    }
}


?>