Skip to content

Commit b2d618a

Browse files
committed
model now tracks allocations [domain_model_complete]
1 parent bebd0fd commit b2d618a

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

model.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22
from dataclasses import dataclass
33
from datetime import date
4-
from typing import Optional
4+
from typing import Optional, Set
55

66

77
@dataclass(frozen=True)
@@ -16,13 +16,24 @@ def __init__(self, ref: str, sku: str, qty: int, eta: Optional[date]):
1616
self.reference = ref
1717
self.sku = sku
1818
self.eta = eta
19-
self.available_quantity = qty
19+
self._purchased_quantity = qty
20+
self._allocations = set() # type: Set[OrderLine]
2021

2122
def allocate(self, line: OrderLine):
22-
self.available_quantity -= line.qty
23+
if self.can_allocate(line):
24+
self._allocations.add(line)
2325

2426
def deallocate(self, line: OrderLine):
25-
self.available_quantity += line.qty
27+
if line in self._allocations:
28+
self._allocations.remove(line)
29+
30+
@property
31+
def allocated_quantity(self) -> int:
32+
return sum(line.qty for line in self._allocations)
33+
34+
@property
35+
def available_quantity(self) -> int:
36+
return self._purchased_quantity - self.allocated_quantity
2637

2738
def can_allocate(self, line: OrderLine) -> bool:
2839
return self.sku == line.sku and self.available_quantity >= line.qty

0 commit comments

Comments
 (0)