class Projecao::IndiceDoCalculoDeProjecao < ApplicationRecord
	has_paper_trail

	belongs_to :calculo_de_projecao, inverse_of: :indices_do_calculo_de_projecao
	belongs_to :indice_de_projecao

	validates_presence_of :calculo_de_projecao_id, :indice_de_projecao_id, :percentual

	validates_numericality_of :percentual, greater_than_or_equal_to: 0, less_than_or_equal_to: 100

	before_create :altera_flag_recalcular_valor_do_calculo_de_projecao
	after_update 	:altera_flag_recalcular_valor_do_calculo_de_projecao
	after_destroy :altera_flag_recalcular_valor_do_calculo_de_projecao

	def grupo_id_do_indice_de_projecao
		return indice_de_projecao.grupo_de_indice_id
	end

	private
	def altera_flag_recalcular_valor_do_calculo_de_projecao
		calculo_de_projecao = Projecao::CalculoDeProjecao.find( calculo_de_projecao_id )
		if calculo_de_projecao.receitas_dos_calculos_de_projecao.present?
			calculo_de_projecao.update(recalcular_valor: true)
		end
	end
end
