<?php
namespace App\Controller;
use App\Repository\Appointment\AppointmentRepository;
use App\Repository\ContactReminderRepository;
use App\Repository\Invoice\InvoiceCompanyRepository;
use App\Repository\Invoice\InvoiceRepository;
use App\Repository\Invoice\InvoiceStatusRepository;
use App\Service\ShopifyAPI;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Exception;
class AdminController extends AbstractController
{
/**
* @throws TransportExceptionInterface
*/
#[Route('/admin/check', name: 'adminCheck')]
public function adminCheck(InvoiceCompanyRepository $invoiceCompanyRepository, InvoiceStatusRepository $invoiceStatusRepository, EntityManagerInterface $entityManager, ContactReminderRepository $contactReminderRepository, MailerInterface $mailer){
$mailReminders = null;
$reminderBool = false;
$todayDate = new DateTime();
$reminders = $contactReminderRepository->findAll();
foreach ($reminders as $reminder){
if($reminder->getSendDate()->format('d') === $todayDate->format('d') and $reminder->getSendDate()->format('m') === $todayDate->format('m') and $reminder->getSendDate()->format('Y') === $todayDate->format('Y')){
$mailReminders[] = $reminder;
$contactReminderRepository->deleteById($reminder->getId()->jsonSerialize());
}
}
if($mailReminders){
$email = (new Email())
->from(new Address('contact@lamusee.com', 'LAMUSEE'))
->subject('Rappels du ' . $todayDate->format('Y/m/d'))
->to('contact@lamusee.com')
->html($this->render('admin/email/reminder.html.twig', ['reminders' => $mailReminders])->getContent());
$mailer->send($email);
$reminderBool = true;
}
$lateBool = false;
$invoices = $invoiceCompanyRepository->findAll();
foreach ($invoices as $invoiceCompany){
if($todayDate > $invoiceCompany->getMaximumValidationDate() && $invoiceCompany->getStatus()->getValue() !== 'late' && $invoiceCompany->getStatus()->getValue() !== 'paid' && ($invoiceCompany->getLeftToPay() > 0 || $invoiceCompany->getPayments()->last()->getPaymentStatus()->getValue() !== 'paid')){
$invoiceCompany->setStatus($invoiceStatusRepository->findOneBy(['value'=> 'late']));
$entityManager->persist($invoiceCompany);
$entityManager->flush();
$invoicesEmail = (new Email())
->from(new Address('contact@lamusee.com', 'LAMUSEE'))
->subject('Retard de paiement Multi-marques ' . $invoiceCompany->getCompany()->getProvider())
->to('contact@lamusee.com')
->html($this->render('admin/email/late_payment.html.twig', ['invoice'=> $invoiceCompany, 'URL_PREFIX' => $this->getParameter('url_prefix')])->getContent());
$mailer->send($invoicesEmail);
$lateBool = true;
}
}
if($reminderBool && $lateBool){
$this->addFlash('success', 'Les relances et les retards de paiement ont été envoyé !');
return new Response(json_encode(['message' => 'OK', 'bool' => true]), 200, ["Content-Type" => "application/json"]);
}else if($reminderBool){
$this->addFlash('success', 'Les relances ont été envoyé !');
return new Response(json_encode(['message' => 'OK', 'bool' => true]), 200, ["Content-Type" => "application/json"]);
}else if($lateBool){
$this->addFlash('success', 'Les retards de paiement ont été envoyé !');
return new Response(json_encode(['message' => 'OK', 'bool' => true]), 200, ["Content-Type" => "application/json"]);
}
return new Response(json_encode(['message' => 'OK']), 200, ["Content-Type" => "application/json"]);
}
/**
* @IsGranted("ROLE_USER")
* @param Request $request
* @param PaginatorInterface $paginator
* @param AppointmentRepository $appointmentRepository
* @param InvoiceRepository $invoiceRepository
* @param ShopifyAPI $shopifyAPI
* @return Response
* @throws Exception|TransportExceptionInterface
*/
#[Route('/', name: 'adminDashboard')]
public function adminDashboard(Request $request, PaginatorInterface $paginator, AppointmentRepository $appointmentRepository, InvoiceRepository $invoiceRepository, ShopifyAPI $shopifyAPI, InvoiceCompanyRepository $invoiceCompanyRepository): Response
{
$todayDate = new DateTime();
$turnoverMonth = 0;
$appointments = $paginator->paginate(
$appointmentRepository->findAllLast(),
$request->query->getInt('page', 1),
4
);
foreach ($invoiceRepository->findByYear($todayDate, true) as $invoice) {
foreach ($invoice->getEstimate() as $estimate) {
if ($invoice->getPaymentDate()->format('Y-m') === $todayDate->format('Y-m')) $turnoverMonth += $estimate->getAllTaxesIncludedPrice();
}
}
$shopifyTurnoverMonth = 0;
$orders = $shopifyAPI->shopifyRequest('orders.json', [
'financial_status' => 'paid'
]);
foreach ($orders as $order){
foreach ($order as $item){
$date = new DateTime($item->created_at);
if($date->format('m') === $todayDate->format('m') and $date->format('Y') === $todayDate->format('Y'))$shopifyTurnoverMonth += $item->current_total_price;
}
}
$multiBrandsTurnoverMonth = 0;
foreach ($invoiceCompanyRepository->findByYear($todayDate, true) as $invoiceCompany) {
foreach ($invoiceCompany->getEstimate() as $estimateCompany) {
if ($invoiceCompany->getPaymentDate()->format('Y-m') === $todayDate->format('Y-m')) $multiBrandsTurnoverMonth += $estimateCompany->getAllTaxesIncludedPrice();
}
}
return $this->render('admin/dashboard.html.twig', [
'todayDate' => $todayDate,
'datas' => [
'turnover' => [
'month' => $turnoverMonth
],
'shopify' => [
'month' => $shopifyTurnoverMonth
],
'events' => [
'appointments' => $appointments
],
'multibrands' => [
'month' => $multiBrandsTurnoverMonth
]
]
]);
}
}