1
1
from dataclasses import dataclass
2
+ from datetime import date
3
+
2
4
3
5
def allocate (order , warehouse , shipments ):
4
6
ordered_sources = [warehouse ] + sorted (shipments )
5
- allocation = Allocation (order , {} )
7
+ allocation = Allocation (order )
6
8
for source in ordered_sources :
7
9
allocation .supplement_with (source .allocation_for (order ))
8
10
allocation .decrement_available_quantities ()
@@ -13,23 +15,23 @@ def allocate(order, warehouse, shipments):
13
15
14
16
class Allocation :
15
17
16
- def __init__ (self , order , sources ):
18
+ def __init__ (self , order ):
17
19
self .order = order
18
- self .sources = sources
20
+ self ._sources = {}
19
21
20
22
def __getitem__ (self , sku ):
21
- return self .sources [sku ]
23
+ return self ._sources [sku ]
22
24
23
25
def __contains__ (self , sku ):
24
- return sku in self .sources
26
+ return sku in self ._sources
25
27
26
28
def supplement_with (self , other ):
27
- for sku , qty in other .sources .items ():
29
+ for sku , qty in other ._sources .items ():
28
30
if sku not in self :
29
- self .sources [sku ] = qty
31
+ self ._sources [sku ] = qty
30
32
31
33
def decrement_available_quantities (self ):
32
- for sku , source in self .sources .items ():
34
+ for sku , source in self ._sources .items ():
33
35
source .decrement_available (sku , self .order [sku ])
34
36
35
37
@@ -41,20 +43,20 @@ class Line:
41
43
42
44
class _SkuLines :
43
45
44
- def __init__ (self , linesdict ):
45
- self .linesdict = linesdict
46
+ def __init__ (self , lines : dict ):
47
+ self ._lines = lines
46
48
47
49
def __getitem__ (self , sku ):
48
- return self .linesdict [sku ]
50
+ return self ._lines [sku ]
49
51
50
52
def __contains__ (self , sku ):
51
- return sku in self .linesdict
53
+ return sku in self ._lines
52
54
53
55
@property
54
56
def lines (self ):
55
57
return [
56
58
Line (sku , qty )
57
- for sku , qty in self .linesdict .items ()
59
+ for sku , qty in self ._lines .items ()
58
60
]
59
61
60
62
@@ -65,15 +67,17 @@ class Order(_SkuLines):
65
67
class _Stock (_SkuLines ):
66
68
67
69
def decrement_available (self , sku , qty ):
68
- self .linesdict [sku ] -= qty
70
+ self ._lines [sku ] -= qty
69
71
70
- def allocation_for (self , order ):
71
- return Allocation (order , {
72
+ def allocation_for (self , order : Order ):
73
+ allocation = Allocation (order )
74
+ allocation ._sources = { # TODO: this ain't right
72
75
line .sku : self
73
76
for line in order .lines
74
77
if line .sku in self
75
78
and self [line .sku ] > line .qty
76
- })
79
+ }
80
+ return allocation
77
81
78
82
79
83
class Warehouse (_Stock ):
@@ -87,9 +91,9 @@ class Shipment(_Stock):
87
91
def __repr__ (self ):
88
92
return f'<Shipment { super ().__repr__ ()} >'
89
93
90
- def __init__ (self , d , eta ):
94
+ def __init__ (self , lines : dict , eta : date ):
91
95
self .eta = eta
92
- super ().__init__ (d )
96
+ super ().__init__ (lines )
93
97
94
98
def __lt__ (self , other ):
95
99
return self .eta < other .eta
0 commit comments