Skip to content

Instantly share code, notes, and snippets.

@winzard
Last active June 7, 2022 12:11
Show Gist options
  • Select an option

  • Save winzard/299f77d171d443c4c9c201b94060c42a to your computer and use it in GitHub Desktop.

Select an option

Save winzard/299f77d171d443c4c9c201b94060c42a to your computer and use it in GitHub Desktop.

Revisions

  1. winzard revised this gist Jun 7, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    @classmethod
    @classmethod
    def get_quantity_by_catalog_item(cls, catalog_item_id: str) -> dict:
    event_repo = cls.event_repo()
    query = {'catalog_item': catalog_item_id}
  2. winzard renamed this gist Jun 7, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. winzard created this gist Jun 7, 2022.
    32 changes: 32 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    @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