ninecompta/src/EventSubscriber/CompanySubscriber.php
2024-11-20 17:09:09 +01:00

54 lines
1.6 KiB
PHP

<?php
namespace App\EventSubscriber;
use App\Entity\Accounting;
use App\Entity\Company;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Events;
class CompanySubscriber
{
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function postPersist(PostPersistEventArgs $args): void
{
$company = $args->getObject();
if (!$company instanceof Company) {
return;
}
$entityManager = $args->getObjectManager();
// Génération des accounting par défaut
$this->insertAccounting($company,"512","000","Banque",true);
$this->insertAccounting($company,"530","000","Caisse",true);
$this->insertAccounting($company,"600","000","Charge",false);
$this->insertAccounting($company,"700","000","Produit",false);
// Génération du year par
}
private function insertAccounting(Company $company,string $num01, string $num02, string $title, bool $isactif): void
{
$accounting=$this->em->getRepository("App\Entity\Accounting")->findOneBy(["company"=>$company,"num01"=>$num01,"num02"=>$num02]);
if(!$accounting) {
$accounting = new Accounting;
$accounting->setCompany($company);
$accounting->setNum01($num01);
$accounting->setNum02($num02);
$accounting->setTitle($title);
$accounting->SetActif($isactif);
$this->em->persist($accounting);
$this->em->flush();
}
}
}