Class: PassesReport

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

Defined Under Namespace

Classes: Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArtfullyOseHelper

amount_and_nongift, bootstrapped_type, build_action_path, build_order_location, channel_checkbox, channel_text, check_mark, clean_full_error_messages, contextual_menu, credit_card_message, date_field_tag, datetime_field_tag, events_to_options, 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, pluralize_word, refund_header, select_event_for_sales_search, select_membership_type_for_sales_search, select_pass_type_for_sales_search, select_show_for_sales_search, sorted_us_state_abbreviations, sorted_us_state_names, thanks_message, ticket_seller_name, time_ago_sentence, time_zone_description, us_states, verb_for_save, widget_script, with_kit

Methods included from LinkHelper

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

Constructor Details

#initialize(organization, pass_type, start_date, end_date) ⇒ PassesReport

Returns a new instance of PassesReport.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/passes_report.rb', line 10

def initialize(organization, pass_type, start_date, end_date)  
  self.pass_type = pass_type
  self.start_date = start_date
  self.end_date = end_date

  @orders = find_orders

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

  build_header

  self.passes_sold      = count_passes_sold
  self.total_tickets    = calculate_total_tickets
  self.tickets_sold     = self.rows.inject(0) { |total, row| total + row.ticket_count}
  self.tickets_remaining= self.total_tickets - self.tickets_sold
  self.original_price   = self.rows.inject(0) { |total, row| total + row.original_price }
  self.discounted       = self.rows.inject(0) { |total, row| total + row.discounted }
end

Instance Attribute Details

#countsObject

Returns the value of attribute counts.



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

def counts
  @counts
end

#discountedObject

Returns the value of attribute discounted.



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

def discounted
  @discounted
end

#end_dateObject

Returns the value of attribute end_date.



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

def end_date
  @end_date
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#original_priceObject

Returns the value of attribute original_price.



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

def original_price
  @original_price
end

#pass_typeObject

Returns the value of attribute pass_type.



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

def pass_type
  @pass_type
end

#passes_soldObject

Returns the value of attribute passes_sold.



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

def passes_sold
  @passes_sold
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

#start_dateObject

Returns the value of attribute start_date.



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

def start_date
  @start_date
end

#tickets_remainingObject

Returns the value of attribute tickets_remaining.



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

def tickets_remaining
  @tickets_remaining
end

#tickets_soldObject

Returns the value of attribute tickets_sold.



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

def tickets_sold
  @tickets_sold
end

#total_ticketsObject

Returns the value of attribute total_tickets.



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

def total_tickets
  @total_tickets
end

Instance Method Details

#build_headerObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/passes_report.rb', line 75

def build_header
  self.header = pass_type_name
  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

#calculate_total_ticketsObject



32
33
34
35
36
37
38
39
# File 'app/models/passes_report.rb', line 32

def calculate_total_tickets
  @passes = Pass.owned
  if self.pass_type.present?
    @passes = @passes.where(:pass_type_id => self.pass_type.id)
  end    

  @passes.sum(:tickets_allowed)
end

#count_passes_soldObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/passes_report.rb', line 41

def count_passes_sold
  @items = Item.sold_or_comped.where(:product_type => "Pass")
  @items = @items.joins("INNER join passes ON items.product_id = passes.id")
  @items = @items.joins("INNER join pass_types ON passes.pass_type_id = pass_types.id")

  if self.pass_type.present?
    @items = @items.where("pass_types.id" => self.pass_type)
  end

  @items = @items.joins(:order)
  @items = @items.where('orders.created_at > ?',self.start_date)  unless start_date.blank?
  @items = @items.where('orders.created_at < ?',self.end_date)    unless end_date.blank?
  @items.count
end

#find_ordersObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/passes_report.rb', line 56

def find_orders
  @orders = Order.includes(:person, :items => [:show => :event])
                 .joins(:items)
                 .joins("INNER join passes ON items.pass_id = passes.id")
                 .joins("INNER join pass_types ON passes.pass_type_id = pass_types.id")
                 .group('orders.id')
                 .order('orders.created_at desc')

  if pass_type.nil?               
    @orders = @orders.where("pass_types.id is not null")
  else
    @orders = @orders.where("pass_types.id" => self.pass_type.id)
  end

  @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?
  @orders
end

#pass_type_nameObject



6
7
8
# File 'app/models/passes_report.rb', line 6

def pass_type_name
  pass_type.try(:name) || PassType::ALL_PASSES_STRING
end