Skip to content

Commit 29ccd64

Browse files
committed
move main allocation algorithm onto method on Allocation
1 parent 9b85bf3 commit 29ccd64

1 file changed

Lines changed: 20 additions & 21 deletions

File tree

‎domain_model.py‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import List
33
from datetime import date
44

5+
56
@dataclass
67
class OrderLine:
78
sku: str
@@ -27,23 +28,11 @@ def quantities(self):
2728
@dataclass
2829
class Order(SkuLines):
2930

30-
@property
31-
def fully_allocated(self):
32-
return self.allocation.is_complete
33-
3431
def allocate(self, stock, shipments):
35-
self.allocation = Allocation(lines=[], order=self)
36-
for source in [stock] + sorted(shipments):
37-
source_allocation = Allocation.for_(self, source)
38-
if source_allocation.is_complete:
39-
self.allocation = source_allocation
40-
self.allocation.apply()
41-
return
42-
self.allocation.supplement_with(source_allocation)
32+
self.allocation = Allocation.for_order(self, stock, shipments)
4333
self.allocation.apply()
4434

4535

46-
4736
@dataclass
4837
class Stock(SkuLines):
4938

@@ -87,14 +76,23 @@ def sources(self):
8776
return {l.sku: l.source for l in self.lines}
8877

8978
@staticmethod
90-
def for_(order: Order, source: Stock):
91-
return Allocation(
92-
lines=[
93-
AllocationLine(sku=line.sku, quantity=line.quantity, source=source) for line in order.lines
94-
if source.can_allocate(line)
95-
],
96-
order=order
97-
)
79+
def for_source(order: Order, source: Stock):
80+
return Allocation(lines=[
81+
AllocationLine(sku=line.sku, quantity=line.quantity, source=source)
82+
for line in order.lines
83+
if source.can_allocate(line)
84+
], order=order)
85+
86+
@staticmethod
87+
def for_order(order: Order, stock: Stock, shipments: List[Shipment]):
88+
split_allocation = Allocation(lines=[], order=order)
89+
for source in [stock] + sorted(shipments):
90+
source_allocation = Allocation.for_source(order, source)
91+
if source_allocation.is_complete:
92+
return source_allocation
93+
split_allocation.supplement_with(source_allocation)
94+
return split_allocation
95+
9896

9997
def supplement_with(self, other_allocation):
10098
self.lines.extend(
@@ -109,3 +107,4 @@ def apply(self):
109107
for line in self.lines:
110108
line.source.allocate(line.sku, line.quantity)
111109

110+

0 commit comments

Comments
 (0)