<?php class panel_model extends CI_Model{

	private $table_name	= 'casos_eap';			// Casos Eap
	private $table_name_2	= 'solicitudes';			// Solicitudes Eap

	public function get_cases_last_months($last_months = 3)
	{
		$this->db->from($this->table_name);
		$this->db->select("year(fecha) as anio,month(fecha)as mes,(month(now())-month(fecha))as mes_orden,estado, count(*) as cantidad",false);
		$this->db->where("fecha >= last_day(now()) + interval 1 day - interval $last_months month",null);
		$this->db->group_by('year(fecha),month(fecha),(month(now())-month(fecha)), estado');
		$query=$this->db->get();
		return $query->result_array();
	}

	public function get_cases_total_by_status($status = null,$date_from = null,$date_until = null,$urgency=null)
	{

		$this->db->from($this->table_name);
		$this->db->select("count(*) as cantidad");

		if ($date_from != null and $status == 'Cerrado') {$this->db->where("fechacierre >= '$date_from'");}
		if ($date_from != null and $status == null) {$this->db->where("fecha >= '$date_from'");}

		// Vencimientos de Abiertos No Informado
		if ($status == 'Abierto No Informado')
		{
			if ($date_from == null and $date_until != null) {$this->db->where("fecha < '$date_until'");}
			if ($date_from != null and $date_until != null) {$this->db->where("fecha >= '$date_from' and fecha < '$date_until'");}
			if ($date_from != null and $date_until == null) {$this->db->where("fecha >= '$date_from'");}
		}

		//if ($date_until != null and $status != 'Cerrado') {$this->db->where("fecha <= '$date_until'");}

		if ($urgency != null) {$this->db->where("bandera",$urgency);}
		if ($status != null) {$this->db->where_in("estado",$status);}
		$query=$this->db->get();
		return $query->row_array();
	}

  // SOLICITUDES
	public function get_requests_total_by_status($status = null,$date_from = null,$date_until = null,$urgency=null)
	{

		$this->db->from($this->table_name_2);
		$this->db->select("count(*) as cantidad");

		if ($date_from != null) {$this->db->where("fecha_creacion >= '$date_from'");}
		if ($date_until!= null) {$this->db->where("fecha_creacion <= '$date_until'");}
		if ($status != null) {$this->db->where_in("estado",$status);}
		if ($urgency != null) {$this->db->where_in("urgencia",$urgency);}
		$query=$this->db->get();
		return $query->row_array();
	}

}
