<?php
namespace App\Finance\Domain\Entity;
use App\Entity\SchoolYear;
use App\Entity\Student;
use App\Finance\Domain\Enum\SolvencyStatus;
use App\Finance\Domain\Repository\StudentFinancialAccountRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: StudentFinancialAccountRepository::class)]
#[ORM\Table(name: 'student_financial_accounts')]
#[ORM\Index(columns: ['student_id'], name: 'idx_student_id')]
#[ORM\Index(columns: ['school_year_id'], name: 'idx_school_year_id')]
#[ORM\Index(columns: ['solvency_status'], name: 'idx_solvency_status')]
class StudentFinancialAccount
{
#[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\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
private string $totalDue = '0.00';
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
private string $totalPaid = '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: SolvencyStatus::class)]
private SolvencyStatus $solvencyStatus = SolvencyStatus::SOLVENT;
#[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é
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;
}
// Raccourci pour compatibilité
public function getSchoolYearId(): ?int
{
return $this->schoolYear?->getId();
}
public function getTotalDue(): string { return $this->totalDue; }
public function setTotalDue(string $totalDue): self
{
$this->totalDue = $totalDue;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getTotalPaid(): string { return $this->totalPaid; }
public function setTotalPaid(string $totalPaid): self
{
$this->totalPaid = $totalPaid;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getBalance(): string { return $this->balance; }
public function setBalance(string $balance): self
{
$this->balance = $balance;
$this->updatedAt = new \DateTimeImmutable();
return $this;
}
public function getSolvencyStatus(): SolvencyStatus { return $this->solvencyStatus; }
public function setSolvencyStatus(SolvencyStatus $solvencyStatus): self
{
$this->solvencyStatus = $solvencyStatus;
$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->totalDue, $this->totalPaid, 2);
$this->updateSolvencyStatus();
$this->updatedAt = new \DateTimeImmutable();
}
private function updateSolvencyStatus(): void
{
$this->solvencyStatus = bccomp($this->balance, '0.00', 2) <= 0
? SolvencyStatus::SOLVENT
: SolvencyStatus::INSOLVENT;
}
}