"""Utils for sales_goals logic.""" from sales_goals.constants import header from sales_goals.constants import models def compose_totals(market_goals): """Compose totals for goals in country. Args: market_goals (list): marketing target goals for sales goal. Returns: dict: totals result containing label_total and distribution_total which is the sum of all label and distribution goal values for country respectively. """ market_goals = list(market_goals) label_total = sum( goal_in_store[models.LABEL_GOAL_VALUE] for goal_in_store in market_goals if goal_in_store[models.LABEL_GOAL_VALUE] is not None) distribution_total = sum( goal_in_store[models.DISTRIBUTION_GOAL_VALUE] for goal_in_store in market_goals if goal_in_store[models.DISTRIBUTION_GOAL_VALUE] is not None) result = { models.LABEL_TOTAL: label_total, models.DISTRIBUTION_TOTAL: distribution_total } return result def is_workstation_user(user_id=None): """Check whether the call was performed on behalf of workstation user. Args: user_id (str): user id from request header. Return: Boolean: True if call was performed on behalf of workstation user, False in all other cases. """ if user_id: return user_id.startswith(header.WORKSTATION_USER_PREFIX) return False def compose_market_goals_result( market_goals, countries_hide_retailers, user_id=None): """Logic for composing target market goals. Args: market_goals (list): list of target market goals to return. countries_hide_retailers (set): set of ids of countries which have hide_retailers equal to True. user_id (str): user id from request header. Return: list: list with target market goal data. """ market_goals_result = [] for market_goal in market_goals.copy(): if is_workstation_user(user_id): if (market_goal['country_id'] in countries_hide_retailers and market_goal['store_id'] != models.OTHER_STORE_ID): continue if (market_goal['country_id'] in countries_hide_retailers and market_goal['store_id'] == models.OTHER_STORE_ID): del market_goal['distribution_goal_value'] market_goals_result.append(market_goal) market_goals = market_goals_result return market_goals