Skip to content

Commit ec48906

Browse files
committed
new reqt re allocating to all if poss
1 parent 86459ce commit ec48906

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

domain_model.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class Shipment:
1919
lines: list
2020

2121

22+
def skus(thing):
23+
try:
24+
return {line.sku for line in thing}
25+
except TypeError:
26+
return {line.sku for line in thing.lines}
27+
28+
29+
2230
def allocate_to_stock(line, stock):
2331
for stock_line in stock:
2432
if stock_line.sku == line.sku:
@@ -31,8 +39,18 @@ def allocate_to_shipments(line, shipments):
3139
line.allocation = shipment.id
3240

3341
def allocate(order, stock, shipments):
42+
if skus(order) <= skus(stock):
43+
for line in order:
44+
line.allocation = 'STOCK'
45+
return
46+
47+
for shipment in shipments:
48+
if skus(order) <= skus(shipment):
49+
for line in order:
50+
line.allocation = shipment.id
51+
return
52+
3453
for line in order:
3554
allocate_to_stock(line, stock)
3655
if line.allocation is None:
3756
allocate_to_shipments(line, shipments)
38-

test_allocation.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,21 @@ def test_can_allocate_to_both_preferring_stock():
154154
assert order[2].allocation == 'STOCK'
155155
assert order[3].allocation == 'STOCK'
156156

157+
158+
def test_mixed_allocations_are_avoided_if_possible():
159+
sku1, sku2 = random_id(), random_id()
160+
order = [
161+
OrderLine(sku=sku1, quantity=10),
162+
OrderLine(sku=sku2, quantity=10),
163+
]
164+
shipment = Shipment(id=random_id(), eta=date.today(), lines=[
165+
Line(sku=sku1, quantity=1000),
166+
Line(sku=sku2, quantity=1000),
167+
])
168+
stock = [Line(sku=sku1, quantity=1000)]
169+
170+
allocate(order, stock, shipments=[shipment])
171+
172+
assert order[0].allocation == shipment.id
173+
assert order[1].allocation == shipment.id
174+

0 commit comments

Comments
 (0)