forked from cosmicpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
101 lines (81 loc) · 2.98 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from __future__ import annotations
from dataclasses import dataclass
from datetime import date
from typing import Optional, Set
from . import commands, events
@dataclass(unsafe_hash=True)
class OrderLine:
orderid: str
sku: str
qty: int
class Batch:
def __init__(self, ref: str, sku: str, qty: int, eta: Optional[date]):
self.reference = ref
self.sku = sku
self.eta = eta
self.purchased_quantity = qty
self.allocations = set() # type: Set[OrderLine]
def __repr__(self):
return f"<Batch {self.reference}>"
def __eq__(self, other):
if not isinstance(other, Batch):
return False
return other.reference == self.reference
def __hash__(self):
return hash(self.reference)
def __gt__(self, other):
if self.eta is None:
return False
if other.eta is None:
return True
return self.eta > other.eta
def allocate(self, line: OrderLine):
if self.can_allocate(line):
self.allocations.add(line)
def deallocate_one(self, line: OrderLine | None = None):
if line and line in self.allocations:
self.allocations.remove(line)
return None
return self.allocations.pop()
@property
def allocated_quantity(self) -> int:
return sum(line.qty for line in self.allocations)
@property
def available_quantity(self) -> int:
return self.purchased_quantity - self.allocated_quantity
def can_allocate(self, line: OrderLine) -> bool:
return self.sku == line.sku and self.available_quantity >= line.qty
class Product:
def __init__(self, sku: str, batches: list[Batch], version_number: int = 0):
self.sku = sku
self.batches = batches
self.version_number = version_number
self.events: list[events.Event | commands.Command] = []
def allocate(self, line: OrderLine) -> str | None:
try:
batches = sorted(self.batches)
batch = next(b for b in batches if b.can_allocate(line))
batch.allocate(line)
self.version_number += 1
self.events.append(
events.Allocated(
orderid=line.orderid,
sku=line.sku,
qty=line.qty,
batchref=batch.reference,
)
)
return batch.reference
except StopIteration:
self.events.append(events.OutOfStock(line.sku))
return None
def add_batch(self, batch: Batch) -> str:
self.version_number += 1
self.batches.append(batch)
return batch.reference
def change_batch_quantity(self, ref: str, qty: int):
batch = next(b for b in self.batches if b.reference == ref)
batch.purchased_quantity = qty
while batch.available_quantity < 0:
line = batch.deallocate_one()
self.events.append(events.Deallocated(line.orderid, line.sku, line.qty))