Last active
September 17, 2022 19:39
-
-
Save mohammadali66/b275517b62fe41aa0c413ce251578998 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from typing import Optional | |
from datetime import date | |
@dataclass(frozen=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 allocate(self, line: OrderLine): | |
if self.can_allocate(line): | |
self._allocations.add(line) | |
def deallocate(self, line: OrderLine): | |
if line in self._allocations: | |
self._allocations.remove(line) | |
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment