<?php

class Sinistro_Model_ProcessosArquivos extends Agro_Db_Table_Abstract {
	protected $_schema = 'sinistro';
	protected $_name = 'processos_arquivos';
	protected $_primary = 'id';

	public function save($arquivo, $processo, $ds_chave_tipo_arquivo) {
		$TipoArquivo = new Sistema_Model_TipoArquivoProcesso();
		$Row = $this->createRow();

		$Row->id_tipo_arquivo = $TipoArquivo->getIdArquivo($ds_chave_tipo_arquivo);
		$Row->ds_nome_arquivo = $arquivo;
		$Row->id_processo = $processo;
		//$Row->id_proposta = $proposta;

		return $Row->save();
	}

	public function getProcessosArquivos($idProcesso, $flExibirNaoEnviados=false, $idProcessoArquivo=null) {
		$Processo = new Sinistro_Model_Processos();
		$Proponente = $Processo->getProponenteProcesso($idProcesso);

		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('pa'=>'sinistro.processos_arquivos'), '*');
		$select->join(array('ta'=>'sistema.tipo_arquivo_processo'), 'pa.id_tipo_arquivo = ta.id', array('ds_tipo_arquivo','ds_chave_tipo_arquivo'));
		$select->join(array('p'=>'sinistro.processos'),'p.id = pa.id_processo',false);
		$select->where('pa.fl_del = false');
		$select->where('pa.fl_ativo = true');
		$select->where('pa.id_processo = ?', $idProcesso);
		
		if($idProcessoArquivo){
			$select->where('pa.id = ?', $idProcessoArquivo);
		}
		
		$select->order(array('p.id desc', 'pa.dt_criacao', 'ta.ds_tipo_arquivo'));
				
		if($idProcessoArquivo){
		    $select->where('pa.id = ?', $idProcessoArquivo);
		}


		return $this->fetchAll($select)->toArray();
	}
	
	
	public function getProcessosArquivosCpf($idProcesso, $flExibirNaoEnviados=false, $idProcessoArquivo=null) {
		$Processo = new Sinistro_Model_Processos();
		$Proponente = $Processo->getProponenteProcesso($idProcesso);
	
		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('pa'=>'sinistro.processos_arquivos'), '*');
		$select->join(array('ta'=>'sistema.tipo_arquivo_processo'), 'pa.id_tipo_arquivo = ta.id', array('ds_tipo_arquivo','ds_chave_tipo_arquivo'));
		$select->join(array('p'=>'sinistro.processos'),'p.id = pa.id_processo',false);
		$select->join(array('pr'=>'seguro.propostas'),'pr.id = p.id_proposta',false);
		$select->join(array('pe'=>'seguro.propostas_endosso'),'pe.id_proposta = pr.id',false);
		$select->join(array('pp'=>'seguro.propostas_proponentes'),'pp.id_proposta = p.id_proposta',false);
		$select->where('pa.fl_del = false');
		$select->where('pa.fl_ativo = true');
		$select->where('pe.fl_proposta_vigente = true');
		$select->where('pp.cpf_cnpj = ?', $Proponente['cpf_cnpj']);
	
		if($flExibirNaoEnviados){
			$select->where('pa.fl_transmitido_seguradora = false');
			$select->where('pa.fl_conferido = true');
		}
		if($idProcessoArquivo){
			$select->where('pa.id = ?', $idProcessoArquivo);
		}
	
		$select->order(array('p.id desc', 'pa.dt_criacao', 'ta.ds_tipo_arquivo'));
	
		return $this->fetchAll($select)->toArray();
	}	

	public function getArquivo($idProcessoArquivo) {
		$Processo = new Sinistro_Model_Processos();

		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select->from(array('pa'=>'sinistro.processos_arquivos'), array('id_processo', 'ds_nome_arquivo'));
		$select->join(array('ta'=>'sistema.tipo_arquivo_processo'), 'pa.id_tipo_arquivo = ta.id', array('ds_tipo_arquivo','ds_chave_tipo_arquivo'));
		$select->where('pa.fl_del = false');
		$select->where('pa.fl_ativo = true');
		$select->where('pa.id = ?', $idProcessoArquivo);

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

	public function getCountTipoArquivo($cpf_cnpj, $idTipoArquivo){
		$select = $this->select();
		$select->setIntegrityCheck(false);
    	$select->from(array('pa'=>'sinistro.processos_arquivos'), array('id'));
        $select->join(array('p'=>'sinistro.processos'), 'p.id = pa.id_processo', false);
    	$select->join(array('pr'=>'seguro.propostas'), 'pr.id = p.id_proposta', false);
    	$select->join(array('pp'=>'seguro.propostas_proponentes'),'pp.id_proposta = pr.id',false);
		$select->where('pa.id_tipo_arquivo = ?', $idTipoArquivo);
    	$select->where('pp.cpf_cnpj = ?', $cpf_cnpj);

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

	public function nextSequenceId() {
		$value = $this->_db->fetchOne("SELECT NEXTVAL('sinistro.processos_arquivos_id_seq')");
		return $value;
    }
}
