<?php

class Produto_Model_Eventos extends Agro_Db_Table_Abstract {
    const DS_CHAVE_ESTIAGEM = 'ESTIAGEM';
    const DS_CHAVE_EV_VENTOS_FORTES = 'EV_VENTOS_FORTES';

	const ID_EVENTO_GRANIZO = 6;

	protected $_schema = 'produto';
	protected $_name = 'eventos';
	protected $_primary = 'id';
	public $_order = 'ds_nome_evento';
	public $_columnExcel = array(
		'id' => 'Cód',
		'ds_nome_evento' => 'Evento',
		'ds_chave_evento' => 'Chave'
	);
	
	public function save($data){
		$Row = $this->getRow($data['id']);
		
		$Row->ds_nome_evento = $data['ds_nome_evento'];
		$Row->cd_evento_seguradora = ($data['cd_evento_seguradora'] ? $data['cd_evento_seguradora'] : null);
		$Row->ds_chave_evento = ($data['ds_chave_evento'] ? $data['ds_chave_evento'] : null);
		
		return $Row->save();
	}
	
	public function fetchCoberturas($cobertura) {
		$select = $this->select();
		$select->distinct(true);
		$select->setIntegrityCheck(false);
		$select->from(array('ce'=>'produto.coberturas_eventos'));
		$select->where('id_cobertura = ?', $cobertura);
		$Rs = $this->fetchAll($select);
		
		$ret = array();
		if (is_array($Rs)) {
			foreach ($Rs as $row) {
				$ret[] = $row->id_evento;
			}
		}
		
		return $ret;
	}
	
	public function getCheckEventos() {
		$rs = $this->fetchAll('fl_ativo = true', $this->_order)->toArray();
		
		if (is_array($rs)) {
			foreach ($rs as $evento) {
				$palavra = $evento['ds_nome_evento'];
				
				
				$this->eventos[$evento['id']] = $evento['ds_nome_evento'];
			}
		}
		
		return $this->eventos;
	}
	
	public function getEventosPorProposta($idProposta, bool $replantio = false, bool $quedaParreiral = false){
		if($idProposta){
		    
		    $whereEventosExcluidos = "(select 
                                            id_evento 
                                        from 
                                            produto.produtos_coberturas_eventos 
                                        where 
                                            fl_excluir_aviso_sinistro=true 
                                            and id_evento=ce.id_evento 
                                            and id_produto=pd.id
                                        )";
            
            $select = $this->select();
            $select->setIntegrityCheck(false);
            $select->from(array('pc'=>'seguro.propostas_coberturas'), null);
            $select->join(array('ce'=>'produto.coberturas_eventos'), 'ce.id_cobertura = pc.id_cobertura', false);
            $select->join(array('e'=>'produto.eventos'), 'e.id = ce.id_evento', array('id','ds_nome_evento','ds_chave_evento'));
            $select->join(array('pr'=>'seguro.propostas'), 'pr.id = pc.id_proposta', null);
            $select->join(array('pd'=>'produto.produtos'), 'pd.id = pr.id_produto', null);
            $select->join(array('pg'=>'produto.produtos_geral'), 'pg.id = pd.id_produto_geral', ['id_cultura']);
            $select->where('pc.id_proposta = ?', $idProposta);
            $select->where('ce.id_evento not in '.$whereEventosExcluidos);

            $inReplantio = $replantio ? '' : 'NOT';
            $select->where("ce.id_cobertura {$inReplantio} in(" . implode(', ', Produto_Model_Coberturas::getCoberturasReplantio()) . ")");

            $inQuedaParreiral = $quedaParreiral ? '' : 'NOT';
            $select->where("ce.id_cobertura {$inQuedaParreiral} in(" . implode(', ', Produto_Model_Coberturas::getCoberturasQuedaParreiral()) . ")");

            $select->group('e.id');
            $select->group('e.ds_nome_evento');
            $select->group('e.ds_chave_evento');
            $select->group('pg.id_cultura');
            $select->order('e.ds_nome_evento');

            return $this->fetchAll($select);
		} else {
			return null;
		}
	}
}