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

class Model_user_recurring extends CI_Model {

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

		$this->db->select('a.*,
			b.action_efective_date,
			c.type as action_type');

		if($staff_id) $this->db->where('a.user_id',$staff_id);
		if($latest) $this->db->where("a.profiling_id IN ($max)");
		if($profiling_id) $this->db->where('a.profiling_id',$profiling_id);
		$this->db->where('a.recurring_type',$recurring_type);

		$this->db->where('a.is_delete',0);
		$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__recurring a');

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

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

	public function editRecurring($input,$id)
	{
		$this->db->set($input);
		$this->db->where('id',$id);
		$this->db->update('user__recurring');
	}
}
?>