src/Flexy/FrontBundle/Themes/Lasalle/Controller/Api/ApiController.php line 202
<?php
namespace App\Flexy\FrontBundle\Themes\Lasalle\Controller\Api;
use App\Flexy\ProductBundle\Repository\ProductRepository;
use App\Flexy\ShopBundle\Entity\Customer\Customer;
use App\Flexy\ShopBundle\Entity\Customer\Visit;
use App\Flexy\ShopBundle\Entity\Order\Order;
use App\Flexy\ShopBundle\Repository\Order\OrderRepository;
use App\Flexy\ShopBundle\Entity\Product\ProductShop;
use App\Flexy\ShopBundle\Entity\Store\Store;
use App\Flexy\ShopBundle\Repository\Customer\CustomerRepository;
use App\Flexy\ShopBundle\Repository\Shipping\ShippingMethodRepository;
use App\Service\FlexySettingsProvider;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
#[Route('/api')]
class ApiController extends AbstractController
{
#[Route('/check-customer-account', name: 'api_check_customer_account',methods: ['POST'])]
public function api_check_customer_account(Request $request ,ManagerRegistry $doctrine,
CustomerRepository $customerRepository,HubInterface $hub,FlexySettingsProvider $flexySettingsProvider,
OrderRepository $orderRepository)
{
//dd($customer);
if ($request->getContentType() == 'json' and $request->getContent()) {
$contentJsonRequest = json_decode($request->getContent(),true, 512, JSON_THROW_ON_ERROR);
if($contentJsonRequest){
$request->request->replace($contentJsonRequest);
}else{
return $this->json([
"status"=>"error","message"=>"Content is not well formated"
]);
}
}
$reference = $request->request->get("reference");
$em = $doctrine->getManager();
$currentDateTime = new \DateTime();
$query = $em->createQuery(
'SELECT customer
FROM App\Flexy\ShopBundle\Entity\Customer\Customer customer
LEFT JOIN customer.productSubscriptions productSubscriptions
WHERE customer.reference = :reference AND (customer.is_actif IS NULL OR customer.is_actif = 0)
AND :currentDateTime BETWEEN productSubscriptions.startAt AND productSubscriptions.endAt
'
)
->setParameter('reference', $reference)
->setParameter('currentDateTime', $currentDateTime);
$customer = $query->getOneOrNullResult();
$content = $request->getContent();
$url = $request->getUri();
$status = (boolean)$customer;
if( $status == 1) {
$value = "true";
$customer = $customerRepository->findOneBy(["reference"=> $reference]);
$customer_id = $customer->getId();
$order = $orderRepository->findOneBy(["customer"=> $customer_id]);
// Vérifiez si l'ordre est "checked"
if ($order && $order->getIsChecked()) {
// Récupérez l'heure actuelle
$currentHour = (new \DateTime())->format('H:i');
// Vérifiez si l'heure est entre 07:00 et 13:30
if ($currentHour >= '07:00' && $currentHour <= '13:30') {
$value = "true";
} else {
$value = "false";
}
}
//dd($value);
// Créer l'entité Visit seulement si $value est "true"
if ($value === "true") {
$visit = new Visit();
$entityManager = $doctrine->getManager();
$visit->setCustomer($customer);
$visit->setCreatedAt(\DateTime::createFromFormat('Y-m-d H:i:s',date('Y-m-d H:i:s')));
if($customer->getStore()){
$visit->setStore($customer->getStore());
}
$visit->setCardReference($reference);
$entityManager->persist($visit);
$entityManager->flush();
}
// Publication Mercure selon la valeur de $value
if ($flexySettingsProvider->get()->isIsMercureEnabled()) {
$status = ($value === "true") ? "success" : "failed";
$update = new Update(
'https://hello.com/books/1',
json_encode([
'status' => $status,
'entity' => 'VisitCustomer',
"customerId" => $customer_id,
'data' => $value === "true" ? $customer : null, // uniquement ajouter data si success
])
);
$hub->publish($update);
}
/*if($flexySettingsProvider->get()->isIsMercureEnabled()){
$update = new Update(
'https://hello.com/books/1',
json_encode(
[
'status' => 'success',
'entity'=>'VisitCustomer',
"customerId"=>$customer_id,
'data'=>$customer,
]
)
);
$hub->publish($update);
}*/
}else{
if($flexySettingsProvider->get()->isIsMercureEnabled()){
$update = new Update(
'https://hello.com/books/1',
json_encode(
[
'status' => 'failed',
'entity'=>'VisitCustomer',
]
)
);
$hub->publish($update);
}
$value = "false";
}
//dd($customer[0]->getStore()->getName());
return new Response($value);
// Logic
/*return $this->json([
"status"=>(boolean)$customer
]);*/
}
#[Route('/visit-customer', name: 'visit-customer')]
public function visitCustomer(
CustomerRepository $customerRepository,
OrderRepository $orderRepository,
ManagerRegistry $doctrine,
Request $request
): Response
{
$timeStatus='';
$customer = $customerRepository->find((int)$request->query->get("customerId"));
$order = $orderRepository->findOneBy(["customer"=> $customer]);
if($order){
$timeStatus = $order->getisChecked();
}
else{
$timeStatus = false;
}
// $customer = $doctrine->getManager()->getRepository(Customer::class)->findOneBy(["reference"=>7890222]);
//dd($customer[0]->getStore());
return $this->render('@Flexy/FrontBundle/Themes/Lasalle/templates/front/customer/visit.html.twig', [
'customer' => $customer,
'timeStatus'=>$timeStatus,
]);
}
}