<?php

class Seguro_Model_PropostasIndiceProducaoForrageira extends Agro_Db_Table_Abstract {
	protected $_schema = 'seguro';
	protected $_name = 'propostas_indice_producao_forrageira';
	protected $_primary = 'id';
	public $_order = 'id';
    
    public function save(array $dados) : int {
        $Row = $this->createRow();
        $Row->id_proposta = $dados['id_proposta'];
        $Row->ano = $dados['ano'];
        $Row->indice = str_replace(',', '.' ,$dados['indice']);
        $Row->indice_esperado = str_replace(',', '.', $dados['indice_esperado']);
        $Row->fl_status = strtolower($dados['fl_status']) == 'true' ? 1 : 0;
        
        return $Row->save();
    }

    public function getIndiceByProposta(int $idProposta) : float {
        $select = $this->select()->setIntegrityCheck(false)
            ->from('seguro.propostas_indice_producao_forrageira', 'indice_esperado')
            ->where('id_proposta = ?', $idProposta)
            ->limit(1);
        
        return $this->getAdapter()->fetchOne($select);
    }
}