@@ -8,37 +8,30 @@ def skus(d):
8
8
return d .keys ()
9
9
10
10
11
- def allocate_line (sku , quantity , source , allocations ):
12
- if source .get (sku , 0 ) > quantity :
13
- allocations [sku ] = source
14
-
15
-
16
11
def allocate_to (order , source ):
17
12
allocations = {}
18
13
for sku , quantity in order .items ():
19
- allocate_line (sku , quantity , source , allocations )
14
+ if source .get (sku , 0 ) > quantity :
15
+ allocations [sku ] = source
20
16
return allocations
21
17
22
18
23
- def allocate_to_sources (order , sources ):
24
- allocations = {}
25
- for sku , quantity in order .items ():
26
- for source in sources :
27
- allocate_line (sku , quantity , source , allocations )
28
- if sku in allocations :
29
- break
30
- return allocations
31
-
32
-
33
-
34
19
def allocate (order , stock , shipments ):
35
- if skus (order ) <= skus (stock ):
36
- return allocate_to (order , stock )
20
+ stock_allocation = allocate_to (order , stock )
21
+ if set (stock_allocation ) == set (order ):
22
+ return stock_allocation
37
23
38
24
shipments .sort (key = lambda s : s .eta )
39
25
26
+ shipment_allocations = []
40
27
for shipment in shipments :
41
- if skus (order ) <= skus (shipment ):
42
- return allocate_to (order , shipment )
28
+ shipment_allocation = allocate_to (order , shipment )
29
+ if set (shipment_allocation ) == set (order ):
30
+ return shipment_allocation
31
+ shipment_allocations .append (shipment_allocation )
32
+
33
+ mixed_allocation = {}
34
+ for allocation in shipment_allocations + [stock_allocation ]:
35
+ mixed_allocation .update (allocation )
36
+ return mixed_allocation
43
37
44
- return allocate_to_sources (order , [stock ] + shipments )
0 commit comments