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

class Colonias_model extends Ion_auth_model {
	
    public function __construct()
    {
        parent::__construct();
	}
	
	/**
	 * colonias
	 *
	 * @return object
	 * @author Ben Edmunds
	 **/

	public function view_all($tabla, $where = NULL)
	{
		$this->trigger_events('colonias');

		// run each where that was passed
		
		$this->response = $this->db->get_where($this->tables[$tabla], $where)->num_rows();
		
		return $this;
	}

	public function dropdown_data($tabla = NULL, $where = NULL, $order_by = NULL)
	{
		if ($where) {
			foreach ($where as $k => $v) {

				$this->response = $this->db->where($k, $v);
			}
		}
		
		if ($order_by) {
			$this->response = $this->db->order_by($order_by, 'ASC');
		}

		$this->response = $this->db->get($this->tables[$tabla]);

		return $this;
	}
	

	/**
	 * colonia
	 *
	 * @return object
	 * @author Ben Edmunds
	 **/

	public function view($tabla, $where = NULL)
	{
		$this->trigger_events('colonia');

		$this->response = $this->db->get_where($this->tables[$tabla], $where);
		
		return $this;
	}

	/**
	 * create_colonia
	 *
	 * @author aditya menon
	*/
	public function create_colonia($arraycampos = NULL)
	{
		// bail if the group name already exists
		$existing_colonia = $this->db->get_where($this->tables['colonias'], $arraycampos)->num_rows();

		
		if($existing_colonia !== 0)
		{
			$this->ion_auth->set_error('colonia_already_exists');
			return FALSE;
		}
		
		// insert the new group
		$this->db->insert($this->tables['colonias'], $arraycampos);
		$colonia_gid = $this->db->insert_id();

		// report success
		$this->set_message('colonia_creation_successful');
		// return the brand new group id
		return $colonia_gid;
	}

	
	
	/**
	 * update_colonia
	 *
	 * @return bool
	 * @author aditya menon
	 **/

    public function update_colonia($gid, $arraycampos)
	{
		$this->db->update($this->tables['colonias'], $arraycampos, array('gid' => $gid));

        $this->ion_auth->set_message('colonias_update_successful');

        return TRUE;
	}

	/**
	 * eliminar_parcelas
	 * elimina parcelas correspondientes a la colonia de la tabla in_temp
	 * @return bool
	 * @author Diego Ferreira
	 **/

    public function eliminar_parcelas($tabla, $where)
	{
		$this->db->delete($this->tables[$tabla], $where);

        return TRUE;
	}

	public function obtener_pericia ($tabla, $where)
	{
		//$pericia = $this->db->pg_query($this->tables['in_planopericia2'], $where);
		$pericia = $this->db->get_where($this->tables[$tabla], $where);
		if ($pericia->num_rows() > 0) {
			$this->response = $pericia;
		}else{
			return false;
		}

		return $this;
	}

	public function generar_pericia ($objectid)
	{
		
		$pericia = $this->db->query('SELECT indert.pericias_generarpericia('.$objectid.');');
		if ($pericia) {
			return TRUE;
		}else{
			return FALSE;
		}
	}

	public function contador_registros ($tabla = NULL, $arraywhere = NULL)
	{
		foreach ($arraywhere as $k => $v) {

			$this->db->where($k, $v);
		}
		$this->db->from($this->tables[$tabla]);
		return $this->db->count_all_results();
	}
}