<?php

class Seguro_Model_PropostasObservacoes extends Agro_Db_Table_Abstract {
	protected $_name = 'propostas_observacoes';
	protected $_schema = 'seguro';
	protected $_primary = 'id';
	protected $_log = true;

	public function save($data,$principal=false) {

	    if($principal){
	        $Row = $this->getRow($this->getIdObservacaoPrincipal($data['id_proposta']));
	    } else {
		    $Row = $this->getRow($data['id_observacao']);
	    }

		$Row->id_proposta = $data['id_proposta'];
		$Row->ds_observacao = ($data['ds_observacao'] ? $data['ds_observacao'] : '');
		$Row->fl_principal = ($data['fl_principal'] ? 1 : 0);
		$Row->fl_observacao_endosso = ($data['fl_observacao_endosso'] ? 1 : 0);
		$Row->fl_ocultar_itens_segurados = ($data['fl_ocultar_itens_segurados'] ? 1 : 0);
		$Row->save();

		return $Row->id;
	}

	public function getInformacoesAdicionais($proposta) {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('o' => 'seguro.propostas_observacoes'));
		$Select->join(array('u' => 'sistema.usuarios'), 'o.id_usuario_criacao = u.id', 'ds_nome_usuario');
		$Select->joinLeft(array('po' => 'seguro.propostas_observacoes_arquivos'), 'po.id_observacao = o.id', 'po.id as id_observacao_arquivo');
		$Select->joinLeft(array('f' => 'sistema.funcionarios'), 'u.id = f.id_usuario', 'ds_nome_funcionario');
		$Select->joinLeft(array('c' => 'sistema.corretores'), 'u.id = c.id_usuario', 'ds_nome_corretor');
		$Select->joinLeft(array('p' => 'sistema.prepostos'), 'u.id = p.id_usuario', 'ds_nome_preposto');
		$Select->where('id_proposta = ?', $proposta);
		$Select->where('fl_principal = false');
		$Select->order('dt_criacao desc');

		$Rs = $this->fetchAll($Select);

		if ($Rs->count()) {
			return $Rs->toArray();
		}
	}

    public function getInformacoesAdicionaisEndosso($endosso, $flEmitido=true) {
        $Select = $this->select()->setIntegrityCheck(false);
        $Select->from(array('o' => 'seguro.propostas_observacoes'));
        $Select->join(array('u' => 'sistema.usuarios'), 'o.id_usuario_criacao = u.id', 'ds_nome_usuario');
        $Select->join(array('pr'=> 'seguro.propostas'), 'o.id_proposta = pr.id', 'id_endosso');
        $Select->joinLeft(array('po' => 'seguro.propostas_observacoes_arquivos'), 'po.id_observacao = o.id', 'po.id as id_observacao_arquivo');
        $Select->joinLeft(array('f' => 'sistema.funcionarios'), 'u.id = f.id_usuario', 'ds_nome_funcionario');
        $Select->joinLeft(array('c' => 'sistema.corretores'), 'u.id = c.id_usuario', 'ds_nome_corretor');
        $Select->joinLeft(array('p' => 'sistema.prepostos'), 'u.id = p.id_usuario', 'ds_nome_preposto');
        $Select->where('id_endosso = ?', $endosso);
        
        if($flEmitido){
	        $Select->where('pr.id_status = ?',  Agro_Util::status('APOLICE_EMITIDA','PR'));
        } else {
        	$Select->where('pr.id_status <> ?',  Agro_Util::status('ENDOSSO_INCOMPLETO','PR'));
            $Select->where('pr.id_status <> ?',  Agro_Util::status('ORCAMENTO_ENDOSSO','PR'));
            $Select->where('pr.id_status <> ?',  Agro_Util::status('ENDOSSO_AGUARDANDO_ENVIO_SEG','PR'));
            $Select->where('pr.id_status <> ?',  Seguro_Model_Propostas::DS_CHAVE_SOLICITACAO_CANCELADA);
        }
        
        $Select->order('dt_criacao desc');

        $Rs = $this->fetchAll($Select);

        if ($Rs->count()) {
            return $Rs->toArray();
        }
    }

    public function ocultarItensSegurados($idProposta){
        $select = $this->select();
        $select->setIntegrityCheck(false);
        $select->from(array('po'=>'seguro.propostas_observacoes'),'fl_ocultar_itens_segurados');
        $select->where('po.id_proposta = ?', $idProposta);
        $select->where('po.fl_ocultar_itens_segurados = ?', true);

        $Row = $this->_db->fetchOne($select);
            if($Row) {
            return $Row;
        } else {
            return false;
        }
    }

    public function getObservacoesEndosso($idProposta){
    	$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('o' => 'seguro.propostas_observacoes'));
		$Select->join(array('u' => 'sistema.usuarios'), 'o.id_usuario_criacao = u.id', 'ds_nome_usuario');
		$Select->joinLeft(array('f' => 'sistema.funcionarios'), 'u.id = f.id_usuario', 'ds_nome_funcionario');
		$Select->joinLeft(array('c' => 'sistema.corretores'), 'u.id = c.id_usuario', 'ds_nome_corretor');
		$Select->joinLeft(array('p' => 'sistema.prepostos'), 'u.id = p.id_usuario', 'ds_nome_preposto');
		$Select->joinLeft(array('poa' => 'seguro.propostas_observacoes_arquivos'), 'poa.id_observacao = o.id', 'ds_nome_arquivo');
		$Select->where('id_proposta = ?', $idProposta);
		$Select->where('fl_principal = false');
		$Select->where('fl_observacao_endosso = true');

		$Select->order('dt_criacao desc');

		$Rs = $this->fetchAll($Select);

		if ($Rs->count()) {
			return $Rs->toArray();
		}
    }

	public function getIdObservacaoPrincipal($proposta) {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('o' => 'seguro.propostas_observacoes'),array('id'));
		$Select->where('id_proposta = ?', $proposta);
		$Select->where('fl_principal = true');
		return $this->_db->fetchOne($Select);
	}

}
