class Base::ContaBancariaPorPagamento < ApplicationRecord
	has_paper_trail

	belongs_to :pagamento, foreign_key: :pagamento_id, class_name: 'Contabilidade::Pagamento'
	belongs_to :conta_bancaria, foreign_key: :conta_bancaria_id, class_name: 'Base::ContaBancaria'

	has_many :pagamentos_do_lote_bancario, class_name: "Contabilidade::PagamentoDoLoteBancario"

	validates_presence_of :conta_bancaria_id

	before_save :apagar_movimento_bancario_do_pagamento

	def apagar_movimento_bancario_do_pagamento
		if self.changed?
			movimentos = Contabilidade::MovimentacaoDaContaBancaria.where(modulo_type: 'Contabilidade::Pagamento', modulo_id: self.pagamento.id)
			movimentos.try(:destroy_all)
		end
	end

	def esta_em_um_lote?
		self.pagamentos_do_lote_bancario.joins(:lote_bancario).where(contabilidade_lotes_bancarios: { status: [1, 2]}).size > 0 ? true : false
	end
end
