src/Services/FactureService.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\DetailFacturePatient;
  4. use App\Entity\FacturePatient;
  5. use App\Entity\IctusCommande;
  6. use App\Entity\TypeFacture;
  7. use App\Repository\FacturePatientRepository;
  8. use App\Repository\TypeFactureRepository;
  9. use DateTime;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. class FactureService
  13. {
  14.     private $facturePatientRepo;
  15.     private $typeFactureRepo;
  16.     private $em;
  17.     private $security;
  18.     public function __construct(
  19.         Security $security,
  20.         EntityManagerInterface $em,
  21.         FacturePatientRepository $facturePatientRepo,
  22.         TypeFactureRepository $typeFactureRepo
  23.     ) {
  24.         $this->em $em;
  25.         $this->security $security;
  26.         $this->facturePatientRepo $facturePatientRepo;
  27.         $this->typeFactureRepo $typeFactureRepo;
  28.     }
  29.     public function editPatient($commande$prisencharge)
  30.     {
  31.         $typeFacture $this->typeFactureRepo->findOneById(1);
  32.         $whereFacture = array(
  33.             "commandespecial" => $commande,
  34.             "typefacture" => 1,
  35.             "isCanceled" => null
  36.         );
  37.         if ($commande instanceof IctusCommande) {
  38.             $whereFacture = array(
  39.                 "commande" => $commande,
  40.                 "typefacture" => 1,
  41.                 "isCanceled" => null
  42.             );
  43.         }
  44.         $facturePatient $this->facturePatientRepo->findOneBy($whereFacture);
  45.         if ($facturePatient) {
  46.             $isPayer null;
  47.             if ($commande->getEtatpaiement()->getId() == 2) {
  48.                 $isPayer true;
  49.             }
  50.             $facturePatient
  51.                 ->setIsLivrer($commande->isIsLivrer())
  52.                 ->setIsPayer($isPayer)
  53.                 ->setPrisencharge($prisencharge);
  54.             foreach ($facturePatient->getDetailFacturePatients() as $detail) {
  55.                 $this->em->remove($detail);
  56.             }
  57.             foreach ($commande->getIctusCommandeLines() as $commandeLine) {
  58.                 if ($commandeLine->isIsValide() && $commandeLine->getChanged() == null) {
  59.                     $detailFacture = new DetailFacturePatient();
  60.                     $detailFacture
  61.                         ->setFacturePatient($facturePatient)
  62.                         ->setDesignation($commandeLine->getDesignation())
  63.                         ->setPrixUnitaireTTC($commandeLine->getPrixunitaire())
  64.                         ->setQuantite($commandeLine->getQuantite());
  65.                     $this->em->persist($detailFacture);
  66.                 }
  67.             }
  68.             $this->em->persist($facturePatient);
  69.         } else {
  70.             $maxReference $this->facturePatientRepo->findMaxReference($commande->getPharmacie())[0]["maxReference"];
  71.             if ($maxReference == null) {
  72.                 $maxReference 0;
  73.             }
  74.             $maxReference $maxReference 1;
  75.             $facturePatient = new FacturePatient();
  76.             $isPayer null;
  77.             if ($commande->getEtatpaiement()->getId() == 2) {
  78.                 $isPayer true;
  79.             }
  80.             $dateNow = new DateTime();
  81.             $dateNow->modify('+2 hours');
  82.             $facturePatient
  83.                 ->setDate($dateNow)
  84.                 ->setPharmacie($commande->getPharmacie())
  85.                 ->setUser($commande->getUser())
  86.                 ->setTypefacture($typeFacture)
  87.                 ->setIsLivrer($commande->isIsLivrer())
  88.                 ->setIsPayer($isPayer)
  89.                 ->setReference($maxReference)
  90.                 ->setRemise($commande->getRemise())
  91.                 ->setPrisencharge($prisencharge);
  92.             if ($commande instanceof IctusCommande) {
  93.                 $facturePatient
  94.                     ->setCommande($commande);
  95.             } else {
  96.                 $facturePatient
  97.                     ->setCommandespecial($commande);
  98.             }
  99.             $this->em->persist($facturePatient);
  100.             foreach ($commande->getIctusCommandeLines() as $commandeLine) {
  101.                 if ($commandeLine->isIsValide() && $commandeLine->getChanged() == null) {
  102.                     $detailFacture = new DetailFacturePatient();
  103.                     $detailFacture
  104.                         ->setFacturePatient($facturePatient)
  105.                         ->setDesignation($commandeLine->getDesignation())
  106.                         ->setPrixUnitaireTTC($commandeLine->getPrixunitaire())
  107.                         ->setQuantite($commandeLine->getQuantite());
  108.                     $this->em->persist($detailFacture);
  109.                 }
  110.             }
  111.         }
  112.         $this->em->flush();
  113.     }
  114. }