Module: Workarea::Inventory

Defined in:
app/models/workarea/inventory.rb,
app/models/workarea/inventory/sku.rb,
app/models/workarea/inventory/capture.rb,
app/models/workarea/inventory/collection.rb,
app/models/workarea/inventory/transaction.rb,
app/models/workarea/inventory/unit_of_work.rb,
app/models/workarea/inventory/policies/base.rb,
app/models/workarea/inventory/policies/ignore.rb,
app/models/workarea/inventory/transaction_item.rb,
app/models/workarea/inventory/collection_status.rb,
app/models/workarea/inventory/policies/standard.rb,
app/models/workarea/inventory/policies/allow_backorder.rb,
app/models/workarea/inventory/policies/displayable_when_out_of_stock.rb

Defined Under Namespace

Modules: CollectionStatus, Policies Classes: Capture, Collection, InsufficientError, Sku, Transaction, TransactionItem, UnitOfWork

Class Method Summary collapse

Class Method Details

.any_available?(*skus) ⇒ Boolean

Checks whether any of a set of SKUs are available

Parameters:

  • skus (Array)

Returns:

  • (Boolean)


22
23
24
25
26
# File 'app/models/workarea/inventory.rb', line 22

def self.any_available?(*skus)
  arr = Array(skus).flatten
  set = Inventory::Collection.new(arr)
  set.empty? || set.any?(&:purchasable?)
end

.displayable?(*skus) ⇒ Boolean

Whether any of the passed SKUs should be displayed in search. Used when indexing a product and deciding whether to show that product.

Returns:

  • (Boolean)


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

def self.displayable?(*skus)
  arr = Array(skus).flatten
  set = Inventory::Collection.new(arr)
  set.empty? || set.any?(&:displayable?)
end

.find_insufficiencies(items) ⇒ Hash

Find a set of insuffciencies for a set of items. Used for adjusting a cart when full quantities aren’t available.

Parameters:

  • items (Hash)

    keys are SKUs, values are quantities

Returns:

  • (Hash)

    a hash with keys of SKUs and values of how many short the inventory is



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/workarea/inventory.rb', line 55

def self.find_insufficiencies(items)
  set = Inventory::Collection.new(items.keys)

  items.inject({}) do |memo, item|
    sku, quantity = *item
    record = set.for_sku(sku)

    if record
      memo[sku] = record.insufficiency_for(quantity)
    else
      memo[sku] = 0
    end

    memo
  end
end

.total_available(*skus) ⇒ Integer

Get total count of units available to sell

Parameters:

  • skus (Array)

Returns:

  • (Integer)


43
44
45
46
# File 'app/models/workarea/inventory.rb', line 43

def self.total_available(*skus)
  arr = Array(skus).flatten
  Inventory::Collection.new(arr).sum(&:available)
end

.total_sales(*skus) ⇒ Integer

Gets the total number of sales for a set of SKUs

Parameters:

  • skus (Array)

Returns:

  • (Integer)


33
34
35
36
# File 'app/models/workarea/inventory.rb', line 33

def self.total_sales(*skus)
  arr = Array(skus).flatten
  Inventory::Collection.new(arr).sum(&:purchased)
end