1
- def skus (d ):
2
- return set (d .keys ())
3
-
4
- def allocated_completely (order , allocation ):
5
- return skus (order ) == skus (allocation )
6
-
1
+ def allocate (order , stock , shipments ):
2
+ allocations = []
3
+ for source in [stock ] + shipments :
4
+ allocation = allocate_to (order , source )
5
+ if allocated_completely (order , allocation ):
6
+ return allocation
7
+ allocations .append (allocation )
8
+ return combine_preferring_first (allocations )
7
9
8
10
def allocate_to (order , source ):
9
11
return {
@@ -13,23 +15,12 @@ def allocate_to(order, source):
13
15
and source [sku ] > quantity
14
16
}
15
17
18
+ def allocated_completely (order , allocation ):
19
+ return order .keys () == allocation .keys ()
16
20
17
- def merge (allocations ):
21
+ def combine_preferring_first (allocations ):
18
22
return {
19
23
k : v
20
- for d in allocations
24
+ for d in reversed ( allocations )
21
25
for k , v in d .items ()
22
26
}
23
-
24
-
25
-
26
- def allocate (order , stock , shipments ):
27
- allocations = []
28
- for source in [stock ] + shipments :
29
- allocation = allocate_to (order , source )
30
- if allocated_completely (order , allocation ):
31
- return allocation
32
- allocations .append (allocation )
33
-
34
- return merge (reversed (allocations ))
35
-
0 commit comments