<?php
namespace App\Finance\Domain\Entity;
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\Column(type: Types::INTEGER)]
#[Assert\NotNull]
private int $studentId;
#[ORM\Column(type: Types::INTEGER)]
#[Assert\NotNull]
private int $schoolYearId;
#[ORM\Column(type: Types::INTEGER)]
#[Assert\NotNull]
private int $feeDefinitionId;
#[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 getStudentId(): int
{
return $this->studentId;
}
public function setStudentId(int $studentId): self
{
$this->studentId = $studentId;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getSchoolYearId(): int
{
return $this->schoolYearId;
}
public function setSchoolYearId(int $schoolYearId): self
{
$this->schoolYearId = $schoolYearId;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getFeeDefinitionId(): int
{
return $this->feeDefinitionId;
}
public function setFeeDefinitionId(int $feeDefinitionId): self
{
$this->feeDefinitionId = $feeDefinitionId;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
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;
}
$now = new \DateTimeImmutable();
return $now > $this->dueDate && bccomp($this->balance, '0.00', 2) > 0;
}
}