Fix: Recipe recursion and updating of product price on updating recipe

This commit is contained in:
Amritanshu Agrawal 2021-11-15 21:46:29 +05:30
parent b0de2d5ae4
commit 8e9babd1dc
2 changed files with 14 additions and 14 deletions

View File

@ -30,7 +30,9 @@ def set_maintenance(
user: UserToken = Security(get_user, scopes=["maintenance"]), user: UserToken = Security(get_user, scopes=["maintenance"]),
) -> Maintenance: ) -> Maintenance:
with SessionFuture() as db: with SessionFuture() as db:
maintenance: Optional[DbSetting] = db.execute(select(DbSetting).where(DbSetting.name == "Maintenance")).scalar_one_or_none() maintenance: Optional[DbSetting] = db.execute(
select(DbSetting).where(DbSetting.name == "Maintenance")
).scalar_one_or_none()
if data.enabled is False and maintenance is not None: if data.enabled is False and maintenance is not None:
db.delete(maintenance) db.delete(maintenance)
maintenance = None maintenance = None

View File

@ -99,8 +99,7 @@ def save(
r_item.recipe_id = recipe.id r_item.recipe_id = recipe.id
db.add(r_item) db.add(r_item)
seen_recipes: Set[uuid.UUID] = set() check_recursion(recipe_sku.product_id, recipe, db)
check_recursion(recipe, seen_recipes, db)
db.commit() db.commit()
set_period(data.valid_from, data.valid_till, request.session) set_period(data.valid_from, data.valid_till, request.session)
return recipe_info(recipe) return recipe_info(recipe)
@ -189,8 +188,8 @@ async def update_route(
recipe.items.append(RecipeItem(None, product.id, quantity, ingredient_cost)) recipe.items.append(RecipeItem(None, product.id, quantity, ingredient_cost))
recipe.cost_price = round(recipe_cost / recipe.recipe_yield, 2) recipe.cost_price = round(recipe_cost / recipe.recipe_yield, 2)
seen_recipes: Set[uuid.UUID] = set() sku.cost_price = round(recipe.cost_price / recipe.recipe_yield, 2)
check_recursion(recipe, seen_recipes, db) check_recursion(sku.product_id, recipe, db)
db.commit() db.commit()
set_period(data.valid_from, data.valid_till, request.session) set_period(data.valid_from, data.valid_till, request.session)
return recipe_info(recipe) return recipe_info(recipe)
@ -201,13 +200,7 @@ async def update_route(
) )
def check_recursion(recipe: Recipe, seen_recipes: Set[uuid.UUID], db: Session) -> None: def check_recursion(product_id: uuid.UUID, recipe: Recipe, db: Session) -> None:
if recipe.sku.product_id in seen_recipes:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Recipe recursion. Some ingredient recipe contains parent recipe.",
)
seen_recipes.add(recipe.sku.product_id)
for item in recipe.items: for item in recipe.items:
item_recipes: List[Recipe] = ( item_recipes: List[Recipe] = (
db.execute( db.execute(
@ -226,7 +219,12 @@ def check_recursion(recipe: Recipe, seen_recipes: Set[uuid.UUID], db: Session) -
.all() .all()
) )
for sub_recipe in item_recipes: for sub_recipe in item_recipes:
check_recursion(sub_recipe, seen_recipes, db) if sub_recipe.sku.product_id == product_id:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Recipe recursion. Some ingredient recipe contains parent recipe.",
)
check_recursion(product_id, sub_recipe, db)
def get_purchased_product_cost(product: Product, start_date: date, finish_date: date, db: Session) -> Decimal: def get_purchased_product_cost(product: Product, start_date: date, finish_date: date, db: Session) -> Decimal:
@ -272,7 +270,7 @@ def get_sub_product_cost(product: Product, start_date: date, finish_date: date,
detail=f"Ingredient {product.name} is not bought and also does not have a recipe for the period.", detail=f"Ingredient {product.name} is not bought and also does not have a recipe for the period.",
) )
return round(recipe.cost_price / recipe.sku.fraction / recipe.sku.product_yield, 5) return round(recipe.cost_price / recipe.sku.fraction / recipe.sku.product_yield, 5) # type: ignore[no-any-return]
def update_old_rows(sku_id: uuid.UUID, valid_from: date, valid_till: date, db: Session) -> None: def update_old_rows(sku_id: uuid.UUID, valid_from: date, valid_till: date, db: Session) -> None: