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

class Model_third_party_payment extends CI_Model {

	public function fetchPayment($staff_id = null, $latest = false, $profiling_id = null)
	{
		//inner query
		$this->db->select('MAX(a.profiling_id)');
		$this->db->from('user__third_party_payment a');
		if($staff_id) $this->db->where('a.user_id',$staff_id);
		$this->db->where('a.is_delete',0);
		$max = $this->db->get_compiled_select();

		//outer query
		$this->db->select('a.*,
							b.action_efective_date,
							c.type as action_type');
		
		if($staff_id) $this->db->where('a.user_id',$staff_id);
		$this->db->where('a.is_delete',0);
		if($latest) $this->db->where("a.profiling_id IN ($max)");
		if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);

		$this->db->join('user__profiling b','b.id = a.profiling_id','left');
		$this->db->join('_ref__action_type c','c.id = b.action_type_id','left');

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

		return ($query->num_rows() > 0)? $query->result() : null;
	}

	public function createPayment($input)
	{
		$this->db->insert('user__third_party_payment',$input);
		return $this->db->insert_id();
	}

	public function editPayment($input,$id)
	{
		$this->db->set($input);
		$this->db->where('id',$id);
		$this->db->update('user__third_party_payment');

		return true;
	}
	
	public function get_payment($id)
	{
		$this->db->where('id',$id);
		return $this->db->get('user__third_party_payment')->result_array();
	}
}
?>