@@ -18,39 +18,39 @@ def skus(thing):
1818 return thing .keys ()
1919
2020
21- def allocate_line (order , sku , quantity , source ):
21+ def allocate_line (sku , quantity , source , allocations ):
2222 if source .get (sku , 0 ) > quantity :
2323 source [sku ] -= quantity
24- order . allocations [sku ] = getattr (source , 'id' , 'STOCK' )
24+ allocations [sku ] = getattr (source , 'id' , 'STOCK' )
2525
2626def allocate_to (order , source ):
27+ allocations = {}
2728 for sku , quantity in order .items ():
28- allocate_line (order , sku , quantity , source )
29+ allocate_line (sku , quantity , source , allocations )
30+ return allocations
2931
3032
31- def allocate_to_shipments (order , shipments ):
33+ def allocate_to_shipments (order , shipments , allocations ):
3234 for sku , quantity in order .items ():
33- if sku in order . allocations :
35+ if sku in allocations :
3436 continue
3537 for shipment in shipments :
36- allocate_line (order , sku , quantity , shipment )
38+ allocate_line (sku , quantity , shipment , order . allocations )
3739 if sku in order .allocations :
3840 break
3941
4042
4143
4244def allocate (order , stock , shipments ):
4345 if skus (order ) <= skus (stock ):
44- allocate_to (order , stock )
45- return order .allocations
46+ return allocate_to (order , stock )
4647
4748 shipments .sort (key = lambda s : s .eta )
4849
4950 for shipment in shipments :
5051 if skus (order ) <= skus (shipment ):
51- allocate_to (order , shipment )
52- return order .allocations
52+ return allocate_to (order , shipment )
5353
54- allocate_to (order , stock )
55- allocate_to_shipments (order , shipments )
54+ order . allocations = allocate_to (order , stock )
55+ allocate_to_shipments (order , shipments , order . allocations )
5656 return order .allocations
0 commit comments