Class: Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveRecord::Transitions
Defined in:
app/models/cart.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#special_instructionsObject

Returns the value of attribute special_instructions.



10
11
12
# File 'app/models/cart.rb', line 10

def special_instructions
  @special_instructions
end

Class Method Details

.create_for_reseller(reseller_id = nil, params = {}) ⇒ Object



159
160
161
# File 'app/models/cart.rb', line 159

def self.create_for_reseller(reseller_id = nil, params = {})
  reseller_id.blank? ? Cart.create(params) : Reseller::Cart.create( params.merge({:reseller => Organization.find(reseller_id)}) )
end

.find_cart(cart_id, reseller_id) ⇒ Object

find_cart is deprecated and will be removed when Widget v1 support is removed. Please use find_or_create.



231
232
233
234
235
236
237
# File 'app/models/cart.rb', line 231

def self.find_cart(cart_id, reseller_id)
  ActiveSupport::Deprecation.warn("find_cart is deprecated and will be removed when Widget v1 support is removed. Please use find_or_create.")
  Rails.logger.debug("Searching for cart [#{cart_id}] for reseller [#{reseller_id}]")
  rel = where(:id => cart_id)
  rel.where(:reseller_id => reseller_id) unless reseller_id.blank?
  rel.first
end

.find_or_create(cart_token, reseller_id) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/cart.rb', line 163

def self.find_or_create(cart_token, reseller_id)
  if cart_token.nil?
    raise ActiveRecord::RecordNotFound.new("No cart with nil token")
  end

  if reseller_id.present?
    if Cart.find_by_token_and_reseller_id(cart_token, reseller_id)
      cart = Cart.find_by_token_and_reseller_id(cart_token, reseller_id)
    else
      cart = Reseller::Cart.create(token: cart_token, reseller_id: reseller_id)
    end
  else
    cart = Cart.find_or_create_by_token!(cart_token)
  end

  if cart.completed?
    cart.transfer_token_to_new_cart
  else
    cart
  end
end

.for_reseller(reseller_id = nil, params = {}) ⇒ Object

for_reseller is deprecated and will be removed when Widget v1 support is removed. Please use find_or_create.



223
224
225
226
# File 'app/models/cart.rb', line 223

def self.for_reseller(reseller_id = nil, params = {})
  ActiveSupport::Deprecation.warn("for_reseller is deprecated and will be removed when Widget v1 support is removed. Please use find_or_create.")
  reseller_id.blank? ? Cart.create(params) : Reseller::Cart.create( params.merge({:reseller => Organization.find(reseller_id)}) )
end

Instance Method Details

#<<(tkts) ⇒ Object



107
108
109
110
111
112
113
114
# File 'app/models/cart.rb', line 107

def <<(tkts)
  tkts = Array.wrap(tkts)
  tkts.each do |t|
    raise "Cannot add tickets to a cart without a ticket type set on the Ticket" if t.ticket_type.nil?
    t.cart_price  = t.ticket_type.price
  end
  self.tickets << tkts.flatten
end

#applied_codeObject



54
55
56
# File 'app/models/cart.rb', line 54

def applied_code
  discount.try(:code) || applied_pass.try(:pass_code)
end

#as_json(options = {}) ⇒ Object



95
96
97
# File 'app/models/cart.rb', line 95

def as_json(options = {})
  super({ :methods => [ 'tickets', 'donations', 'memberships' ]}.merge(options))
end

#calculate_fees(obj) ⇒ Object



103
104
105
# File 'app/models/cart.rb', line 103

def calculate_fees(obj)
  FeeCalculator.apply(FeeStrategy.new).to(self)
end

#can_hold?(ticket) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/cart.rb', line 73

def can_hold?(ticket)
  true
end

#checkout_classObject



46
47
48
# File 'app/models/cart.rb', line 46

def checkout_class
  Checkout
end

#clear!Object



58
59
60
61
62
63
64
65
66
# File 'app/models/cart.rb', line 58

def clear!
  clear_tickets
  clear_donations
  clear_memberships
  clear_passes
  self.discount = nil
  self.applied_pass = nil
  save
