Skip to content

Instantly share code, notes, and snippets.

@mohammadali66
Last active September 17, 2022 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohammadali66/b275517b62fe41aa0c413ce251578998 to your computer and use it in GitHub Desktop.
Save mohammadali66/b275517b62fe41aa0c413ce251578998 to your computer and use it in GitHub Desktop.
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