Class: Workarea::Inventory::UnitOfWork

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/inventory/unit_of_work.rb

Overview

This class handles the transaction details of doing an inventory purchase or rollback.

Defined Under Namespace

Classes: Failure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ UnitOfWork

Returns a new instance of UnitOfWork.



11
12
13
# File 'app/models/workarea/inventory/unit_of_work.rb', line 11

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'app/models/workarea/inventory/unit_of_work.rb', line 9

def items
  @items
end

Instance Method Details

#commitObject

Perform the purchase and set the results on the items. Rolls back any saved changed on an InsufficientError.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/workarea/inventory/unit_of_work.rb', line 26

def commit
  items.each do |item|
    record = records.for_sku(item.sku)
    results = record.purchase(item.total)

    item.attributes = results.except(:success) if results.present?
  end

rescue InsufficientError => e
  rollback
  raise Failure, e.message
end

#recordsInventory::Collection

The Skus collection for the items we’re going to operate on.



19
20
21
# File 'app/models/workarea/inventory/unit_of_work.rb', line 19

def records
  @records ||= Inventory::Collection.new(items.map(&:sku).flatten)
end

#restock(quantities = {}) ⇒ Object

Restock inventory from the purchase. Also updates the item quantities to reflect the current state of captured inventory.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/workarea/inventory/unit_of_work.rb', line 54

def restock(quantities = {})
  quantities.each do |sku, quantity|
    item = items.detect { |i| i.sku == sku }

    if item.expired_backorder?
      available_to_restock = quantity
      backordered_to_restock = 0
    else
      available_to_restock = [item.available, quantity].min
      backordered_to_restock = quantity - available_to_restock
    end

    record = records.for_sku(sku)
    record.release(available_to_restock, backordered_to_restock)

    available_restocked = [available_to_restock, item.available].min

    item.available -= available_restocked
    item.backordered -= if item.expired_backorder?
                          available_to_restock - available_restocked
                        else
                          backordered_to_restock
                        end
  end
end

#rollbackObject

Roll back the purchase from all of the items. Also resets the items to reflect that they have no captured inventory units.



42
43
44
45
46
47
48
49
# File 'app/models/workarea/inventory/unit_of_work.rb', line 42

def rollback
  items.each do |item|
    record = records.for_sku(item.sku)
    record.release(item.available, item.backordered)

    item.attributes = { available: 0, backordered: 0 }
  end
end