Sha256: 0cf3c1a1bb3039949996ddca8c53b15444393c33b2d90025833a1879ed76c373

Contents?: true

Size: 873 Bytes

Versions: 109

Compression:

Stored size: 873 Bytes

Contents

module Comee
  module Core
    class SupplierPayment < ApplicationRecord
      belongs_to :payment_order
      belongs_to :supplier_invoice

      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_order supplier_invoice]
      end

      def validate_amount
        return unless amount && payment_order

        running_total = payment_order.supplier_payments.sum(:amount)
        running_total -= amount_was if id
        remaining = payment_order.amount - running_total
        error = "Amount exceeded. The maximum amount you can set is #{remaining}"
        errors.add(:base, error) if amount > remaining
      end
    end
  end
end

Version data entries

109 entries across 109 versions & 1 rubygems

Version Path
comee_core-0.3.62 app/models/comee/core/supplier_payment.rb
comee_core-0.3.61 app/models/comee/core/supplier_payment.rb
comee_core-0.3.60 app/models/comee/core/supplier_payment.rb
comee_core-0.3.59 app/models/comee/core/supplier_payment.rb
comee_core-0.3.58 app/models/comee/core/supplier_payment.rb
comee_core-0.3.57 app/models/comee/core/supplier_payment.rb
comee_core-0.3.56 app/models/comee/core/supplier_payment.rb
comee_core-0.3.55 app/models/comee/core/supplier_payment.rb
comee_core-0.3.54 app/models/comee/core/supplier_payment.rb
comee_core-0.3.53 app/models/comee/core/supplier_payment.rb
comee_core-0.3.51 app/models/comee/core/supplier_payment.rb
comee_core-0.3.50 app/models/comee/core/supplier_payment.rb
comee_core-0.3.49 app/models/comee/core/supplier_payment.rb
comee_core-0.3.48 app/models/comee/core/supplier_payment.rb
comee_core-0.3.47 app/models/comee/core/supplier_payment.rb
comee_core-0.3.46 app/models/comee/core/supplier_payment.rb
comee_core-0.3.45 app/models/comee/core/supplier_payment.rb
comee_core-0.3.44 app/models/comee/core/supplier_payment.rb
comee_core-0.3.43 app/models/comee/core/supplier_payment.rb
comee_core-0.3.42 app/models/comee/core/supplier_payment.rb