<?php
namespace App\Finance\Domain\Entity;
use App\Entity\Student;
use App\Entity\SchoolYear;
use App\Finance\Domain\Repository\CashPaymentRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: CashPaymentRepository::class)]
#[ORM\Table(name: 'cash_payments')]
#[ORM\Index(columns: ['student_id'], name: 'idx_student_id')]
#[ORM\Index(columns: ['school_year_id'], name: 'idx_school_year_id')]
#[ORM\Index(columns: ['payment_date'], name: 'idx_payment_date')]
class CashPayment
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, length: 50, unique: true)]
private string $receiptNumber;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]
#[Assert\NotBlank(message: 'Le montant est obligatoire.')]
#[Assert\Positive(message: 'Le montant doit être supérieur à zéro.')]
private string $amount;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $paymentDate;
#[ORM\Column(type: Types::STRING, length: 255)]
private string $receivedBy;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $notes = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $pdfPath = null;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
private \DateTimeImmutable $createdAt;
// Relations
#[ORM\ManyToOne(targetEntity: Student::class)]
#[ORM\JoinColumn(nullable: false, name: 'student_id')]
#[Assert\NotNull(message: 'Veuillez sélectionner un élève.')]
private ?Student $student = null;
#[ORM\ManyToOne(targetEntity: SchoolYear::class)]
#[ORM\JoinColumn(nullable: false, name: 'school_year_id')]
#[Assert\NotNull(message: 'Veuillez sélectionner une année scolaire.')]
private ?SchoolYear $schoolYear = null;
public function __construct()
{
$this->paymentDate = new \DateTimeImmutable();
$this->createdAt = new \DateTimeImmutable();
$this->generateReceiptNumber();
}
private function generateReceiptNumber(): void
{
$this->receiptNumber = 'RCP-' . date('Ymd') . '-' . strtoupper(substr(uniqid(), -6));
}
// ----- Getters & Setters -----
public function getId(): ?int
{
return $this->id;
}
public function getReceiptNumber(): string
{
return $this->receiptNumber;
}
public function setReceiptNumber(string $receiptNumber): self
{
$this->receiptNumber = $receiptNumber;
return $this;
}
public function getAmount(): string
{
return $this->amount;
}
public function setAmount(string|float|int $amount): self
{
// Conversion propre en string avec 2 décimales
$this->amount = number_format((float) $amount, 2, '.', '');
return $this;
}
public function getPaymentDate(): \DateTimeImmutable
{
return $this->paymentDate;
}
public function setPaymentDate(\DateTimeImmutable $paymentDate): self
{
$this->paymentDate = $paymentDate;
return $this;
}
public function getReceivedBy(): string
{
return $this->receivedBy;
}
public function setReceivedBy(string $receivedBy): self
{
$this->receivedBy = $receivedBy;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): self
{
$this->notes = $notes;
return $this;
}
public function getPdfPath(): ?string
{
return $this->pdfPath;
}
public function setPdfPath(?string $pdfPath): self
{
$this->pdfPath = $pdfPath;
return $this;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
public function getStudent(): ?Student
{
return $this->student;
}
public function setStudent(?Student $student): self
{
$this->student = $student;
return $this;
}
public function getSchoolYear(): ?SchoolYear
{
return $this->schoolYear;
}
public function setSchoolYear(?SchoolYear $schoolYear): self
{
$this->schoolYear = $schoolYear;
return $this;
}
}