Class: Statement

Inherits:
Object
  • Object
show all
Includes:
Ext::Due
Defined in:
app/models/statement.rb

Overview

Statement uses items from a show to calculate things like gross, net, and cut the data up into various sections. The reason we don’t use tickets is:

  • Item is the table of record for revenue

  • a Ticket can have many items (refunds, exchanges, etc…) It’s awkward (and db intensive) to get the sold_item for a ticket

The downside is that this approach requires a bit of hoop jumping in Row (see if, elsif, else block) which is indeed ugly.

Defined Under Namespace

Modules: Row Classes: DiscountRow, OrderLocationRow, PassRow, PaymentTypeRow, TicketTypeRow

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ext::Due

#calculate_due

Instance Attribute Details

#cc_netObject

Returns the value of attribute cc_net.



17
18
19
# File 'app/models/statement.rb', line 17

def cc_net
  @cc_net
end

#datetimeObject

Returns the value of attribute datetime.



17
18
19
# File 'app/models/statement.rb', line 17

def datetime
  @datetime
end

#discount_rowsObject

Returns the value of attribute discount_rows.



17
18
19
# File 'app/models/statement.rb', line 17

def discount_rows
  @discount_rows
end

#gross_revenueObject

Returns the value of attribute gross_revenue.



17
18
19
# File 'app/models/statement.rb', line 17

def gross_revenue
  @gross_revenue
end

#net_revenueObject

Returns the value of attribute net_revenue.



17
18
19
# File 'app/models/statement.rb', line 17

def net_revenue
  @net_revenue
end

#order_location_rowsObject

Returns the value of attribute order_location_rows.



17
18
19
# File 'app/models/statement.rb', line 17

def order_location_rows
  @order_location_rows
end

#pass_rowsObject

Returns the value of attribute pass_rows.



17
18
19
# File 'app/models/statement.rb', line 17

def pass_rows
  @pass_rows
end

#payment_method_rowsObject

Returns the value of attribute payment_method_rows.



17
18
19
# File 'app/models/statement.rb', line 17

def payment_method_rows
  @payment_method_rows
end

#potential_revenueObject

Returns the value of attribute potential_revenue.



17
18
19
# File 'app/models/statement.rb', line 17

def potential_revenue
  @potential_revenue
end

#processingObject

Returns the value of attribute processing.



17
18
19
# File 'app/models/statement.rb', line 17

def processing
  @processing
end

#settledObject

Returns the value of attribute settled.



17
18
19
# File 'app/models/statement.rb', line 17

def settled
  @settled
end

#ticket_type_rowsObject

Returns the value of attribute ticket_type_rows.



17
18
19
# File 'app/models/statement.rb', line 17

def ticket_type_rows
  @ticket_type_rows
end

#tickets_compedObject

Returns the value of attribute tickets_comped.



17
18
19
# File 'app/models/statement.rb', line 17

def tickets_comped
  @tickets_comped
end

#tickets_soldObject

Returns the value of attribute tickets_sold.



17
18
19
# File 'app/models/statement.rb', line 17

def tickets_sold
  @tickets_sold
end

Class Method Details

.for_show(show, imported = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/statement.rb', line 32

def self.for_show(show, imported=false)
  if show.nil?
    return new
  end
  
  new.tap do |statement|

    # Some of this overlaps with Ticket::Glance.  Consider refactoring to combine the two.
    statement.datetime          = show.datetime_local_to_event
    statement.tickets_sold      = show.tickets.select{|t| t.sold?}.size
    statement.tickets_comped    = show.tickets.select{|t| t.comped?}.size
    statement.gross_revenue     = show.items.inject(0) { |gross, item| gross += item.price }
    statement.net_revenue       = show.items.inject(0) { |net, item| net += item.net }
    statement.processing        = statement.gross_revenue - statement.net_revenue
    
    statement.calculate_due(show, imported)

    #
    # PAYMENT METHOD
    #
    payment_method_hash         = show.items.group_by { |item| item.order.payment_method }
    statement.payment_method_rows         = {}
    
    # Initialize with the three common payment types
    statement.payment_method_rows[::CreditCardPayment.payment_method.downcase] = PaymentTypeRow.new(::CreditCardPayment.payment_method)
    statement.payment_method_rows[::CashPayment.payment_method.downcase] = PaymentTypeRow.new(::CashPayment.payment_method)
    statement.payment_method_rows[::CompPayment.payment_method.downcase] = PaymentTypeRow.new(::CompPayment.payment_method)
    
    payment_method_hash.each do |payment_method, items|
      payment_method = (payment_method.try(:downcase) || "")
      row = statement.payment_method_rows[payment_method] || PaymentTypeRow.new(payment_method)
      items.each {|item| row << item}
      statement.payment_method_rows[payment_method] = row
    end
    
    #
    # ORDER LOCATION
    #  
    order_location_hash         = show.items.group_by do |item| 
      item.order.original_order.location 
    end
    
    statement.order_location_rows = {}
    statement.order_location_rows[::WebOrder.location]        = OrderLocationRow.new(::WebOrder.location)
    statement.order_location_rows[BoxOffice::Order.location]  = OrderLocationRow.new(BoxOffice::Order.location)
    
    order_location_hash.each do |order_location, items|
      row = statement.order_location_rows[order_location] || OrderLocationRow.new(order_location)
      items.each {|item| row << item}
      statement.order_location_rows[order_location] = row
    end

    statement.build_discount_rows(show.items)
    statement.build_pass_rows(show.items)
    statement.build_ticket_type_rows(show, show.items)
  end
end

Instance Method Details

#build_discount_rows(items) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/statement.rb', line 110

def build_discount_rows(items)
  self.discount_rows         = {}
  items.each do |item|
    unless item.discount.nil?
      row = self.discount_rows[item.discount.code] || DiscountRow.new(item.discount.code, item.discount.to_s)
      row << item
      row.discount += (item.original_price - item.price)
      self.discount_rows[item.discount.code] = row
    end
  end
end

#build_pass_rows(items) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'app/models/statement.rb', line 122

def build_pass_rows(items)
  self.pass_rows         = {}
  items.each do |item|
    unless item.pass.nil?
      row = self.pass_rows[item.pass.pass_type.name] || PassRow.new(item.pass.pass_type.name)
      row << item
      self.pass_rows[item.pass.pass_type.name] = row
    end
  end
end

#build_ticket_type_rows(show, items) ⇒ Object

TODO: These are super-related to the procs in class Slices. Get these two on the same page and DRY it up



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/statement.rb', line 93

def build_ticket_type_rows(show, items)
  self.ticket_type_rows         = {}

  show.chart.ticket_types.each do |ticket_type|
    self.ticket_type_rows[ticket_type.name] = TicketTypeRow.new(ticket_type)
  end

  items.each do |item|
    #refunded items now have nil ticket types
    unless item.product.ticket_type.nil?
      row = self.ticket_type_rows[item.product.ticket_type.name] || TicketTypeRow.new(item.product.ticket_type)
      row << item
      self.ticket_type_rows[item.product.ticket_type.name] = row
    end
  end
end