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.2.81 app/models/comee/core/supplier_payment.rb
comee_core-0.2.80 app/models/comee/core/supplier_payment.rb
comee_core-0.2.78 app/models/comee/core/supplier_payment.rb
comee_core-0.2.77 app/models/comee/core/supplier_payment.rb
comee_core-0.2.76 app/models/comee/core/supplier_payment.rb
comee_core-0.2.75 app/models/comee/core/supplier_payment.rb
comee_core-0.2.70 app/models/comee/core/supplier_payment.rb
comee_core-0.2.69 app/models/comee/core/supplier_payment.rb
comee_core-0.2.68 app/models/comee/core/supplier_payment.rb
comee_core-0.2.67 app/models/comee/core/supplier_payment.rb
comee_core-0.2.66 app/models/comee/core/supplier_payment.rb
comee_core-0.2.65 app/models/comee/core/supplier_payment.rb
comee_core-0.2.64 app/models/comee/core/supplier_payment.rb
comee_core-0.2.63 app/models/comee/core/supplier_payment.rb
comee_core-0.2.62 app/models/comee/core/supplier_payment.rb
comee_core-0.2.61 app/models/comee/core/supplier_payment.rb
comee_core-0.2.60 app/models/comee/core/supplier_payment.rb
comee_core-0.2.59 app/models/comee/core/supplier_payment.rb
comee_core-0.2.58 app/models/comee/core/supplier_payment.rb
comee_core-0.2.57 app/models/comee/core/supplier_payment.rb