Class: Order

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ActionView::Helpers::TextHelper, ArtfullyOseHelper, Ext::DelayedIndexing, Ext::Integrations::Order, OhNoes::Destroy
Defined in:
app/models/orders/order.rb

Overview

Subclasses (and their type) should speak to the location or nature of the order, not the contents of the items WebOrder, BoxOfficeOrder for example. NOT DonationOrder, since orders may contain multiple different item types

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ext::DelayedIndexing

included

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

Methods included from OhNoes::Destroy

#destroy

Methods included from Ext::Integrations::Order

#fa_id, included

Instance Attribute Details

#per_item_processing_chargeObject

This is a lambda used to by the items to calculate their net



11
12
13
# File 'app/models/orders/order.rb', line 11

def per_item_processing_charge
  @per_item_processing_charge
end

#skip_actionsObject

Returns the value of attribute skip_actions.



24
25
26
# File 'app/models/orders/order.rb', line 24

def skip_actions
  @skip_actions
end

Class Method Details

.in_range(start, stop, organization_id = nil) ⇒ Object



83
84
85
86
87
88
89
90
# File 'app/models/orders/order.rb', line 83

def self.in_range(start, stop, organization_id = nil)
  query = after(start).before(stop).includes(:items, :person, :organization).order("created_at DESC")
  if organization_id.present?
    query.where('organization_id = ?', organization_id)
  else
    query
  end
end

.locationObject



100
101
102
# File 'app/models/orders/order.rb', line 100

def self.location
  ""
end

Instance Method Details

#<<(products) ⇒ Object



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

def <<(products)
  self.items << Array.wrap(products).collect { |product|  Item.for(product, @per_item_processing_charge) }
end

#all_donationsObject



151
152
153
# File 'app/models/orders/order.rb', line 151

def all_donations
  all_items.select(&:donation?)
end

#all_itemsObject



138
139
140
# File 'app/models/orders/order.rb', line 138

def all_items
  merge_and_sort_items
end

#all_ticketsObject



142
143
144
# File 'app/models/orders/order.rb', line 142

def all_tickets
  all_items.select(&:ticket?)
end

#artfully?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/orders/order.rb', line 92

def artfully?
  !transaction_id.nil?
end

#cash?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'app/models/orders/order.rb', line 242

def cash?
  payment_method.eql? CashPayment.payment_method
end

#contact_emailObject



272
273
274
# File 'app/models/orders/order.rb', line 272

def contact_email
  items.try(:first).try(:show).try(:event).try(:contact_email)
end

#create_donation_actionsObject



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/models/orders/order.rb', line 276

def create_donation_actions
  items.select(&:donation?).collect do |item|
    action                    = GiveAction.new
    action.person             = person
    action.subject            = self
    action.organization_id    = organization.id
    action.details            = donation_details
    action.occurred_at        = created_at
    action.subtype            = "Monetary"
    action.save!
    action
  end
end

#create_purchase_actionObject



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'app/models/orders/order.rb', line 291

def create_purchase_action
  unless all_tickets.empty?
    action                  = purchase_action_class.new
    action.person           = person
    action.subject          = self
    action.organization     = organization
    action.details          = ticket_details
    action.occurred_at      = created_at

    #Weird, but Rails can't initialize these so the subtype is hardcoded in the model
    action.subtype          = action.subtype
    action.import           = self.import if self.import
    action.save!
    action
  end
end

#credit?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'app/models/orders/order.rb', line 238

def credit?
  payment_method.eql? CreditCardPayment.payment_method
end

#destroyable?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/orders/order.rb', line 112

def destroyable?
  ( (type.eql? "ApplicationOrder") || (type.eql? "ImportedOrder") ) && !is_fafs? && !artfully? && has_single_donation?
end

#discounts_usedObject

Will return an array of all discount codes on all items on this order



200
201
202
# File 'app/models/orders/order.rb', line 200

def discounts_used
  items.map{|i| i.discount.try(:code)}.reject(&:blank?).uniq
end

#donation_detailsObject



222
223
224
225
226
227
228
# File 'app/models/orders/order.rb', line 222

def donation_details
  if is_fafs?
    "#{number_as_cents sum_donations} donation made through Fractured Atlas"
  else
    "#{number_as_cents sum_donations} donation"
  end
end

#donationsObject



155
156
157
# File 'app/models/orders/order.rb', line 155

def donations
  items.select(&:donation?)
end

#editable?Boolean

Returns:

  • (Boolean)


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

def editable?
  ( (type.eql? "ApplicationOrder") || (type.eql? "ImportedOrder") ) && !is_fafs? && !artfully? && has_single_donation? 
