Sha256: 524c5d06ddc3fcd29a4ffa7d53353aad6a7919da09709d719dc8842f79d49139

Contents?: true

Size: 1.56 KB

Versions: 6

Compression:

Stored size: 1.56 KB

Contents

class DailyMembershipReport
  attr_accessor :rows, :start_date, :organization
  extend ::ArtfullyOseHelper  

  def initialize(organization, date=nil)
    @organization = organization
    @start_date = (date || 1.day.ago).in_time_zone(@organization.time_zone).midnight
    @end_date = @start_date + 1.day
    orders = organization.orders.includes(:person, :items => :product)
    orders = orders.csv_not_imported.after(@start_date).before(@end_date) || []

    @rows = []
    orders.each do |order|
      next if order.memberships.empty?
      next if !order.revenue_applies_to_range(@start_date, @end_date)

      unique_memberships = order.memberships.collect{|item| item.product.membership_type.name}.uniq
      unique_memberships.each do |membership_type_name|
        @rows << Row.new(order.items.select {|item| item.product.membership_type.name == membership_type_name})        
      end
    end
  end

  def total
    @rows.collect(&:order).sum{|o| o.memberships.sum(&:total_price)}
  end

  class Row
    attr_accessor :id, :details, :total, :person, :person_id, :order, :quantity, :membership
    def initialize(items)
      @order = items.first.order
      @id = items.first.order.id
      @membership = items.first.product.membership_type.name
      @quantity = items.length
      @total = DailyMembershipReport.number_to_currency(items.sum(&:price).to_f/100)
      @person = items.first.order.person
      @person_id = items.first.order.person.id
    end

    def calculate_total
      DailyMembershipReport.number_to_currency(@order.memberships.sum(&:price).to_f/100)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
artfully_ose-1.2.0.pre.12 app/models/daily_membership_report.rb
artfully_ose-1.2.0.pre.11 app/models/daily_membership_report.rb
artfully_ose-1.2.0.pre.10 app/models/daily_membership_report.rb
artfully_ose-1.2.0.pre.9 app/models/daily_membership_report.rb
artfully_ose-1.2.0.pre.8 app/models/daily_membership_report.rb
artfully_ose-1.2.0.pre.7 app/models/daily_membership_report.rb