47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
from collections import OrderedDict
|
|
from decimal import Decimal
|
|
|
|
from apps.tasks.models import Task
|
|
|
|
|
|
def build_monthly_report(user, year, month, company=None):
|
|
tasks = (
|
|
Task.objects.filter(
|
|
assignee=user,
|
|
work_date__year=year,
|
|
work_date__month=month,
|
|
status=Task.Status.COMPLETED,
|
|
)
|
|
.select_related("project__company", "assignee")
|
|
.order_by("project__company__name", "project__name", "work_date", "id")
|
|
)
|
|
if company is not None:
|
|
tasks = tasks.filter(project__company=company)
|
|
|
|
companies = OrderedDict()
|
|
total_minutes = 0
|
|
total_amount = Decimal("0")
|
|
for task in tasks:
|
|
company_data = companies.setdefault(task.project.company_id, {
|
|
"company": task.project.company, "projects": OrderedDict(), "minutes": 0, "amount": Decimal("0")
|
|
})
|
|
project_data = company_data["projects"].setdefault(task.project_id, {
|
|
"project": task.project, "tasks": [], "minutes": 0, "amount": Decimal("0")
|
|
})
|
|
project_data["tasks"].append(task)
|
|
project_data["minutes"] += task.duration_minutes
|
|
project_data["amount"] += task.amount
|
|
company_data["minutes"] += task.duration_minutes
|
|
company_data["amount"] += task.amount
|
|
total_minutes += task.duration_minutes
|
|
total_amount += task.amount
|
|
|
|
return {
|
|
"user": user,
|
|
"year": year,
|
|
"month": month,
|
|
"companies": list(companies.values()),
|
|
"total_minutes": total_minutes,
|
|
"total_amount": total_amount,
|
|
}
|