Last active
November 9, 2021 18:00
-
-
Save voglster/43cf15739b36bd937927fb6ab852acfb 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 typing import List | |
from bol.model import Bol | |
from order.enums import MovementValidationType | |
from order.modelv2 import OrderV2 | |
def get_extra_supply(order: OrderV2, bols: List[Bol]): | |
# union all supply from bols | |
bol_supply = set(supply for bol in bols for supply in bol.supply()) | |
order_supply = order.supply() | |
return order_supply - bol_supply, bol_supply - order_supply | |
def update_bol_order_validation( | |
order: OrderV2, | |
extra_supply, | |
mv_type: MovementValidationType, | |
found_on: str, | |
missing_from: str, | |
): | |
if extra_supply: | |
word = "is" if len(extra_supply) == 1 else "are" | |
text = f"{extra_supply} {word} found on the {found_on} but not in the {missing_from}" | |
order.upsert_validation_issue(mv_type, text) | |
else: | |
order.clear_validation_issue(mv_type) | |
def update_order_supply_validation(order: OrderV2, extra_supply): | |
update_bol_order_validation( | |
order, extra_supply, MovementValidationType.order_supply, "order", "bols" | |
) | |
def update_bol_supply_validation(order: OrderV2, extra_supply): | |
update_bol_order_validation( | |
order, extra_supply, MovementValidationType.bol_supply, "bols", "order" | |
) | |
def update_total_volume_validation( | |
order: OrderV2, bols: List[Bol], allowable_variance=None | |
): | |
order_total_volume = order.total_gallons | |
bol_total_volume = sum(x.total_volume for x in bols) | |
allowable_variance = allowable_variance or 500 | |
# TODO: replace the 500 with a config option | |
variance = abs(order_total_volume - bol_total_volume) | |
if variance > allowable_variance: | |
text = ( | |
f"High variance ({variance}) between " | |
f"order volume ({order_total_volume}) " | |
f"and bol volume ({bol_total_volume})" | |
) | |
order.upsert_validation_issue(MovementValidationType.total_volume, text) | |
else: | |
order.clear_validation_issue(MovementValidationType.total_volume) | |
def update_order_validation(order: OrderV2, bols: List[Bol]): | |
order_extra, bol_extra = get_extra_supply(order, bols) | |
update_order_supply_validation(order, order_extra) | |
update_bol_supply_validation(order, bol_extra) | |
update_total_volume_validation(order, bols, allowable_variance=500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment