src/Finance/Domain/Entity/StudentFinancialAccount.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Finance\Domain\Entity;
  3. use App\Entity\SchoolYear;
  4. use App\Entity\Student;
  5. use App\Finance\Domain\Enum\SolvencyStatus;
  6. use App\Finance\Domain\Repository\StudentFinancialAccountRepository;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. #[ORM\Entity(repositoryClass: StudentFinancialAccountRepository::class)]
  11. #[ORM\Table(name: 'student_financial_accounts')]
  12. #[ORM\Index(columns: ['student_id'], name: 'idx_student_id')]
  13. #[ORM\Index(columns: ['school_year_id'], name: 'idx_school_year_id')]
  14. #[ORM\Index(columns: ['solvency_status'], name: 'idx_solvency_status')]
  15. class StudentFinancialAccount
  16. {
  17. #[ORM\Id]
  18. #[ORM\GeneratedValue]
  19. #[ORM\Column(type: Types::INTEGER)]
  20. private ?int $id = null;
  21. #[ORM\ManyToOne(targetEntity: Student::class)]
  22. #[ORM\JoinColumn(nullable: false, name: 'student_id')]
  23. #[Assert\NotNull]
  24. private ?Student $student = null;
  25. #[ORM\ManyToOne(targetEntity: SchoolYear::class)]
  26. #[ORM\JoinColumn(nullable: false, name: 'school_year_id')]
  27. #[Assert\NotNull]
  28. private ?SchoolYear $schoolYear = null;
  29. #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
  30. private string $totalDue = '0.00';
  31. #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
  32. private string $totalPaid = '0.00';
  33. #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, options: ['default' => '0.00'])]
  34. private string $balance = '0.00';
  35. #[ORM\Column(type: Types::STRING, length: 20, enumType: SolvencyStatus::class)]
  36. private SolvencyStatus $solvencyStatus = SolvencyStatus::SOLVENT;
  37. #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
  38. private \DateTimeImmutable $createdAt;
  39. #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
  40. private \DateTimeImmutable $updatedAt;
  41. public function __construct()
  42. {
  43. $this->createdAt = new \DateTimeImmutable();
  44. $this->updatedAt = new \DateTimeImmutable();
  45. }
  46. public function getId(): ?int { return $this->id; }
  47. public function getStudent(): ?Student { return $this->student; }
  48. public function setStudent(?Student $student): self
  49. {
  50. $this->student = $student;
  51. $this->updatedAt = new \DateTimeImmutable();
  52. return $this;
  53. }
  54. // Raccourci pour compatibilité
  55. public function getStudentId(): ?int
  56. {
  57. return $this->student?->getId();
  58. }
  59. public function getSchoolYear(): ?SchoolYear { return $this->schoolYear; }
  60. public function setSchoolYear(?SchoolYear $schoolYear): self
  61. {
  62. $this->schoolYear = $schoolYear;
  63. $this->updatedAt = new \DateTimeImmutable();
  64. return $this;
  65. }
  66. // Raccourci pour compatibilité
  67. public function getSchoolYearId(): ?int
  68. {
  69. return $this->schoolYear?->getId();
  70. }
  71. public function getTotalDue(): string { return $this->totalDue; }
  72. public function setTotalDue(string $totalDue): self
  73. {
  74. $this->totalDue = $totalDue;
  75. $this->updatedAt = new \DateTimeImmutable();
  76. return $this;
  77. }
  78. public function getTotalPaid(): string { return $this->totalPaid; }
  79. public function setTotalPaid(string $totalPaid): self
  80. {
  81. $this->totalPaid = $totalPaid;
  82. $this->updatedAt = new \DateTimeImmutable();
  83. return $this;
  84. }
  85. public function getBalance(): string { return $this->balance; }
  86. public function setBalance(string $balance): self
  87. {
  88. $this->balance = $balance;
  89. $this->updatedAt = new \DateTimeImmutable();
  90. return $this;
  91. }
  92. public function getSolvencyStatus(): SolvencyStatus { return $this->solvencyStatus; }
  93. public function setSolvencyStatus(SolvencyStatus $solvencyStatus): self
  94. {
  95. $this->solvencyStatus = $solvencyStatus;
  96. $this->updatedAt = new \DateTimeImmutable();
  97. return $this;
  98. }
  99. public function getCreatedAt(): \DateTimeImmutable { return $this->createdAt; }
  100. public function getUpdatedAt(): \DateTimeImmutable { return $this->updatedAt; }
  101. public function recalculateBalance(): void
  102. {
  103. $this->balance = bcsub($this->totalDue, $this->totalPaid, 2);
  104. $this->updateSolvencyStatus();
  105. $this->updatedAt = new \DateTimeImmutable();
  106. }
  107. private function updateSolvencyStatus(): void
  108. {
  109. $this->solvencyStatus = bccomp($this->balance, '0.00', 2) <= 0
  110. ? SolvencyStatus::SOLVENT
  111. : SolvencyStatus::INSOLVENT;
  112. }
  113. }