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

class Model_flight_booking extends CI_Model {

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

	public function getlistName($department_id)
	{
        if($department_id > 0)
		{
			$this->db->where('a.department_id',$department_id);
        }

        $query = $this->db->select('a.*')
				->where('a.is_delete',0)
				->order_by('a.name', 'ASC')
				->get('user__employment_detail a')
				->result_array();
		//die(debug($this->db->last_query()));
		return (!empty($query))?$query:null;
	}

	public function getListFlightBooking($ID = null, $requester_id=null, $year = null)
	{
		// debug($requester_id);
		$query = $this->db->select('a.*, b.name as requestor, c.shortName as department_short_name, c.id as department_id, c.name as dept_name, d.name as branchName, e.email as requestor_email, e.staff_id, group_concat(f.ID) as flight_guest_id, group_concat(f.guest_id) as guest_id, group_concat(g.name) as guest_name, group_concat(if(g.phone != "",g.phone,"N/A")) as guest_phone, group_concat(if(f.passport != "",f.passport,"N/A")) as guest_passport, group_concat(if(g.ic_no != "",g.ic_no,"N/A")) as guest_ic_no,')
				->select('h.name as created_name, h.email as created_email, i.department_id as created_department_id, j.name as created_dept_name, k.name as created_branchName')
				->select('(select status from sv__approval where table_name="sv__flight__booking" 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__flight__booking" and id_table=a.ID and is_delete=0 order by ID desc limit 1) as status_reason')
                ->join('user__employment_detail b', 'b.id = a.requester_id', 'LEFT')
                ->join('company__department c', 'c.id = b.department_id', 'LEFT')
                ->join('company__branch d', 'd.id = c.branch_id')
                ->join('user e', 'e.id = a.requester_id','LEFT')
                ->join('sv__flight__booking_guest f','f.flight_id = a.ID and f.is_delete = 0','LEFT')
                ->join('user g', 'g.id = f.guest_id','LEFT')
                ->join('user h', 'h.id = a.createdBY','LEFT')
                ->join('user__employment_detail i', 'i.id = a.createdBY', 'LEFT')
                ->join('company__department j', 'j.id = i.department_id', 'LEFT')
                ->join('company__branch k', 'k.id = j.branch_id')
				->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.createdBY = '.$requester_id.')');
			// $this->db->where('a.requester_id',$requester_id);
   //          $this->db->or_where('a.createdBY',$requester_id);
		}

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

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

	public function getBookingFlightByWeek()
	{
		$query = $this->db->select('a.*, 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.departure_date) BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 1 WEEK)')
				->where('a.is_delete',0)
				->order_by('a.ID asc')
				->get('sv__flight__booking a')
				->result_array();
				
		return (!empty($query))?$query:null;
	}

	public function update_flight($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__flight__booking');
		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__flight__booking d','d.requester_id = c.id AND d.is_delete = 0 and YEAR(d.createdTS) = '.$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;
    }
}


?>