<?php

class Seguro_Model_PropostasBoletosProrrogacoes extends Agro_Db_Table_Abstract {
    protected $_log = true;
    protected $_schema = 'seguro';
    protected $_name = 'propostas_boletos_prorrogacoes';
    protected $_primary = 'id';
    
    /**
     * Retorna a prorrogação mais atual da proposta, parcela e versão informadas
     *
     * @param int $idProposta
     * @param int $nrParcela
     * @param int|null $idVersao
     * @return array
     */
    public function getByPropostaParcela(int $idProposta, int $nrParcela, ?int $idVersao = null): array
    {
        $select = $this->select()->setIntegrityCheck(false);
    	$select->from(["spbp" => "{$this->_schema}.{$this->_name}"]);
        $select->where("spbp.fl_ativo is true");
        $select->where("spbp.id_proposta = ?", $idProposta);
        $select->where("spbp.nr_parcela = ?", $nrParcela);
        
        if (!empty($idVersao)) {
            $select->where("spbp.id_versao = ?", $idVersao);
        }

        $select->order('spbp.id DESC');

        $row = Agro_Db_Table_Abstract::getDefaultAdapter()->fetchRow($select);
        return !empty($row) ? $row : [];
    }
}