Class: Workarea::Inventory::Sku

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

Defined Under Namespace

Classes: InvalidPolicy

Instance Attribute Summary collapse

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

Instance Attribute Details

#sellableInteger

Experimental field to persist #available_to_sell. Currently only used for reporting on low inventory. It is not recommended at this time to use sellable for any critical business logic and is subject to removal.

Returns:

  • (Integer)

    The inventory that can be sold, based on policy.



26
# File 'app/models/workarea/inventory/sku.rb', line 26

field :sellable, type: Integer, default: 0

Class Method Details

.sortsObject



48
49
50
# File 'app/models/workarea/inventory/sku.rb', line 48

def self.sorts
  [Sort.modified, Sort.newest, Sort.available, Sort.sku, Sort.purchased]
end

Instance Method Details

#backordered?Boolean

Determines if a sku is available from backorder

Returns:

  • (Boolean)

    Sku available via backorder



64
65
66
# File 'app/models/workarea/inventory/sku.rb', line 64

def backordered?
  available == 0 && allow_backorder? && backordered > 0
end

#capture(desired_available, desired_backordered = 0) ⇒ Hash

Atomically decrement units for this SKU. Used when purchasing from a Policies::Base class. Will raise an InsufficientError if there is not enough inventory on the SKU to capture.

If there is a failure, this means the in memory Workarea::Inventory::Sku model is out of sync with the database, so this method will call #reload (but not try to capture again, since different arguments will be required).

The returned value is a Hash with keys for success, number of available captured and number of backordered captured.

Parameters:

  • desired_available (Integer)
  • desired_backordered (Integer) (defaults to: 0)

Returns:

  • (Hash)


108
109
110
111
112
113
114
# File 'app/models/workarea/inventory/sku.rb', line 108

def capture(desired_available, desired_backordered = 0)
  capture = Capture.new(self, desired_available, desired_backordered)
  capture.perform

  reload unless capture.result[:success]
  capture.result
end

#insufficiency_for(quantity) ⇒ Integer

Returns the difference betweent the requested quantity and the available quantity. Used for determining how to auto-limit the quantity you can have in a cart.

Parameters:

  • quantity (Integer)

Returns:

  • (Integer)


84
85
86
87
88
89
90
# File 'app/models/workarea/inventory/sku.rb', line 84

def insufficiency_for(quantity)
  if available_to_sell >= quantity
    0
  else
    quantity - available_to_sell
  end
end

#nameString

This is for compatibility with the admin, all models must implement this

Returns:



56
57
58
# File 'app/models/workarea/inventory/sku.rb', line 56

def name
  I18n.t('workarea.inventory_sku.name', id: id)
end

#purchasable?(quantity = 1) ⇒ Boolean

Whether the sku is purchasable based on policy and stock

Parameters:

  • quantity (Integer) (defaults to: 1)

Returns:

  • (Boolean)


73
74
75
# File 'app/models/workarea/inventory/sku.rb', line 73

def purchasable?(quantity = 1)
  quantity <= available_to_sell
end

#release(release_available, release_backordered = 0) ⇒ Boolean

Atomically free inventory units that were previously captured in a purchase. Used to rollback an inventory capture that succeeded after one fails in a transaction. Also reduces the purchase count to reflect the rollback.

Parameters:

  • release_available (Integer)
  • release_backordered (Integer) (defaults to: 0)

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
# File 'app/models/workarea/inventory/sku.rb', line 126

def release(release_available, release_backordered = 0)
  total = release_available + release_backordered
  return true if total.zero?

  inc(
    available: release_available,
    backordered: release_backordered,
    sellable: total,
    purchased: 0 - total
  )
end