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

class Mo_customers extends CI_Model {

	public $table = 't_customers';
	
    function fnGetData()
    {
        $this->db->order_by('f_customer_id', 'DESC');
        return $this->db->get($this->table)->result();		
	}

	function getDataId($id)
	{
		$sql = $this->db->get_where($this->table,array('f_customer_id'=> $id))->row_array();
		return $sql;
	}

	function saveData()
	{
		$data = array(
				//'f_customer_id' => $this->input->post('f_customer_id'),
				'f_customer_name' => $this->input->post('f_customer_name'),
				'f_customer_link' => $this->input->post('f_customer_link'),
				'f_customer_active' => $this->input->post('f_customer_active'),
				'f_created_on' => date('Y-m-d H:i:s'),
				'f_created_by' => $this->session->userdata('sId'),
		);
		$sql = $this->db->insert($this->table,$data);
		return $sql;
	}

	function updateData($id)
	{
		$data = array(
				//'f_customer_id' => $this->input->post('f_customer_id'),
				'f_customer_name' => $this->input->post('f_customer_name'),
				'f_customer_link' => $this->input->post('f_customer_link'),
				'f_customer_active' => $this->input->post('f_customer_active'),
				'f_updated_on' => date('Y-m-d H:i:s'),
				'f_updated_by' => $this->session->userdata('sId'),
		);

		$this->db->where('f_customer_id',$id);
		$sql = $this->db->update($this->table,$data);
		return $sql;		
	}

	function deleteData($id)
	{
		$this->db->where('f_customer_id',$id);
		$sql = $this->db->delete($this->table);
		return $sql;		
	}
	
	function cekLink($id = null)
	{
		$this->db->where('f_customer_link', $this->input->post('f_customer_link'));
		if($id) $this->db->where('f_customer_id !=',$id);
		$sql = $this->db->get('t_customers');
		if ($sql->num_rows() > 0)
		{
			return FALSE;
		}
		else
		{
			return TRUE;
		}		
	}
	
	function comboData()
	{
		$sql = $this->db->get('t_customers');
		if($sql)
		{
			foreach($sql->result() as $r):
				$list[$r->f_customer_id] = $r->f_customer_id;
			endforeach;
		}
		return $list;
	}

	function remoteComboData()
	{
		if($this->input->get('term')) $this->db->like('f_customer_name',$this->input->get('term')); // pencarian name
		if($this->input->get('id')) $this->db->where('f_customer_id',$this->input->get('id')); // saat pencarian update
		
		$this->db->limit('10');
		$result = $this->db->get($this->table)->result();
		$DataJson = array();
		foreach($result as $row):
			$data['id']	= $row->f_customer_id; // value
			$data['text']	= $row->f_customer_name; // change display 
			array_push($DataJson,$data);
		endforeach;
		return $DataJson;
	}
}

/* End of file Mo_customers.php */
/* Location: ./application/modules/Customers/models/Mo_customers.php */	

	


