Skip to content

Commit

Permalink
Added mpc where group was already used
Browse files Browse the repository at this point in the history
  • Loading branch information
reinout committed Dec 16, 2024
1 parent 0b4af8a commit 02d22bd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
8 changes: 7 additions & 1 deletion trs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,14 @@
views.FinancialExcelView.as_view(),
name="trs.financial.excel",
),
# The next two differentiate in their pk.
re_path(
r"^overviews/financial_excel/(?P<pk>\d+)/$",
r"^overviews/financial_excel/group/(?P<group_pk>\d+)/$",
views.FinancialExcelView.as_view(),
name="trs.financial.excel",
),
re_path(
r"^overviews/financial_excel/mpc/(?P<mpc_pk>\d+)/$",
views.FinancialExcelView.as_view(),
name="trs.financial.excel",
),
Expand Down
99 changes: 90 additions & 9 deletions trs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from trs.models import Booking
from trs.models import BudgetItem
from trs.models import Group
from trs.models import MPC
from trs.models import Invoice
from trs.models import Payable
from trs.models import Person
Expand Down Expand Up @@ -333,6 +334,10 @@ def sentry_javascript_dsn(self):
def group_choices(self):
return list(Group.objects.all().values_list("pk", "name"))

@cached_property
def mpc_choices(self):
return list(MPC.objects.all().values_list("pk", "name"))


class BaseView(LoginAndPermissionsRequiredMixin, TemplateView, BaseMixin):
pass
Expand All @@ -341,7 +346,7 @@ class BaseView(LoginAndPermissionsRequiredMixin, TemplateView, BaseMixin):
class PersonsView(BaseView):

title = "Medewerkers"
normally_visible_filters = ["status", "group", "year"]
normally_visible_filters = ["status", "group", "mpc", "year"]

@cached_property
def results_for_selection_pager(self):
Expand Down Expand Up @@ -397,6 +402,21 @@ def filters_and_choices(self):
]
+ [{"value": "geen", "title": "Zonder groep", "q": Q(group=None)}],
},
{
"title": "MPC",
"param": "mpc",
"default": "all",
"choices": [{"value": "all", "title": "Geen filter", "q": Q()}]
+ [
{
"value": str(mpc.id),
"title": mpc.name,
"q": Q(mpc=mpc.id),
}
for mpc in MPC.objects.all()
]
+ [{"value": "geen", "title": "Zonder MPC", "q": Q(mpc=None)}],
},
{
"title": "Jaar",
"param": "year",
Expand Down Expand Up @@ -813,6 +833,21 @@ def filters_and_choices(self):
]
+ [{"value": "geen", "title": "Zonder groep", "q": Q(group=None)}],
},
{
"title": "MPC",
"param": "mpc",
"default": "all",
"choices": [{"value": "all", "title": "Geen filter", "q": Q()}]
+ [
{
"value": str(mpc.id),
"title": mpc.name,
"q": Q(mpc=mpc.id),
}
for mpc in MPC.objects.all()
]
+ [{"value": "geen", "title": "Zonder MPC", "q": Q(mpc=None)}],
},
{
"title": "Projectleider",
"param": "project_leader",
Expand Down Expand Up @@ -906,7 +941,7 @@ def filters_and_choices(self):

@cached_property
def normally_visible_filters(self):
result = ["status", "group", "year"]
result = ["status", "group", "mpc", "year"]
if self.can_see_everything:
result += [
"is_subsidized",
Expand Down Expand Up @@ -1602,6 +1637,7 @@ def fields(self):
"code",
"description",
"group",
"mpc",
"internal",
"hidden",
"hourless",
Expand Down Expand Up @@ -1689,6 +1725,7 @@ class ProjectCreateView(LoginAndPermissionsRequiredMixin, CreateView, BaseMixin)
"code",
"description",
"group",
"mpc",
"internal",
"hidden",
"hourless",
Expand Down Expand Up @@ -1863,7 +1900,7 @@ def form_valid(self, form):
class PersonEditView(LoginAndPermissionsRequiredMixin, UpdateView, BaseMixin):
template_name = "trs/edit.html"
model = Person
fields = ["name", "user", "group", "is_management", "archived"]
fields = ["name", "user", "group", "mpc", "is_management", "archived"]

@cached_property
def person(self):
Expand Down Expand Up @@ -2631,7 +2668,7 @@ def rows(self):

class PayablesView(BaseView):
template_name = "trs/payables.html"
normally_visible_filters = ["status", "year", "projectstatus", "group"]
normally_visible_filters = ["status", "year", "projectstatus", "group", "mpc"]

@cached_property
def results_for_selection_pager(self):
Expand Down Expand Up @@ -2707,6 +2744,21 @@ def filters_and_choices(self):
}
],
},
{
"title": "MPC",
"param": "mpc",
"default": "all",
"choices": [{"value": "all", "title": "Geen filter", "q": Q()}]
+ [
{
"value": str(mpc.id),
"title": mpc.name,
"q": Q(mpc=mpc.id),
}
for mpc in MPC.objects.all()
]
+ [{"value": "geen", "title": "Zonder MPC", "q": Q(mpc=None)}],
},
]

