src/Controller/AdminController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\Appointment\AppointmentRepository;
  4. use App\Repository\ContactReminderRepository;
  5. use App\Repository\Invoice\InvoiceCompanyRepository;
  6. use App\Repository\Invoice\InvoiceRepository;
  7. use App\Repository\Invoice\InvoiceStatusRepository;
  8. use App\Service\ShopifyAPI;
  9. use DateTime;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Mime\Address;
  19. use Symfony\Component\Mime\Email;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Exception;
  22. class AdminController extends AbstractController
  23. {
  24.     /**
  25.      * @throws TransportExceptionInterface
  26.      */
  27.     #[Route('/admin/check'name'adminCheck')]
  28.     public function adminCheck(InvoiceCompanyRepository $invoiceCompanyRepositoryInvoiceStatusRepository $invoiceStatusRepositoryEntityManagerInterface $entityManagerContactReminderRepository $contactReminderRepositoryMailerInterface $mailer){
  29.         $mailReminders null;
  30.         $reminderBool false;
  31.         $todayDate = new DateTime();
  32.         $reminders $contactReminderRepository->findAll();
  33.         foreach ($reminders as $reminder){
  34.             if($reminder->getSendDate()->format('d') === $todayDate->format('d') and $reminder->getSendDate()->format('m') === $todayDate->format('m') and $reminder->getSendDate()->format('Y') === $todayDate->format('Y')){
  35.                 $mailReminders[] = $reminder;
  36.                 $contactReminderRepository->deleteById($reminder->getId()->jsonSerialize());
  37.             }
  38.         }
  39.         if($mailReminders){
  40.             $email = (new Email())
  41.                 ->from(new Address('contact@lamusee.com''LAMUSEE'))
  42.                 ->subject('Rappels du ' $todayDate->format('Y/m/d'))
  43.                 ->to('contact@lamusee.com')
  44.                 ->html($this->render('admin/email/reminder.html.twig', ['reminders' => $mailReminders])->getContent());
  45.             $mailer->send($email);
  46.             $reminderBool true;
  47.         }
  48.         $lateBool false;
  49.         $invoices $invoiceCompanyRepository->findAll();
  50.         foreach ($invoices as $invoiceCompany){
  51.             if($todayDate $invoiceCompany->getMaximumValidationDate() && $invoiceCompany->getStatus()->getValue() !== 'late' && $invoiceCompany->getStatus()->getValue() !== 'paid' && ($invoiceCompany->getLeftToPay() > || $invoiceCompany->getPayments()->last()->getPaymentStatus()->getValue() !== 'paid')){
  52.                 $invoiceCompany->setStatus($invoiceStatusRepository->findOneBy(['value'=> 'late']));
  53.                 $entityManager->persist($invoiceCompany);
  54.                 $entityManager->flush();
  55.                 $invoicesEmail = (new Email())
  56.                     ->from(new Address('contact@lamusee.com''LAMUSEE'))
  57.                     ->subject('Retard de paiement Multi-marques ' $invoiceCompany->getCompany()->getProvider())
  58.                     ->to('contact@lamusee.com')
  59.                     ->html($this->render('admin/email/late_payment.html.twig', ['invoice'=> $invoiceCompany'URL_PREFIX' => $this->getParameter('url_prefix')])->getContent());
  60.                 $mailer->send($invoicesEmail);
  61.                 $lateBool true;
  62.             }
  63.         }
  64.         if($reminderBool && $lateBool){
  65.             $this->addFlash('success''Les relances et les retards de paiement ont été envoyé !');
  66.             return new Response(json_encode(['message' => 'OK''bool' => true]), 200, ["Content-Type" => "application/json"]);
  67.         }else if($reminderBool){
  68.             $this->addFlash('success''Les relances ont été envoyé !');
  69.             return new Response(json_encode(['message' => 'OK''bool' => true]), 200, ["Content-Type" => "application/json"]);
  70.         }else if($lateBool){
  71.             $this->addFlash('success''Les retards de paiement ont été envoyé !');
  72.             return new Response(json_encode(['message' => 'OK''bool' => true]), 200, ["Content-Type" => "application/json"]);
  73.         }
  74.         return new Response(json_encode(['message' => 'OK']), 200, ["Content-Type" => "application/json"]);
  75.     }
  76.     /**
  77.      * @IsGranted("ROLE_USER")
  78.      * @param Request $request
  79.      * @param PaginatorInterface $paginator
  80.      * @param AppointmentRepository $appointmentRepository
  81.      * @param InvoiceRepository $invoiceRepository
  82.      * @param ShopifyAPI $shopifyAPI
  83.      * @return Response
  84.      * @throws Exception|TransportExceptionInterface
  85.      */
  86.     #[Route('/'name'adminDashboard')]
  87.     public function adminDashboard(Request $requestPaginatorInterface $paginatorAppointmentRepository $appointmentRepositoryInvoiceRepository $invoiceRepositoryShopifyAPI $shopifyAPIInvoiceCompanyRepository $invoiceCompanyRepository): Response
  88.     {
  89.         $todayDate = new DateTime();
  90.         $turnoverMonth 0;
  91.         $appointments $paginator->paginate(
  92.             $appointmentRepository->findAllLast(),
  93.             $request->query->getInt('page'1),
  94.             4
  95.         );
  96.         foreach ($invoiceRepository->findByYear($todayDatetrue) as $invoice) {
  97.             foreach ($invoice->getEstimate() as $estimate) {
  98.                 if ($invoice->getPaymentDate()->format('Y-m') === $todayDate->format('Y-m')) $turnoverMonth += $estimate->getAllTaxesIncludedPrice();
  99.             }
  100.         }
  101.         $shopifyTurnoverMonth 0;
  102.         $orders $shopifyAPI->shopifyRequest('orders.json', [
  103.             'financial_status'   => 'paid'
  104.         ]);
  105.         foreach ($orders as $order){
  106.             foreach ($order as $item){
  107.                 $date = new DateTime($item->created_at);
  108.                 if($date->format('m') === $todayDate->format('m') and $date->format('Y') === $todayDate->format('Y'))$shopifyTurnoverMonth += $item->current_total_price;
  109.             }
  110.         }
  111.         $multiBrandsTurnoverMonth 0;
  112.         foreach ($invoiceCompanyRepository->findByYear($todayDatetrue) as $invoiceCompany) {
  113.             foreach ($invoiceCompany->getEstimate() as $estimateCompany) {
  114.                 if ($invoiceCompany->getPaymentDate()->format('Y-m') === $todayDate->format('Y-m')) $multiBrandsTurnoverMonth += $estimateCompany->getAllTaxesIncludedPrice();
  115.             }
  116.         }
  117.         return $this->render('admin/dashboard.html.twig', [
  118.             'todayDate' => $todayDate,
  119.             'datas' => [
  120.                 'turnover' => [
  121.                     'month' => $turnoverMonth
  122.                 ],
  123.                 'shopify' => [
  124.                     'month' => $shopifyTurnoverMonth
  125.                 ],
  126.                 'events' => [
  127.                     'appointments' => $appointments
  128.                 ],
  129.                 'multibrands' => [
  130.                     'month' => $multiBrandsTurnoverMonth
  131.                 ]
  132.             ]
  133.         ]);
  134.     }
  135. }