Merge pull request #9150 from paulwer/fix-nordigen-amount

fix: nordigen amount imported as absolute
This commit is contained in:
David Bomba 2024-01-15 08:07:07 +11:00 committed by GitHub
commit 63e6811fc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,85 +64,85 @@ use Log;
class TransactionTransformer implements BankRevenueInterface class TransactionTransformer implements BankRevenueInterface
{ {
use AppSetup; use AppSetup;
public function transform($transactionResponse) public function transform($transactionResponse)
{ {
$data = []; $data = [];
if (!array_key_exists('transactions', $transactionResponse) || !array_key_exists('booked', $transactionResponse["transactions"])) if (!array_key_exists('transactions', $transactionResponse) || !array_key_exists('booked', $transactionResponse["transactions"]))
throw new \Exception('invalid dataset'); throw new \Exception('invalid dataset');
foreach ($transactionResponse["transactions"]["booked"] as $transaction) { foreach ($transactionResponse["transactions"]["booked"] as $transaction) {
$data[] = $this->transformTransaction($transaction); $data[] = $this->transformTransaction($transaction);
}
return $data;
} }
public function transformTransaction($transaction) return $data;
{ }
if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction)) public function transformTransaction($transaction)
throw new \Exception('invalid dataset'); {
// description could be in varios places if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction))
$description = ''; throw new \Exception('invalid dataset');
if (array_key_exists('remittanceInformationStructured', $transaction))
$description = $transaction["remittanceInformationStructured"];
else if (array_key_exists('remittanceInformationStructuredArray', $transaction))
$description = implode('\n', $transaction["remittanceInformationStructuredArray"]);
else if (array_key_exists('remittanceInformationUnstructured', $transaction))
$description = $transaction["remittanceInformationUnstructured"];
else if (array_key_exists('remittanceInformationUnstructuredArray', $transaction))
$description = implode('\n', $transaction["remittanceInformationUnstructuredArray"]);
else
Log::warning("Missing description for the following transaction: " . json_encode($transaction));
// participant // description could be in varios places
$participant = array_key_exists('debtorAccount', $transaction) && array_key_exists('iban', $transaction["debtorAccount"]) ? $description = '';
$transaction['debtorAccount']['iban'] : if (array_key_exists('remittanceInformationStructured', $transaction))
(array_key_exists('creditorAccount', $transaction) && array_key_exists('iban', $transaction["creditorAccount"]) ? $description = $transaction["remittanceInformationStructured"];
$transaction['creditorAccount']['iban'] : null); else if (array_key_exists('remittanceInformationStructuredArray', $transaction))
$participant_name = array_key_exists('debtorName', $transaction) ? $description = implode('\n', $transaction["remittanceInformationStructuredArray"]);
$transaction['debtorName'] : else if (array_key_exists('remittanceInformationUnstructured', $transaction))
(array_key_exists('creditorName', $transaction) ? $description = $transaction["remittanceInformationUnstructured"];
$transaction['creditorName'] : null); else if (array_key_exists('remittanceInformationUnstructuredArray', $transaction))
$description = implode('\n', $transaction["remittanceInformationUnstructuredArray"]);
else
Log::warning("Missing description for the following transaction: " . json_encode($transaction));
return [ // participant
'nordigen_transaction_id' => $transaction["transactionId"], $participant = array_key_exists('debtorAccount', $transaction) && array_key_exists('iban', $transaction["debtorAccount"]) ?
'amount' => abs((int) $transaction["transactionAmount"]["amount"]), $transaction['debtorAccount']['iban'] :
'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]), (array_key_exists('creditorAccount', $transaction) && array_key_exists('iban', $transaction["creditorAccount"]) ?
'category_id' => null, $transaction['creditorAccount']['iban'] : null);
'category_type' => array_key_exists('additionalInformation', $transaction) ? $transaction["additionalInformation"] : '', $participant_name = array_key_exists('debtorName', $transaction) ?
'date' => $transaction["bookingDate"], $transaction['debtorName'] :
'description' => $description, (array_key_exists('creditorName', $transaction) ?
'participant' => $participant, $transaction['creditorName'] : null);
'participant_name' => $participant_name,
'base_type' => (int) $transaction["transactionAmount"]["amount"] <= 0 ? 'DEBIT' : 'CREDIT',
];
return [
'transaction_id' => $transaction["transactionId"],
'amount' => (float) $transaction["transactionAmount"]["amount"],
'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]),
'category_id' => null,
'category_type' => array_key_exists('additionalInformation', $transaction) ? $transaction["additionalInformation"] : '',
'date' => $transaction["bookingDate"],
'description' => $description,
'participant' => $participant,
'participant_name' => $participant_name,
'base_type' => (int) $transaction["transactionAmount"]["amount"] <= 0 ? 'DEBIT' : 'CREDIT',
];
}
private function convertCurrency(string $code)
{
$currencies = Cache::get('currencies');
if (!$currencies) {
$this->buildCache(true);
} }
private function convertCurrency(string $code) $currency = $currencies->filter(function ($item) use ($code) {
{ return $item->code == $code;
})->first();
$currencies = Cache::get('currencies'); if ($currency)
return $currency->id;
if (!$currencies) { return 1;
$this->buildCache(true);
}
$currency = $currencies->filter(function ($item) use ($code) { }
return $item->code == $code;
})->first();
if ($currency)
return $currency->id;
return 1;
}
} }