src/Service/Aeat/AeatService.php line 114

Open in your IDE?
  1. <?php
  2. namespace App\Service\Aeat;
  3. use App\Entity\AeatResponseLogs;
  4. use App\Entity\CustomerAeat;
  5. use App\Util\AeatResponseLogUtil;
  6. use App\Service\Aeat\AeatRentDataProcessingService;
  7. use GuzzleHttp\Client;
  8. use GuzzleHttp\Exception\GuzzleException;
  9. final class AeatService
  10. {
  11.     protected Client $client;
  12.     protected AeatResponseLogUtil $aeatResponseLogsUtil;
  13.     protected AeatRentDataProcessingService $aeatRentDataProcessingService;
  14.     public function __construct(
  15.         AeatResponseLogUtil $aeatResponseLogsUtil,
  16.         AeatRentDataProcessingService $aeatRentDataProcessingService
  17.     )
  18.     {
  19.         $this->client = new Client([
  20.             'base_uri' => $_ENV['AEAT_URI'],
  21.             'headers' => [
  22.                 'api-key' => $_ENV['AEAT_KEY'],
  23.             ]
  24.         ]);
  25.         $this->aeatResponseLogsUtil $aeatResponseLogsUtil;
  26.         $this->aeatRentDataProcessingService $aeatRentDataProcessingService;
  27.     }
  28.     public function esDomicilioRatificado(string $yearstring $nifCustomerAeat $customerAeat null): bool
  29.     {
  30.         $array = [
  31.             'year' => $year,
  32.             'nif' => $nif,
  33.         ];
  34.         try {
  35.             $response $this->client->get(sprintf('api/checkAddressStatus?%s'http_build_query($array)));
  36.             $statusCode $response->getStatusCode();
  37.             $parsed json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  38.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['ES_DOMICILIO'], 'cms/customer/{hash}/aeat/esDomicilioRatificado'$array$parsed$customerAeat$statusCodetrue);
  39.             return $parsed['isConfirmed'] ?? false;
  40.         } catch (\Exception $exception) {
  41.             if (isset($parsed$statusCode) && is_array($parsed)) {// @phpstan-ignore-line
  42.                 $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['ES_DOMICILIO'], 'cms/customer/{hash}/aeat/esDomicilioRatificado'$array$parsed$customerAeat$statusCodefalse);// @phpstan-ignore-line
  43.             }
  44.             throw new \RuntimeException($exception->getMessage());
  45.         }
  46.     }
  47.     /**
  48.      * @throws GuzzleException
  49.      * @throws \JsonException
  50.      */
  51.     public function ratificate(string $nifstring $referenceint $situationCustomerAeat $customerAeat null): array
  52.     {
  53.         $array = [
  54.             'nif' => $nif,
  55.             'reference' => $reference,
  56.             'situation' => "$situation",
  57.         ];
  58.         $response $this->client->post('api/ratificate', [
  59.             'json' => $array
  60.         ]);
  61.         $statusCode $response->getStatusCode();
  62.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  63.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['RATIFICATE'], 'cms/customer/{hash}/aeat/ratificate'$array$response$customerAeat$statusCode, !$response['error']);
  64.         return $response ?? [];
  65.     }
  66.     /**
  67.      * @throws GuzzleException
  68.      * @throws \JsonException
  69.      */
  70.     public function obtenerReferencia(string $nif, ?string $validity null, ?string $support null, ?float $check null, ?string $iban nullCustomerAeat $customerAeat null): array // @phpcs:ignore
  71.     {
  72.         $array = [
  73.             'nif' => $nif,
  74.             'validity' => $validity,
  75.             'support' => $support,
  76.             'check' => number_format($check2','''),
  77.             'iban' => $iban,
  78.         ];
  79.         $response $this->client->get(sprintf('api/reference?%s'http_build_query($array)));
  80.         $statusCode $response->getStatusCode();
  81.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  82.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['OBTENER_REFERENCIA'], 'cms/customer/{hash}/aeat/obtenerReferencia'$array$response$customerAeat$statusCode, !$response['error']);
  83.         return $response ?? [];
  84.     }
  85.     /**
  86.      * @throws GuzzleException
  87.      * @throws \JsonException
  88.      */
  89.     public function descargarDatosFiscales(string $nif, ?string $validity null, ?string $support null, ?float $check null, ?string $iban null, ?string $reference nullCustomerAeat $customerAeat): array // @phpcs:ignore
  90.     {
  91.         $array = [
  92.             'nif' => $nif,
  93.             'validity' => $validity,
  94.             'support' => $support,
  95.             'check' => $check number_format($check2',''') : null,
  96.             'iban' => $iban,
  97.             'reference' => $reference,
  98.         ];
  99.         $response $this->client->get(sprintf('api/data?%s'http_build_query($array)));
  100.         $statusCode $response->getStatusCode();
  101.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  102.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['DESCARGAR_DATOS'], 'cms/customer/{hash}/aeat/datosFiscales'$array$response$customerAeat$statusCode, !$response['error']);
  103.         return $response ?? [];
  104.     }
  105.     public function obtenerDatosFiscales(CustomerAeat $customerAeat)
  106.     {
  107.         $datos_fiscales $customerAeat->getDatosFiscales();
  108.         $datos_fiscales_unleashed explode("\r\n"$datos_fiscales);
  109.         $response = [];
  110.         $datos_procesados = [];
  111.         
  112.         foreach ($datos_fiscales_unleashed as $datos_para_procesado) {
  113.             $response[0] = array_merge($datos_procesados$this->aeatRentDataProcessingService->procesaTiposRegistro($datos_procesados$datos_para_procesado));
  114.         }
  115.         return $response;
  116.     }
  117. }