You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.8 KiB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Camt\Conversion;
  4. use App\Exceptions\ImporterErrorException;
  5. use App\Services\Camt\Transaction;
  6. use App\Services\Shared\Configuration\Configuration;
  7. use App\Services\Shared\Conversion\ProgressInformation;
  8. use Illuminate\Support\Facades\Log;
  9. class TransactionConverter
  10. {
  11. use ProgressInformation;
  12. public function __construct(private Configuration $configuration)
  13. {
  14. Log::debug('Constructed TransactionConverter.');
  15. }
  16. /**
  17. * @throws ImporterErrorException
  18. */
  19. public function convert(array $transactions): array
  20. {
  21. $total = count($transactions);
  22. Log::debug(sprintf('Convert all %d transactions into pseudo-transactions.', $total));
  23. $result = [];
  24. /** @var Transaction $transaction */
  25. foreach ($transactions as $index => $transaction) {
  26. Log::debug(sprintf('[%d/%d] Now working on transaction.', $index + 1, $total));
  27. $result[] = $this->convertSingle($transaction);
  28. Log::debug(sprintf('[%d/%d] Now done with transaction.', $index + 1, $total));
  29. }
  30. Log::debug(sprintf('Done converting all %d transactions into pseudo-transactions.', $total));
  31. return $result;
  32. }
  33. /**
  34. * @throws ImporterErrorException
  35. */
  36. private function convertSingle(Transaction $transaction): array
  37. {
  38. Log::debug('Convert single transaction into pseudo-transaction.');
  39. $result = [
  40. 'transactions' => [],
  41. ];
  42. $configuredRoles = $this->getConfiguredRoles();
  43. $mapping = $this->configuration->getMapping();
  44. $allRoles = $this->configuration->getRoles();
  45. $count = $transaction->countSplits();
  46. $count = 0 === $count ? 1 : $count; // add at least one transaction inside the Transaction.
  47. $fieldNames = array_keys(config('camt.fields'));
  48. $result['splits'] = $count;
  49. for ($i = 0; $i < $count; ++$i) {
  50. // loop all available roles, see if they're configured and if so, get the associated field from the transaction.
  51. // some roles can be configured multiple times, so the $current array may hold multiple values.
  52. // the final response to this may be to join these fields or only use the last one.
  53. $current = [];
  54. foreach ($fieldNames as $field) {
  55. $role = $allRoles[$field] ?? '_ignore';
  56. if ('_ignore' !== $role) {
  57. Log::debug(sprintf('Field "%s" was given role "%s".', $field, $role));
  58. }
  59. if ('_ignore' === $role) {
  60. Log::debug(sprintf('Field "%s" is ignored!', $field));
  61. }
  62. // get by index, so grab it from the appropriate split or get the first one.
  63. $value = trim($transaction->getFieldByIndex($field, $i));
  64. if ('' !== $value) {
  65. $current[$role] ??= [
  66. 'data' => [],
  67. 'mapping' => [],
  68. ];
  69. if (array_key_exists($field, $mapping)) {
  70. $current[$role]['mapping'] = array_merge($mapping[$field], $current[$role]['mapping']);
  71. }
  72. $current[$role]['data'][$field] = $value;
  73. $current[$role]['data'] = array_unique($current[$role]['data']);
  74. }
  75. }
  76. $result['transactions'][] = $current;
  77. }
  78. Log::debug(sprintf('Pseudo-transaction is: %s', json_encode($result)));
  79. return $result;
  80. }
  81. private function getConfiguredRoles(): array
  82. {
  83. return array_unique(array_values($this->configuration->getRoles()));
  84. }
  85. }