end

#exchangeable_itemsObject



173
174
175
# File 'app/models/orders/order.rb', line 173

def exchangeable_items
  items.select(&:exchangeable?)
end

#for_organization(org) ⇒ Object



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

def for_organization(org)
  self.organization = org
end

#has_donation?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'app/models/orders/order.rb', line 189

def has_donation?
  items.select(&:donation?).present?
end

#has_single_donation?Boolean

End dupes

Returns:

  • (Boolean)


160
161
162
# File 'app/models/orders/order.rb', line 160

def has_single_donation?
  (donations.size == 1) && tickets.empty?
end

#has_ticket?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'app/models/orders/order.rb', line 185

def has_ticket?
  items.select(&:ticket?).present?
end

#is_fafs?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'app/models/orders/order.rb', line 218

def is_fafs?
  !fa_id.nil?
end

#locationObject



96
97
98
# File 'app/models/orders/order.rb', line 96

def location
  self.class.location
end

#nongift_amountObject



108
109
110
# File 'app/models/orders/order.rb', line 108

def nongift_amount
  all_items.inject(0) {|sum, item| sum + item.nongift_amount.to_i }
end

#num_ticketsObject



181
182
183
# File 'app/models/orders/order.rb', line 181

def num_tickets
  all_tickets.size
end

#original_orderObject



246
247
248
249
250
251
252
# File 'app/models/orders/order.rb', line 246

def original_order
  if self.parent.nil?
    return self
  else
    return self.parent.original_order
  end
end

#paymentObject



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

def payment
  CreditCardPayment.new(:transaction_id => transaction_id)
end

#purchase_action_classObject



309
310
311
# File 'app/models/orders/order.rb', line 309

def purchase_action_class
  GetAction
end

#record_exchange!(exchanged_items) ⇒ Object



132
133
134
135
136
# File 'app/models/orders/order.rb', line 132

def record_exchange!(exchanged_items)
  items.each_with_index do |item, index|
    item.to_exchange! exchanged_items[index]
  end
end

#refundable_itemsObject



168
169
170
171
# File 'app/models/orders/order.rb', line 168

def refundable_items
  return [] unless Payment.create(payment_method).refundable?
  items.select(&:refundable?)
end

#returnable_itemsObject



177
178
179
# File 'app/models/orders/order.rb', line 177

def returnable_items
  items.select { |i| i.returnable? and i.comped? and not i.refundable? }
end

#sell_ticketsObject



262
263
264
265
266
# File 'app/models/orders/order.rb', line 262

def sell_tickets
  all_tickets.each do |item|
    item.product.sell_to(self.person, self.created_at)
  end
end

#settleable_donationsObject



164
165
166
# File 'app/models/orders/order.rb', line 164

def settleable_donations
  all_donations.reject(&:modified?)
end

#sum_donationsObject



193
194
195
# File 'app/models/orders/order.rb', line 193

def sum_donations
  all_donations.collect{|item| item.total_price.to_i}.sum
end

#ticket_detailsObject



204
205
206
207
208
209
210
# File 'app/models/orders/order.rb', line 204

def ticket_details
  discount_string = ""
  unless discounts_used.empty?
    discount_string = ", used #{'discount'.pluralize(discounts_used.length)} " + discounts_used.join(",")
  end
  Ticket.to_sentence(self.tickets.map(&:product)) + discount_string
end

#ticket_summaryObject



230
231
232
233
234
235
236
# File 'app/models/orders/order.rb', line 230

def ticket_summary
  summary = TicketSummary.new
  items.select(&:ticket?).each do |item|
    summary << item.product
  end
  summary
end

#ticketsObject

TODO: Undupe these methods



147
148
149
# File 'app/models/orders/order.rb', line 147

def tickets
  items.select(&:ticket?)
end

#time_zoneObject



268
269
270
# File 'app/models/orders/order.rb', line 268

def time_zone
  "Eastern Time (US & Canada)"
end

#to_comp!Object



212
213
214
215
216
# File 'app/models/orders/order.rb', line 212

def to_comp!
  items.each do |item|
    item.to_comp!
  end
end

#totalObject



104
105
106
# File 'app/models/orders/order.rb', line 104

def total
  all_items.inject(0) {|sum, item| sum + item.total_price.to_i }
end

#transaction_idObject

If this order has no transaction_id, run up the parent chain until we hit one This is needed for exchanges that ultimately need to be refunded



258
259
260
# File 'app/models/orders/order.rb', line 258

def transaction_id
  read_attribute(:transaction_id) || self.parent.try(:transaction_id)
end