forked from 202009-91TDD/Team3-budget-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudgetservice.py
42 lines (27 loc) · 1.39 KB
/
budgetservice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from datetime import datetime
import calendar
class BudgetService(object):
def query(self, start, end):
if start > end:
return 0
start_month_end = calendar.monthrange(start.year, start.month)[1]
begin_month = datetime.strftime(start, "%Y%m")
end_month = datetime.strftime(end, "%Y%m")
budgetsList = self.get_budgets()
amount = 0
for budget in budgetsList:
budget_datetime = datetime.strptime(budget.yearMonth, "%Y%m")
budget_day = calendar.monthrange(budget_datetime.year, budget_datetime.month)[1]
if budget_datetime.year == start.year and budget_datetime.month == start.month and start.year == end.year and start.month == end.month:
amount += round(budget.amount*(end.day - start.day + 1)/budget_day, 2)
return amount
if budget_datetime.year == start.year and budget_datetime.month == start.month:
amount += round(budget.amount * (budget_day- start.day + 1) / budget_day,2)
if budget.yearMonth > begin_month and budget.yearMonth < end_month:
amount += budget.amount
if budget_datetime.year == end.year and budget_datetime.month == end.month:
amount += round(budget.amount * end.day / budget_day, 2)
return amount
return 0
def get_budgets(self):
pass