From 2ad973359b2e14cdd447a8a9f96c10f997ab1f87 Mon Sep 17 00:00:00 2001 From: paulwer Date: Wed, 20 Dec 2023 12:42:47 +0100 Subject: [PATCH] fix https://github.com/invoiceninja/invoiceninja/pull/9004#discussion_r1432302929 --- .../Transformer/TransactionTransformer.php | 106 +++++++++--------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php b/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php index 05350d87398b..6df825131224 100644 --- a/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php +++ b/app/Helpers/Bank/Nordigen/Transformer/TransactionTransformer.php @@ -64,73 +64,73 @@ use Log; class TransactionTransformer implements BankRevenueInterface { - use AppSetup; + use AppSetup; - public function transform($transactionResponse) - { - $data = []; + public function transform($transactionResponse) + { + $data = []; - if (!array_key_exists('transactions', $transactionResponse) || !array_key_exists('booked', $transactionResponse["transactions"])) - throw new \Exception('invalid dataset'); + if (!array_key_exists('transactions', $transactionResponse) || !array_key_exists('booked', $transactionResponse["transactions"])) + throw new \Exception('invalid dataset'); - foreach ($transactionResponse["transactions"]["booked"] as $transaction) { - $data[] = $this->transformTransaction($transaction); + foreach ($transactionResponse["transactions"]["booked"] as $transaction) { + $data[] = $this->transformTransaction($transaction); + } + + return $data; } - return $data; - } + public function transformTransaction($transaction) + { - public function transformTransaction($transaction) - { + if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction)) + throw new \Exception('invalid dataset'); - if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction)) - throw new \Exception('invalid dataset'); + // description could be in varios places + $description = ''; + if (array_key_exists('remittanceInformationStructured', $transaction)) + $description = $transaction["remittanceInformationStructured"]; + else if (array_key_exists('remittanceInformationStructuredArray', $transaction)) + $description = implode(' \r\n', $transaction["remittanceInformationStructuredArray"]); + else if (array_key_exists('remittanceInformationUnstructured', $transaction)) + $description = $transaction["remittanceInformationUnstructured"]; + else + Log::warning("Missing description for the following transaction: " . json_encode($transaction)); - // description could be in varios places - $description = ''; - if (array_key_exists('remittanceInformationStructured', $transaction)) - $description = $transaction["remittanceInformationStructured"]; - else if (array_key_exists('remittanceInformationStructuredArray', $transaction)) - $description = implode(' \r\n', $transaction["remittanceInformationStructuredArray"]); - else if (array_key_exists('remittanceInformationUnstructured', $transaction)) - $description = $transaction["remittanceInformationUnstructured"]; - else - Log::warning("Missing description for the following transaction: " . json_encode($transaction)); + return [ + 'transaction_id' => $transaction["transactionId"], + 'amount' => abs((int) $transaction["transactionAmount"]["amount"]), + 'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]), + 'category_id' => null, // nordigen has no categories + 'category_type' => array_key_exists('additionalInformation', $transaction) ? $transaction["additionalInformation"] : '', // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc + 'date' => $transaction["bookingDate"], + 'description' => $description, + 'participant' => array_key_exists('debtorAccount', $transaction) && array_key_exists('iban', $transaction["debtorAccount"]) ? $transaction['debtorAccount']['iban'] : null, + 'participant_name' => array_key_exists('debtorName', $transaction) ? $transaction['debtorName'] : null, + 'base_type' => (int) $transaction["transactionAmount"]["amount"] <= 0 ? 'DEBIT' : 'CREDIT', + ]; - return [ - 'transaction_id' => $transaction["transactionId"], - 'amount' => abs((int) $transaction["transactionAmount"]["amount"]), - 'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]), - 'category_id' => 0, // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc - 'category_type' => array_key_exists('additionalInformation', $transaction) ? $transaction["additionalInformation"] : '', // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc - 'date' => $transaction["bookingDate"], - 'description' => $description, - 'participant' => array_key_exists('debtorAccount', $transaction) && array_key_exists('iban', $transaction["debtorAccount"]) ? $transaction['debtorAccount']['iban'] : null, - 'participant_name' => array_key_exists('debtorName', $transaction) ? $transaction['debtorName'] : null, - 'base_type' => (int) $transaction["transactionAmount"]["amount"] <= 0 ? 'DEBIT' : 'CREDIT', - ]; - - } - - private function convertCurrency(string $code) - { - - $currencies = Cache::get('currencies'); - - if (!$currencies) { - $this->buildCache(true); } - $currency = $currencies->filter(function ($item) use ($code) { - return $item->code == $code; - })->first(); + private function convertCurrency(string $code) + { - if ($currency) - return $currency->id; + $currencies = Cache::get('currencies'); - return 1; + if (!$currencies) { + $this->buildCache(true); + } - } + $currency = $currencies->filter(function ($item) use ($code) { + return $item->code == $code; + })->first(); + + if ($currency) + return $currency->id; + + return 1; + + } }