feat: Ajout des frais d'inscription lors de la création d'un RF [#18]

This commit is contained in:
N3WT DE COMPET
2025-01-25 16:40:08 +01:00
parent 799e1c6717
commit ece23deb19
12 changed files with 333 additions and 136 deletions

View File

@ -1,10 +1,13 @@
import { useState } from 'react';
import { User, Mail, Phone, UserCheck } from 'lucide-react';
import { useState, useEffect } from 'react';
import { User, Mail, Phone, UserCheck, DollarSign, Percent } from 'lucide-react';
import InputTextIcon from '@/components/InputTextIcon';
import ToggleSwitch from '@/components/ToggleSwitch';
import Button from '@/components/Button';
import Table from '@/components/Table';
import FeesSection from '@/components/Structure/Tarification/FeesSection';
import DiscountsSection from '../Structure/Tarification/DiscountsSection';
const InscriptionForm = ( { students, onSubmit }) => {
const InscriptionForm = ( { students, registrationDiscounts, tuitionDiscounts, registrationFees, tuitionFees, onSubmit }) => {
const [formData, setFormData] = useState({
studentLastName: '',
studentFirstName: '',
@ -12,14 +15,26 @@ const InscriptionForm = ( { students, onSubmit }) => {
guardianPhone: '',
selectedGuardians: [],
responsableType: 'new',
autoMail: false
autoMail: false,
selectedRegistrationDiscounts: [],
selectedRegistrationFees: registrationFees.map(fee => fee.id)
});
const [step, setStep] = useState(1);
const [step, setStep] = useState(0);
const [selectedStudent, setSelectedEleve] = useState('');
const [existingGuardians, setExistingGuardians] = useState([]);
const [totalRegistrationAmount, setTotalRegistrationAmount] = useState(0);
const maxStep = 4
useEffect(() => {
// Calcul du montant total lors de l'initialisation
const initialTotalAmount = calculateFinalRegistrationAmount(
registrationFees.map(fee => fee.id),
[]
);
setTotalRegistrationAmount(initialTotalAmount);
}, [registrationDiscounts, registrationFees]);
const handleToggleChange = () => {
setFormData({ ...formData, autoMail: !formData.autoMail });
};
@ -39,7 +54,7 @@ const InscriptionForm = ( { students, onSubmit }) => {
};
const prevStep = () => {
if (step > 1) {
if (step >= 1) {
setStep(step - 1);
}
};
@ -66,8 +81,122 @@ const InscriptionForm = ( { students, onSubmit }) => {
onSubmit(formData);
}
const handleFeeSelection = (feeId) => {
setFormData((prevData) => {
const selectedRegistrationFees = prevData.selectedRegistrationFees.includes(feeId)
? prevData.selectedRegistrationFees.filter(id => id !== feeId)
: [...prevData.selectedRegistrationFees, feeId];
const finalAmount = calculateFinalRegistrationAmount(selectedRegistrationFees, prevData.selectedRegistrationDiscounts);
setTotalRegistrationAmount(finalAmount);
return { ...prevData, selectedRegistrationFees };
});
};
const handleDiscountSelection = (discountId) => {
setFormData((prevData) => {
const selectedRegistrationDiscounts = prevData.selectedRegistrationDiscounts.includes(discountId)
? prevData.selectedRegistrationDiscounts.filter(id => id !== discountId)
: [...prevData.selectedRegistrationDiscounts, discountId];
const finalAmount = calculateFinalRegistrationAmount(prevData.selectedRegistrationFees, selectedRegistrationDiscounts);
setTotalRegistrationAmount(finalAmount);
return { ...prevData, selectedRegistrationDiscounts };
});
};
const calculateFinalRegistrationAmount = (selectedRegistrationFees, selectedRegistrationDiscounts) => {
const totalFees = selectedRegistrationFees.reduce((sum, feeId) => {
const fee = registrationFees.find(f => f.id === feeId);
if (fee && !isNaN(parseFloat(fee.base_amount))) {
return sum + parseFloat(fee.base_amount);
}
return sum;
}, 0);
console.log(totalFees);
const totalDiscounts = selectedRegistrationDiscounts.reduce((sum, discountId) => {
const discount = registrationDiscounts.find(d => d.id === discountId);
if (discount) {
if (discount.discount_type === 0 && !isNaN(parseFloat(discount.amount))) { // Currency
return sum + parseFloat(discount.amount);
} else if (discount.discount_type === 1 && !isNaN(parseFloat(discount.amount))) { // Percent
return sum + (totalFees * parseFloat(discount.amount) / 100);
}
}
return sum;
}, 0);
const finalAmount = totalFees - totalDiscounts;
return finalAmount.toFixed(2);
};
const isLabelAttenuated = (item) => {
return !formData.selectedRegistrationDiscounts.includes(parseInt(item.id));
};
const isLabelFunction = (item) => {
return item.name + ' : ' + item.amount
};
return (
<div className="space-y-4 mt-8">
{step === 0 && (
<div>
<h2 className="text-l font-bold mb-4">Frais d'inscription</h2>
{registrationFees.length > 0 ? (
<>
<div className="mb-4">
<FeesSection
fees={registrationFees}
type={0}
subscriptionMode={true}
selectedFees={formData.selectedRegistrationFees}
handleFeeSelection={handleFeeSelection}
/>
</div>
<h2 className="text-l font-bold mb-4">Réductions</h2>
<div className="mb-4">
{registrationDiscounts.length > 0 ? (
<DiscountsSection
discounts={registrationDiscounts}
type={0}
subscriptionMode={true}
selectedDiscounts={formData.selectedRegistrationDiscounts}
handleDiscountSelection={handleDiscountSelection}
/>
) : (
<p className="bg-orange-100 border border-orange-400 text-orange-700 px-4 py-3 rounded relative" role="alert">
<strong className="font-bold">Information</strong>
<span className="block sm:inline"> Aucune réduction n'a été créée sur les frais d'inscription.</span>
</p>
)}
</div>
<Table
data={[ {id: 1}]}
columns={[
{
name: 'LIBELLE',
transform: () => <span>MONTANT TOTAL</span>
},
{
name: 'TOTAL',
transform: () => <b>{totalRegistrationAmount} €</b>
}
]}
defaultTheme='bg-cyan-100'
/>
</>
) : (
<p className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">
<strong className="font-bold">Attention!</strong>
<span className="block sm:inline"> Aucun frais d'inscription n'a été créé.</span>
</p>
)}
</div>
)}
{step === 1 && (
<div>
<h2 className="text-l font-bold mb-4">Nouvel élève</h2>
@ -270,7 +399,7 @@ const InscriptionForm = ( { students, onSubmit }) => {
)}
<div className="flex justify-end mt-4 space-x-4">
{step > 1 && (
{step >= 1 && (
<Button text="Précédent"
onClick={prevStep}
className="px-4 py-2 bg-gray-300 text-gray-700 rounded-md shadow-sm hover:bg-gray-400 focus:outline-none"