<?php
namespace App\Entity;
use App\Repository\CompteComptableRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: CompteComptableRepository::class)]
class CompteComptable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 20, unique: true)]
private ?string $numero = null;
#[ORM\Column(length: 255)]
private ?string $libelle = null;
#[ORM\Column(length: 50)]
private ?string $classe = null; // 1-9: Classe du compte SYSCOHADA
#[ORM\Column(length: 50)]
private ?string $nature = null; // ACTIF, PASSIF, CHARGE, PRODUIT
#[ORM\Column]
private ?bool $estActif = true;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
#[ORM\OneToMany(mappedBy: 'compteDebit', targetEntity: LigneEcriture::class)]
private Collection $lignesDebit;
#[ORM\OneToMany(mappedBy: 'compteCredit', targetEntity: LigneEcriture::class)]
private Collection $lignesCredit;
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'sousComptes')]
#[ORM\JoinColumn(nullable: true)]
private ?self $compteParent = null;
#[ORM\OneToMany(mappedBy: 'compteParent', targetEntity: self::class)]
private Collection $sousComptes;
#[ORM\Column]
private ?float $soldeInitial = 0;
public function __construct()
{
$this->lignesDebit = new ArrayCollection();
$this->lignesCredit = new ArrayCollection();
$this->sousComptes = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNumero(): ?string
{
return $this->numero;
}
public function setNumero(string $numero): static
{
$this->numero = $numero;
return $this;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(string $libelle): static
{
$this->libelle = $libelle;
return $this;
}
public function getClasse(): ?string
{
return $this->classe;
}
public function setClasse(string $classe): static
{
$this->classe = $classe;
return $this;
}
public function getNature(): ?string
{
return $this->nature;
}
public function setNature(string $nature): static
{
$this->nature = $nature;
return $this;
}
public function isEstActif(): ?bool
{
return $this->estActif;
}
public function setEstActif(bool $estActif): static
{
$this->estActif = $estActif;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): static
{
$this->description = $description;
return $this;
}
/**
* @return Collection<int, LigneEcriture>
*/
public function getLignesDebit(): Collection
{
return $this->lignesDebit;
}
public function addLigneDebit(LigneEcriture $ligneDebit): static
{
if (!$this->lignesDebit->contains($ligneDebit)) {
$this->lignesDebit->add($ligneDebit);
$ligneDebit->setCompteDebit($this);
}
return $this;
}
public function removeLigneDebit(LigneEcriture $ligneDebit): static
{
if ($this->lignesDebit->removeElement($ligneDebit)) {
if ($ligneDebit->getCompteDebit() === $this) {
$ligneDebit->setCompteDebit(null);
}
}
return $this;
}
/**
* @return Collection<int, LigneEcriture>
*/
public function getLignesCredit(): Collection
{
return $this->lignesCredit;
}
public function addLigneCredit(LigneEcriture $ligneCredit): static
{
if (!$this->lignesCredit->contains($ligneCredit)) {
$this->lignesCredit->add($ligneCredit);
$ligneCredit->setCompteCredit($this);
}
return $this;
}
public function removeLigneCredit(LigneEcriture $ligneCredit): static
{
if ($this->lignesCredit->removeElement($ligneCredit)) {
if ($ligneCredit->getCompteCredit() === $this) {
$ligneCredit->setCompteCredit(null);
}
}
return $this;
}
public function getCompteParent(): ?self
{
return $this->compteParent;
}
public function setCompteParent(?self $compteParent): static
{
$this->compteParent = $compteParent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getSousComptes(): Collection
{
return $this->sousComptes;
}
public function addSousCompte(self $sousCompte): static
{
if (!$this->sousComptes->contains($sousCompte)) {
$this->sousComptes->add($sousCompte);
$sousCompte->setCompteParent($this);
}
return $this;
}
public function removeSousCompte(self $sousCompte): static
{
if ($this->sousComptes->removeElement($sousCompte)) {
if ($sousCompte->getCompteParent() === $this) {
$sousCompte->setCompteParent(null);
}
}
return $this;
}
public function getSoldeInitial(): ?float
{
return $this->soldeInitial;
}
public function setSoldeInitial(float $soldeInitial): static
{
$this->soldeInitial = $soldeInitial;
return $this;
}
/**
* Calcule le solde du compte (Débit - Crédit)
*/
public function calculerSolde(): float
{
$debit = $this->soldeInitial;
$credit = 0;
foreach ($this->lignesDebit as $ligne) {
if ($ligne->getEcritureComptable()->isEstValide()) {
$debit += $ligne->getMontant();
}
}
foreach ($this->lignesCredit as $ligne) {
if ($ligne->getEcritureComptable()->isEstValide()) {
$credit += $ligne->getMontant();
}
}
return $debit - $credit;
}
public function __toString(): string
{
return $this->numero . ' - ' . $this->libelle;
}
}