Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @subscriptions_api.route("/overview")
- class SubscriptionsOverview(Resource):
- @subscriptions_api.doc("Overview data for graph and total")
- @subscriptions_api.marshal_with(SubscriptionModel.subscriptions_overview_model)
- @authorize_employee
- def get(self):
- """Overview data for graph and total"""
- company_id = request.employee.company_id
- try:
- if request.employee.role == UserRolesEnum.ADMIN.value:
- active_record = SubscriptionCompanyAggregationActive.get(company_id=company_id)
- else:
- active_record = SubscriptionUserAggregationActive.get(
- company_id=company_id, employee_email=request.employee.email
- )
- except db_exceptions.DbSelectException:
- active_record = {}
- current_date = datetime.now()
- last_month_date = datetime.now() - relativedelta(months=1)
- current_mtg = active_record.get("ytg", {}).get(str(current_date.year), {}).get(str(current_date.month), {})
- last_mtg = active_record.get("ytg", {}).get(str(last_month_date.year), {}).get(str(last_month_date.month), {})
- spend_this_month = sum(current_mtg.values()) or Decimal(0)
- expected_this_month = spend_this_month
- max_days = monthrange(current_date.year, current_date.month)[1]
- data = {}
- for day in range(1, max_days + 1):
- data[str(day)] = {
- "current": current_mtg.get(str(day), 0),
- "expected": 0,
- "index": str(day),
- "expected_tooltip": []
- }
- kwargs = dict(company_id=company_id, status=SubscriptionStatusEnum.ACTIVE.value)
- if request.employee.role == UserRolesEnum.USER.value:
- kwargs["employee_email"] = request.employee.email
- for subscription in paginate_through_all_subscriptions(**kwargs):
- last_payment = subscription.get("last_payment", 0)
- service = SubscriptionServicesCrud.get(url=subscription.get("service"))
- if last_payment == 0:
- date = subscription.get("expected_first_payment", subscription.get("created_at", None))
- budget_limit = subscription.get("budget_limit", None)
- if date is not None and budget_limit is not None:
- date = datetime.fromtimestamp(date)
- if (
- (date.year == current_date.year and date.month == current_date.month) or
- subscription.get("billed") == SubscriptionBilledEnum.MONTHLY.value
- ):
- try:
- data[str(date.day)]["expected"] += budget_limit
- data[str(date.day)]["expected_tooltip"].append({
- "service": service.get("name") or service.get("url"),
- "value": budget_limit
- })
- except KeyError:
- # e.g. it is february but subscription day is 31st, so then we take the last day of month
- data[max(data)]["expected"] += budget_limit
- data[max(data)]["expected_tooltip"].append({
- "service": service.get("name") or service.get("url"),
- "value": budget_limit
- })
- # if date.day >= current_date.day:
- # expected_this_month += budget_limit
- expected_this_month += budget_limit
- elif subscription.get("billed") == SubscriptionBilledEnum.MONTHLY.value:
- ytg = subscription.get("ytg")
- year_data = ytg.get(max(ytg))
- month_data = year_data.get(max(year_data))
- expected_by_subscription = 0
- for day, money in month_data.items():
- try:
- data[day]["expected"] += money
- data[day]["expected_tooltip"].append({
- "service": service.get("name") or service.get("url"),
- "value": money
- })
- except KeyError:
- # e.g. it is february but subscription day is 31st, so then we take the last day of month
- data[max(data)]["expected"] += money
- data[max(data)]["expected_tooltip"].append({
- "service": service.get("name") or service.get("url"),
- "value": money
- })
- finally:
- expected_by_subscription += money
- # if date.day >= current_date.day:
- # expected_this_month += budget_limit
- expected_this_month += expected_by_subscription
- return {
- "trend": calculate_trend(current_month=spend_this_month, previous_month=expected_this_month),
- "active": active_record.get("active", 0),
- "expected": expected_this_month,
- "spend": spend_this_month,
- "data": list(data.values())
- }
Advertisement
Add Comment
Please, Sign In to add comment