Class: DiscountsReport

Inherits:
Object
  • Object
show all
Extended by:
ArtfullyOseHelper
Defined in:
app/models/discounts_report.rb

Defined Under Namespace

Classes: Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArtfullyOseHelper

action_and_subtype, amount_and_nongift, bootstrapped_type, build_order_location, channel_checkbox, channel_text, check_mark, contextual_menu, credit_card_message, date_field_tag, datetime_field_tag, events_to_options, full_details, fully_qualified_asset_path, get_selected_class, icon_link_to, icon_tag, link_to_add_fields, link_to_remove_fields, nav_dropdown, number_as_cents, number_to_dollars, select_event_for_sales_search, select_show_for_sales_search, sorted_us_state_abbreviations, sorted_us_state_names, ticket_seller_name, time_zone_description, us_states, verb_for_save, widget_script

Methods included from LinkHelper

#active?, #active_link_to, #active_section, #in_section, #in_sub_section

Constructor Details

#initialize(organization, discount_code, start_date, end_date) ⇒ DiscountsReport

Returns a new instance of DiscountsReport.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/discounts_report.rb', line 6

def initialize(organization, discount_code, start_date, end_date)
  if discount_code.nil?
    self.header = "ALL DISCOUNTS"
    self.discount_code = "ALL DISCOUNTS"
    self.discount = Discount.where(:organization_id => organization.id)
  else

    # discount is an array since there could be multiple instances of FIVEOFF
    self.discount = Discount.where(:organization_id => organization.id).where(:id => discount_code).first
    self.discount ||= Discount.where(:organization_id => organization.id).where(:code => discount_code).all
    self.discount = Array.wrap(self.discount)
    self.header = self.discount.first.code
    self.discount_code = self.discount.first.code
  end

  self.start_date = start_date
  self.end_date = end_date

  #
  # This used to select on Items with a sum on price, original_price and a group by order_id
  # but ARel simply would not select the sums, they'd be overwritten by the actual values
  #
  @orders = Order.includes(:person, :items => [:discount, [:show => :event]]).where("items.discount_id" => self.discount).order('orders.created_at desc')
  @orders = @orders.where('orders.created_at > ?',self.start_date)  unless start_date.blank?
  @orders = @orders.where('orders.created_at < ?',self.end_date)    unless end_date.blank?

  self.rows = []
  @orders.each do |order|
    self.rows << Row.new(order)
  end

  build_header

  self.tickets_sold     = self.rows.inject(0) { |total, row| total + row.ticket_count}
  self.original_price   = self.rows.inject(0) { |total, row| total + row.original_price }
  self.discounted       = self.rows.inject(0) { |total, row| total + row.discounted }
  self.gross            = self.rows.inject(0) { |total, row| total + row.gross }
end

Instance Attribute Details

#countsObject

Returns the value of attribute counts.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def counts
  @counts
end

#discountObject

Returns the value of attribute discount.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def discount
  @discount
end

#discount_codeObject

Returns the value of attribute discount_code.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def discount_code
  @discount_code
end

#discountedObject

Returns the value of attribute discounted.



3
4
5
# File 'app/models/discounts_report.rb', line 3

def discounted
  @discounted
end

#end_dateObject

Returns the value of attribute end_date.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def end_date
  @end_date
end

#grossObject

Returns the value of attribute gross.



3
4
5
# File 'app/models/discounts_report.rb', line 3

def gross
  @gross
end

#headerObject

Returns the value of attribute header.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def header
  @header
end

#original_priceObject

Returns the value of attribute original_price.



3
4
5
# File 'app/models/discounts_report.rb', line 3

def original_price
  @original_price
end

#rowsObject

Returns the value of attribute rows.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def rows
  @rows
end

#start_dateObject

Returns the value of attribute start_date.



2
3
4
# File 'app/models/discounts_report.rb', line 2

def start_date
  @start_date
end

#tickets_soldObject

Returns the value of attribute tickets_sold.



3
4
5
# File 'app/models/discounts_report.rb', line 3

def tickets_sold
  @tickets_sold
end

Instance Method Details

#build_headerObject



45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/discounts_report.rb', line 45

def build_header
  if self.start_date.blank? && self.end_date.blank? 
    return
  elsif self.start_date.blank?
    self.header = self.header + " through #{I18n.localize(DateTime.parse(self.end_date), :format => :slashed_date)}"
  elsif self.end_date.blank?
    self.header = self.header + " since #{I18n.localize(DateTime.parse(self.start_date), :format => :slashed_date)}"
  else
    self.header = self.header + " from #{I18n.localize(DateTime.parse(self.start_date), :format => :slashed_date)} through #{I18n.localize(DateTime.parse(self.end_date), :format => :slashed_date)}"
  end
end