return result
Expand Down Expand Up @@ -2977,6 +3029,7 @@ def render_to_response(self, context, **response_kwargs):
workbook = xlsxwriter.Workbook(response)
worksheet = workbook.add_worksheet()
worksheet.add_write_handler(Group, _django_model_instance_to_string)
worksheet.add_write_handler(MPC, _django_model_instance_to_string)
worksheet.add_write_handler(Person, _django_model_instance_to_string)
worksheet.add_write_handler(Project, _django_model_instance_to_string)

Expand Down Expand Up @@ -3004,6 +3057,7 @@ def has_form_permissions(self):
"Omschrijving",
"Opdrachtgever",
"Groep",
"MPC",
"Intern",
"Gesubsidieerd",
"Gearchiveerd",
Expand Down Expand Up @@ -3064,6 +3118,7 @@ def excel_lines(self):
project.description.replace(",", " "),
project.principal,
project.group,
project.mpc,
project.internal,
project.is_subsidized,
project.archived,
Expand Down Expand Up @@ -3709,6 +3764,7 @@ def render_to_response(self, context, **response_kwargs):
for person in self.relevant_persons:
worksheet = workbook.add_worksheet(person)
worksheet.add_write_handler(Group, _django_model_instance_to_string)
worksheet.add_write_handler(MPC, _django_model_instance_to_string)
worksheet.add_write_handler(Person, _django_model_instance_to_string)
worksheet.add_write_handler(Project, _django_model_instance_to_string)

Expand Down Expand Up @@ -3743,7 +3799,12 @@ def download_links(self):
for pk, name in Group.objects.all().values_list("pk", "name"):
yield {
"name": name,
"url": reverse("trs.financial.excel", kwargs={"pk": pk}),
"url": reverse("trs.financial.excel", kwargs={"group_pk": pk}),
}
for pk, name in MPC.objects.all().values_list("pk", "name"):
yield {
"name": "MPC: " + name,
"url": reverse("trs.financial.excel", kwargs={"mpc_pk": pk}),
}


Expand All @@ -3761,28 +3822,39 @@ def header_line(self):

@cached_property
def group(self):
if "pk" in self.kwargs:
return Group.objects.get(id=self.kwargs["pk"])
if "group_pk" in self.kwargs:
return Group.objects.get(id=self.kwargs["group_pk"])
return

@cached_property
def mpc(self):
if "mpc_pk" in self.kwargs:
return MPC.objects.get(id=self.kwargs["mpc_pk"])
return

@property
def projects(self):
queryset = Project.objects.filter(internal=False)
if self.group:
queryset = queryset.filter(group=self.group)
if self.mpc:
queryset = queryset.filter(mpc=self.mpc)
return queryset

@property
def persons(self):
if self.group:
return Person.objects.filter(group=self.group)
else:
return Person.objects.all()
if self.mpc:
return Person.objects.filter(mpc=self.mpc)
return Person.objects.all()

@cached_property
def for_who(self):
if self.group:
return self.group.name
elif self.mpc:
return self.mpc.name
else:
return "het gehele bedrijf"

Expand All @@ -3797,6 +3869,8 @@ def _info_from_bookings(self, year):
relevant_persons = Person.objects.filter(id__in=relevant_person_ids)
if self.group:
relevant_persons = relevant_persons.filter(group=self.group)
if self.mpc:
relevant_persons = relevant_persons.filter(mpc=self.mpc)
pycs = [core.get_pyc(person=person, year=year) for person in relevant_persons]
return {
"turnover": sum([pyc.turnover for pyc in pycs]),
Expand Down Expand Up @@ -3840,6 +3914,8 @@ def invoice_table(self):
invoices = Invoice.objects.all()
if self.group:
invoices = invoices.filter(project__group=self.group)
if self.mpc:
invoices = invoices.filter(project__mpc=self.mpc)
# For both defaultdicts, the first key is year, the second the month.
invoiced_per_year_month = defaultdict(dict)
cumulative_per_year_month = defaultdict(dict)
Expand Down Expand Up @@ -3926,9 +4002,12 @@ def total_payables(self):
def target(self):
if self.group:
return self.group.target
elif self.mpc:
return self.mpc.target
else:
# The target of the whole company is the sum of all groups'
# targets.
# TODO: this doesn't match MPC's, really
return Group.objects.all().aggregate(models.Sum("target"))["target__sum"]

@cached_property
Expand Down Expand Up @@ -4072,6 +4151,7 @@ class PayablesExcelView(ExcelResponseMixin, PayablesView):
"Factuurdatum",
"Factuurnummer",
"Groep",
"MPC",
"Project",
"Gearchiveerd",
"Opdrachtgever",
Expand All @@ -4087,6 +4167,7 @@ def excel_lines(self):
payable.date,
payable.number,
payable.project.group,
payable.project.mpc,
payable.project.code,
payable.project.archived,
payable.project.principal,
Expand Down

0 comments on commit 02d22bd

Please sign in to comment.