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

class Panel_control_model extends Ion_auth_model {
	
    public function __construct()
    {
        parent::__construct();
	}
/*********************** IN_TEMP *******************************/
	/**
	 * in_temp
	 *
	 * @return object
	 * @author Ben Edmunds
	 **/
	public function view_all($select,$tabla, $where = FALSE, $group_by = FALSE, $order_by = FALSE)
	{ 

		$this->response = $this->db->select($select);
		$this->response = $this->db->from($this->tables[$tabla]);
		if ($where) {
			foreach ($where as $k => $v) {

				$this->response = $this->db->where($k, $v);
			}
		}

		if ($group_by) {
			$this->response = $this->db->group_by($group_by);
		}

		if ($order_by) {
			$this->response = $this->db->order_by($order_by);
		}
		

		//$this->response = $this->db->order_by($group_by);		
		
		$this->response = $this->db->get();

		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;
	}

	
	/**
	 * View
	 *
	 * @return array //campos de un registro
	 * @author Ben Edmunds
	 **/

	public function view($tabla = NULL, $arraywhere = NULL) 
	{
		$this->trigger_events('view');
		
		$this->response = $this->db->select('*');
		$this->response = $this->db->from($this->tables[$tabla]);
		foreach ($arraywhere as $k => $v) {

			$this->response = $this->db->where($k, $v);
		}

		$this->response = $this->db->limit(1);


		$this->response = $this->db->get();

		return $this;
	}

	public function viewlist_abm($tabla = NULL, $id_registro_lote = NULL) 
	{
		$this->trigger_events('viewlist_abm');
		
		if (isset($id_registro_lote))
		{
			$this->response = $this->db->get_where($this->tables[$tabla],array('id_registro_lote' => $id_registro_lote));			
		}

		return $this;
	}
	
	/**
	 * update_gepcolonia
	 *
	 * @return bool
	 * @author aditya menon
	 **/
	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();
	}

    public function actualizar_registro($tabla = NULL, $arraycampos = NULL, $where = NULL)
	{
		if (empty($arraycampos)) return FALSE;

		$this->db->update($this->tables[$tabla], $arraycampos, $where);

		$this->set_message('in_temp_update_successful');

		return TRUE;
	}

	public function crear_registro($tabla = NULL, $arraycampos = NULL)
	{
		if (empty($arraycampos)) return FALSE;

		$this->db->insert($this->tables[$tabla], $arraycampos);

		$this->set_message('in_temp_update_successful');

		return TRUE;
	}

	public function eliminar_registro($tabla = NULL, $arraywhere){

		$this->db->delete($tabla, $arraywhere);

		return TRUE;
	}

	public function estadistica_verificador ($usuario)
	{
		$this->response = $this->db->query("SELECT 	sum(a.cantidad_registros) registros_asignados,
												   	d.count procesados,
												   	b.registros_analizados terminados,
												   	c.count liberados,
												   	e.count rechazados,
												   	f.count confirmados
											FROM asignacionlotes a, 
												(SELECT COUNT(*) registros_analizados 
												 FROM MONITOREO 
												 WHERE USUARIO_VERIFICADOR =  ".$usuario." AND 
												 REGISTRO_ANALIZADO = 1) b,
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_VERIFICADOR =  ".$usuario." AND 
												 liberado_reg_analizado = 1) c, 
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_VERIFICADOR =  ".$usuario." AND 
												 proceso = 'A03') d,
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_VERIFICADOR =  ".$usuario." AND 
												 proceso = 'A07') e, 
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_VERIFICADOR =  ".$usuario." AND 
												 proceso = 'A06') f 
											WHERE a.codigo_verificador = ".$usuario." 
											GROUP BY b.registros_analizados, c.count, d.count,e.count,f.count");
		return $this;
	}

	public function estadistica_responsable ($usuario)
	{
		$this->response = $this->db->query("SELECT 	sum(a.cantidad_registros) registros_asignados,
												   	d.count procesados,
												   	b.registros_analizados terminados,
												   	c.count liberados_verificador,
												   	e.count rechazados,
												   	f.count confirmados,
												   	g.count liberados_responsable
												   	--g.count con
											FROM asignacionlotes a, 
												(SELECT COUNT(*) registros_analizados 
												 FROM MONITOREO 
												 WHERE USUARIO_RESPONSABLE_COLONIA =  ".$usuario." AND 
												 REGISTRO_ANALIZADO = 1) b,
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_RESPONSABLE_COLONIA =  ".$usuario." AND 
												 liberado_reg_analizado = 1) c, 
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_RESPONSABLE_COLONIA =  ".$usuario." AND 
												 proceso = 'A03') d,
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_RESPONSABLE_COLONIA =  ".$usuario." AND 
												 proceso = 'A07') e, 
												(SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_RESPONSABLE_COLONIA =  ".$usuario." AND 
												 proceso = 'A06') f, 
												 (SELECT COUNT(*)
												 FROM MONITOREO 
												 WHERE USUARIO_RESPONSABLE_COLONIA =  ".$usuario." AND 
												 liberado_responsable_colonia = 1) g
											WHERE a.responsable_colonia = ".$usuario." 
											GROUP BY b.registros_analizados, c.count, d.count,e.count,f.count, g.count");
		return $this;
	}
}
	