Handle backorder cancellations

This commit is contained in:
Maikel Linke
2024-09-13 13:50:53 +10:00
parent 283db8f9d0
commit 5ef85aef3e
4 changed files with 568 additions and 11 deletions

View File

@@ -34,16 +34,23 @@ class CompleteBackorderJob < ApplicationJob
broker = FdcOfferBroker.new(BackorderJob.load_catalog(user))
order.lines.each do |line|
line.quantity = line.quantity.to_i
wholesale_product_id = line.offer.offeredItem.semanticId
transformation = broker.wholesale_to_retail(wholesale_product_id)
linked_variant = variants.linked_to(transformation.retail_product_id)
# Note that a division of integers dismisses the remainder, like `floor`:
wholesale_items_contained_in_stock = linked_variant.on_hand / transformation.factor
line.quantity = line.quantity.to_i - wholesale_items_contained_in_stock
retail_stock_changes = wholesale_items_contained_in_stock * transformation.factor
# But maybe we didn't actually order that much:
deductable_quantity = [line.quantity, wholesale_items_contained_in_stock].min
line.quantity -= deductable_quantity
retail_stock_changes = deductable_quantity * transformation.factor
linked_variant.on_hand -= retail_stock_changes
end
# Clean up empty lines:
order.lines.reject! { |line| line.quantity.zero? }
end
end

View File

@@ -56,9 +56,7 @@ class FdcBackorderer
end
def find_order(semantic_id)
import(semantic_id).find do |o|
o.semanticType == "dfc-b:Order"
end
find_subject(import(semantic_id), "dfc-b:Order")
end
def find_or_build_order_line(order, offer)
@@ -88,6 +86,14 @@ class FdcBackorderer
end
end
def find_subject(object_or_graph, type)
if object_or_graph.is_a?(Array)
object_or_graph.find { |i| i.semanticType == type }
else
object_or_graph
end
end
def import(url)
api = DfcRequest.new(user)
json = api.call(url)
@@ -110,7 +116,7 @@ class FdcBackorderer
end
result = api.call(backorder.semanticId, json, method:)
DfcIo.import(result).find { |i| i.semanticType == "dfc-b:Order" }
find_subject(DfcIo.import(result), "dfc-b:Order")
end
def complete_order(backorder)

File diff suppressed because one or more lines are too long

View File

@@ -32,11 +32,6 @@ RSpec.describe CompleteBackorderJob do
variant.semantic_links << SemanticLink.new(
semantic_id: retail_product.semanticId
)
# We are assuming 12 cans in a slab.
# We got more stock than we need.
variant.on_hand = 13
ofn_order.order_cycle = create(
:simple_order_cycle,
distributors: [distributor],
@@ -45,6 +40,10 @@ RSpec.describe CompleteBackorderJob do
end
it "completes an order", vcr: true do
# We are assuming 12 cans in a slab.
# We got more stock than we need.
variant.on_hand = 13
current_order = order
expect {
@@ -60,5 +59,28 @@ RSpec.describe CompleteBackorderJob do
variant.on_hand
}.from(13).to(1)
end
it "removes line items", vcr: true do
# We are assuming 12 cans in a slab.
# We backordered 3 slabs, which is 36 cans.
# And now we would have more than 4 slabs (4*12 + 1 = 49)
# We got more stock than we need.
variant.on_hand = 49
current_order = order
expect {
subject.perform(user, distributor, order_cycle, order.semanticId)
current_order = orderer.find_order(order.semanticId)
}.to change {
current_order.orderStatus[:path]
}.from("Held").to("Complete")
.and change {
current_order.lines.count
}.from(1).to(0)
.and change {
variant.on_hand
}.from(49).to(13) # minus 3 backordered slabs (3 * 12 = 36)
end
end
end