22from typing import List
33from datetime import date
44
5-
65@dataclass
76class OrderLine :
87 sku : str
98 quantity : int
109
1110
11+
1212@dataclass
13- class Order :
13+ class SkuLines :
1414 lines : List [OrderLine ]
1515
1616 @property
@@ -21,6 +21,12 @@ def skus(self):
2121 def quantities (self ):
2222 return {l .sku : l .quantity for l in self .lines }
2323
24+
25+
26+
27+ @dataclass
28+ class Order (SkuLines ):
29+
2430 @property
2531 def fully_allocated (self ):
2632 return self .allocation .is_complete
@@ -37,21 +43,9 @@ def allocate(self, stock, shipments):
3743 self .allocation .apply ()
3844
3945
40- class StockLine (OrderLine ):
41- pass
42-
4346
4447@dataclass
45- class Stock :
46- lines : List [StockLine ]
47-
48- @property
49- def skus (self ):
50- return set (l .sku for l in self .lines )
51-
52- @property
53- def quantities (self ):
54- return {l .sku : l .quantity for l in self .lines }
48+ class Stock (SkuLines ):
5549
5650 def can_allocate (self , line : OrderLine ):
5751 return line .sku in self .skus and self .quantities [line .sku ] > line .quantity
@@ -60,6 +54,8 @@ def allocate(self, sku, quantity):
6054 for line in self .lines :
6155 if line .sku == sku :
6256 line .quantity -= quantity
57+ return
58+ raise Exception (f'sku { sku } not found in stock skus ({ self .skus } )' )
6359
6460
6561@dataclass
@@ -90,7 +86,7 @@ def sources(self):
9086 return {l .sku : l .source for l in self .lines }
9187
9288 @staticmethod
93- def for_ (order , source ):
89+ def for_ (order : Order , source : Stock ):
9490 return Allocation (
9591 lines = [
9692 AllocationLine (sku = line .sku , source = source ) for line in order .lines
@@ -113,4 +109,3 @@ def apply(self):
113109 for line in self .lines :
114110 line .source .allocate (line .sku , self .order .quantities [line .sku ])
115111
116-
0 commit comments