darkor

overview done

Mar 5th, 2023
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.25 KB | None | 0 0
  1. @subscriptions_api.route("/overview")
  2. class SubscriptionsOverview(Resource):
  3.  
  4.     @subscriptions_api.doc("Overview data for graph and total")
  5.     @subscriptions_api.marshal_with(SubscriptionModel.subscriptions_overview_model)
  6.     @authorize_employee
  7.     def get(self):
  8.         """Overview data for graph and total"""
  9.         company_id = request.employee.company_id
  10.  
  11.         try:
  12.             if request.employee.role == UserRolesEnum.ADMIN.value:
  13.                 active_record = SubscriptionCompanyAggregationActive.get(company_id=company_id)
  14.  
  15.             else:
  16.                 active_record = SubscriptionUserAggregationActive.get(
  17.                     company_id=company_id, employee_email=request.employee.email
  18.                 )
  19.  
  20.         except db_exceptions.DbSelectException:
  21.             active_record = {}
  22.  
  23.         current_date = datetime.now()
  24.         last_month_date = datetime.now() - relativedelta(months=1)
  25.  
  26.         current_mtg = active_record.get("ytg", {}).get(str(current_date.year), {}).get(str(current_date.month), {})
  27.         last_mtg = active_record.get("ytg", {}).get(str(last_month_date.year), {}).get(str(last_month_date.month), {})
  28.  
  29.         spend_this_month = sum(current_mtg.values()) or Decimal(0)
  30.         expected_this_month = spend_this_month
  31.  
  32.         max_days = monthrange(current_date.year, current_date.month)[1]
  33.  
  34.         data = {}
  35.         for day in range(1, max_days + 1):
  36.             data[str(day)] = {
  37.                 "current": current_mtg.get(str(day), 0),
  38.                 "expected": 0,
  39.                 "index": str(day),
  40.                 "expected_tooltip": []
  41.             }
  42.  
  43.         kwargs = dict(company_id=company_id, status=SubscriptionStatusEnum.ACTIVE.value)
  44.         if request.employee.role == UserRolesEnum.USER.value:
  45.             kwargs["employee_email"] = request.employee.email
  46.  
  47.         for subscription in paginate_through_all_subscriptions(**kwargs):
  48.             last_payment = subscription.get("last_payment", 0)
  49.             service = SubscriptionServicesCrud.get(url=subscription.get("service"))
  50.  
  51.             if last_payment == 0:
  52.                 date = subscription.get("expected_first_payment", subscription.get("created_at", None))
  53.                 budget_limit = subscription.get("budget_limit", None)
  54.  
  55.                 if date is not None and budget_limit is not None:
  56.                     date = datetime.fromtimestamp(date)
  57.  
  58.                     if (
  59.                         (date.year == current_date.year and date.month == current_date.month) or
  60.                         subscription.get("billed") == SubscriptionBilledEnum.MONTHLY.value
  61.                     ):
  62.                         try:
  63.                             data[str(date.day)]["expected"] += budget_limit
  64.                             data[str(date.day)]["expected_tooltip"].append({
  65.                                 "service": service.get("name") or service.get("url"),
  66.                                 "value": budget_limit
  67.                             })
  68.                         except KeyError:
  69.                             # e.g. it is february but subscription day is 31st, so then we take the last day of month
  70.                             data[max(data)]["expected"] += budget_limit
  71.                             data[max(data)]["expected_tooltip"].append({
  72.                                 "service": service.get("name") or service.get("url"),
  73.                                 "value": budget_limit
  74.                             })
  75.  
  76.                         # if date.day >= current_date.day:
  77.                         #     expected_this_month += budget_limit
  78.                         expected_this_month += budget_limit
  79.  
  80.             elif subscription.get("billed") == SubscriptionBilledEnum.MONTHLY.value:
  81.                 ytg = subscription.get("ytg")
  82.                 year_data = ytg.get(max(ytg))
  83.                 month_data = year_data.get(max(year_data))
  84.  
  85.                 expected_by_subscription = 0
  86.                 for day, money in month_data.items():
  87.                     try:
  88.                         data[day]["expected"] += money
  89.                         data[day]["expected_tooltip"].append({
  90.                             "service": service.get("name") or service.get("url"),
  91.                             "value": money
  92.                         })
  93.                     except KeyError:
  94.                         # e.g. it is february but subscription day is 31st, so then we take the last day of month
  95.                         data[max(data)]["expected"] += money
  96.                         data[max(data)]["expected_tooltip"].append({
  97.                             "service": service.get("name") or service.get("url"),
  98.                             "value": money
  99.                         })
  100.                     finally:
  101.                         expected_by_subscription += money
  102.  
  103.                 # if date.day >= current_date.day:
  104.                 #     expected_this_month += budget_limit
  105.                 expected_this_month += expected_by_subscription
  106.  
  107.         return {
  108.             "trend": calculate_trend(current_month=spend_this_month, previous_month=expected_this_month),
  109.             "active": active_record.get("active", 0),
  110.             "expected": expected_this_month,
  111.             "spend": spend_this_month,
  112.             "data": list(data.values())
  113.         }
Advertisement
Add Comment
Please, Sign In to add comment