Class: Workarea::Inventory::Transaction
- Inherits:
-
Object
- Object
- Workarea::Inventory::Transaction
- Includes:
- ApplicationDocument
- Defined in:
- app/models/workarea/inventory/transaction.rb
Class Method Summary collapse
- .captured_for_order(id) ⇒ Object
-
.expired ⇒ Mongoid::Criteria
Query for inventory orders that are expired.
-
.from_order(id, items) ⇒ Inventory::Transaction
Creates a Transaction based on order ID and items.
Instance Method Summary collapse
-
#purchase ⇒ self
Capture the inventory quantities for this order.
-
#restock(quantities) ⇒ self
Reverts the operations from a capture to free up the appropriate inventory.
-
#rollback ⇒ self
Reverts the operations from a capture to free up the appropriate inventory.
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list
Methods included from Mongoid::Document
Class Method Details
.captured_for_order(id) ⇒ Object
31 32 33 |
# File 'app/models/workarea/inventory/transaction.rb', line 31 def self.captured_for_order(id) find_by(order_id: id, captured: true) rescue nil end |
.expired ⇒ Mongoid::Criteria
Query for inventory orders that are expired. This means they were never captured in 6 months.
40 41 42 |
# File 'app/models/workarea/inventory/transaction.rb', line 40 def self.expired where(:updated_at.lt => Time.current - 6.months, captured: false) end |
.from_order(id, items) ⇒ Inventory::Transaction
Creates a Workarea::Inventory::Transaction based on order ID and items
22 23 24 25 26 27 28 29 |
# File 'app/models/workarea/inventory/transaction.rb', line 22 def self.from_order(id, items) inventory_order = new(order_id: id) items.each do |sku, quantity| inventory_order.items.build(sku: sku, total: quantity) end inventory_order end |
Instance Method Details
#purchase ⇒ self
Capture the inventory quantities for this order. This method uses MongoDB’s findAndModify to ensure we have valid inventory quantities to capture against.
When successful, it updates item states to reflect what type of inventory (available vs backorder) so we have this data from time of capture.
This method is used when placing the order in checkout.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/workarea/inventory/transaction.rb', line 57 def purchase unit = UnitOfWork.new(items) unit.commit self.captured = true save! rescue UnitOfWork::Failure => e self.captured = false errors.add(:base, e.) self end |
#restock(quantities) ⇒ self
Reverts the operations from a capture to free up the appropriate inventory. Used to restock inventory when canceling an order item with that option selected.
95 96 97 98 99 |
# File 'app/models/workarea/inventory/transaction.rb', line 95 def restock(quantities) UnitOfWork.new(items).restock(quantities) save! self end |
#rollback ⇒ self
Reverts the operations from a capture to free up the appropriate inventory. Used to rollback when another order-placing dependency (e.g. payment) fails.
78 79 80 81 82 83 84 85 86 |
# File 'app/models/workarea/inventory/transaction.rb', line 78 def rollback unit = UnitOfWork.new(items) unit.rollback self.captured = false save! self end |