Skip to content

Commit

Permalink
feat: add csv export for children and employees
Browse files Browse the repository at this point in the history
That way it's possible to use the data within other software to do
further calculations.
  • Loading branch information
toabctl committed Jul 7, 2024
1 parent e274f9d commit e276a86
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ <h5 class="title">
<div class="box">
<h5 class="title">
{{ object_list.count }} {% translate "children" %} ({{ historydate }})
<a class="button is-link is-small is-outlined" href="{% url 'kitamanager:child-list-csv' %}?historydate={{ historydate|date:"Y-m-d" }}">{% translate "download as .csv" %}</a>
</h5>
{% include "kitamanager/child_table.inc.html" with object_list=object_list %}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ <h5 class="title">
<div class="box">
<h5 class="title">
{{ object_list.count }} {% translate "employees" %} ({{ historydate }})
<a class="button is-link is-small is-outlined" href="{% url 'kitamanager:employee-list-csv' %}?historydate={{ historydate|date:"Y-m-d" }}">{% translate "download as .csv" %}</a>
</h5>
{% include "kitamanager/employeecontract_table.inc.html" with object_list=object_list %}
</div>
Expand Down
14 changes: 14 additions & 0 deletions django-kitamanager/kitamanager/tests/test_child_urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import datetime
from django.urls import reverse
from kitamanager.models import Child, ChildPaymentPlan, ChildPaymentTable, ChildPaymentTableEntry
from kitamanager.tests.common import _childcontract_create, _childpaymentplan_create
Expand All @@ -11,6 +12,19 @@ def test_child_list(admin_client):
assert response.status_code == 200


@pytest.mark.django_db
def test_child_list_csv(admin_client):
historydate = datetime.date.today()
response = admin_client.get(reverse("kitamanager:child-list-csv"))
assert response.status_code == 200
assert response.headers["Content-Type"] == "text/csv"
assert response.headers["Content-Disposition"] == f'attachment; filename="child-list-{historydate}.csv"'
assert (
response.content.decode()
== "ID,First Name,Last Name,Age,Voucher,Area,Pay tags,Requirement (full time person),Payment (Euro)\r\n"
)


@pytest.mark.django_db
def test_child_list_with_child_no_contract(admin_client):
Child.objects.create(first_name="1", last_name="11", birth_date="2017-10-22")
Expand Down
15 changes: 15 additions & 0 deletions django-kitamanager/kitamanager/tests/test_employee_urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import datetime
from django.urls import reverse
from kitamanager.tests.common import _employeecontract_create
from kitamanager.models import Employee, EmployeePaymentPlan, Area
Expand All @@ -21,6 +22,20 @@ def test_employee_list(admin_client):
assert response.status_code == 200


@pytest.mark.django_db
def test_employee_list_csv(admin_client):
historydate = datetime.date.today()
response = admin_client.get(reverse("kitamanager:employee-list-csv"))
assert response.status_code == 200
assert response.headers["Content-Type"] == "text/csv"
assert response.headers["Content-Disposition"] == f'attachment; filename="employee-list-{historydate}.csv"'
assert (
response.content.decode() == "ID,First Name,Last Name,Begin date,pay plan,pay group,pay level,pay "
"level next,area,qualification,hours child per week,hours management per week,"
"hours team per week,hours misc per week,monthly salary (Euro)\r\n"
)


