Class: OrderHandler

Inherits:
Object
  • Object
show all
Defined in:
app/models/order_handler.rb

Overview

Handles orders in progress for which customers have not paid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cart, member) ⇒ OrderHandler

Returns a new instance of OrderHandler.



7
8
9
10
# File 'app/models/order_handler.rb', line 7

def initialize(cart, member)
  self.cart = cart
  self.member = member
end

Instance Attribute Details

#cartObject

Returns the value of attribute cart.



5
6
7
# File 'app/models/order_handler.rb', line 5

def cart
  @cart
end

#discount_errorObject

Returns the value of attribute discount_error.



5
6
7
# File 'app/models/order_handler.rb', line 5

def discount_error
  @discount_error
end

#errorObject

Returns the value of attribute error.



5
6
7
# File 'app/models/order_handler.rb', line 5

def error
  @error
end

#memberObject

Returns the value of attribute member.



5
6
7
# File 'app/models/order_handler.rb', line 5

def member
  @member
end

#over_limitObject

Returns the value of attribute over_limit.



5
6
7
# File 'app/models/order_handler.rb', line 5

def over_limit
  @over_limit
end

Instance Method Details

#handle(params, store_organization) ⇒ Object



12
13
14
15
16
17
18
# File 'app/models/order_handler.rb', line 12

def handle(params, store_organization)
  self.handle_tickets(params)    
  self.handle_donation(params, store_organization)
  self.handle_memberships(params, self.member)
  self.handle_passes(params)
  self.handle_discount_or_pass_code(params)
end

#handle_discount_or_pass_code(params) ⇒ Object



105
106
107
108
109
# File 'app/models/order_handler.rb', line 105

def handle_discount_or_pass_code(params)
  if params[:discount_or_pass_code].present?
    is_pass?(params[:discount_or_pass_code]) ? apply_pass(params) : handle_discount(params)
  end
end

#handle_donation(params, organization) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/order_handler.rb', line 43

def handle_donation(params, organization)
  donation_amount = params[:donation_amount].present? ? params[:donation_amount] : params[:donation_amount_fixed]
  if donation_amount
    self.cart.clear_donations
    if donation_amount.to_i == 0
      self.error = "Please enter a donation amount."
      return
    end
    
    donation = Donation.new
    donation.amount = donation_amount.to_i * 100
    donation.organization = organization
    self.cart.donations << donation
  end
end

#handle_memberships(params, member = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/order_handler.rb', line 60

def handle_memberships(params, member = nil)
  unless params[:membership_type].blank?
    membership_type_id = params[:membership_type][:id]
    quantity = params[:quantity].to_i
    
    if membership_type_id.blank?
      self.error = "Please select a membership."
      return
    end
    
    (1..quantity).each do |i|
      if allowed_to_add?(membership_type_id)
        self.cart.memberships << Membership.for(MembershipType.find(membership_type_id), member)
      else
        self.error = "Sorry, we can't add any more of this membership type to your cart."
      end
    end
  end
end

#handle_passes(params) ⇒ Object

Note that this handles passes *currently being purchased* NOT passes that are being applied to the cart



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/order_handler.rb', line 83

def handle_passes(params)
  Rails.logger.debug(params)
  unless params[:pass_type].blank?
    pass_type_id = params[:pass_type][:id]
    quantity = params[:quantity].to_i

    if pass_type_id.blank?
      self.error = "Please select a pass."
      return
    end

    (1..quantity).each do |i|
      self.cart.passes << Pass.for(PassType.find(pass_type_id))
    end
    
  end    
end

#handle_tickets(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/order_handler.rb', line 20

def handle_tickets(params)
  if params[:ticket_type_id]
    ticket_ids = []
    over_limit = []

    ticket_type = TicketType.find(params[:ticket_type_id])
    Rails.logger.debug("QUANTITY #{params[:quantity].to_i}")
    tickets = ticket_type.available_tickets(params[:quantity].to_i, member)
    ids = tickets.collect(&:id)
    Rails.logger.debug("TICKET IDS: #{ids}")
    Ticket.lock(tickets, ticket_type, self.cart)

    Rails.logger.debug("OVER LIMIT? #{ids.length < params[:quantity].to_i}")
    if ids.length < params[:quantity].to_i
      Rails.logger.debug("OVER THE LINE!")
      #TODO: return and display a sensible error message
    end

    params = params.merge(:tickets => ticket_ids) if ticket_ids.any?
  end
  params
end

#is_pass?(code) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/order_handler.rb', line 101

def is_pass?(code)
  Pass.where(:pass_code => code).any?
end