end

#clear_donationsObject



77
78
79
80
81
82
83
84
85
# File 'app/models/cart.rb', line 77

def clear_donations
  temp = []

  #This won't work if there is more than 1 FAFS donation on the order
  donations.each do |donation|
    temp = donations.delete(donations)
  end
  temp
end

#clear_membershipsObject



87
88
89
# File 'app/models/cart.rb', line 87

def clear_memberships
  self.memberships.destroy_all
end

#clear_passesObject



91
92
93
# File 'app/models/cart.rb', line 91

def clear_passes
  self.passes.destroy_all
end

#clear_ticketsObject



68
69
70
71
# File 'app/models/cart.rb', line 68

def clear_tickets
  Ticket.unlock(self.tickets, self)
  self.tickets = []
end

#completed?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/cart.rb', line 136

def completed?
  approved?
end

#discount_amountObject



128
129
130
# File 'app/models/cart.rb', line 128

def discount_amount
  total_before_discount - total
end

#fee_in_centsObject



99
100
101
# File 'app/models/cart.rb', line 99

def fee_in_cents
  items.sum(&:service_fee)
end

#generate_donationsObject



140
141
142
143
144
145
146
147
148
# File 'app/models/cart.rb', line 140

def generate_donations
  organizations.collect do |organization|
    if organization.can?(:receive, Donation)
      donation = Donation.new
      donation.organization = organization
      donation
    end
  end.compact
end

#has_discount_or_pass?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/cart.rb', line 50

def has_discount_or_pass?
  !discount.nil? || !applied_pass.nil?
end

#itemsObject

Note that this does return Item objects. It returns Tickets, Donations, Memberships, etc…

TODO: Refactor this to another name to avoid confusion with Item



42
43
44
# File 'app/models/cart.rb', line 42

def items
  self.tickets + self.donations + self.memberships + self.passes
end

#organizationsObject



150
151
152
153
154
155
156
157
# File 'app/models/cart.rb', line 150

def organizations
  ids = []
  [:donations, :memberships, :tickets, :passes].each do |collection|
    ids = ids + self.send(collection).collect(&:organization_id)
  end
  ids = ids.uniq
  Organization.find(ids)
end

#prepare_for_discount!Object



208
209
210
211
212
# File 'app/models/cart.rb', line 208

def prepare_for_discount!
  transaction do
    tickets.each {|ticket| ticket.prepare_for_discount! }
  end
end

#prepare_for_pass!Object



202
203
204
205
206
# File 'app/models/cart.rb', line 202

def prepare_for_pass!
  transaction do
    tickets.each {|ticket| ticket.prepare_for_pass! }
  end
end

#reseller_is?(reseller_id) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'app/models/cart.rb', line 198

def reseller_is?(reseller_id)
  (self.reseller_id.blank? && reseller_id.blank?) || (self.reseller_id == reseller_id.to_i)
end

#reset_prices_on_ticketsObject



214
215
216
217
218
# File 'app/models/cart.rb', line 214

def reset_prices_on_tickets
  transaction do
    tickets.each {|ticket| ticket.reset_price! }
  end
end

#subtotalObject



116
117
118
# File 'app/models/cart.rb', line 116

def subtotal
  items.sum(&:price)
end

#totalObject



124
125
126
# File 'app/models/cart.rb', line 124

def total
  items.sum(&:cart_price) + fee_in_cents
end

#total_before_discountObject



120
121
122
# File 'app/models/cart.rb', line 120

def total_before_discount
  items.sum(&:price) + fee_in_cents
end

#transfer_token_to_new_cartObject



185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/cart.rb', line 185

def transfer_token_to_new_cart
  new_cart = Cart.new
  transaction do
    new_cart.type = self.type
    new_cart.reseller_id = self.reseller_id
    new_cart.token = self.token
    self.token = nil
    self.save!
    new_cart.save!
  end
  new_cart
end

#unfinished?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'app/models/cart.rb', line 132

def unfinished?
  started? or rejected?
end