@@ -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
3035def 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
3542def 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
4149def 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 :
0 commit comments