<?php
namespace App\Finance\Domain\Entity;
use App\Entity\SchoolYear;
use App\Entity\Student;
use App\Finance\Domain\Enum\FeeType;
use App\Finance\Domain\Enum\StudentFeeStatus;
use App\Finance\Domain\Repository\StudentFeeRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: StudentFeeRepository::class)]
#[ORM\Table(name: 'student_fees')]
#[ORM\Index(columns: ['student_id'], name: 'idx_student_id')]
#[ORM\Index(columns: ['school_year_id'], name: 'idx_school_year_id')]
#[ORM\Index(columns: ['fee_type'], name: 'idx_fee_type')]
#[ORM\Index(columns: ['status'], name: 'idx_status')]
#[ORM\Index(columns: ['due_date'], name: 'idx_due_date')]
class StudentFee
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Student::class)]
#[ORM\JoinColumn(nullable: false, name: 'student_id')]
#[Assert\NotNull]
private ?Student $student = null;
#[ORM\ManyToOne(targetEntity: SchoolYear::class)]
#[ORM\JoinColumn(nullable: false, name: 'school_year_id')]
#[Assert\NotNull]
private ?SchoolYear $schoolYear = null;
#[ORM\ManyToOne(targetEntity: FeeDefinition::class)]
#[ORM\JoinColumn(nullable: false, name: 'fee_definition_id')]
#[Assert\NotNull]
private ?FeeDefinition $feeDefinition = null;
#[ORM\Column(type: Types::STRING, length: 20, enumType: FeeType::class)]
#[Assert\NotNull]
private FeeType $feeType;
#[ORM\Column(type: Types::STRING, length: 255)]
#[Assert\NotBlank]
private string $name;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]
#[Assert\NotNull]
#[Assert\PositiveOrZero]
private string $amountDue;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
private string $amountPaid = '0.00';
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
private string $balance = '0.00';
#[ORM\Column(type: Types::STRING, length: 20, enumType: StudentFeeStatus::class)]
private StudentFeeStatus $status = StudentFeeStatus::PENDING;
#[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
private ?\DateTimeImmutable $dueDate = null;
#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $installmentNumber = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $createdAt;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $updatedAt;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
public function getId(): ?int { return $this->id; }
public function getStudent(): ?Student { return $this->student; }
public function setStudent(?Student $student): self
{
$this->student = $student;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
// Raccourci pour compatibilitĂ© avec les requĂȘtes DQL/Repository
public function getStudentId(): ?int
{
return $this->student?->getId();
}
public function getSchoolYear(): ?SchoolYear { return $this->schoolYear; }
public function setSchoolYear(?SchoolYear $schoolYear): self
{
$this->schoolYear = $schoolYear;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getSchoolYearId(): ?int
{
return $this->schoolYear?->getId();
}
public function getFeeDefinition(): ?FeeDefinition { return $this->feeDefinition; }
public function setFeeDefinition(?FeeDefinition $feeDefinition): self
{
$this->feeDefinition = $feeDefinition;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getFeeDefinitionId(): ?int
{
return $this->feeDefinition?->getId();
}
public function getFeeType(): FeeType { return $this->feeType; }
public function setFeeType(FeeType $feeType): self
{
$this->feeType = $feeType;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getName(): string { return $this->name; }
public function setName(string $name): self
{
$this->name = $name;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getAmountDue(): string { return $this->amountDue; }
public function setAmountDue(string $amountDue): self
{
$this->amountDue = $amountDue;
$this->recalculateBalance();
return $this;
}
public function getAmountPaid(): string { return $this->amountPaid; }
public function setAmountPaid(string $amountPaid): self
{
$this->amountPaid = $amountPaid;
$this->recalculateBalance();
return $this;
}
public function getBalance(): string { return $this->balance; }
public function getStatus(): StudentFeeStatus { return $this->status; }
public function setStatus(StudentFeeStatus $status): self
{
$this->status = $status;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getDueDate(): ?\DateTimeImmutable { return $this->dueDate; }
public function setDueDate(?\DateTimeImmutable $dueDate): self
{
$this->dueDate = $dueDate;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getInstallmentNumber(): ?int { return $this->installmentNumber; }
public function setInstallmentNumber(?int $installmentNumber): self
{
$this->installmentNumber = $installmentNumber;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getCreatedAt(): \DateTimeImmutable { return $this->createdAt; }
public function getUpdatedAt(): \DateTimeImmutable { return $this->updatedAt; }
public function recalculateBalance(): void
{
$this->balance = bcsub($this->amountDue, $this->amountPaid, 2);
$this->updateStatus();
$this->updatedAt = new \DateTimeImmutable();
}
private function updateStatus(): void
{
if (bccomp($this->balance, '0.00', 2) <= 0) {
$this->status = StudentFeeStatus::PAID;
} elseif (bccomp($this->amountPaid, '0.00', 2) > 0) {
$this->status = StudentFeeStatus::PARTIAL;
} else {
$this->status = StudentFeeStatus::PENDING;
}
}
public function isLate(): bool
{
if (!$this->dueDate) return false;
return new \DateTimeImmutable() > $this->dueDate
&& bccomp($this->balance, '0.00', 2) > 0;
}
}