Sha256: d958a651dba529ec8ada51d60bd0d23e7a9103b42c0e46f448bd766fa1bdfd4f

Contents?: true

Size: 1.2 KB

Versions: 32

Compression:

Stored size: 1.2 KB

Contents

module Valuation

  #
  # Anything with a :lifetime_fees column and has_many orders can use this module.
  #
  module LifetimeFees
    extend ActiveSupport::Concern
    
    included do
      def self.with_lifetime_fees_between(start_date, end_date)
        joins(:orders => :items)
        .select("#{table_name}.*, sum(items.service_fee) as lifetime_fees")
        .where('orders.revenue_applies_at > ?', start_date)
        .where('orders.revenue_applies_at < ?', end_date)
        .group("#{table_name}.id")
      end
    end

    #
    # Includers can define a method called lifetime_orders which
    # will override this method.
    #
    # lifetime_orders should return the orders that this model wants to include in the calculation
    #  
    def lifetime_orders
      orders
    end
    
    #
    # Calculate the lifetime fees of this model by summing the price of all items 
    # attached to orders attached to this model.  Save the value in lifetime_fees.
    # Return the value
    #
    def calculate_lifetime_fees
      self.lifetime_fees = Organization.where(:id => self.id).joins(:orders => :items).sum('items.service_fee').to_i
      self.save(:validate => false)
      self.lifetime_fees
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
artfully_ose-1.3.0.pre4 app/models/valuation/lifetime_fees.rb
artfully_ose-1.3.0.pre3 app/models/valuation/lifetime_fees.rb
artfully_ose-1.3.0.pre2 app/models/valuation/lifetime_fees.rb
artfully_ose-1.3.0.pre1 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.beta.1 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.alpha.2 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.alpha.1 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.27 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.26 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.24 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.23 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.21 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.20 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.19 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.18 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.17 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.16 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.15 app/models/valuation/lifetime_fees.rb
artfully_ose-1.2.0.pre.12 app/models/valuation/lifetime_fees.rb