feat: Mise en place des paiements en plusieurs fois - partie BACK [#25]

This commit is contained in:
N3WT DE COMPET
2025-02-09 19:20:43 +01:00
parent c269b89d3d
commit 274db249aa
4 changed files with 75 additions and 9 deletions

View File

@ -68,10 +68,11 @@ class Planning(models.Model):
def __str__(self):
return f'Planning for {self.level} of {self.school_class.atmosphere_name}'
class PaymentOptions(models.IntegerChoices):
SINGLE_PAYMENT = 0, _('Paiement en une seule fois')
FOUR_TIME_PAYMENT = 1, _('Paiement en 4 fois')
TEN_TIME_PAYMENT = 2, _('Paiement en 10 fois')
class PaymentPlanType(models.IntegerChoices):
ONE_TIME = 1, '1 fois'
THREE_TIMES = 3, '3 fois'
TEN_TIMES = 10, '10 fois'
TWELVE_TIMES = 12, '12 fois'
class DiscountType(models.IntegerChoices):
CURRENCY = 0, 'Currency'
@ -102,3 +103,9 @@ class Fee(models.Model):
def __str__(self):
return self.name
class PaymentPlan(models.Model):
frequency = models.IntegerField(choices=PaymentPlanType.choices, default=PaymentPlanType.ONE_TIME)
due_dates = ArrayField(models.DateField(), blank=True)
type = models.IntegerField(choices=FeeType.choices, default=FeeType.REGISTRATION_FEE)

View File

@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import Teacher, Speciality, SchoolClass, Planning, LEVEL_CHOICES, Discount, Fee
from .models import Teacher, Speciality, SchoolClass, Planning, LEVEL_CHOICES, Discount, Fee, PaymentPlan
from Auth.models import Profile
from N3wtSchool import settings, bdd
from django.utils import timezone
@ -190,4 +190,9 @@ class FeeSerializer(serializers.ModelSerializer):
utc_time = timezone.localtime(obj.updated_at)
local_tz = pytz.timezone(settings.TZ_APPLI)
local_time = utc_time.astimezone(local_tz)
return local_time.strftime("%d-%m-%Y %H:%M")
return local_time.strftime("%d-%m-%Y %H:%M")
class PaymentPlanSerializer(serializers.ModelSerializer):
class Meta:
model = PaymentPlan
fields = '__all__'

View File

@ -13,6 +13,8 @@ from School.views import (
FeeView,
DiscountsView,
DiscountView,
PaymentPlansView,
PaymentPlanView
)
urlpatterns = [
@ -39,4 +41,8 @@ urlpatterns = [
re_path(r'^discounts/(?P<_filter>[a-zA-z]+)$$', DiscountsView.as_view(), name="discounts"),
re_path(r'^discount$', DiscountView.as_view(), name="discount"),
re_path(r'^discount/([0-9]+)$', DiscountView.as_view(), name="discount"),
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"),
]

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
from .serializers import TeacherSerializer, SpecialitySerializer, SchoolClassSerializer, PlanningSerializer, DiscountSerializer, FeeSerializer
from .models import Teacher, Speciality, SchoolClass, Planning, Discount, Fee, PaymentPlan
from .serializers import TeacherSerializer, SpecialitySerializer, SchoolClassSerializer, PlanningSerializer, DiscountSerializer, FeeSerializer, PaymentPlanSerializer
from N3wtSchool import bdd
from N3wtSchool.bdd import delete_object, getAllObjects, getObject
@ -307,4 +307,52 @@ class FeeView(APIView):
return JsonResponse(fee_serializer.errors, safe=False, status=400)
def delete(self, request, _id):
return delete_object(Fee, _id)
return delete_object(Fee, _id)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PaymentPlansView(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
paymentPlans = PaymentPlan.objects.filter(type=type_value)
payment_plans_serializer = PaymentPlanSerializer(paymentPlans, many=True)
return JsonResponse(payment_plans_serializer.data, safe=False, status=200)
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')
class PaymentPlanView(APIView):
def get(self, request, _id):
try:
payment_plan = PaymentPlan.objects.get(id=_id)
payment_plan_serializer = PaymentPlanSerializer(payment_plan)
return JsonResponse(payment_plan_serializer.data, safe=False)
except PaymentPlan.DoesNotExist:
return JsonResponse({'error': 'No object found'}, status=404)
def post(self, request):
payment_plan_data = JSONParser().parse(request)
payment_plan_serializer = PaymentPlanSerializer(data=payment_plan_data)
if payment_plan_serializer.is_valid():
payment_plan_serializer.save()
return JsonResponse(payment_plan_serializer.data, safe=False, status=201)
return JsonResponse(payment_plan_serializer.errors, safe=False, status=400)
def put(self, request, _id):
payment_plan_data = JSONParser().parse(request)
try:
payment_plan = PaymentPlan.objects.get(id=_id)
except PaymentPlan.DoesNotExist:
return JsonResponse({'error': 'No object found'}, status=404)
payment_plan_serializer = PaymentPlanSerializer(payment_plan, data=payment_plan_data, partial=True)
if payment_plan_serializer.is_valid():
payment_plan_serializer.save()
return JsonResponse(payment_plan_serializer.data, safe=False)
return JsonResponse(payment_plan_serializer.errors, safe=False, status=400)
def delete(self, request, _id):
return delete_object(PaymentPlan, _id)