Skip to content

Instantly share code, notes, and snippets.

@winzard
Last active June 7, 2022 12:11
Show Gist options
  • Save winzard/299f77d171d443c4c9c201b94060c42a to your computer and use it in GitHub Desktop.
Save winzard/299f77d171d443c4c9c201b94060c42a to your computer and use it in GitHub Desktop.
@classmethod
def get_quantity_by_catalog_item(cls, catalog_item_id: str) -> dict:
event_repo = cls.event_repo()
query = {'catalog_item': catalog_item_id}
events = event_repo.find(query)
pivot = {}
for event in events:
company = event.internal_agent
storage = event.location
delta = event.quantity*event.direction
if company not in pivot:
pivot[company] = {}
if storage not in pivot[company]:
pivot[company][storage] = {'balance': Decimal(0), 'reserved': Decimal(0)}
pivot[company][storage]['balance'] += delta
reserved_commitments = CommitmentRepository().find(
{
'reservation': True,
'catalog_item_id': catalog_item_id
}
)
for commitment in reserved_commitments:
company = commitment.internal_agent
storage = commitment.location
delta = commitment.get_unfulfilled_quantity()
if company not in pivot:
pivot[company] = {}
if storage not in pivot[company]:
pivot[company][storage] = {'balance': Decimal(0), 'reserved': Decimal(0)}
pivot[company][storage]['reserved'] += delta
return pivot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment