mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-10 20:24:37 -04:00
initial changes
This commit is contained in:
parent
f2fe4ed092
commit
01cf5fe5a5
@ -64,83 +64,92 @@ 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;
|
return $data;
|
||||||
}
|
|
||||||
|
|
||||||
public function transformTransaction($transaction)
|
|
||||||
{
|
|
||||||
|
|
||||||
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('\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
|
|
||||||
$participant = array_key_exists('debtorAccount', $transaction) && array_key_exists('iban', $transaction["debtorAccount"]) ?
|
|
||||||
$transaction['debtorAccount']['iban'] :
|
|
||||||
(array_key_exists('creditorAccount', $transaction) && array_key_exists('iban', $transaction["creditorAccount"]) ?
|
|
||||||
$transaction['creditorAccount']['iban'] : null);
|
|
||||||
$participant_name = array_key_exists('debtorName', $transaction) ?
|
|
||||||
$transaction['debtorName'] :
|
|
||||||
(array_key_exists('creditorName', $transaction) ?
|
|
||||||
$transaction['creditorName'] : null);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$currency = $currencies->filter(function ($item) use ($code) {
|
public function transformTransaction($transaction)
|
||||||
return $item->code == $code;
|
{
|
||||||
})->first();
|
|
||||||
|
|
||||||
if ($currency)
|
if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction))
|
||||||
return $currency->id;
|
throw new \Exception('invalid dataset');
|
||||||
|
|
||||||
return 1;
|
$amount = (float) $transaction["transactionAmount"]["amount"];
|
||||||
|
|
||||||
}
|
// 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('\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));
|
||||||
|
|
||||||
|
// enrich description with currencyExchange informations
|
||||||
|
if (array_key_exists('currencyExchange', $transaction))
|
||||||
|
foreach ($transaction["currencyExchange"] as $exchangeRate) {
|
||||||
|
$targetAmount = round($amount * (float) $exchangeRate["exchangeRate"], 2);
|
||||||
|
$description += '\nexchangeRate: ' . $amount . " " . $exchangeRate["sourceCurrency"] . " = " . $targetAmount . " " . $exchangeRate["targetCurrency"] . " (" . $exchangeRate["quotationDate"] . ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
// participant data
|
||||||
|
$participant = array_key_exists('debtorAccount', $transaction) && array_key_exists('iban', $transaction["debtorAccount"]) ?
|
||||||
|
$transaction['debtorAccount']['iban'] :
|
||||||
|
(array_key_exists('creditorAccount', $transaction) && array_key_exists('iban', $transaction["creditorAccount"]) ?
|
||||||
|
$transaction['creditorAccount']['iban'] : null);
|
||||||
|
$participant_name = array_key_exists('debtorName', $transaction) ?
|
||||||
|
$transaction['debtorName'] :
|
||||||
|
(array_key_exists('creditorName', $transaction) ?
|
||||||
|
$transaction['creditorName'] : null);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'transaction_id' => $transaction["transactionId"],
|
||||||
|
'amount' => $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);
|
||||||
|
}
|
||||||
|
|
||||||
|
$currency = $currencies->filter(function ($item) use ($code) {
|
||||||
|
return $item->code == $code;
|
||||||
|
})->first();
|
||||||
|
|
||||||
|
if ($currency)
|
||||||
|
return $currency->id;
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user