feat: Ajout des modes de paiements + création d'une commande dans le

back permettant d'initialiser des données de test (pour les tarifs)
This commit is contained in:
N3WT DE COMPET
2025-02-12 15:13:15 +01:00
parent 23203c0397
commit 0c5e3aa098
12 changed files with 299 additions and 44 deletions

View File

@ -0,0 +1,85 @@
from django.core.management.base import BaseCommand
from School.models import Fee, Discount, FeeType, DiscountType
class Command(BaseCommand):
help = 'Initialize or update Fees and Discounts'
def handle(self, *args, **kwargs):
self.create_or_update_fees()
self.create_or_update_discounts()
def create_or_update_fees(self):
fees_data = [
{
"name": "Frais d'inscription",
"base_amount": "150.00",
"description": "Montant de base",
"is_active": True,
"type": FeeType.REGISTRATION_FEE
},
{
"name": "Matériel",
"base_amount": "85.00",
"description": "Livres / jouets",
"is_active": True,
"type": FeeType.REGISTRATION_FEE
},
{
"name": "Sorties périscolaires",
"base_amount": "120.00",
"description": "Sorties",
"is_active": True,
"type": FeeType.REGISTRATION_FEE
},
{
"name": "Les colibris",
"base_amount": "4500.00",
"description": "TPS / PS / MS / GS",
"is_active": True,
"type": FeeType.TUITION_FEE
},
{
"name": "Les butterflies",
"base_amount": "5000.00",
"description": "CP / CE1 / CE2 / CM1 / CM2",
"is_active": True,
"type": FeeType.TUITION_FEE
}
]
for fee_data in fees_data:
Fee.objects.update_or_create(
name=fee_data["name"],
type=fee_data["type"],
defaults=fee_data
)
self.stdout.write(self.style.SUCCESS('Fees initialized or updated successfully'))
def create_or_update_discounts(self):
discounts_data = [
{
"name": "Parrainage",
"amount": "10.00",
"description": "Réduction pour parrainage",
"discount_type": DiscountType.PERCENT,
"type": FeeType.TUITION_FEE
},
{
"name": "Réinscription",
"amount": "100.00",
"description": "Réduction pour Réinscription",
"discount_type": DiscountType.PERCENT,
"type": FeeType.REGISTRATION_FEE
}
]
for discount_data in discounts_data:
Discount.objects.update_or_create(
name=discount_data["name"],
type=discount_data["type"],
discount_type=discount_data["discount_type"],
defaults=discount_data
)
self.stdout.write(self.style.SUCCESS('Discounts initialized or updated successfully'))

View File

@ -0,0 +1,25 @@
from django.core.management.base import BaseCommand
from School.models import PaymentMode, PaymentModeType, FeeType
class Command(BaseCommand):
help = 'Initialize or update Payment Modes'
def handle(self, *args, **kwargs):
self.create_or_update_payment_modes()
def create_or_update_payment_modes(self):
for fee_type in FeeType.choices:
fee_type_value = fee_type[0]
for mode in PaymentModeType.choices:
mode_value = mode[0]
PaymentMode.objects.update_or_create(
mode=mode_value,
type=fee_type_value,
defaults={
'is_active': False
}
)
self.stdout.write(self.style.SUCCESS('Payment Modes initialized or updated successfully'))

View File

@ -82,6 +82,12 @@ class FeeType(models.IntegerChoices):
REGISTRATION_FEE = 0, 'Registration Fee'
TUITION_FEE = 1, 'Tuition Fee'
class PaymentModeType(models.IntegerChoices):
SEPA = 1, 'Prélèvement SEPA'
TRANSFER = 2, 'Virement'
CHECK = 3, 'Chèque'
CASH = 4, 'Espèce'
class Discount(models.Model):
name = models.CharField(max_length=255, unique=True)
amount = models.DecimalField(max_digits=10, decimal_places=2, default=0)
@ -113,3 +119,11 @@ class PaymentPlan(models.Model):
def __str__(self):
return f"{self.get_frequency_display()} - {self.get_type_display()}"
class PaymentMode(models.Model):
mode = models.IntegerField(choices=PaymentModeType.choices, default=PaymentModeType.SEPA)
type = models.IntegerField(choices=FeeType.choices, default=FeeType.REGISTRATION_FEE)
is_active = models.BooleanField(default=False)
def __str__(self):
return f"{self.get_mode_display()} - {self.get_type_display()}"

View File

@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Teacher, Speciality, SchoolClass, Planning, LEVEL_CHOICES, Discount, Fee, PaymentPlan
from .models import Teacher, Speciality, SchoolClass, Planning, LEVEL_CHOICES, Discount, Fee, PaymentPlan, PaymentMode
from Auth.models import Profile
from N3wtSchool import settings, bdd
from django.utils import timezone
@ -195,4 +195,9 @@ class FeeSerializer(serializers.ModelSerializer):
class PaymentPlanSerializer(serializers.ModelSerializer):
class Meta:
model = PaymentPlan
fields = '__all__'
class PaymentModeSerializer(serializers.ModelSerializer):
class Meta:
model = PaymentMode
fields = '__all__'

