Class: Workarea::Inventory::Transaction

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/inventory/transaction.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

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

#embedded_children

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

.expiredMongoid::Criteria

Query for inventory orders that are expired. This means they were never captured in 6 months.

Returns:



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

Parameters:

  • ID (String)
  • items (Hash)

    keys are skus, values are quantities

Returns:



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

#purchaseself

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.

Returns:

  • (self)


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.message)

  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.

Returns:

  • (self)


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

#rollbackself

Reverts the operations from a capture to free up the appropriate inventory. Used to rollback when another order-placing dependency (e.g. payment) fails.

Returns:

  • (self)


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