<?php

class Produto_Model_CoberturasEventos extends Agro_Db_Table_Abstract {
	protected $_schema = 'produto';
	protected $_name = 'coberturas_eventos';
	protected $_primary = array('id_cobertura', 'id_evento');
	protected $_sequence = false;
		
	public function getEventos($cobertura) {
		$select = $this->select()->setIntegrityCheck(false)->distinct(true);
		$select->from(array('ce'=>'produto.coberturas_eventos'));
		$select->where(' id_cobertura = ?', $cobertura);

		$Rs = $this->fetchAll($select); 
		
		$ret = array();
		if ($Rs) {
			foreach ($Rs as $row) {
				$ret[] = $row->id_evento;
			}
		}

		return $ret;
	}
    
    public function getCoberturaEventos($idCobertura, bool $somenteChecked = false) {
		$select = $this->select()->setIntegrityCheck(false);
		$select->from(array('e'=>'produto.eventos'),array('id as id_evento', 'ds_nome_evento'));
        $select->joinLeft(array('ce'=>'produto.coberturas_eventos'), 'ce.id_evento = e.id and ce.id_cobertura = '.$idCobertura, array('ce.id_evento as checked','ce.cd_evento_seguradora'));
		$select->where('e.fl_ativo = ?', true);

        if ($somenteChecked) {
            $select->where('ce.id_evento is not null');
        }

		return $this->fetchAll($select)->toArray();
    }

    public function getEventosByCobertura($idCobertura) {
    	$select = $this->select()->setIntegrityCheck(false);
    	$select->from(array('e'=>'produto.eventos'),array('id as id_evento', 'ds_nome_evento'));
    	$select->join(array('ce'=>'produto.coberturas_eventos'), 'ce.id_evento = e.id' , array('ce.id_evento as checked','ce.ds_observacao'));
    	$select->where('e.fl_ativo = ?', true);
    	$select->where('ce.id_cobertura = ?', $idCobertura);
    	$select->order('e.ds_nome_evento');
    	return $this->fetchAll($select)->toArray();
    }    

    public function getCoberturasByEvento($idEvento) {
    	$select = $this->select()->setIntegrityCheck(false);
    	$select->from(array('c'=>'produto.coberturas'),array('id as id_cobertura', 'ds_nome_cobertura', 'ds_chave_cobertura'));
    	$select->join(array('ce'=>'produto.coberturas_eventos'), 'ce.id_cobertura = c.id' , false);
    	$select->where('c.fl_ativo = ?', true);
    	$select->where('ce.id_evento = ?', $idEvento);
    	$select->order('c.ds_nome_cobertura');
    	return $this->fetchAll($select)->toArray();
    }    
    
	public function getData($idCobertura){
		$select = $this->select()->setIntegrityCheck(false);
		$select->from(array('ce'=>'produto.coberturas_eventos'),array('id_evento'));
		$select->join(array('e'=>'produto.eventos'),'ce.id_evento = e.id','ds_nome_evento');
		$select->where(' ce.id_cobertura = ' . $idCobertura . 'and ce.fl_ativo = true');
		
		return $select;
	}

	
	/**
	 * Retorna o código do evento de acordo com o de/para de ids antigos do evento e os novos (válidos a partir de 2025)
	 *
	 * @param int $idProposta
	 * @param int $cdEventoSeguradora
	 * @return int
	 */
	public static function deParaI4Pro(int $idProposta, int $cdEventoSeguradora): int
	{
		$sql = "SELECT produto.de_para_eventos_i4pro({$idProposta}, {$cdEventoSeguradora});";
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchOne($sql);
	}


	/**
	 * Retorna o código do evento de acordo com o ID do Aviso informado
	 *
	 * @param int $idAviso
	 * @return int
	 */
	public static function getEventoI4ProByAviso(int $idAviso): int
	{
		$sql = "SELECT produto.get_evento_i4pro_by_aviso({$idAviso});";
		return Agro_Db_Table_Abstract::getDefaultAdapter()->fetchOne($sql);
	}
}
