Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.43% |
29 / 70 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @file |
| 5 | * Fiscal action: generate unsigned fiscal authorization PDF for preview (review before signing). |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | require_once __DIR__.'/../../../../vendor/autoload.php'; |
| 11 | require_once __DIR__.'/Template.php'; |
| 12 | |
| 13 | use Dompdf\Dompdf; |
| 14 | |
| 15 | /** |
| 16 | * Action callable: build unsigned authorization PDF bytes. |
| 17 | * |
| 18 | * @param string $body Request body with JSON-encoded fields. |
| 19 | * @param array<string, mixed> $query Query params: device, session, place. |
| 20 | * @param \ConxHelper $conx Connection helper. |
| 21 | * @param \Psr\Log\LoggerInterface $logger Logger. |
| 22 | * @param \UppServices\SessionService $sessionService Session service. |
| 23 | * @return array{output: string, contentType: string, filename?: string} |
| 24 | */ |
| 25 | return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array { |
| 26 | $retval = \ApplicationError::Success; |
| 27 | $payload = []; |
| 28 | $place = null; |
| 29 | $pdfOutput = ''; |
| 30 | $filename = ''; |
| 31 | |
| 32 | if (success($retval)) { |
| 33 | $payload = json_decode($body, true); |
| 34 | if (!is_array($payload)) { |
| 35 | $retval = \ApplicationError::Parameters; |
| 36 | $logger->error("Invalid JSON payload for fiscal preview endpoint."); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | $device = $query['device'] ?? null; |
| 41 | if (success($retval) && !$device) { |
| 42 | $retval = \ApplicationError::Parameters; |
| 43 | $logger->error("No device provided on URL. Operation cancelled."); |
| 44 | } |
| 45 | |
| 46 | if (success($retval)) { |
| 47 | $session = $query['session'] ?? null; |
| 48 | if (!$session) { |
| 49 | $retval = \ApplicationError::Parameters; |
| 50 | $logger->error("No session provided on URL. Operation cancelled."); |
| 51 | } |
| 52 | else { |
| 53 | $userid = null; |
| 54 | $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (success($retval)) { |
| 59 | $place = $query['place'] ?? null; |
| 60 | if (!$place) { |
| 61 | $retval = \ApplicationError::Parameters; |
| 62 | $logger->error("Mandatory parameter 'place' not provided on URL. Operation cancelled."); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | $requiredFields = [ |
| 67 | 'authorizerName', 'authorizerTaxId', 'authorizerAddress', |
| 68 | 'authorizerEmail', 'authorizerPhone', |
| 69 | 'signatureCity', |
| 70 | ]; |
| 71 | if (success($retval)) { |
| 72 | foreach ($requiredFields as $field) { |
| 73 | $value = (string) ($payload[$field] ?? ''); |
| 74 | if (trim($value) === '') { |
| 75 | $retval = \ApplicationError::Parameters; |
| 76 | $logger->error("Mandatory field '".$field."' not provided on unsigned authorization payload."); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (success($retval)) { |
| 83 | $isLegalRepresentative = ((string) ($payload['isLegalRepresentative'] ?? '0')) === '1'; |
| 84 | if ($isLegalRepresentative) { |
| 85 | foreach (['legalRepresentativeName', 'legalRepresentativeTaxId'] as $field) { |
| 86 | $value = (string) ($payload[$field] ?? ''); |
| 87 | if (trim($value) === '') { |
| 88 | $retval = \ApplicationError::Parameters; |
| 89 | $logger->error("Missing legal representative field '".$field."' in unsigned authorization payload."); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (success($retval)) { |
| 97 | try { |
| 98 | $html = authorization_build_html($payload); |
| 99 | $dompdf = new Dompdf(); |
| 100 | $dompdf->loadHtml($html, 'UTF-8'); |
| 101 | $dompdf->setPaper('A4', 'portrait'); |
| 102 | $dompdf->render(); |
| 103 | $pdfOutput = $dompdf->output(); |
| 104 | $filename = str_pad((string) ((int) $place), 8, '0', STR_PAD_LEFT).'_autorizacion.pdf'; |
| 105 | } |
| 106 | catch (\Throwable $error) { |
| 107 | $retval = \ApplicationError::Unknown; |
| 108 | $logger->error("Could not generate unsigned authorization PDF: ".$error->getMessage()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!success($retval)) { |
| 113 | $result = json_encode(['errorcode' => $retval]); |
| 114 | return ['output' => $result, 'contentType' => 'application/json; charset=utf-8']; |
| 115 | } |
| 116 | |
| 117 | return [ |
| 118 | 'output' => $pdfOutput, |
| 119 | 'contentType' => 'application/pdf', |
| 120 | 'filename' => $filename |
| 121 | ]; |
| 122 | }; |