<?phpnamespace App\Services;use App\Entity\DetailFacturePatient;use App\Entity\FacturePatient;use App\Entity\IctusCommande;use App\Entity\TypeFacture;use App\Repository\FacturePatientRepository;use App\Repository\TypeFactureRepository;use DateTime;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\Security\Core\Security;class FactureService{ private $facturePatientRepo; private $typeFactureRepo; private $em; private $security; public function __construct( Security $security, EntityManagerInterface $em, FacturePatientRepository $facturePatientRepo, TypeFactureRepository $typeFactureRepo ) { $this->em = $em; $this->security = $security; $this->facturePatientRepo = $facturePatientRepo; $this->typeFactureRepo = $typeFactureRepo; } public function editPatient($commande, $prisencharge) { $typeFacture = $this->typeFactureRepo->findOneById(1); $whereFacture = array( "commandespecial" => $commande, "typefacture" => 1, "isCanceled" => null ); if ($commande instanceof IctusCommande) { $whereFacture = array( "commande" => $commande, "typefacture" => 1, "isCanceled" => null ); } $facturePatient = $this->facturePatientRepo->findOneBy($whereFacture); if ($facturePatient) { $isPayer = null; if ($commande->getEtatpaiement()->getId() == 2) { $isPayer = true; } $facturePatient ->setIsLivrer($commande->isIsLivrer()) ->setIsPayer($isPayer) ->setPrisencharge($prisencharge); foreach ($facturePatient->getDetailFacturePatients() as $detail) { $this->em->remove($detail); } foreach ($commande->getIctusCommandeLines() as $commandeLine) { if ($commandeLine->isIsValide() && $commandeLine->getChanged() == null) { $detailFacture = new DetailFacturePatient(); $detailFacture ->setFacturePatient($facturePatient) ->setDesignation($commandeLine->getDesignation()) ->setPrixUnitaireTTC($commandeLine->getPrixunitaire()) ->setQuantite($commandeLine->getQuantite()); $this->em->persist($detailFacture); } } $this->em->persist($facturePatient); } else { $maxReference = $this->facturePatientRepo->findMaxReference($commande->getPharmacie())[0]["maxReference"]; if ($maxReference == null) { $maxReference = 0; } $maxReference = $maxReference + 1; $facturePatient = new FacturePatient(); $isPayer = null; if ($commande->getEtatpaiement()->getId() == 2) { $isPayer = true; } $dateNow = new DateTime(); $dateNow->modify('+2 hours'); $facturePatient ->setDate($dateNow) ->setPharmacie($commande->getPharmacie()) ->setUser($commande->getUser()) ->setTypefacture($typeFacture) ->setIsLivrer($commande->isIsLivrer()) ->setIsPayer($isPayer) ->setReference($maxReference) ->setRemise($commande->getRemise()) ->setPrisencharge($prisencharge); if ($commande instanceof IctusCommande) { $facturePatient ->setCommande($commande); } else { $facturePatient ->setCommandespecial($commande); } $this->em->persist($facturePatient); foreach ($commande->getIctusCommandeLines() as $commandeLine) { if ($commandeLine->isIsValide() && $commandeLine->getChanged() == null) { $detailFacture = new DetailFacturePatient(); $detailFacture ->setFacturePatient($facturePatient) ->setDesignation($commandeLine->getDesignation()) ->setPrixUnitaireTTC($commandeLine->getPrixunitaire()) ->setQuantite($commandeLine->getQuantite()); $this->em->persist($detailFacture); } } } $this->em->flush(); }}