View File

@ -14,7 +14,9 @@ from School.views import (
DiscountsView,
DiscountView,
PaymentPlansView,
PaymentPlanView
PaymentPlanView,
PaymentModesView,
PaymentModeView
)
urlpatterns = [
@ -45,4 +47,8 @@ urlpatterns = [
re_path(r'^paymentPlans/(?P<_filter>[a-zA-z]+)$', PaymentPlansView.as_view(), name="paymentPlans"),
re_path(r'^paymentPlan$', PaymentPlanView.as_view(), name="paymentPlan"),
re_path(r'^paymentPlan/([0-9]+)$', PaymentPlanView.as_view(), name="paymentPlan"),
re_path(r'^paymentModes/(?P<_filter>[a-zA-z]+)$', PaymentModesView.as_view(), name="paymentModes"),
re_path(r'^paymentMode$', PaymentModeView.as_view(), name="paymentMode"),
re_path(r'^paymentMode/([0-9]+)$', PaymentModeView.as_view(), name="paymentMode"),
]

View File

@ -5,8 +5,8 @@ from rest_framework.parsers import JSONParser
from rest_framework.views import APIView
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from .models import Teacher, Speciality, SchoolClass, Planning, Discount, Fee, PaymentPlan
from .serializers import TeacherSerializer, SpecialitySerializer, SchoolClassSerializer, PlanningSerializer, DiscountSerializer, FeeSerializer, PaymentPlanSerializer
from .models import Teacher, Speciality, SchoolClass, Planning, Discount, Fee, PaymentPlan, PaymentMode
from .serializers import TeacherSerializer, SpecialitySerializer, SchoolClassSerializer, PlanningSerializer, DiscountSerializer, FeeSerializer, PaymentPlanSerializer, PaymentModeSerializer
from N3wtSchool import bdd
from N3wtSchool.bdd import delete_object, getAllObjects, getObject
@ -355,4 +355,49 @@ class PaymentPlanView(APIView):
return JsonResponse(payment_plan_serializer.errors, safe=False, status=400)
def delete(self, request, _id):
return delete_object(PaymentPlan, _id)
return delete_object(PaymentPlan, _id)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PaymentModesView(APIView):
def get(self, request, _filter, *args, **kwargs):
if _filter not in ['registration', 'tuition']:
return JsonResponse({"error": "Invalid type parameter. Must be 'registration' or 'tuition'."}, safe=False, status=400)
type_value = 0 if _filter == 'registration' else 1
paymentModes = PaymentMode.objects.filter(type=type_value)
payment_modes_serializer = PaymentModeSerializer(paymentModes, many=True)
return JsonResponse(payment_modes_serializer.data, safe=False, status=200)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PaymentModeView(APIView):
def get(self, request, _id):
try:
payment_mode = PaymentMode.objects.get(id=_id)
payment_mode_serializer = PaymentModeSerializer(payment_mode)
return JsonResponse(payment_mode_serializer.data, safe=False)
except PaymentMode.DoesNotExist:
return JsonResponse({'error': 'No object found'}, status=404)
def post(self, request):
payment_mode_data = JSONParser().parse(request)
payment_mode_serializer = PaymentModeSerializer(data=payment_mode_data)
if payment_mode_serializer.is_valid():
payment_mode_serializer.save()
return JsonResponse(payment_mode_serializer.data, safe=False, status=201)
return JsonResponse(payment_mode_serializer.errors, safe=False, status=400)
def put(self, request, _id):
payment_mode_data = JSONParser().parse(request)
try:
payment_mode = PaymentMode.objects.get(id=_id)
except PaymentMode.DoesNotExist:
return JsonResponse({'error': 'No object found'}, status=404)
payment_mode_serializer = PaymentModeSerializer(payment_mode, data=payment_mode_data, partial=True)
if payment_mode_serializer.is_valid():
payment_mode_serializer.save()
return JsonResponse(payment_mode_serializer.data, safe=False)
return JsonResponse(payment_mode_serializer.errors, safe=False, status=400)

View File

@ -19,7 +19,9 @@ commands = [
["python", "manage.py", "makemigrations", "Auth", "--noinput"],
["python", "manage.py", "makemigrations", "School", "--noinput"],
["python", "manage.py", "migrate", "--noinput"],
["python", "manage.py", "init_payment_plans"]
["python", "manage.py", "init_payment_plans"],
["python", "manage.py", "init_payment_modes"],
["python", "manage.py", "init_data"]
]
for command in commands: