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\Entity\Taxation;
  6. use App\Util\AeatResponseLogUtil;
  7. use App\Service\Aeat\AeatRentDataProcessingService;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. final class AeatService
  11. {
  12.     protected Client $client;
  13.     protected AeatResponseLogUtil $aeatResponseLogsUtil;
  14.     protected AeatRentDataProcessingService $aeatRentDataProcessingService;
  15.     public function __construct(
  16.         AeatResponseLogUtil $aeatResponseLogsUtil,
  17.         AeatRentDataProcessingService $aeatRentDataProcessingService
  18.     )
  19.     {
  20.         $this->client = new Client([
  21.             'base_uri' => $_ENV['AEAT_URI'],
  22.             'headers' => [
  23.                 'api-key' => $_ENV['AEAT_KEY'],
  24.             ]
  25.         ]);
  26.         $this->aeatResponseLogsUtil $aeatResponseLogsUtil;
  27.         $this->aeatRentDataProcessingService $aeatRentDataProcessingService;
  28.     }
  29.     public function esDomicilioRatificado(string $yearstring $nifCustomerAeat $customerAeat null): bool
  30.     {
  31.         $array = [
  32.             'year' => $year,
  33.             'nif' => $nif,
  34.         ];
  35.         try {
  36.             $response $this->client->get(sprintf('api/checkAddressStatus?%s'http_build_query($array)));
  37.             $statusCode $response->getStatusCode();
  38.             $parsed json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  39.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['ES_DOMICILIO'], 'cms/customer/{hash}/aeat/esDomicilioRatificado'$array$parsed$customerAeat$statusCodetrue);
  40.             return $parsed['isConfirmed'] ?? false;
  41.         } catch (\Exception $exception) {
  42.             if (isset($parsed$statusCode) && is_array($parsed)) {// @phpstan-ignore-line
  43.                 $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['ES_DOMICILIO'], 'cms/customer/{hash}/aeat/esDomicilioRatificado'$array$parsed$customerAeat$statusCodefalse);// @phpstan-ignore-line
  44.             }
  45.             throw new \RuntimeException($exception->getMessage());
  46.         }
  47.     }
  48.     /**
  49.      * @throws GuzzleException
  50.      * @throws \JsonException
  51.      */
  52.     public function ratificate(string $nifstring $referenceint $situationCustomerAeat $customerAeat null): array
  53.     {
  54.         $array = [
  55.             'nif' => $nif,
  56.             'reference' => $reference,
  57.             'situation' => "$situation",
  58.         ];
  59.         $response $this->client->post('api/ratificate', [
  60.             'json' => $array
  61.         ]);
  62.         $statusCode $response->getStatusCode();
  63.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  64.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['RATIFICATE'], 'cms/customer/{hash}/aeat/ratificate'$array$response$customerAeat$statusCode, !$response['error']);
  65.         return $response ?? [];
  66.     }
  67.     /**
  68.      * @throws GuzzleException
  69.      * @throws \JsonException
  70.      */
  71.     public function obtenerReferencia(string $nif, ?string $validity null, ?string $support null, ?float $check null, ?string $iban nullCustomerAeat $customerAeat null): array // @phpcs:ignore
  72.     {
  73.         $array = [
  74.             'nif' => $nif,
  75.             'validity' => $validity,
  76.             'support' => $support,
  77.             'check' => number_format($check2','''),
  78.             'iban' => $iban,
  79.         ];
  80.         $response $this->client->get(sprintf('api/reference?%s'http_build_query($array)));
  81.         $statusCode $response->getStatusCode();
  82.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  83.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['OBTENER_REFERENCIA'], 'cms/customer/{hash}/aeat/obtenerReferencia'$array$response$customerAeat$statusCode, !$response['error']);
  84.         return $response ?? [];
  85.     }
  86.     /**
  87.      * @throws GuzzleException
  88.      * @throws \JsonException
  89.      */
  90.     public function descargarDatosFiscales(string $nif, ?string $validity null, ?string $support null, ?float $check null, ?string $iban null, ?string $reference nullCustomerAeat $customerAeat): array // @phpcs:ignore
  91.     {
  92.         $array = [
  93.             'nif' => $nif,
  94.             'validity' => $validity,
  95.             'support' => $support,
  96.             'check' => $check number_format($check2',''') : null,
  97.             'iban' => $iban,
  98.             'reference' => $reference,
  99.         ];
  100.         $response $this->client->get(sprintf('api/data?%s'http_build_query($array)));
  101.         $statusCode $response->getStatusCode();
  102.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  103.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['DESCARGAR_DATOS'], 'cms/customer/{hash}/aeat/datosFiscales'$array$response$customerAeat$statusCode, !$response['error']);
  104.         return $response ?? [];
  105.     }
  106.     /**
  107.      * @throws GuzzleException
  108.      * @throws \JsonException
  109.      */
  110.     public function validarImpuesto(string $xmlTaxation $taxation, ?array $data): array // @phpcs:ignore
  111.     {
  112.         if (str_starts_with($taxation->getService()->getAlias(), "P005")) {
  113.             $model '100';
  114.         }
  115.         if (str_starts_with($taxation->getService()->getAlias(), "MOD210")) {
  116.             $model '210';
  117.         }
  118.         $array = [
  119.             'xml' => $xml,
  120.             'year' => $taxation->getServiceYear(),
  121.             'model' => $model ?? null
  122.         ];
  123.         $response $this->client->post(sprintf('api/aeat-draft'), [
  124.             'json' => $array
  125.         ]);
  126.         $statusCode $response->getStatusCode();
  127.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  128.         if (isset($data['origin']) && $data['origin'] == 'API') {
  129.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['VALIDAR-IMPUESTO'], 'api/taxations/{hash}/validate'$array$responsenull$statusCode, !$response['error'], $taxation);
  130.         } else {
  131.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['VALIDAR-IMPUESTO'], 'cms/taxations/{hash}/validate'$array$responsenull$statusCode, !$response['error'], $taxation);
  132.         }
  133.         return $response ?? [];
  134.     }
  135.     /**
  136.      * @throws GuzzleException
  137.      * @throws \JsonException
  138.      */
  139.     public function presentarImpuesto(string $xmlTaxation $taxation, ?array $data): array // @phpcs:ignore
  140.     {
  141.         if (str_starts_with($taxation->getService()->getAlias(), "P005")) {
  142.             $model '100';
  143.         }
  144.         if (str_starts_with($taxation->getService()->getAlias(), "MOD210")) {
  145.             $model '210';
  146.         }
  147.         $array = [
  148.             'xml' => $xml,
  149.             'year' => $taxation->getServiceYear(),
  150.             'model' => $model ?? null
  151.         ];
  152.         if ($taxation->getNRC() !== null) {
  153.             $array['nrc'] = $taxation->getNRC();
  154.         }
  155.         $response $this->client->post(sprintf('api/aeat-final'), [
  156.             'json' => $array
  157.         ]);
  158.         $statusCode $response->getStatusCode();
  159.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  160.         if (isset($data['origin']) && $data['origin'] == 'API') {
  161.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['PRESENTAR-IMPUESTO'], 'api/taxations/{hash}/present'$array$responsenull$statusCode, !$response['error'], $taxation);
  162.         } else {
  163.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['PRESENTAR-IMPUESTO'], 'cms/taxations/{hash}/present'$array$responsenull$statusCode, !$response['error'], $taxation);
  164.         }
  165.         return $response ?? [];
  166.     }
  167.     /**
  168.      * @throws GuzzleException
  169.      * @throws \JsonException
  170.      */
  171.     public function obtenerNRC(Taxation $taxation, ?array $data): array // @phpcs:ignore
  172.     {
  173.         if (str_starts_with($taxation->getService()->getAlias(), "P005")) {
  174.             $model '100';
  175.         }
  176.         if (str_starts_with($taxation->getService()->getAlias(), "MOD210")) {
  177.             $model '210';
  178.             $owner $taxation->getTaxationPropertyOwner()->getOwner();
  179.             $document_number $owner->getDocumentNumber();
  180.             $full_name $owner->getSurname() . ' ' $owner->getName();
  181.         }
  182.         $array = [
  183.             'year' => $taxation->getServiceYear(),
  184.             'model' => $model ?? null,
  185.             'period' => $taxation->getPeriod() ?? null,
  186.             'nif' => $document_number ?? null,
  187.             'full_name' => $full_name ?? null,
  188.             'amount' => $taxation->getResult(),
  189.             'iban' => $taxation->getBankAccount()
  190.         ];
  191.         $response $this->client->post(sprintf('api/aeat-get-nrc'), [
  192.             'json' => $array
  193.         ]);
  194.         $statusCode $response->getStatusCode();
  195.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  196.         if (isset($data['origin']) && $data['origin'] == 'API') {
  197.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['PRESENTAR-IMPUESTO'], 'api/taxations/{hash}/get-nrc'$array$responsenull$statusCode, !$response['error'], $taxation);
  198.         } else {
  199.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['PRESENTAR-IMPUESTO'], 'cms/taxations/{hash}/get-nrc'$array$responsenull$statusCode, !$response['error'], $taxation);
  200.         }
  201.         return $response ?? [];
  202.     }
  203.     public function obtenerDatosFiscales(CustomerAeat $customerAeat)
  204.     {
  205.         $datos_fiscales $customerAeat->getDatosFiscales();
  206.         $datos_fiscales_unleashed explode("\r\n"$datos_fiscales);
  207.         $response = [];
  208.         $datos_procesados = [];
  209.         
  210.         foreach ($datos_fiscales_unleashed as $datos_para_procesado) {
  211.             $response[0] = array_merge($datos_procesados$this->aeatRentDataProcessingService->procesaTiposRegistro($datos_procesados$datos_para_procesado));
  212.         }
  213.         return $response;
  214.     }
  215. }