Class: Effective::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Order
- Defined in:
- app/models/effective/order.rb
Instance Attribute Summary collapse
-
#confirmed_checkout ⇒ Object
Set on the Checkout Step 1.
-
#send_mark_as_paid_email_to_buyer ⇒ Object
Set by Admin::Orders#mark_as_paid.
-
#send_payment_request_to_buyer ⇒ Object
Settings in the /admin action forms.
-
#skip_buyer_validations ⇒ Object
Set by Admin::Orders#create.
-
#terms_and_conditions ⇒ Object
Yes, I agree to the terms and conditions.
Instance Method Summary collapse
-
#add(*items, quantity: 1) ⇒ Object
Items can be an Effective::Cart, an Effective::order, a single acts_as_purchasable, or multiple acts_as_purchasables add(Product.first) => returns an Effective::OrderItem add(Product.first, current_cart) => returns an array of Effective::OrderItems.
-
#assign_confirmed_if_valid! ⇒ Object
This lets us skip to the confirmed workflow for an admin...
-
#confirm! ⇒ Object
Used by admin checkout only.
- #confirmed? ⇒ Boolean
- #decline!(payment: 'none', provider: 'none', card: 'none', validate: true) ⇒ Object
- #declined? ⇒ Boolean
- #defer!(provider: 'none', email: true) ⇒ Object
- #deferred? ⇒ Boolean
-
#emails_send_to ⇒ Object
Doesn't control anything.
- #free? ⇒ Boolean
-
#initialize(atts = nil, &block) ⇒ Order
constructor
Effective::Order.new(items: Product.first) Effective::Order.new(items: [Product.first, Product.second], user: User.first) Effective::Order.new(items: Product.first, user: User.first, billing_address: Effective::Address.new, shipping_address: Effective::Address.new).
- #num_items ⇒ Object
-
#pending! ⇒ Object
This is called from admin/orders#create This is intended for use as an admin action only It skips any address or bad user validations It's basically the same as save! on a new order, except it might send the payment request to buyer.
- #pending? ⇒ Boolean
- #purchasables ⇒ Object
-
#purchase!(payment: 'none', provider: 'none', card: 'none', email: true, skip_buyer_validations: false) ⇒ Object
Effective::Order.new(items: Product.first, user: User.first).purchase!(email: false).
- #purchased?(provider = nil) ⇒ Boolean
- #refund? ⇒ Boolean
- #send_mark_as_paid_email_to_buyer? ⇒ Boolean
- #send_order_receipt_to_admin! ⇒ Object
- #send_order_receipt_to_buyer! ⇒ Object
- #send_order_receipts! ⇒ Object
- #send_payment_request_to_buyer! ⇒ Object
- #send_payment_request_to_buyer? ⇒ Boolean
- #send_pending_order_invoice_to_buyer! ⇒ Object
- #send_refund_notification! ⇒ Object
- #skip_buyer_validations? ⇒ Boolean
- #skip_qb_sync! ⇒ Object
- #subtotal ⇒ Object
- #tax ⇒ Object
- #tax_rate ⇒ Object
- #to_s ⇒ Object
- #total ⇒ Object
- #update_prices! ⇒ Object
Constructor Details
#initialize(atts = nil, &block) ⇒ Order
Effective::Order.new(items: Product.first) Effective::Order.new(items: [Product.first, Product.second], user: User.first) Effective::Order.new(items: Product.first, user: User.first, billing_address: Effective::Address.new, shipping_address: Effective::Address.new)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/models/effective/order.rb', line 162 def initialize(atts = nil, &block) super(state: EffectiveOrders::PENDING) # Initialize with state: PENDING return unless atts.present? if atts.kind_of?(Hash) items = Array(atts.delete(:item)) + Array(atts.delete(:items)) self.user = atts.delete(:user) || (items.first.user if items.first.respond_to?(:user)) if (address = atts.delete(:billing_address)).present? self.billing_address = address self.billing_address.full_name ||= user.to_s.presence end if (address = atts.delete(:shipping_address)).present? self.shipping_address = address self.shipping_address.full_name ||= user.to_s.presence end atts.each { |key, value| self.send("#{key}=", value) } add(items) if items.present? else # Attributes are not a Hash self.user = atts.user if atts.respond_to?(:user) add(atts) end end |
Instance Attribute Details
#confirmed_checkout ⇒ Object
Set on the Checkout Step 1
24 25 26 |
# File 'app/models/effective/order.rb', line 24 def confirmed_checkout @confirmed_checkout end |
#send_mark_as_paid_email_to_buyer ⇒ Object
Set by Admin::Orders#mark_as_paid
28 29 30 |
# File 'app/models/effective/order.rb', line 28 def send_mark_as_paid_email_to_buyer @send_mark_as_paid_email_to_buyer end |
#send_payment_request_to_buyer ⇒ Object
Settings in the /admin action forms
27 28 29 |
# File 'app/models/effective/order.rb', line 27 def send_payment_request_to_buyer @send_payment_request_to_buyer end |
#skip_buyer_validations ⇒ Object
Set by Admin::Orders#create
29 30 31 |
# File 'app/models/effective/order.rb', line 29 def skip_buyer_validations @skip_buyer_validations end |
#terms_and_conditions ⇒ Object
Yes, I agree to the terms and conditions
23 24 25 |
# File 'app/models/effective/order.rb', line 23 def terms_and_conditions @terms_and_conditions end |
Instance Method Details
#add(*items, quantity: 1) ⇒ Object
Items can be an Effective::Cart, an Effective::order, a single acts_as_purchasable, or multiple acts_as_purchasables add(Product.first) => returns an Effective::OrderItem add(Product.first, current_cart) => returns an array of Effective::OrderItems
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'app/models/effective/order.rb', line 194 def add(*items, quantity: 1) raise 'unable to alter a purchased order' if purchased? raise 'unable to alter a declined order' if declined? cart_items = items.flatten.flat_map do |item| if item.kind_of?(Effective::Cart) item.cart_items.to_a elsif item.kind_of?(ActsAsPurchasable) Effective::CartItem.new(quantity: quantity, purchasable: item) elsif item.kind_of?(Effective::Order) # Duplicate an existing order self.note_to_buyer ||= item.note_to_buyer self.note_internal ||= item.note_internal item.order_items.select { |oi| oi.purchasable.kind_of?(Effective::Product) }.map do |oi| product = Effective::Product.new(name: oi.purchasable.purchasable_name, price: oi.purchasable.price, tax_exempt: oi.purchasable.tax_exempt) Effective::CartItem.new(quantity: oi.quantity, purchasable: product) end else raise 'add() expects one or more acts_as_purchasable objects, or an Effective::Cart' end end.compact # Make sure to reset stored aggregates self.total = nil self.subtotal = nil self.tax = nil retval = cart_items.map do |item| order_items.build( name: item.name, quantity: item.quantity, price: item.price, tax_exempt: (item.tax_exempt || false), ).tap { |order_item| order_item.purchasable = item.purchasable } end retval.size == 1 ? retval.first : retval end |
#assign_confirmed_if_valid! ⇒ Object
This lets us skip to the confirmed workflow for an admin...
352 353 354 355 356 357 358 359 360 361 |
# File 'app/models/effective/order.rb', line 352 def assign_confirmed_if_valid! return unless pending? self.state = EffectiveOrders::CONFIRMED return true if valid? self.errors.clear self.state = EffectiveOrders::PENDING false end |
#confirm! ⇒ Object
Used by admin checkout only
346 347 348 349 |
# File 'app/models/effective/order.rb', line 346 def confirm! return false if purchased? update!(state: EffectiveOrders::CONFIRMED) end |
#confirmed? ⇒ Boolean
268 269 270 |
# File 'app/models/effective/order.rb', line 268 def confirmed? state == EffectiveOrders::CONFIRMED end |
#decline!(payment: 'none', provider: 'none', card: 'none', validate: true) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'app/models/effective/order.rb', line 417 def decline!(payment: 'none', provider: 'none', card: 'none', validate: true) return false if declined? raise EffectiveOrders::AlreadyPurchasedException.new('order already purchased') if purchased? error = nil assign_attributes( state: EffectiveOrders::DECLINED, purchased_at: nil, payment: payment_to_h(payment), payment_provider: provider, payment_card: (card.presence || 'none'), skip_buyer_validations: true ) Effective::Order.transaction do begin save!(validate: validate) rescue => e self.state = state_was error = e. raise ::ActiveRecord::Rollback end end raise "Failed to decline order: #{error || errors..to_sentence}" unless error.nil? run_purchasable_callbacks(:after_decline) true end |
#declined? ⇒ Boolean
282 283 284 |
# File 'app/models/effective/order.rb', line 282 def declined? state == EffectiveOrders::DECLINED end |
#defer!(provider: 'none', email: true) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'app/models/effective/order.rb', line 402 def defer!(provider: 'none', email: true) return false if purchased? assign_attributes( state: EffectiveOrders::DEFERRED, payment_provider: provider ) save! send_payment_request_to_buyer! if email true end |
#deferred? ⇒ Boolean
272 273 274 |
# File 'app/models/effective/order.rb', line 272 def deferred? state == EffectiveOrders::DEFERRED end |
#emails_send_to ⇒ Object
Doesn't control anything. Purely for the flash messaging
452 453 454 |
# File 'app/models/effective/order.rb', line 452 def emails_send_to [email, cc.presence].compact.to_sentence end |
#free? ⇒ Boolean
306 307 308 |
# File 'app/models/effective/order.rb', line 306 def free? total == 0 end |
#num_items ⇒ Object
314 315 316 |
# File 'app/models/effective/order.rb', line 314 def num_items order_items.map { |oi| oi.quantity }.sum end |
#pending! ⇒ Object
This is called from admin/orders#create This is intended for use as an admin action only It skips any address or bad user validations It's basically the same as save! on a new order, except it might send the payment request to buyer
334 335 336 337 338 339 340 341 342 343 |
# File 'app/models/effective/order.rb', line 334 def pending! return false if purchased? self.state = EffectiveOrders::PENDING self.addresses.clear if addresses.any? { |address| address.valid? == false } save! send_payment_request_to_buyer! if send_payment_request_to_buyer? true end |
#pending? ⇒ Boolean
264 265 266 |
# File 'app/models/effective/order.rb', line 264 def pending? state == EffectiveOrders::PENDING end |
#purchasables ⇒ Object
286 287 288 |
# File 'app/models/effective/order.rb', line 286 def purchasables order_items.map { |order_item| order_item.purchasable } end |
#purchase!(payment: 'none', provider: 'none', card: 'none', email: true, skip_buyer_validations: false) ⇒ Object
Effective::Order.new(items: Product.first, user: User.first).purchase!(email: false)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'app/models/effective/order.rb', line 364 def purchase!(payment: 'none', provider: 'none', card: 'none', email: true, skip_buyer_validations: false) return false if purchased? error = nil assign_attributes( state: EffectiveOrders::PURCHASED, payment: payment_to_h(payment), payment_provider: provider, payment_card: (card.presence || 'none'), skip_buyer_validations: skip_buyer_validations ) self.purchased_at ||= Time.zone.now Effective::Order.transaction do begin run_purchasable_callbacks(:before_purchase) save! update_purchasables_purchased_order! rescue => e self.state = state_was self.purchased_at = nil error = e. raise ::ActiveRecord::Rollback end end raise "Failed to purchase order: #{error || errors..to_sentence}" unless error.nil? run_purchasable_callbacks(:after_purchase) send_refund_notification! if email && refund? send_order_receipts! if email true end |
#purchased?(provider = nil) ⇒ Boolean
276 277 278 279 280 |
# File 'app/models/effective/order.rb', line 276 def purchased?(provider = nil) return false if (state != EffectiveOrders::PURCHASED) return true if provider.nil? || payment_provider == provider.to_s false end |
#refund? ⇒ Boolean
310 311 312 |
# File 'app/models/effective/order.rb', line 310 def refund? total.to_i < 0 end |
#send_mark_as_paid_email_to_buyer? ⇒ Boolean
322 323 324 |
# File 'app/models/effective/order.rb', line 322 def send_mark_as_paid_email_to_buyer? EffectiveResources.truthy?(send_mark_as_paid_email_to_buyer) end |
#send_order_receipt_to_admin! ⇒ Object
461 462 463 |
# File 'app/models/effective/order.rb', line 461 def send_order_receipt_to_admin! send_email(:order_receipt_to_admin, to_param) if purchased? end |
#send_order_receipt_to_buyer! ⇒ Object
465 466 467 |
# File 'app/models/effective/order.rb', line 465 def send_order_receipt_to_buyer! send_email(:order_receipt_to_buyer, to_param) if purchased? end |
#send_order_receipts! ⇒ Object
456 457 458 459 |
# File 'app/models/effective/order.rb', line 456 def send_order_receipts! send_order_receipt_to_admin! if EffectiveOrders.mailer[:send_order_receipt_to_admin] send_order_receipt_to_buyer! if EffectiveOrders.mailer[:send_order_receipt_to_buyer] end |
#send_payment_request_to_buyer! ⇒ Object
469 470 471 |
# File 'app/models/effective/order.rb', line 469 def send_payment_request_to_buyer! send_email(:payment_request_to_buyer, to_param) unless purchased? end |
#send_payment_request_to_buyer? ⇒ Boolean
318 319 320 |
# File 'app/models/effective/order.rb', line 318 def send_payment_request_to_buyer? EffectiveResources.truthy?(send_payment_request_to_buyer) && !free? && !refund? end |
#send_pending_order_invoice_to_buyer! ⇒ Object
473 474 475 |
# File 'app/models/effective/order.rb', line 473 def send_pending_order_invoice_to_buyer! send_email(:pending_order_invoice_to_buyer, to_param) unless purchased? end |
#send_refund_notification! ⇒ Object
477 478 479 |
# File 'app/models/effective/order.rb', line 477 def send_refund_notification! send_email(:refund_notification_to_admin, to_param) if purchased? && refund? end |
#skip_buyer_validations? ⇒ Boolean
326 327 328 |
# File 'app/models/effective/order.rb', line 326 def skip_buyer_validations? EffectiveResources.truthy?(skip_buyer_validations) end |
#skip_qb_sync! ⇒ Object
481 482 483 |
# File 'app/models/effective/order.rb', line 481 def skip_qb_sync! EffectiveOrders.use_effective_qb_sync ? EffectiveQbSync.skip_order!(self) : true end |
#subtotal ⇒ Object
290 291 292 |
# File 'app/models/effective/order.rb', line 290 def subtotal self[:subtotal] || order_items.map { |oi| oi.subtotal }.sum end |
#tax ⇒ Object
298 299 300 |
# File 'app/models/effective/order.rb', line 298 def tax self[:tax] || get_tax() end |
#tax_rate ⇒ Object
294 295 296 |
# File 'app/models/effective/order.rb', line 294 def tax_rate self[:tax_rate] || get_tax_rate() end |
#to_s ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 |
# File 'app/models/effective/order.rb', line 252 def to_s if refund? "Refund ##{to_param}" elsif purchased? "Receipt ##{to_param}" elsif pending? "Pending Order ##{to_param}" else "Order ##{to_param}" end end |
#total ⇒ Object
302 303 304 |
# File 'app/models/effective/order.rb', line 302 def total (self[:total] || (subtotal + tax.to_i)).to_i end |
#update_prices! ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'app/models/effective/order.rb', line 235 def update_prices! raise('already purchased') if purchased? raise('must be pending or confirmed') unless pending? || confirmed? order_items.each do |item| purchasable = item.purchasable if purchasable.blank? || purchasable.marked_for_destruction? item.mark_for_destruction else item.price = purchasable.price end end save! end |