@pytest.mark.django_db
def test_employee_detail(admin_client):
e = Employee.objects.create(first_name="1", last_name="11", birth_date="2017-10-22")
Expand Down
4 changes: 4 additions & 0 deletions django-kitamanager/kitamanager/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib.auth import views as auth_views
from kitamanager.views_employee import (
employee_list,
employee_list_csv,
employee_detail,
employee_statistics,
employee_bonuspayment,
Expand All @@ -15,6 +16,7 @@
from kitamanager.views_bank import bankaccount_list, bankaccount_charts_sum_balance_by_month
from kitamanager.views_child import (
child_list,
child_list_csv,
child_list_future,
child_detail,
child_statistics,
Expand Down Expand Up @@ -42,6 +44,7 @@
path("employeepayment/", employeepayment_list, name="employeepayment-list"),
path("employeepayment/<str:plan>/", employeepayment_detail, name="employeepayment-detail"),
path("employee/", employee_list, name="employee-list"),
path("employee/csv/", employee_list_csv, name="employee-list-csv"),
path("employee/statistics/", employee_statistics, name="employee-statistics"),
path("employee/bonus/", employee_bonuspayment, name="employee-bonuspayment"),
path("employee/check-sage-payroll", employee_check_sage_payroll, name="employee-check-sage-payroll"),
Expand All @@ -58,6 +61,7 @@
),
# child
path("child/", child_list, name="child-list"),
path("child/csv/", child_list_csv, name="child-list-csv"),
path("child/future/", child_list_future, name="child-list-future"),
path("child/statistics/", child_statistics, name="child-statistics"),
path("child/<int:pk>/", child_detail, name="child-detail"),
Expand Down
54 changes: 53 additions & 1 deletion django-kitamanager/kitamanager/views_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from kitamanager.forms import HistoryDateForm
from kitamanager.models import Child, ChildContract, ChildPaymentPlan, ChildPaymentTable, RevenueEntry
from dateutil.relativedelta import relativedelta
from django.http import JsonResponse
from django.http import JsonResponse, HttpResponse
from kitamanager.definitions import CHART_COLORS, REVENUE_NAME_BERLIN
import csv


@login_required
Expand Down Expand Up @@ -39,6 +40,57 @@ def child_list(request):
)


@login_required
def child_list_csv(request):
"""
Get a list of children for the given date as CSV
"""
historydate = datetime.date.today()
if request.method == "GET":
form = HistoryDateForm(request.GET)
if form.is_valid():
historydate = form.cleaned_data["historydate"]
else:
form = HistoryDateForm(initial={"historydate": historydate.strftime("%Y-%m-%d")})
else:
form = HistoryDateForm()

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": f'attachment; filename="child-list-{historydate}.csv"'},
)
writer = csv.writer(response)
# header row
writer.writerow(
[
"ID",
"First Name",
"Last Name",
"Age",
"Voucher",
"Area",
"Pay tags",
"Requirement (full time person)",
"Payment (Euro)",
]
)
for child in ChildContract.objects.by_date(historydate):
writer.writerow(
[
child.person.pk,
child.person.first_name,
child.person.last_name,
child.person.age(historydate),
child.person.voucher,
child.area,
child.pay_tags,
child.requirement(historydate)[0],
child.payment(historydate),
]
)
return response


@login_required
def child_list_future(request):
historydate = datetime.date.today()
Expand Down
66 changes: 65 additions & 1 deletion django-kitamanager/kitamanager/views_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from kitamanager.sage_payroll import SagePayrolls
from django.utils.translation import gettext_lazy as _
import datetime
from django.http import JsonResponse
from django.http import JsonResponse, HttpResponse
from kitamanager.definitions import CHART_COLORS, SALARY_EMPLOYER_ADDITION
from decimal import Decimal
from typing import Dict, List
import csv


@login_required
Expand Down Expand Up @@ -46,6 +47,69 @@ def employee_list(request):
)


@login_required
def employee_list_csv(request):
"""
list available Employee for the given historydate and return as CSV
"""
historydate = datetime.date.today()
if request.method == "GET":
form = HistoryDateForm(request.GET)
if form.is_valid():
historydate = form.cleaned_data["historydate"]
else:
form = HistoryDateForm(initial={"historydate": historydate.strftime("%Y-%m-%d")})
else:
form = HistoryDateForm()

response = HttpResponse(
content_type="text/csv",
headers={"Content-Disposition": f'attachment; filename="employee-list-{historydate}.csv"'},
)
writer = csv.writer(response)
# header row
writer.writerow(
[
"ID",
"First Name",
"Last Name",
"Begin date",
"pay plan",
"pay group",
"pay level",
"pay level next",
"area",
"qualification",
"hours child per week",
"hours management per week",
"hours team per week",
"hours misc per week",
"monthly salary (Euro)",
]
)
for employee in EmployeeContract.objects.by_date(date=historydate):
writer.writerow(
[
employee.person.pk,
employee.person.first_name,
employee.person.last_name,
employee.person.begin_date,
employee.pay_plan.name,
employee.pay_group,
employee.pay_level,
employee.person.pay_level_next(historydate),
employee.area.name,
employee.qualification.name,
employee.hours_child,
employee.hours_management,
employee.hours_team,
employee.hours_misc,
employee.person.salary(historydate),
]
)
return response


@login_required
def employeepayment_list(request):
"""
Expand Down

0 comments on commit e276a86

Please sign in to comment.