Sha256: 49d714c11d3c83c9246630a3605fa5f7cd8856e943910b7413900821a0774dbe

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

module ESA
  module Associations
    # Association extension for has_many :amounts relations. Internal.
    module AmountsExtension
      # Returns a total sum of the referenced Amount objects
      def total
        sums = group('esa_amounts.amount is null').
               sum(:amount)

        checked = sums.map do |s|
          amount_is_nil, amount = s.flatten
          amount
        end

        if checked.all?
          return checked.inject(BigDecimal(0)){|x,y| x + y}
        else
          return nil
        end
      end

      # Returns a sum of the referenced Amount objects.
      def balance
        sums = group('esa_amounts.amount is null').
               group('esa_amounts.type').
               sum(:amount)

        signed = sums.map do |s|
          amount_is_nil, amount_type, amount = s.flatten
          amount_type = amount_type.demodulize.downcase

          if amount_type == "debit"
            amount
          elsif amount_type == "credit"
            - amount
          else
            nil
          end
        end

        if signed.all?
          return signed.inject(BigDecimal(0)){|x,y| x + y}
        else
          return nil
        end
      end

      # Returns a sum of the referenced Amount objects.
      def iterated_total
        amounts = map(&:amount)

        if amounts.all?
          amounts.inject(BigDecimal(0)){|x,y| x + y}
        else
          return nil
        end
      end

      # Returns a sum of the referenced Amount objects.
      def iterated_balance
        amounts = map do |a|
          if a.is_debit?
            a.amount
          elsif a.is_credit? and not a.amount.nil?
            - a.amount
          else
            nil
          end
        end
        
        if amounts.all?
          amounts.inject(BigDecimal(0)){|x,y| x + y}
        else
          return nil
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
event_sourced_accounting-0.2.6 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.2.4 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.2.3 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.2.2 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.1.6 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.1.4 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.1.3 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.1.1 app/models/esa/associations/amounts_extension.rb
event_sourced_accounting-0.1.0 app/models/esa/associations/amounts_extension.rb