Class: Workarea::Inventory::Sku
- Inherits:
-
Object
- Object
- Workarea::Inventory::Sku
- Includes:
- ApplicationDocument
- Defined in:
- app/models/workarea/inventory/sku.rb
Defined Under Namespace
Classes: InvalidPolicy
Instance Attribute Summary collapse
-
#sellable ⇒ Integer
Experimental field to persist #available_to_sell.
Class Method Summary collapse
Instance Method Summary collapse
-
#backordered? ⇒ Boolean
Determines if a sku is available from backorder.
-
#capture(desired_available, desired_backordered = 0) ⇒ Hash
Atomically decrement units for this SKU.
-
#insufficiency_for(quantity) ⇒ Integer
Returns the difference betweent the requested quantity and the available quantity.
-
#name ⇒ String
This is for compatibility with the admin, all models must implement this.
-
#purchasable?(quantity = 1) ⇒ Boolean
Whether the sku is purchasable based on policy and stock.
-
#release(release_available, release_backordered = 0) ⇒ Boolean
Atomically free inventory units that were previously captured in a purchase.
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
assert_valid_config!, async, disable, enable, inline, #run_callbacks
Methods included from Mongoid::Document
Instance Attribute Details
#sellable ⇒ Integer
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.
26 |
# File 'app/models/workarea/inventory/sku.rb', line 26 field :sellable, type: Integer, default: 0 |
Class Method Details
Instance Method Details
#backordered? ⇒ Boolean
Determines if a sku is available from 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.
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.
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 |
#name ⇒ String
This is for compatibility with the admin, all models must implement this
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
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.
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 |