diff --git a/people/views.py b/people/views.py index 7ae714ab..78d41265 100644 --- a/people/views.py +++ b/people/views.py @@ -7,7 +7,7 @@ from datetime import date, timedelta import json -from collections import namedtuple +from dataclasses import dataclass from django.shortcuts import render, redirect from django.http import Http404, HttpResponseRedirect, HttpResponse @@ -16,6 +16,7 @@ from django.utils.translation import gettext as _ from django.db.models import Count, Sum from django.db.models.functions import TruncMonth +from django.conf import settings from people.models import Consultant from crm.models import Company @@ -194,44 +195,85 @@ def consultant_achievements(request, consultant_id): except Consultant.DoesNotExist: raise Http404 + @dataclass + class Achievement: + key: str + name: str + icon: str + value: int + link: str + format: str = None + level_name: str = "" + level_color: str = "black" + + def __post_init__(self): + LEVEL_COLORS = ("#8bbdd8", "#83a3bd", "#7c789c", "#876089", "#6d4672") + rank = -1 + for level_settings in settings.ACHIEVEMENTS.get(self.key, []): + if self.value >= level_settings[1]: + self.level_name = level_settings[0] + rank += 1 + try: + self.level_color = LEVEL_COLORS[rank] + except IndexError: + self.level_color = LEVEL_COLORS[-1] + + def value_repr(self): + if self.format: + return self.format % self.value + else: + return self.value + achievements = [] - Achievement = namedtuple("Achievement", ["name", "icon", "value", "link"]) - achievements.append(Achievement(name=_("Mission done"), + + achievements.append(Achievement(key="MISSION_COUNT", + name=_("Mission done"), icon="list-ul", value=Mission.objects.filter(nature="PROD", active=False, timesheet__consultant=consultant).distinct().count(), link="#")) - achievements.append(Achievement(name=_("Active mission count"), + achievements.append(Achievement(key="ACTIVE_MISSION_COUNT", + name=_("Active mission count"), icon="list-ul", value=Mission.objects.filter(nature="PROD", active=True).filter(staffing__consultant=consultant).distinct().count(), link="#")) - achievements.append(Achievement(name=_("Turnover"), + achievements.append(Achievement(key="TURNOVER", + name=_("Turnover"), icon="calculator", - value="%i k€" % ((consultant.get_turnover() or 0)/1000), + value=(consultant.get_turnover() or 0)/1000, + format="%i k€", link="#")) - achievements.append(Achievement(name=_("Turnover last 12 months"), + achievements.append(Achievement(key="LAST_YEAR_TURNOVER", + name=_("Turnover last 12 months"), icon="calculator", - value="%i k€" % ((consultant.get_turnover(start_date=(date.today() - timedelta(365)).replace(day=1)) or 0) / 1000), + value=(consultant.get_turnover(start_date=(date.today() - timedelta(365)).replace(day=1)) or 0) / 1000, + format="%i k€", link="#")) longest_mission_qs = Timesheet.objects.filter(mission__nature="PROD", consultant=consultant).values("mission").annotate(Sum("charge")).order_by("charge__sum").last() if longest_mission_qs: - achievements.append(Achievement(name=_("Longest mission: %s") % Mission.objects.get(id=longest_mission_qs["mission"]), + achievements.append(Achievement(key="LONGEST_MISSION", + name=_("Longest mission: %s") % Mission.objects.get(id=longest_mission_qs["mission"]), icon="hourglass", - value=_("%i days") % longest_mission_qs.get("charge__sum", 0), + value= longest_mission_qs.get("charge__sum", 0), + format=_("%i days"), link=reverse("staffing:mission_home", args=[longest_mission_qs["mission"], ]))) max_mission_per_month_qs = Mission.objects.filter(nature="PROD", timesheet__consultant=consultant) max_mission_per_month_qs = max_mission_per_month_qs.annotate(month=TruncMonth("timesheet__working_date")).values("month") max_mission_per_month_qs = max_mission_per_month_qs.annotate(Count("id", distinct=True)).order_by("id__count").last() if max_mission_per_month_qs: - achievements.append(Achievement(name=_("Max mission in one month (%s)") % max_mission_per_month_qs["month"].strftime("%m/%Y"), + achievements.append(Achievement(key="MAX_MISSION_PER_MONTH", + name=_("Max mission in one month (%s)") % max_mission_per_month_qs["month"].strftime("%m/%Y"), icon="list-nested", value=max_mission_per_month_qs["id__count"], link="#")) + #TODO: nb of distinct client so far and number of client last 12 month + #TODO number of missions last year + return render(request, "people/consultant_achievements.html", {"consultant": consultant, "achievements": achievements}) diff --git a/pydici/settings/pydici.py b/pydici/settings/pydici.py index e8cca5bb..cacd976b 100644 --- a/pydici/settings/pydici.py +++ b/pydici/settings/pydici.py @@ -3,6 +3,7 @@ # Pydici specific parameters import os +from django.utils.translation import gettext_lazy as _ # Root dir PYDICI_ROOTDIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir, os.path.pardir) @@ -46,4 +47,14 @@ TELEGRAM_CHAT_MANAGER_LEVEL = 4 # Every people with level < to his will be notified individually if concerned # Default company logo -COMPANY_LOGO = "/media/pydici/company_logo.png" \ No newline at end of file +COMPANY_LOGO = "/media/pydici/company_logo.png" + +# Achievements configuration +ACHIEVEMENTS = { + "MISSION_COUNT": [(_("rookie"), 0), (_("pro"), 50), (_("veteran"), 100), (_("emperor"), 200), (_("grandmaster"), 300)], + "ACTIVE_MISSION_COUNT": [(_("rookie"), 0), (_("busy"), 10), (_("expert"), 10), (_("master"), 15), (_("archivist killer"), 50)], + "TURNOVER": [(_("rookie"), 0), (_("pro"), 500), (_("good winner"), 1_000), (_("Cresus"), 2_000), (_("Picsou"), 5_000)], + "LAST_YEAR_TURNOVER": [(_("rookie"), 0), (_("good winner"), 100), (_("alchemist"), 150), (_("wolf of Wall Street"), 250), (_("grandmaster"), 350)], + "LONGEST_MISSION": [(_("rookie"), 0), (_("sprinter"), 50), (_("marathoner"), 100), (_("survivor"), 200), (_("grandmaster"), 500)], + "MAX_MISSION_PER_MONTH": [(_("rookie"), 0), (_("multithread"), 3), (_("ubiquitous"), 5), (_("ninja"), 10), (_("grandmaster"), 15)], +} diff --git a/templates/people/consultant_achievements.html b/templates/people/consultant_achievements.html index 47fbd5e4..dfeb3a09 100644 --- a/templates/people/consultant_achievements.html +++ b/templates/people/consultant_achievements.html @@ -7,15 +7,15 @@