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

class Mo_admin extends CI_Model {

	public $table = 't_user';
	
	function Auth($username,$password)
	{
		$this->db->where('f_user_email',$username);
		$this->db->where('f_user_password',sha1(md5($password)));
		$sql = $this->db->get($this->table);
		if($sql->num_rows()>0)
		{
			return $sql->row();
		}
		else
		{
			return FALSE;
		}
		
	}
	
	function fnActionPlan()
	{
		$this->db->select('a.f_cfl_activity_id, a.f_cfl_id, b.f_cfl_cro_id,f_account_name, f_training_category, f_cfl_activity_action_date, f_cfl_activity_plan,f_emp_name',false);
		$this->db->where('a.f_cfl_status is null');
		$this->db->group_by('b.f_account_id,b.f_training_category_id,a.f_cfl_id');
		$this->db->limit(0,30);
		$this->db->join('(select f_emp_name,f_user_id from t_employee a,t_user b where a.f_emp_id=b.f_emp_id) e','e.f_user_id=a.f_cfl_plan_cro');
	}

	function fnVisit()
	{
		$this->db->select('a.f_cfl_activity_id, a.f_cfl_id, f_account_name, f_training_category, f_cfl_activity_date, f_cfl_activity_note, (select f_status_activity_name from t_status_activity where f_status_activity_id=a.f_cfl_status) as f_cfl_status,f_emp_name',false);
		$this->db->where('a.f_cfl_status is not null');
		$this->db->group_by('b.f_account_id,b.f_training_category_id,a.f_cfl_id');
		$this->db->limit(0,30);
		$this->db->join('(select f_emp_name,f_user_id from t_employee a,t_user b where a.f_emp_id=b.f_emp_id) e','e.f_user_id=a.f_cfl_visit_cro');
	}
	
	function fnActivity()
	{
//		if($this->session->userdata('sLevel') == 3) $this->db->where('b.f_cfl_cro_id',$this->session->userdata('sId'));
		if($this->session->userdata('member')) $this->db->where_in('b.f_cfl_cro_id',$this->session->userdata('member'));
		$this->db->join('t_cfl_master b','b.f_cfl_id=a.f_cfl_id and b.f_cfl_active=1');
		$this->db->join('t_training_category c','c.f_training_category_id=b.f_training_category_id','left');
		$this->db->join('t_accounts d','d.f_account_id=b.f_account_id');
		$sql = $this->db->get('t_cfl_activity a');
		if($sql->num_rows()>0)
		{
			return $sql->result();
		}
		else
		{
			return FALSE;
		}		
	}
	
	function fnCountStatus()
	{
		$this->db->select('a.f_cfl_status,b.f_status_activity_name,count(a.f_cfl_status) total',false);
		$this->db->join('t_status_activity b','b.f_status_activity_id=a.f_cfl_status','left outer');
		//if($this->session->userdata('sLevel') == 3)$this->db->where('a.f_cfl_cro_id',$this->session->userdata('sId'));
		if($this->session->userdata('member')) $this->db->where_in('a.f_cfl_cro_id',$this->session->userdata('member'));
		$this->db->where('a.f_cfl_active',1);
		$this->db->group_by('a.f_cfl_status');
		$sql = $this->db->get('t_cfl_master a');
		
		$act = array();
		if($sql->num_rows()> 0)
		{
			foreach($sql->result() as $a)
			{
				if($a->f_cfl_status)
				{
					$act[$a->f_cfl_status] = array(
						"name"=> $a->f_status_activity_name,
						"total"=> $a->total,
					);
				}
			}
		}
		
		$sql1 = $this->db->get('t_status_activity')->result();
		foreach($sql1 as $b)
		{
			$final[$b->f_status_activity_id] = ($act[$b->f_status_activity_id]) ? $act[$b->f_status_activity_id]['total'] : 0;
		}
		//echo "result <pre>";echo print_r($final)."</pre><br/>";
		return $final; 
	}
	
	function fnStatus($id)
	{
		$this->db->where('a.f_cfl_active',1);
		$this->db->where('a.f_status_activity_id',$id);
		$sql = $this->db->get_where('t_status_activity a');
		if($sql->num_rows()>0)
		{
			$a = $sql->row();
			return $a->f_status_activity_name;
		}
		else
		{
			return FALSE;
		}
	}
}

/* End of file Mo_user.php */
/* Location: ./application/modules/User/models/Mo_user.php */	
	


