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

class Customers_model extends CI_Model
{
	
	public function __construct() {
		parent::__construct();
	}
	
	public function getCustomerByID($id) 
	{
		$q = $this->db->get_where('customers', array('id' => $id), 1); 
		if( $q->num_rows() > 0 ) {
			return $q->row();
		} 
		return FALSE;
	}
	
	public function addCustomer($data = array())
	{
		if($this->db->insert('customers', $data)) {
			return $this->db->insert_id();
		}
		return false;
	}
	
	public function updateCustomer($id, $data = array())
	{
		if($this->db->update('customers', $data, array('id' => $id))) {
			return true;
		}
		return false;
	}
	
	public function deleteCustomer($id) 
	{
		//*****************************TRJ059 - ALEXANDER ROCA - 21/06/2019************************
		/*if($this->db->delete('customers', array('id' => $id))) {
			return true;
		}*/
		
		if($this->db->update('customers', array('estado' => 0), array('id' => $id))) {
			return true;
		}
		//*****************************TRJ059 - ALEXANDER ROCA - 21/06/2019************************
		return FALSE;
	}
	// funcion para validar si existe el numero de documento
	public function check_document_number($document_number,$id)
	{
		//validar si el id es didiferente de null o vacio
		if($id != null || $id != ''){
			$this->db->where('id <>', $id);
		}
		$q = $this->db->get_where('customers', array('cf1' => $document_number), 1);		
		if( $q->num_rows() > 0 ) {
			return $q->row();
		} 
		return FALSE;
	}
	//funcion para validar si existe el numero de ruc
	public function check_numero_ruc($numero_ruc,$id)
	{
		if($id != null || $id != ''){
			$this->db->where('id <>', $id);
		}
		$q = $this->db->get_where('customers', array('cf2' => $numero_ruc), 1); 
		if( $q->num_rows() > 0 ) {
			return $q->row();
		} 
		return FALSE;
	}

}
