src/Controller/BillingController.php line 120

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Knp\Snappy\Pdf;
  8. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use App\Service\Booking\BookingService;
  12. use App\Entity\Invoice;
  13. use App\Entity\Booking;
  14. use App\Entity\Item;
  15. use App\Entity\Payment;
  16. use App\Entity\Hotel;
  17. use App\Entity\Guest;
  18. use App\Entity\Season;
  19. use App\Form\InvoiceType;
  20. class BillingController extends AbstractController
  21. {
  22.     private $invoice_repository;
  23.     public function __construct(
  24.         private EntityManagerInterface $entityManager,
  25.         private BookingService $booking_service,
  26.         private Pdf $knp_snappy_pdf,
  27.     ) {
  28.         $this->invoice_repository $entityManager->getRepository(Invoice::class);
  29.     }
  30.     
  31.     public function listInvoice(): Response
  32.     {
  33.         $list_invoice $this->invoice_repository->findAll();
  34.         $list_season $this->entityManager->getRepository(Season::class)->findBy([],['year' => 'ASC''number' => 'ASC']);
  35.         $id_array = [];
  36.         foreach($list_season as $season){
  37.             $id_array[$season->getNumber().' '.$season->getYear()] = $season->getNumber().' Trimestre '.$season->getYear().' ('.$season->getName().')';
  38.         }
  39.         
  40.         return $this->render('setting/billing/list_invoice.html.twig', [
  41.             'list_invoice' => $list_invoice,
  42.             'id_array' => json_encode($id_array),
  43.         ]);
  44.     }
  45.     
  46.     public function addInvoice(Request $request$id): Response 
  47.     {
  48.         $invoice $id != $this->manageBooking($id) : $this->manageNewInvoice();
  49.         
  50.         $list_invoice $this->invoice_repository->findByYear((new \DateTime())->format('Y'));
  51.         $invoice->setNumber(count($list_invoice) + 1);
  52.         
  53.         $form $this->createForm(InvoiceType::class, $invoice);
  54.         
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) { 
  57.             if ($id != 0){
  58.                 $booking $this->entityManager->find(Booking::class, $id);
  59.                 $booking->setBilled(true);
  60.                 $this->entityManager->persist($booking);
  61.             }
  62.             
  63.             $this->invoice_repository->add($invoicetrue);
  64.             
  65.             $this->addFlash('success''Vous avez créé la facture avec succès!');
  66.                     
  67.             return $this->redirectToRoute('admin_setting_billing_preview_invoice', [
  68.                 'id' => $invoice->getId(),
  69.             ]);
  70.         } 
  71.         
  72.         return $this->render('setting/billing/add_invoice.html.twig', [
  73.             'form' => $form->createView(),
  74.             'invoice' => $invoice,
  75.         ]);
  76.     }
  77.     
  78.     public function previewInvoice(Invoice $invoice): Response
  79.     {
  80.         $previous_invoice $this->invoice_repository->findOneBy(['year' => $invoice->getYear(), 'number' => $invoice->getNumber()-1]);
  81.         if(!$previous_invoice){
  82.             $previous 0;
  83.         }else{
  84.             $previous $previous_invoice->getId();
  85.         }
  86.         
  87.         $next_invoice $this->invoice_repository->findOneBy(['year' => $invoice->getYear(), 'number' => $invoice->getNumber()+1]);
  88.         if(!$next_invoice){
  89.             $next 0;
  90.         }else{
  91.             $next $next_invoice->getId();
  92.         }
  93.         
  94.         return $this->render('setting/billing/preview_invoice.html.twig', [
  95.             'invoice' => $invoice,
  96.             'next' => $next,
  97.             'previous' => $previous,
  98.         ]);
  99.     }
  100.     
  101.     public function donwloadPdf(Invoice $invoice): PdfResponse
  102.     {
  103.         $html $this->renderView('setting/billing/preview_invoice.html.twig', [
  104.             'invoice' => $invoice,
  105.         ]);
  106.         return new PdfResponse(
  107.             $this->knp_snappy_pdf->getOutputFromHtml($html),
  108.             $invoice->getDate()->format('Y-m-d').'-'.$invoice->getGuest()->getCompany().'-'.$invoice->getNumber().'.pdf'
  109.         );
  110.     }
  111.     
  112.     public function editInvoice(Request $requestInvoice $invoice): Response
  113.     {
  114.         $form $this->createForm(InvoiceType::class, $invoice);
  115.         
  116.         $form->handleRequest($request);
  117.         if ($form->isSubmitted() && $form->isValid()) { 
  118.             $this->invoice_repository->add($invoicetrue);
  119.             
  120.             $this->addFlash('success''Vous avez modifié la facture avec succès!');
  121.                     
  122.             return $this->redirectToRoute('admin_setting_billing_preview_invoice', [
  123.                 'id' => $invoice->getId(),
  124.             ]);
  125.         } 
  126.         
  127.         return $this->render('setting/billing/edit_invoice.html.twig', [
  128.             'form' => $form->createView(),
  129.             'invoice' => $invoice,
  130.         ]);
  131.     }
  132.     
  133.     public function copyInvoice(Request $requestInvoice $invoice): Response 
  134.     {
  135.         $clonedInvoice $this->cloneInvoice($invoice);
  136.         
  137.         $this->addFlash('success''Vous avez copié la facture avec succès!');
  138.                     
  139.         return $this->redirectToRoute('admin_setting_billing_preview_invoice', [
  140.             'id' => $clonedInvoice->getId(),
  141.         ]);
  142.     }
  143.     
  144.     public function deleteInvoice(Invoice $invoice): Response
  145.     {
  146.         $next_invoice $this->invoice_repository->findOneByNumberAndYear($invoice->getNumber()+1$invoice->getYear());
  147.         if($next_invoice){
  148.             $this->addFlash('danger''La facture ne peut pas être supprimée!');
  149.         }else{
  150.             $this->invoice_repository->remove($invoicetrue);
  151.             $this->addFlash('success''La facture a été supprimé avec succès avec succès!');
  152.         }
  153.             
  154.         return $this->redirectToRoute('admin_setting_billing_list_invoice');
  155.     }
  156.     
  157.     public function deleteMultiInvoices(Request $request): Response
  158.     {
  159.         $array_id $request->get('data');
  160.         
  161.         if(!is_array($array_id)){
  162.             $this->addFlash('warning'"Vous n'avez pas sélectionné de factures.");
  163.         
  164.             return new Response('ko');
  165.         }
  166.         
  167.         foreach($array_id as $id){
  168.             $invoice $this->invoice_repository->find($id);
  169.             $next_invoice $this->invoice_repository->findOneByNumberAndYear($invoice->getNumber()+1$invoice->getYear());
  170.             if($next_invoice){
  171.                 $this->addFlash('danger'"Les factures n'ont pas pu être supprimées.");
  172.                 
  173.                 return new Response('ko');
  174.             }else{
  175.                 $this->invoice_repository->remove($invoicetrue);
  176.             }
  177.         }
  178.         
  179.         $this->addFlash('success''Les factures ont pu être supprimées.');
  180.         
  181.         return new Response('ok');
  182.     }
  183.     
  184.     public function printMultiInvoices($ids): Response
  185.     {
  186.         $array_invoice_id json_decode($ids);
  187.         $list_invoice=[];
  188.         foreach($array_invoice_id as $id){
  189.             $invoice $this->invoice_repository->find($id);
  190.             $list_invoice[] = $invoice;
  191.         }
  192.         
  193.         return $this->render('setting/billing/print_invoice.html.twig', [
  194.             'list_invoice' => $list_invoice,
  195.         ]);
  196.     }
  197.     
  198.     public function seasonal($number$year): Response 
  199.     {
  200.         $season $this->entityManager->getRepository(Season::class)->findOneBy(['number' => $number'year' => $year'billed' => 0]);
  201.         if(!$season){
  202.             $this->addFlash('warning''Vous avez déjà facturé cette saison.');
  203.         
  204.             return $this->redirectToRoute('admin_setting_billing_list_invoice');         
  205.         }
  206.         
  207.         $list_invoice $this->invoice_repository->findByYear((new \DateTime())->format('Y'));
  208.         $invoice_number count($list_invoice) + 1;
  209.         $hotels $this->entityManager->getRepository(Hotel::class)->findall();
  210.         foreach($hotels as $hotel){
  211.             $price 0;
  212.             $bookings $this->entityManager->getRepository(Booking::class)->findBySeasonAndHotel($season->getId(), $hotel->getId());
  213.             foreach($bookings as $booking){
  214.                 $price += $booking->getAmount()->getPrice();
  215.             }
  216.             
  217.             if($price == 0){
  218.                 continue 1;
  219.             }
  220.             
  221.             $invoice = new Invoice;
  222.             $invoice->setNumber($invoice_number);
  223.             
  224.             $invoice->setGuest($this->getHotelGuest($hotel));
  225.             
  226.             $item = new Item();   
  227.             $item->manageHotelItem($season$price);
  228.             $invoice->addItem($item);
  229.             
  230.             $subtotal $item->getSubtotal();
  231.             $invoice->setSubtotal($subtotal);
  232.             $invoice->setTax(round($subtotal * (100), 2));
  233.             $invoice->setTotal($subtotal + (round($subtotal * (100), 2)));
  234.             
  235.             $invoice->setStatus('paid');
  236.             
  237.             $this->invoice_repository->add($invoicetrue);
  238.             $invoice_number++;
  239.         }
  240.         
  241.         $season->setBilled(true);
  242.         $this->entityManager->persist($season);
  243.         $this->entityManager->flush();
  244.         
  245.         $this->addFlash('success''Les factures saisonnières ont correctement été créées.');
  246.         
  247.         return $this->redirectToRoute('admin_setting_billing_list_invoice');            
  248.     }
  249.     
  250.     private function manageBooking($id)
  251.     {
  252.         /** @var Booking $booking */
  253.         $booking $this->entityManager->find(Booking::class, $id);
  254.         
  255.         $invoice = new Invoice;
  256.         $invoice->setGuest($booking->getGuest());
  257.         
  258.         $imported_booking $this->booking_service->getBookingFromBeds24($booking->getHotel()->getBeds24Setting()->getPropKey(), $booking->getBookId());
  259.         
  260.         $subtotal $paid 0;
  261.         foreach($imported_booking['invoice'] as $imported_item){
  262.             if($imported_item['qty'] < 0){
  263.                 $payment = new Payment;
  264.                 $payment->manageImportedItem($imported_item);
  265.                 $invoice->addPayment($payment);
  266.                 $paid $paid $payment->getPrice();
  267.             }elseif ($imported_booking['price'] > 0){
  268.                 $item = new Item();
  269.                 $item->manageImportedItem($imported_itemtrue);
  270.                 $subtotal $subtotal $item->getSubtotal();
  271.                 $invoice->addItem($item);
  272.             }  
  273.         }
  274.         
  275.         $invoice->setSubtotal($subtotal);
  276.         $invoice->setTax(round($subtotal * (100), 2));
  277.         $invoice->setTotal($subtotal + (round($subtotal * (100), 2)));
  278.         
  279.         if($invoice->getTotal() == $paid){
  280.             $invoice->setStatus('paid');
  281.         }
  282.         
  283.         return $invoice;
  284.     }
  285.     
  286.     private function manageNewInvoice(){
  287.         $invoice = new Invoice;
  288.         $item = new Item;
  289.         $invoice->addItem($item);
  290.         $payment = new Payment();
  291.         $invoice->addPayment($payment);
  292.         
  293.         return $invoice;
  294.     }
  295.     
  296.     private function gethotelGuest(Hotel $hotel): Guest 
  297.     {
  298.         $guest $this->entityManager->getRepository(Guest::class)->findByFirstnameAndName('Vente'$hotel->getName());
  299.         if(!$guest){
  300.             $location = clone $hotel->getLocation();
  301.             $guest = new Guest();
  302.             $guest->manageHotel($hotel);            
  303.             $guest->setLocation($location);
  304.         }
  305.         
  306.         return $guest;
  307.     }
  308.     
  309.     private function cloneInvoice(Invoice $invoice): Invoice
  310.     {
  311.         // Cloner l'objet Invoice
  312.         $clonedInvoice = clone $invoice;
  313.         
  314.         $list_invoice $this->invoice_repository->findByYear((new \DateTime())->format('Y'));
  315.         $clonedInvoice->setNumber(count($list_invoice) + 1);
  316.         
  317.         // Si nécessaire, vous pouvez ajuster d'autres propriétés comme la date, le numéro de facture, etc.
  318.         $clonedInvoice->setDate(new \DateTime());
  319.         $clonedInvoice->setStatus('unpaid');
  320.         // Cloner les items et les réassocier à la nouvelle facture
  321.         foreach ($invoice->getItems() as $item) {
  322.             $clonedItem = clone $item;
  323.             $clonedItem->setInvoice($clonedInvoice);
  324.             $clonedInvoice->addItem($clonedItem);
  325.         }
  326.         
  327.         foreach ($invoice->getPayments() as $payment) {
  328.             $clonedInvoice->removePayment($payment);
  329.         }
  330.         
  331.         // Persist et flush l'entité clonée
  332.         $this->invoice_repository->add($clonedInvoicetrue);
  333.         return $clonedInvoice;
  334.     }
  335. }