Sha256: ed201bf829b848bb8b7ac89c8a9e55e0b6aa4f5dccdbc6013b3cd608f0369472
Contents?: true
Size: 1013 Bytes
Versions: 23
Compression:
Stored size: 1013 Bytes
Contents
module Comee module Core class Payment < ApplicationRecord after_save :calculate_amount_paid belongs_to :invoice belongs_to :payment_deposit validates :amount, presence: true, numericality: {greater_than: 0} validate :validate_amount def self.ransackable_attributes(_auth_object = nil) %w[id created_at updated_at] end def self.ransackable_associations(_auth_object = nil) %w[payment_deposit invoice] end def validate_amount return unless amount && payment_deposit running_total = payment_deposit.payments.sum(:amount) running_total -= amount_was if id remaining = payment_deposit.amount - running_total error = "Amount exceeded. The maximum amount you can set is #{remaining}" errors.add(:base, error) if amount > remaining end def calculate_amount_paid invoice.amount_paid = invoice.calculate_amount_paid invoice.save! end end end end
Version data entries
23 entries across 23 versions & 1 rubygems