Remember local stock of backordered products

When retail variants are mapped to wholesale variants, we usually have a
some leftover stock at the end of an order cycle. For example, we
backordered a slab of 12 cans of tomatoes but our customers bought only
9 of those. Then we have 3 left for the next order cycle.

Even when the product is not available for backorder with the supplier,
we still want to sell off our leftover stock, the three cans of tomatoes
in our example.

And it might be that the product will come back in the future.
This commit is contained in:
Maikel Linke
2025-03-20 16:25:15 +11:00
parent 2a81d26ef1
commit e35a29cc29
2 changed files with 14 additions and 5 deletions

View File

@@ -88,14 +88,23 @@ module Admin
# Reset stock for any variants that were removed from the catalog.
#
# When variants are removed from the remote catalog, there not for sale
# anymore. We prevent them from being sold by reseting stock to zero.
# When variants are removed from the remote catalog, we can't place
# backorders for them anymore. If our copy of the product has limited
# stock then we need to set the stock to zero to prevent any more sales.
#
# But if our product is on-demand/backorderable then our stock level is
# a representation of remaining local stock. We then need to limit sales
# to this local stock and set on-demand to false.
#
# We don't delete the variant because it may come back at a later time and
# we don't want to lose the connection to previous orders.
def reset_absent_variants(catalog)
absent_variants(catalog).map do |variant|
variant.on_demand = false
variant.on_hand = 0
if variant.on_demand
variant.on_demand = false
else
variant.on_hand = 0
end
end
end

View File

@@ -106,7 +106,7 @@ RSpec.describe "DFC Product Import" do
.and change { linked_variant.on_demand }.to(true)
.and change { linked_variant.on_hand }.by(0)
.and change { unlinked_variant.on_demand }.to(false)
.and change { unlinked_variant.on_hand }.to(0)
.and change { unlinked_variant.on_hand }.by(0)
product = Spree::Product.last
expect(product.variants[0].semantic_links).to be_present