<?php
namespace App\Controller;
use App\DTO\ChangePasswordDTO;
use App\DTO\ServiceProvider\Sale\Response\Individual\CancelCardSoldResponse;
use App\DTO\ServiceProvider\Sale\Response\Individual\IndividualRechargeResponse;
use App\Form\ChangePasswordType;
use App\Services\ConfigurationService;
use App\Services\EnterpriseService;
use App\Services\ServiceProviderService;
use App\Services\UserService;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class IndexController extends AbstractController
{
/**
* @Route("/", name="index")
* @return Response
*/
public function index(): Response
{
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('batch_index');
} else {
return $this->redirectToRoute('card_index');
}
// return $this->render('index/index.html.twig');
}
public function topbar(RequestStack $requestStack, ServiceProviderService $serviceProviderService,
EnterpriseService $enterpriseService,
ConfigurationService $configurationService): Response
{
$currentRequest = $requestStack->getMainRequest();
$balanceResponse = $serviceProviderService->GetBalance($currentRequest->getSession());
// $available = '_';
if ($balanceResponse['status']) {
$available = $balanceResponse['data']->getBalance();
} else {
$available = 0;
}
$config = $configurationService->getConfiguration();
$expireDate = '';
$days = 99999;
if ($config->getServiceProviderPassword() && $config->getServiceProviderPassowrdGeneratedAt()) {
$currentDate = new \DateTimeImmutable();
$generatedAt = $config->getServiceProviderPassowrdGeneratedAt();
$generatedAt = $generatedAt->modify('+3 month');
$generatedAt = $generatedAt->modify('-1 day');
$diff = date_diff($generatedAt, $currentDate);
$expireDate = $generatedAt->format('Y-m-d');
$days = $diff->days;
}
return $this->render('structure/_topbar.html.twig', [
'availableAmount' => $available,
'expireDate' => $expireDate,
'daysToExpire' => $days,
'walletAmount' => $enterpriseService->getWalletAmount($this->getUser()->getEnterprise()->getId())
]);
}
public function sidebar(RequestStack $requestStack)
{
$currentRequest = $requestStack->getMainRequest();
$currentPath = $currentRequest->attributes->get('_route');
return $this->render('structure/_sidebar.html.twig', [
'currentPath' => $currentPath
]);
}
/**
* @Route("/footer", name="footer")
* @param Request $request
* @param UserService $userService
* @param UserPasswordHasherInterface $passwordHasher
* @param TranslatorInterface $translator
* @return Response
*/
public function footer(Request $request, UserService $userService,
UserPasswordHasherInterface $passwordHasher, TranslatorInterface $translator): Response
{
$dto = new ChangePasswordDTO();
$form = $this->createForm(ChangePasswordType::class, $dto);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
if ($userService->changePassword($this->getUser()->getId(), $form->getData()->getPassword(), $passwordHasher)) {
$this->addFlash('success', $translator->trans('app.user.change.password.success'));
}
} else {
$this->addFlash('warning', $form->getErrors()->current());
}
return $this->redirectToRoute('index');
}
return $this->render('structure/_footer.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route(path="/enterprise/wallet/info", methods={"GET"}, name="enterprise_wallet_info")
* @param EnterpriseService $enterpriseService
* @return Response
*/
public function getCommissionOperations(EnterpriseService $enterpriseService): Response
{
$data = $enterpriseService->getCommissionOperations($this->getUser()->getEnterprise()->getId());
$data = array_map(function ($item) {
$item['createdAt'] = $item['createdAt']->format('Y-m-d H:i');
return $item;
}, $data);
return $this->json([
'status' => true,
'data' => $data,
'totalAmount' => $enterpriseService->getWalletAmount($this->getUser()->getEnterprise()->getId())
]);
}
/**
* @Route("/test/service", methods={"GET"})
* @param Request $request
* @param ServiceProviderService $serviceProviderService
* @return JsonResponse
* @throws InvalidArgumentException
*/
public function testAction(Request $request, ServiceProviderService $serviceProviderService): Response
{
// $provinces = $serviceProviderService->getProvinces($request->getSession());
// var_dump(json_encode($provinces));
$content = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><CancelSaleResponse xmlns="urn:Cubacel.ExternalServices.VirtualPayment.ServiceContracts"><Result xmlns:a="urn:Cubacel.ExternalServices.VirtualPayment.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:ValueOk>true</a:ValueOk><a:Message>OK</a:Message><a:RequestTime>2024-11-29T09:58:24.9761717-05:00</a:RequestTime><a:ResponseTime>2024-11-29T09:58:33.5114263-05:00</a:ResponseTime></Result></CancelSaleResponse></s:Body></s:Envelope>';
// $content = str_replace('a:Provinces', 'Provinces1', $content);
// $content = str_replace('a:Id', 'Id', $content);
// $content = str_replace('a:Name', 'Name', $content);
$object = $serviceProviderService->getSerializer()->deserialize($content, CancelCardSoldResponse::class, 'soap');
// $object = $serviceProviderService->getSerializer()->deserialize($content, GetBalanceResponse::class, 'soap');
// $object = $serviceProviderService->getSerializer()->deserialize($content, SuppleCustInfoResponse::class, 'soap');
// $session = $request->getSession();
// $session->remove('ITTP_SESSION');
// $serviceProviderService->auth($session);
$data = [
'arrivalDate' => '26/07/2023',
'documentNumber' => '123456789',
'documentType' => '9',
'bornDate' => '03/12/1984',
'name' => 'Test',
'lastName' => 'Test',
'gender' => 1,
'address' => 'Address',
'iccid' => '120110620068',
'country' => '1192'
];
$deliveryDate = date_create_immutable_from_format('d/m/Y', '26/06/2023');
$transactionId = '20230617106';
$obj = $serviceProviderService->GetSuppleInfo($session, '100778758', $transactionId);
return $this->json(['data' => $obj]);
}
}