From db87a1f7ce81880a524b136abdbe4267ce5db472 Mon Sep 17 00:00:00 2001 From: marosmola Date: Wed, 28 Oct 2020 22:33:52 +0100 Subject: [PATCH] create admin site with finance summary and graph --- teachers/admin.py | 62 ++++++++++++++++- teachers/migrations/0002_teachersalary.py | 25 +++++++ teachers/models.py | 11 +++- .../admin/sale_summary_change_list.html | 66 +++++++++++++++++++ 4 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 teachers/migrations/0002_teachersalary.py create mode 100644 teachers/templates/admin/sale_summary_change_list.html diff --git a/teachers/admin.py b/teachers/admin.py index 976664e0..dbc84062 100644 --- a/teachers/admin.py +++ b/teachers/admin.py @@ -1,13 +1,17 @@ +import json + from django import forms from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.utils.translation import ugettext_lazy as _ +from django.db.models import Avg, Sum +from django.db.models.functions import Trunc -from .models import Teacher, TeacherSalary +from .models import Teacher, TeacherSalary, FinanceSummary from easyschool.utils import ExportCsvMixin - +from students.models import StudentFee # Change User Creation Form class UserCreationFormExtended(UserCreationForm): @@ -95,3 +99,57 @@ def get_actions(self, request): actions = [ 'export_as_csv', ] + +@admin.register(FinanceSummary) +class FinanceSummaryAdmin(admin.ModelAdmin): + change_list_template = 'admin/sale_summary_change_list.html' + + def changelist_view(self, request, extra_context=None): + response = super().changelist_view( + request, + extra_context=extra_context, + ) + + monthly_salaries = TeacherSalary.objects.annotate( + month=Trunc('paid_on', 'month')).values('month').annotate(total=Sum('total_amount')) + monthly_fees = StudentFee.objects.annotate( + month=Trunc('date_submitted', 'month')).values('month').annotate(total=Sum('total_amount')) + + data = {} + for salary in monthly_salaries: + month_year = "{}-{}".format(salary['month'].month, salary['month'].year) + data[month_year] = { + "salaries": salary['total'], + "fees": 0 + } + + for fee in monthly_fees: + month_year = "{}-{}".format(fee['month'].month, fee['month'].year) + data[month_year]['fees'] = fee['total'] + + + # Prepare data to graph format + labels, salaries, fees = ([] for i in range(3)) + for key, value in data.items(): + labels.append(key) + salaries.append(value['salaries']) + fees.append(value['fees']) + + + # Scructure of graph_data is defined by Graph.js + graph_data = { + 'labels': labels, + 'datasets': [{ + 'label': "Teacher", + 'backgroundColor': "blue", + 'data': salaries + }, { + 'label': "Student", + 'backgroundColor': "red", + 'data': fees + }] + } + + response.context_data['data'] = data + response.context_data['graph_data'] = json.dumps(graph_data) + return response diff --git a/teachers/migrations/0002_teachersalary.py b/teachers/migrations/0002_teachersalary.py new file mode 100644 index 00000000..4180cd33 --- /dev/null +++ b/teachers/migrations/0002_teachersalary.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.2 on 2020-10-27 12:34 + +import datetime +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('teachers', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='TeacherSalary', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('valid_until', models.DateField(default=datetime.date(2020, 11, 1), verbose_name='Valid Until')), + ('total_amount', models.PositiveIntegerField(default=0)), + ('paid_on', models.DateTimeField(auto_now_add=True)), + ('teacher', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='teachers.teacher')), + ], + ), + ] diff --git a/teachers/models.py b/teachers/models.py index b89ae92e..9a6459a2 100644 --- a/teachers/models.py +++ b/teachers/models.py @@ -37,8 +37,15 @@ class TeacherSalary(models.Model): paid_on = models.DateTimeField(auto_now_add=True) def __str__(self): - return f'Fee : {self.student.full_name()} {str(self.date_submitted)}' + return f'Salary : {self.teacher.full_name} {str(self.paid_on)}' @property def month_name(self): - return calendar.month_name[valid_until.month] \ No newline at end of file + return calendar.month_name[valid_until.month] + + +class FinanceSummary(TeacherSalary): + class Meta: + proxy = True + verbose_name = 'Finance Summary' + verbose_name_plural = 'Finance Summary' diff --git a/teachers/templates/admin/sale_summary_change_list.html b/teachers/templates/admin/sale_summary_change_list.html new file mode 100644 index 00000000..700f0976 --- /dev/null +++ b/teachers/templates/admin/sale_summary_change_list.html @@ -0,0 +1,66 @@ +{% extends 'admin/change_list.html' %} +{% block content_title %} +

Finance Summary

+{% endblock %} + +{% load humanize %} +{% block result_list %} + + +
+ + + + + + + + + + {% for key, value in data.items %} + + + + + + {% endfor %} + +
+
+ Month +
+
+
+ Salaries +
+
+
+ Fees +
+
{{ key }} {{ value.salaries }} {{ value.fees }}
+
+ + + +{% endblock %} +{% block pagination %}{% endblock %} \ No newline at end of file