danielgp /
eFactura
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * Copyright (c) 2024 - 2025 Daniel Popiniuc. |
||
| 5 | * All rights reserved. This program and the accompanying materials |
||
| 6 | * are made available under the terms of the Eclipse Public License v1.0 |
||
| 7 | * which accompanies this distribution, and is available at |
||
| 8 | * http://www.eclipse.org/legal/epl-v10.html |
||
| 9 | * |
||
| 10 | * Contributors: |
||
| 11 | * Daniel Popiniuc |
||
| 12 | */ |
||
| 13 | |||
| 14 | namespace danielgp\efactura; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Configurable & Re-usable methods facilitating calls to known Romanian services for Electronic Invoice |
||
| 18 | * |
||
| 19 | * @author Daniel Popiniuc |
||
| 20 | */ |
||
| 21 | trait TraitBackEndRomania |
||
| 22 | { |
||
| 23 | use \danielgp\io_operations\InputOutputCurl; |
||
| 24 | use \danielgp\efactura\TraitBasic; |
||
| 25 | |||
| 26 | private array $arraystandardMesageFilters = [ |
||
| 27 | [ |
||
| 28 | 'Code' => 'E', |
||
| 29 | 'Detail' => 'raspuns cu erorile si semnatura MF, livrat in urma transmiterii unei facturi' |
||
| 30 | . ', daca NU este validata de sistem', |
||
| 31 | 'Value' => 'ERORI FACTURA', |
||
| 32 | ], |
||
| 33 | [ |
||
| 34 | 'Code' => 'P', |
||
| 35 | 'Detail' => 'factura primita in calitate de cunparator de la un emitent' |
||
| 36 | . ' care a folosit sistemul national' |
||
| 37 | . ' privind factura electronica RO e-factura', |
||
| 38 | 'Value' => 'FACTURA PRIMITA', |
||
| 39 | ], |
||
| 40 | [ |
||
| 41 | 'Code' => 'T', |
||
| 42 | 'Detail' => 'raspuns cu factura originala si semnatura MF' |
||
| 43 | . ', transmis in urma transmiterii unei facturi, daca este validata de sistem', |
||
| 44 | 'Value' => 'FACTURA TRIMISA', |
||
| 45 | ], |
||
| 46 | [ |
||
| 47 | 'Code' => 'R', |
||
| 48 | 'Detail' => 'mesaj de la cumparator catre emitentul facturii' |
||
| 49 | . ' (încărcate în sistem prin serviciul de upload ca RASP)', |
||
| 50 | 'Value' => 'MESAJ CUMPARATOR PRIMIT / MESAJ CUMPARATOR TRANSMIS', |
||
| 51 | ], |
||
| 52 | ]; |
||
| 53 | // below variable has to be initialized by class consuming this Trait |
||
| 54 | protected array $arraySolutionCustomSettings = [ |
||
| 55 | 'ArrayElectronicInvoiceSecrets' => [ |
||
| 56 | 'Client_ID' => '', |
||
| 57 | 'Client_Secret' => '', |
||
| 58 | 'Token' => '', |
||
| 59 | ], |
||
| 60 | 'ArrayFolders' => [ |
||
| 61 | 'Generated XML' => '', |
||
| 62 | 'Uploaded XML' => '', |
||
| 63 | 'Downloaded ZIP' => '', |
||
| 64 | ], |
||
| 65 | 'ArrayStrategyForUpload' => [ |
||
| 66 | 'B2B' => 'File base name end with|__b2b.xml', |
||
| 67 | 'B2C' => 'File base name end with|__b2c.xml', |
||
| 68 | 'B2G' => 'File base name end with|__b2g.xml', |
||
| 69 | ], |
||
| 70 | 'IntegerNumberOfDaysToCheckMessages' => 0, |
||
| 71 | 'IntegerTaxIdentificationNumber' => 0, |
||
| 72 | 'StringElectronicInvoiceEnvironment' => '', |
||
| 73 | 'StringElectronicInvoiceStandard' => 'UBL', |
||
| 74 | 'StringOptionalUploadParameters' => '', // could be one of the following: extern=DA | autofactura=DA | executare=DA |
||
| 75 | ]; |
||
| 76 | |||
| 77 | private function authorizeConnectionToLegalAuthority(): void |
||
| 78 | { |
||
| 79 | $this->checkPrerequisite([ |
||
| 80 | 'Empty Secrets Array' => $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets'], |
||
| 81 | ]); |
||
| 82 | $strRelevantUrl = $this->arraySettings['Infrastructure']['RO']['Servers']['Login'] |
||
| 83 | . $this->arraySettings['Infrastructure']['RO']['Calls']['Login']['Authorisation'] |
||
| 84 | . '?' . implode('&', [ |
||
| 85 | 'client_id=' . $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets']['Client_ID'], |
||
| 86 | 'client_secret=' . $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets']['Client_Secret'], |
||
| 87 | 'response_type=' . 'code', |
||
| 88 | 'redirect_uri=' . $this->arraySettings['Infrastructure']['RO']['Servers']['Redirect'] |
||
| 89 | . $this->arraySettings['Infrastructure']['RO']['Calls']['Redirect'], |
||
| 90 | ]); |
||
| 91 | header('Location: ' . $strRelevantUrl); |
||
| 92 | } |
||
| 93 | |||
| 94 | private function buildHeaderAsArray(): array |
||
| 95 | { |
||
| 96 | return [ |
||
| 97 | 'Authorization: Bearer ' . $this->arraySolutionCustomSettings['ArrayElectronicInvoiceSecrets']['Token'], |
||
| 98 | 'Content-Type: text/plain', |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | private function checkPrerequisite(array $arrayAssignedChecks): void |
||
| 103 | { |
||
| 104 | $arrayErrorMessages = []; |
||
| 105 | foreach ($arrayAssignedChecks as $strLabelCheck => $elementChecked) { |
||
| 106 | switch($strLabelCheck) { |
||
| 107 | case 'Empty Environment': |
||
| 108 | if ($elementChecked == '') { |
||
| 109 | $arrayErrorMessages[] = 'Environment is NOT allowed to be empty' |
||
| 110 | . ', please ensure proper environment from your custom class...'; |
||
| 111 | } |
||
| 112 | break; |
||
| 113 | case 'Empty Secrets Array': |
||
| 114 | if ($elementChecked == []) { |
||
| 115 | $arrayErrorMessages[] = 'Secrets array is NOT allowed to be empty' |
||
| 116 | . ', please ensure proper building from your custom class...'; |
||
| 117 | } |
||
| 118 | break; |
||
| 119 | case 'Message Filter Value': |
||
| 120 | $arrayAllowedValues = array_column($this->arraystandardMesageFilters, 'Value'); |
||
| 121 | if (($elementChecked !== '') && !in_array($elementChecked, $arrayAllowedValues)) { |
||
| 122 | $arrayErrorMessages[] = vsprintf('Message Filter provided has a value of %s that is not allowed' |
||
| 123 | . ' as can only be one of the following: %s' |
||
| 124 | . ', please ensure proper value is given from your custom class...', [ |
||
| 125 | $elementChecked, |
||
| 126 | '"' . implode('" or "', array_keys($arrayAllowedValues)) . '"', |
||
| 127 | ]); |
||
| 128 | } |
||
| 129 | break; |
||
| 130 | case 'Message Type Value': |
||
| 131 | $arrayAllowedValues = array_keys($this->arraySettings['Infrastructure']['RO']['Calls']['Content']['Message']); |
||
| 132 | if (!in_array($elementChecked, $arrayAllowedValues)) { |
||
| 133 | $arrayErrorMessages[] = vsprintf('Message Type provided has a value of %s that is not allowed' |
||
| 134 | . ' as can only be one of the following: %s' |
||
| 135 | . ', please ensure proper value is given from your custom class...', [ |
||
| 136 | $elementChecked, |
||
| 137 | '"' . implode('" or "', array_keys($arrayAllowedValues)) . '"', |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | break; |
||
| 141 | case 'Non-Standard Environment Value': |
||
| 142 | $arrayAllowedValues = ['prod', 'test']; |
||
| 143 | if (($elementChecked !== '') && !in_array($elementChecked, $arrayAllowedValues)) { |
||
| 144 | $arrayErrorMessages[] = vsprintf('Environment provided has a value of %s that is not allowed' |
||
| 145 | . ' as can only be one of the following: %s' |
||
| 146 | . ', please ensure proper value is given from your custom class...', [ |
||
| 147 | $elementChecked, |
||
| 148 | '"' . implode('" or "', $arrayAllowedValues) . '"', |
||
| 149 | ]); |
||
| 150 | } |
||
| 151 | break; |
||
| 152 | case 'Zero Value': |
||
| 153 | if ($elementChecked === 0) { |
||
| 154 | $arrayErrorMessages[] = 'Tax Identification Number is not allowed to be 0' |
||
| 155 | . ', please ensure you pass proper value from your custom class...'; |
||
| 156 | } |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | if ($arrayErrorMessages != []) { |
||
| 161 | error_log(implode(PHP_EOL, $arrayErrorMessages)); |
||
| 162 | throw new \RuntimeException(implode(PHP_EOL, $arrayErrorMessages)); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | private function decideRelevantUniformResourceLocatorForUpload(string $strFileName): string |
||
| 167 | { |
||
| 168 | $strLabel = $this->setLabelBasedOnRule($strFileName); |
||
| 169 | error_log(sprintf('For given file %s following Label %s has been established!', $strFileName, $strLabel)); |
||
| 170 | $strUrlForUpload = $this->arraySettings['Infrastructure']['RO']['Calls']['Content']['Upload'][$strLabel]; |
||
| 171 | return vsprintf($strUrlForUpload, [ |
||
| 172 | $this->arraySolutionCustomSettings['StringElectronicInvoiceStandard'], |
||
| 173 | $this->arraySolutionCustomSettings['IntegerTaxIdentificationNumber'], |
||
| 174 | $this->arraySolutionCustomSettings['StringOptionalUploadParameters'], |
||
| 175 | ]); |
||
| 176 | } |
||
| 177 | |||
| 178 | private function exposeFeedbackOnFileProcessed(int $intFileFoundNo, int $intFileProcessed): void |
||
| 179 | { |
||
| 180 | if ($intFileFoundNo === 0) { |
||
| 181 | error_log('NO files were found to be processed.'); |
||
| 182 | } elseif ($intFileFoundNo == $intFileProcessed) { |
||
| 183 | error_log(sprintf('All %u files found were processed successfully!', $intFileFoundNo)); |
||
| 184 | } else { |
||
| 185 | error_log(vsprintf('Out of %u files found only %u were processed successfully, hence %u were missed.' |
||
| 186 | . ' Check your source folder where residual files are still there.', [ |
||
| 187 | $intFileFoundNo, |
||
| 188 | $intFileProcessed, |
||
| 189 | ($intFileFoundNo - $intFileProcessed), |
||
| 190 | ])); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Work In Progress =================> UNIFNISHED !!! |
||
| 196 | * |
||
| 197 | * @param string $strType |
||
| 198 | * @param array $arrayParameters |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | protected function getElectronicInvoiceMessages(string $strType, array $arrayParameters): void |
||
| 202 | { |
||
| 203 | $this->getServerDiscutionValidations([ |
||
| 204 | 'Message Type Value', $strType, |
||
| 205 | 'Message Parameters' => $arrayParameters, |
||
| 206 | ]); |
||
| 207 | $this->getServerMessageParametersValidation($strType, $arrayParameters); |
||
| 208 | $this->authorizeConnectionToLegalAuthority(); |
||
| 209 | $strContentBranch = ''; |
||
| 210 | $arrayFinalParameterValues = []; |
||
| 211 | $strFilter = ''; |
||
| 212 | switch($strType) { |
||
| 213 | case 'ListAll': |
||
| 214 | $strContentBranch = 'ListAllMessages'; |
||
| 215 | $arrayFinalParameterValues[] = $arrayParameters['Days']; |
||
| 216 | $arrayFinalParameterValues[] = $this->arraySolutionCustomSettings['IntegerTaxIdentificationNumber']; |
||
| 217 | $arrayFinalParameterValues[] = $this->getMessageFilterCode($strFilter); |
||
| 218 | break; |
||
| 219 | case 'ListSinglePage': |
||
| 220 | $strContentBranch = 'ListMessagesPaged'; |
||
| 221 | $arrayFinalParameterValues[] = $arrayParameters['StartTime']; |
||
| 222 | $arrayFinalParameterValues[] = $arrayParameters['EndTime']; |
||
| 223 | $arrayFinalParameterValues[] = $arrayParameters['Page']; |
||
| 224 | $arrayFinalParameterValues[] = $this->getMessageFilterCode($strFilter); |
||
| 225 | break; |
||
| 226 | case 'Single': |
||
| 227 | $strContentBranch = 'SingleMessageStatus'; |
||
| 228 | $arrayFinalParameterValues[] = $arrayParameters['LoadingId']; |
||
| 229 | break; |
||
| 230 | } |
||
| 231 | $strRelevantUrl = $this->arraySettings['Infrastructure']['RO']['Servers']['Content']['OAuth2'] |
||
| 232 | . $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'] |
||
| 233 | . vsprintf($this->arraySettings['Infrastructure']['RO']['Calls']['Content'][$strContentBranch], $arrayFinalParameterValues); |
||
| 234 | error_log(vsprintf('Relevant URL for Message reading of type %s is %s', [ |
||
| 235 | $strType, |
||
| 236 | $strRelevantUrl, |
||
| 237 | ])); |
||
| 238 | // sent XML content using CURL and capture feedback |
||
| 239 | $arrayFeedback = $this->getContentFromUrlThroughCurl($strRelevantUrl, [ |
||
| 240 | 'HttpHeader' => $this->buildHeaderAsArray(), |
||
| 241 | ]); |
||
| 242 | if ($arrayFeedback['errNo'] === 0) { |
||
| 243 | if (json_validate($arrayFeedback['response'])) { |
||
| 244 | $arrayFeedback['response'] = json_decode($arrayFeedback['response'], true, 512, \JSON_OBJECT_AS_ARRAY); |
||
| 245 | $intMessageNo = 0; |
||
| 246 | error_log('Response = ' . json_encode($arrayFeedback['response'])); |
||
| 247 | if (array_key_exists('eroare', $arrayFeedback['response'])) { |
||
| 248 | error_log('Error response = ' . $arrayFeedback['response']['eroare']); |
||
| 249 | } else { |
||
| 250 | foreach ($arrayFeedback['response']['mesaje'] as $arrayMessageDetails) { |
||
| 251 | $strTargetFile = $this->arraySolutionCustomSettings['ArrayFolders']['Downloaded ZIP'] |
||
| 252 | . strval($arrayMessageDetails['id']) . '.zip'; |
||
| 253 | if (!file_exists($strTargetFile)) { |
||
| 254 | $this->getElectronicInvoiceResponseFile($arrayMessageDetails['id'], $strTargetFile); |
||
| 255 | } |
||
| 256 | error_log('Current message is ' . json_encode($arrayMessageDetails)); |
||
| 257 | $intMessageNo++; |
||
| 258 | } |
||
| 259 | error_log(sprintf('%u messages were processed!', strval($intMessageNo))); |
||
| 260 | } |
||
| 261 | } else { |
||
| 262 | error_log('Response is expected to be a JSON but is not...'); |
||
| 263 | error_log(json_encode($arrayFeedback)); |
||
| 264 | } |
||
| 265 | } else { |
||
| 266 | error_log($arrayFeedback['errNo'] . ' => ' . $arrayFeedback['errMsg']); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | protected function getElectronicInvoiceResponseFile(int $intResponseId, string $strTargetFile): void |
||
| 271 | { |
||
| 272 | error_log(sprintf('Will download the response ZIP file having ID = %s', $intResponseId)); |
||
| 273 | $strRelevantUrl = vsprintf($this->arraySettings['Infrastructure']['RO']['Servers']['Content']['OAuth2'] |
||
| 274 | . $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'] |
||
| 275 | . $this->arraySettings['Infrastructure']['RO']['Calls']['Content']['Download'], [ |
||
| 276 | $intResponseId, |
||
| 277 | ]); |
||
| 278 | error_log(vsprintf('Relevant URL for Downloading Response ZIP file havign ID = %s is %s', [ |
||
| 279 | strval($intResponseId), |
||
| 280 | $strRelevantUrl, |
||
| 281 | ])); |
||
| 282 | // sent XML content using CURL and capture feedback |
||
| 283 | $datafile = $this->getContentFromUrlThroughCurl($strRelevantUrl, [ |
||
| 284 | 'HttpHeader' => $this->buildHeaderAsArray(), |
||
| 285 | ]); |
||
| 286 | $ffileHandler = fopen($strTargetFile, 'w'); |
||
| 287 | fwrite($ffileHandler, $datafile['response']); |
||
| 288 | fclose($ffileHandler); |
||
| 289 | if (file_exists($strTargetFile)) { |
||
| 290 | error_log(vsprintf('Response ZIP file having ID = %s was sucessfully saved to %s', [ |
||
| 291 | strval($intResponseId), |
||
| 292 | $strTargetFile, |
||
| 293 | ])); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | private function getMessageFilterCode(string $strFilter): string |
||
| 298 | { |
||
| 299 | $strReturn = ''; |
||
| 300 | if ($strFilter != '') { |
||
| 301 | $strReturn = '&filtru=' . array_column($this->arraystandardMesageFilters, 'Code', 'Value')[$strFilter]; |
||
| 302 | } |
||
| 303 | return $strReturn; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * implementing strong validations to avoid issues later in the logic |
||
| 308 | * |
||
| 309 | * @param array $arrayAdditionalValidations |
||
| 310 | */ |
||
| 311 | private function getServerDiscutionValidations(array $arrayAdditionalValidations = []): void |
||
| 312 | { |
||
| 313 | $arrayUniversalValidations = [ |
||
| 314 | 'Zero Value' => $this->arraySolutionCustomSettings['IntegerTaxIdentificationNumber'], |
||
| 315 | 'Empty Environment' => $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'], |
||
| 316 | 'Non-Standard Environment Value' => $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'], |
||
| 317 | ]; |
||
| 318 | $arrayValidations = $arrayUniversalValidations; |
||
| 319 | if ($arrayAdditionalValidations != []) { |
||
| 320 | $arrayValidations = array_merge($arrayValidations, $arrayAdditionalValidations); |
||
| 321 | } |
||
| 322 | $this->checkPrerequisite($arrayValidations); |
||
| 323 | } |
||
| 324 | |||
| 325 | private function getServerMessageParametersValidation(string $strType, array $arrayParameters): void |
||
| 326 | { |
||
| 327 | $arrayAllowedParameters = match ($strType) { |
||
| 328 | 'ListAll' => [ |
||
| 329 | 'Mandatory' => ['Days'], |
||
| 330 | 'Optional' => ['Filter'], |
||
| 331 | ], |
||
| 332 | 'ListSinglePage' => [ |
||
| 333 | 'Mandatory' => ['StartTime', 'EndTime', 'Page'], |
||
| 334 | 'Optional' => ['Filter'], |
||
| 335 | ], |
||
| 336 | 'Single' => [ |
||
| 337 | 'Mandatory' => ['LoadingId'], |
||
| 338 | ], |
||
| 339 | }; |
||
| 340 | $arrayAllowedCombined = $arrayAllowedParameters['Mandatory']; |
||
| 341 | if (array_key_exists('Optional', $arrayAllowedParameters)) { |
||
| 342 | $arrayAllowedCombined = array_merge($arrayAllowedParameters['Mandatory'], $arrayAllowedParameters['Optional']); |
||
| 343 | } |
||
| 344 | $arrayGivenKeys = array_keys($arrayParameters); |
||
| 345 | $arrayErrors = []; |
||
| 346 | if (array_diff($arrayAllowedParameters['Mandatory'], $arrayGivenKeys) != []) { |
||
| 347 | $arrayErrors[] = vsprintf('Provided parameters %s does contain all mandatory ones: %s...', [ |
||
| 348 | json_encode($arrayGivenKeys), |
||
| 349 | json_encode($arrayAllowedParameters['Mandatory']), |
||
| 350 | ]); |
||
| 351 | } elseif (($arrayGivenKeys != $arrayAllowedParameters['Mandatory']) && (array_diff($arrayAllowedCombined, $arrayGivenKeys) != [])) { |
||
| 352 | $arrayErrors[] = vsprintf('Provided parameters %s does contain all mandatory & optional ones: %s...', [ |
||
| 353 | json_encode($arrayGivenKeys), |
||
| 354 | json_encode($arrayAllowedCombined), |
||
| 355 | ]); |
||
| 356 | } else { |
||
| 357 | // here we have to validate actual value passed as parameters |
||
| 358 | foreach ($arrayParameters as $strParameterKey => $strParameterValue) { |
||
| 359 | switch($strParameterKey) { |
||
| 360 | case 'Days': |
||
| 361 | case 'LoadingId': |
||
| 362 | case 'Page': |
||
| 363 | $arrayRangeAllowedPieces = explode('-', match ($strParameterKey) { |
||
| 364 | 'Days' => '1-60', |
||
| 365 | 'LoadingId' => '1-' . strval(9 * pow(10, 10)), |
||
| 366 | 'Page' => '0-' . strval(9 * pow(10, 6)), |
||
| 367 | }); |
||
| 368 | $regs = null; |
||
| 369 | preg_match('/[0-9]{1,20}/', $strParameterValue, $regs, PREG_UNMATCHED_AS_NULL); |
||
| 370 | if (is_array($regs) && ($regs !== []) && ($regs[0] != $strParameterValue)) { |
||
| 371 | $arrayErrors[] = vsprintf('Parameter "%s" is expected to be of integer type' |
||
| 372 | . ' but something else is given "%s"...', [ |
||
| 373 | $strParameterKey, |
||
| 374 | json_encode($strParameterValue), |
||
| 375 | ]); |
||
| 376 | } elseif (($strParameterValue < $arrayRangeAllowedPieces[0]) || ($strParameterValue > $arrayRangeAllowedPieces[1])) { |
||
| 377 | $arrayErrors[] = vsprintf('Parameter "%s" is an integer value' |
||
| 378 | . ' and within range between 1 and 60 but %s is given...', [ |
||
| 379 | $strParameterKey, |
||
| 380 | $strParameterValue, |
||
| 381 | ]); |
||
| 382 | } |
||
| 383 | break; |
||
| 384 | case 'Filter': |
||
| 385 | $arrayAllowedValues = array_column($this->arraystandardMesageFilters, 'Value'); |
||
| 386 | if (($strParameterValue !== '') && !in_array($strParameterValue, $arrayAllowedValues)) { |
||
| 387 | $arrayErrors[] = vsprintf('Message Filter provided has a value of %s' |
||
| 388 | . ' that is not allowed' |
||
| 389 | . ' as can only be one of the following: %s' |
||
| 390 | . ', please ensure proper value is given from your custom class...', [ |
||
| 391 | $strParameterValue, |
||
| 392 | '"' . implode('" or "', array_keys($arrayAllowedValues)) . '"', |
||
| 393 | ]); |
||
| 394 | } |
||
| 395 | break; |
||
| 396 | case 'StartTime': |
||
| 397 | case 'EndTime': |
||
| 398 | $arrayRangeAllowedPieces = explode('|', strval((\DateTime::createFromFormat('Y-m-d G:i:s', '2021-01-01 00:00:00', new \DateTimeZone('UTC')))->format('U') * 1000) |
||
| 399 | . '|' . strval((new \DateTime('now', new \DateTimeZone('UTC')))->format('U') * 1000)); |
||
| 400 | $arrayRangeAllowedPiecesForHumans = explode('|', (\DateTime::createFromFormat('U', intval(($arrayRangeAllowedPieces[0] / 1000)), new \DateTimeZone('UTC')))->format('Y-m-d H:i:s') |
||
| 401 | . '|' . (\DateTime::createFromFormat('U', intval(($arrayRangeAllowedPieces[1] / 1000)), new \DateTimeZone('UTC')))->format('Y-m-d H:i:s')); |
||
| 402 | $arrayErrors = []; |
||
| 403 | if (is_integer($strParameterValue) && (($strParameterValue < $arrayRangeAllowedPieces[0]) || ($strParameterValue > $arrayRangeAllowedPieces[1]))) { |
||
| 404 | $arrayErrors[] = vsprintf('Parameter "%s" is given as an integer value %s (which translate in %s) ' |
||
| 405 | . ' but that is NOT within allowed range, which is between %s and %s...', [ |
||
| 406 | $strParameterKey, |
||
| 407 | $strParameterValue, |
||
| 408 | (\DateTime::createFromFormat('U', intval(($strParameterValue / 1000)), new \DateTimeZone('UTC')))->format('Y-m-d H:i:s'), |
||
| 409 | $arrayRangeAllowedPiecesForHumans[0], |
||
| 410 | $arrayRangeAllowedPiecesForHumans[1], |
||
| 411 | ]); |
||
| 412 | } elseif (is_string($strParameterValue) && (strlen($strParameterValue) == 19)) { |
||
| 413 | $regs = null; |
||
| 414 | preg_match('/(1|2)[0-9]{3}\-((01|03|05|07|08|10|12)\-(0{1}[1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1})|(04|06|09|11)\-(0{1}[1-9]{1}|[1-2]{1}[0-9]{1}|30)|02\-[0-1-2]{1}[0-9]{1})\s(0[0-9]{1}|1[0-9]{1}|2[0-3]{1})\:[0-5]{1}[0-9]{1}\:[0-5]{1}[0-9]{1}/m', $strParameterValue, $regs, PREG_UNMATCHED_AS_NULL); |
||
| 415 | if (is_array($regs) && ($regs !== []) && ($regs[0] == $strParameterValue)) { |
||
| 416 | $intValueToCompare = (\DateTime::createFromFormat('Y-m-d G:i:s', $strParameterValue, new \DateTimeZone('UTC')))->format('U') * 1000; |
||
| 417 | if (($intValueToCompare < $arrayRangeAllowedPieces[0]) || ($intValueToCompare > $arrayRangeAllowedPieces[1])) { |
||
| 418 | $arrayErrors[] = vsprintf('Parameter "%s" is given as a TimeStamp value of %s ' |
||
| 419 | . ' but that is NOT within allowed range, which is between %s and %s...', [ |
||
| 420 | $strParameterKey, |
||
| 421 | $strParameterValue, |
||
| 422 | $arrayRangeAllowedPiecesForHumans[0], |
||
| 423 | $arrayRangeAllowedPiecesForHumans[1], |
||
| 424 | ]); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | } |
||
| 428 | break; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 | if ($arrayErrors != []) { |
||
| 433 | error_log(implode(PHP_EOL, $arrayErrors)); |
||
| 434 | throw new \RuntimeException(implode(PHP_EOL, $arrayErrors)); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | private function setLabelBasedOnRule(string $strFileName): string |
||
| 439 | { |
||
| 440 | $arrayKnownLabels = ['B2B', 'B2C', 'B2G']; |
||
| 441 | $arrayKnownRules = [ |
||
| 442 | 'File base name end with', |
||
| 443 | 'File base name starts with', |
||
| 444 | ]; |
||
| 445 | $strLabel = ''; |
||
| 446 | foreach ($arrayKnownLabels as $strCurrentLabel) { |
||
| 447 | $arrayPieces = explode('|', $this->arraySolutionCustomSettings['ArrayStrategyForUpload'][$strCurrentLabel]); |
||
| 448 | switch($arrayPieces[0]) { |
||
| 449 | case 'File base name end with': |
||
| 450 | if (str_ends_with($strFileName, $arrayPieces[1])) { |
||
| 451 | $strLabel = $strCurrentLabel; |
||
| 452 | } |
||
| 453 | break; |
||
| 454 | case 'File base name starts with': |
||
| 455 | if (str_starts_with($strFileName, $arrayPieces[1])) { |
||
| 456 | $strLabel = $strCurrentLabel; |
||
| 457 | } |
||
| 458 | break; |
||
| 459 | default: |
||
| 460 | throw \RuntimeException('Unknown/missconfigured rule given' |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 461 | . ', please ensure only proper rules (' |
||
| 462 | . 'that is one of these values "' . implode('", "', $arrayKnownRules) . '"' |
||
| 463 | . ') are configured in your custom class...'); |
||
| 464 | break; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | return $strLabel; |
||
| 468 | } |
||
| 469 | |||
| 470 | protected function uploadElectronicInvoicesFromFolder(): void |
||
| 471 | { |
||
| 472 | $this->getServerDiscutionValidations(); |
||
| 473 | $arrayHttpHeader = $this->buildHeaderAsArray(); |
||
| 474 | $intFileFoundNo = 0; |
||
| 475 | $intFileProcessed = 0; |
||
| 476 | $arrayFiles = new \RecursiveDirectoryIterator($this->arraySolutionCustomSettings['ArrayFolders']['Generated XML'], \FilesystemIterator::SKIP_DOTS); |
||
| 477 | foreach ($arrayFiles as $strCrtFile) { |
||
| 478 | if ($strCrtFile->isFile()) { // only Files are relevant for processing |
||
| 479 | if ($intFileFoundNo === 0) { // authorization is only needed for 1st file |
||
| 480 | $this->authorizeConnectionToLegalAuthority(); |
||
| 481 | } |
||
| 482 | $strRawUrl = $this->decideRelevantUniformResourceLocatorForUpload($strCrtFile->getRealPath()); |
||
| 483 | $strRelevantUrl = $this->arraySettings['Infrastructure']['RO']['Servers']['Content']['OAuth2'] |
||
| 484 | . $this->arraySolutionCustomSettings['StringElectronicInvoiceEnvironment'] |
||
| 485 | . $strRawUrl; |
||
| 486 | error_log(vsprintf('I will be uploading content of %s file to %s url', [ |
||
| 487 | $strCrtFile->getRealPath(), |
||
| 488 | $strRelevantUrl, |
||
| 489 | ])); |
||
| 490 | $xmlCurrentFileContent = $this->getFileJsonContent($strCrtFile->getPath(), $strCrtFile->getRealPath()); |
||
| 491 | // sent XML content using CURL and capture feedback |
||
| 492 | $arrayFeedback = $this->getContentFromUrlThroughCurl($strRelevantUrl, [ |
||
| 493 | 'HttpHeader' => $arrayHttpHeader, |
||
| 494 | 'PostFields' => $xmlCurrentFileContent, |
||
| 495 | ]); |
||
| 496 | error_log(json_encode($arrayFeedback)); |
||
| 497 | if ($arrayFeedback['errNo'] === 0) { |
||
| 498 | $arrayResponse = json_decode(json_encode(simplexml_load_string($arrayFeedback['response'])), true, 512, \JSON_OBJECT_AS_ARRAY); |
||
| 499 | error_log('Complete content of response is: ' . json_encode($arrayResponse)); |
||
| 500 | error_log(vsprintf('File %s has been sucessfully loaded with %u index', [ |
||
| 501 | $strCrtFile->getRealPath(), |
||
| 502 | $arrayResponse['@attributes']['index_incarcare'], |
||
| 503 | ])); |
||
| 504 | // if response has no error move the file to processed folder |
||
| 505 | rename($strCrtFile->getRealPath(), $this->arraySolutionCustomSettings['ArrayFolders']['Uploaded XML'] |
||
| 506 | . $strCrtFile->getBasename()); |
||
| 507 | $intFileProcessed++; |
||
| 508 | } |
||
| 509 | $intFileFoundNo++; |
||
| 510 | } |
||
| 511 | } |
||
| 512 | $this->exposeFeedbackOnFileProcessed($intFileFoundNo, $intFileProcessed); |
||
| 513 | } |
||
| 514 | } |
||
| 515 |