darkor

overview

Feb 22nd, 2023
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 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.         for day, value in list(last_mtg.items()):
  33.             if day >= str(current_date.day):
  34.                 expected_this_month += value
  35.  
  36.         max_days = max([
  37.             monthrange(last_month_date.year, last_month_date.month)[1],
  38.             monthrange(current_date.year, current_date.month)[1],
  39.         ])
  40.  
  41.         data = []
  42.         for day in range(1, max_days + 1):
  43.             data.append({
  44.                 "current": current_mtg.get(str(day), 0),
  45.                 "expected": last_mtg.get(str(day), 0),
  46.                 "index": str(day)
  47.             })
  48.  
  49.         kwargs = dict(company_id=company_id, last_payment=0, status=SubscriptionStatusEnum.ACTIVE.value)
  50.         if request.employee.role == UserRolesEnum.USER.value:
  51.             kwargs["employee_email"] = request.employee.email
  52.  
  53.         for subscription in paginate_through_all_subscriptions(**kwargs):
  54.             date = subscription.get("expected_first_payment", subscription.get("created_at", None))
  55.             budget_limit = subscription.get("budget_limit", None)
  56.  
  57.             if date is not None and budget_limit is not None:
  58.                 date = datetime.fromtimestamp(date)
  59.  
  60.                 if date.year == current_date.year and date.month == current_date.month:
  61.                     for item in data:
  62.                         if item.get("index") == str(date.day):
  63.                             item["expected"] += budget_limit
  64.  
  65.                     if date.day >= current_date.day:
  66.                         expected_this_month += budget_limit
  67.  
  68.         return {
  69.             "trend": calculate_trend(current_month=spend_this_month, previous_month=expected_this_month),
  70.             "active": active_record.get("active", 0),
  71.             "expected": expected_this_month,
  72.             "spend": spend_this_month,
  73.             "data": data
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment