Skip to content

Commit 14e27e5

Browse files
committed
allocation checks basic quantity
1 parent 2a583b1 commit 14e27e5

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

domain_model.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,36 @@ def skus(thing):
2626
return {line.sku for line in thing.lines}
2727

2828

29+
def allocate_to(line, allocation, quantities):
30+
for quantity in quantities:
31+
if quantity.sku == line.sku and quantity.quantity > line.quantity:
32+
line.allocation = allocation
33+
return
2934

3035
def allocate_to_stock(line, stock):
31-
for stock_line in stock:
32-
if stock_line.sku == line.sku:
33-
line.allocation = 'STOCK'
36+
allocate_to(line, 'STOCK', stock)
37+
38+
def allocate_to_shipment(line, shipment):
39+
allocate_to(line, shipment.id, shipment.lines)
40+
3441

3542
def allocate_to_shipments(line, shipments):
3643
for shipment in shipments:
37-
for shipment_line in shipment.lines:
38-
if shipment_line.sku == line.sku:
39-
line.allocation = shipment.id
44+
allocate_to_shipment(line, shipment)
45+
if line.allocation is not None:
46+
return
47+
4048

4149
def allocate(order, stock, shipments):
4250
if skus(order) <= skus(stock):
4351
for line in order:
44-
line.allocation = 'STOCK'
52+
allocate_to_stock(line, stock)
4553
return
4654
shipments.sort(key=lambda s: s.eta)
4755
for shipment in shipments:
4856
if skus(order) <= skus(shipment):
4957
for line in order:
50-
line.allocation = shipment.id
58+
allocate_to(line, shipment.id, shipment.lines)
5159
return
5260

5361
for line in order:

test_allocation.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,23 @@ def test_prefer_allocating_to_earlier_even_if_multiple_shipments():
212212
assert order[1].allocation == shipment2.id
213213
assert order[2].allocation == shipment2.id
214214

215+
216+
def test_cannot_allocate_if_insufficent_quantity_in_stock():
217+
order = [OrderLine(sku='a-sku', quantity=10)]
218+
stock = [Line(sku='a-sku', quantity=5)]
219+
220+
allocate(order, stock, shipments=[])
221+
222+
assert order[0].allocation is None
223+
224+
225+
def test_cannot_allocate_if_insufficent_quantity_in_shipment():
226+
order = [OrderLine(sku='a-sku', quantity=10)]
227+
shipment = Shipment(id='shipment-id', eta=date.today(), lines=[
228+
Line(sku='a-sku', quantity=5),
229+
])
230+
231+
allocate(order, stock=[], shipments=[shipment])
232+
233+
assert order[0].allocation is None
234+

0 commit comments

Comments
 (0)