<?php
namespace App\Entity;
use App\Repository\EcritureComptableRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EcritureComptableRepository::class)]
class EcritureComptable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 50, unique: true)]
private ?string $numeroEcriture = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
private ?\DateTimeInterface $dateEcriture = null;
#[ORM\Column(length: 255)]
private ?string $libelle = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $notes = null;
#[ORM\Column]
private ?bool $estValide = false;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $dateValidation = null;
#[ORM\ManyToOne(inversedBy: 'ecrituresComptables')]
#[ORM\JoinColumn(nullable: false)]
private ?JournalComptable $journal = null;
#[ORM\ManyToOne(inversedBy: 'ecrituresComptables')]
#[ORM\JoinColumn(nullable: false)]
private ?Exercice $exercice = null;
#[ORM\OneToMany(mappedBy: 'ecritureComptable', targetEntity: LigneEcriture::class, cascade: ['persist', 'remove'])]
private Collection $lignes;
#[ORM\ManyToOne]
private ?User $utilisateur = null;
#[ORM\ManyToOne]
private ?User $validePar = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $createdAt = null;
#[ORM\Column(length: 50, nullable: true)]
private ?string $pieceReference = null; // Référence facture, vente, etc.
#[ORM\Column(length: 50, nullable: true)]
private ?string $typeOrigine = null; // VENTE, ACHAT, PAIEMENT, etc.
#[ORM\Column(nullable: true)]
private ?int $idOrigine = null; // ID de la vente, facture, etc.
#[ORM\ManyToOne(inversedBy: 'ecrituresComptables')]
private ?Boutique $boutique = null;
public function __construct()
{
$this->lignes = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->dateEcriture = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getNumeroEcriture(): ?string
{
return $this->numeroEcriture;
}
public function setNumeroEcriture(string $numeroEcriture): static
{
$this->numeroEcriture = $numeroEcriture;
return $this;
}
public function getDateEcriture(): ?\DateTimeInterface
{
return $this->dateEcriture;
}
public function setDateEcriture(\DateTimeInterface $dateEcriture): static
{
$this->dateEcriture = $dateEcriture;
return $this;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(string $libelle): static
{
$this->libelle = $libelle;
return $this;
}
public function getNotes(): ?string
{
return $this->notes;
}
public function setNotes(?string $notes): static
{
$this->notes = $notes;
return $this;
}
public function isEstValide(): ?bool
{
return $this->estValide;
}
public function setEstValide(bool $estValide): static
{
$this->estValide = $estValide;
return $this;
}
public function getDateValidation(): ?\DateTimeInterface
{
return $this->dateValidation;
}
public function setDateValidation(?\DateTimeInterface $dateValidation): static
{
$this->dateValidation = $dateValidation;
return $this;
}
public function getJournal(): ?JournalComptable
{
return $this->journal;
}
public function setJournal(?JournalComptable $journal): static
{
$this->journal = $journal;
return $this;
}
public function getExercice(): ?Exercice
{
return $this->exercice;
}
public function setExercice(?Exercice $exercice): static
{
$this->exercice = $exercice;
return $this;
}
/**
* @return Collection<int, LigneEcriture>
*/
public function getLignes(): Collection
{
return $this->lignes;
}
public function addLigne(LigneEcriture $ligne): static
{
if (!$this->lignes->contains($ligne)) {
$this->lignes->add($ligne);
$ligne->setEcritureComptable($this);
}
return $this;
}
public function removeLigne(LigneEcriture $ligne): static
{
if ($this->lignes->removeElement($ligne)) {
if ($ligne->getEcritureComptable() === $this) {
$ligne->setEcritureComptable(null);
}
}
return $this;
}
public function getUtilisateur(): ?User
{
return $this->utilisateur;
}
public function setUtilisateur(?User $utilisateur): static
{
$this->utilisateur = $utilisateur;
return $this;
}
public function getValidePar(): ?User
{
return $this->validePar;
}
public function setValidePar(?User $validePar): static
{
$this->validePar = $validePar;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getPieceReference(): ?string
{
return $this->pieceReference;
}
public function setPieceReference(?string $pieceReference): static
{
$this->pieceReference = $pieceReference;
return $this;
}
public function getTypeOrigine(): ?string
{
return $this->typeOrigine;
}
public function setTypeOrigine(?string $typeOrigine): static
{
$this->typeOrigine = $typeOrigine;
return $this;
}
public function getIdOrigine(): ?int
{
return $this->idOrigine;
}
public function setIdOrigine(?int $idOrigine): static
{
$this->idOrigine = $idOrigine;
return $this;
}
public function getBoutique(): ?Boutique
{
return $this->boutique;
}
public function setBoutique(?Boutique $boutique): static
{
$this->boutique = $boutique;
return $this;
}
/**
* Calcule le total débit
*/
public function getTotalDebit(): float
{
$total = 0;
foreach ($this->lignes as $ligne) {
$total += $ligne->getDebit();
}
return $total;
}
/**
* Calcule le total crédit
*/
public function getTotalCredit(): float
{
$total = 0;
foreach ($this->lignes as $ligne) {
$total += $ligne->getCredit();
}
return $total;
}
/**
* Vérifie si l'écriture est équilibrée (Débit = Crédit)
*/
public function estEquilibree(): bool
{
return abs($this->getTotalDebit() - $this->getTotalCredit()) < 0.01;
}
public function __toString(): string
{
return $this->numeroEcriture . ' - ' . $this->libelle;
}
}