1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Helpers/Bank/Yodlee/Transformer/IncomeTransformer.php

191 lines
4.5 KiB
PHP
Raw Normal View History

2022-08-05 05:45:53 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-08-05 05:45:53 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Bank\Yodlee\Transformer;
2022-08-08 04:46:41 +02:00
use App\Helpers\Bank\BankRevenueInterface;
2022-09-22 07:54:58 +02:00
use App\Utils\Traits\AppSetup;
use Illuminate\Support\Facades\Cache;
2022-08-05 05:45:53 +02:00
/**
"date": "string",
"sourceId": "string",
"symbol": "string",
"cusipNumber": "string",
"highLevelCategoryId": 0,
"detailCategoryId": 0,
"description": {},
"memo": "string",
"settleDate": "string",
"type": "string",
"intermediary": [],
"baseType": "CREDIT",
"categorySource": "SYSTEM",
"principal": {},
"lastUpdated": "string",
"interest": {},
"price": {},
"commission": {},
"id": 0,
"merchantType": "string",
"amount": {
"amount": 0,
"convertedAmount": 0,
"currency": "USD",
"convertedCurrency": "USD"
},
"checkNumber": "string",
"isPhysical": true,
"quantity": 0,
"valoren": "string",
"isManual": true,
"merchant": {
"website": "string",
"address": {},
"contact": {},
"categoryLabel": [],
"coordinates": {},
"name": "string",
"id": "string",
"source": "YODLEE",
"logoURL": "string"
},
"sedol": "string",
"transactionDate": "string",
"categoryType": "TRANSFER",
"accountId": 0,
"createdDate": "string",
"sourceType": "AGGREGATED",
"CONTAINER": "bank",
"postDate": "string",
"parentCategoryId": 0,
"subType": "OVERDRAFT_CHARGE",
"category": "string",
"runningBalance": {},
"categoryId": 0,
"holdingDescription": "string",
"isin": "string",
"status": "POSTED"
2023-02-16 02:36:09 +01:00
2022-08-08 04:46:41 +02:00
(
[CONTAINER] => bank
[id] => 103953585
[amount] => stdClass Object
(
[amount] => 480.66
[currency] => USD
)
[categoryType] => UNCATEGORIZE
[categoryId] => 1
[category] => Uncategorized
[categorySource] => SYSTEM
[highLevelCategoryId] => 10000017
[createdDate] => 2022-08-04T21:50:17Z
[lastUpdated] => 2022-08-04T21:50:17Z
[description] => stdClass Object
(
[original] => CHEROKEE NATION TAX TA TAHLEQUAH OK
)
2023-02-16 02:36:09 +01:00
[isManual] =>
2022-08-08 04:46:41 +02:00
[sourceType] => AGGREGATED
[date] => 2022-08-03
[transactionDate] => 2022-08-03
[postDate] => 2022-08-03
[status] => POSTED
[accountId] => 12331794
[runningBalance] => stdClass Object
(
[amount] => 480.66
[currency] => USD
)
[checkNumber] => 998
)
*/
2022-08-05 05:45:53 +02:00
2022-08-08 04:46:41 +02:00
class IncomeTransformer implements BankRevenueInterface
2022-08-05 05:45:53 +02:00
{
2022-09-22 07:54:58 +02:00
use AppSetup;
2022-08-05 05:45:53 +02:00
2022-08-08 04:46:41 +02:00
public function transform($transaction)
{
$data = [];
2023-02-16 02:36:09 +01:00
if (!property_exists($transaction, 'transaction')) {
2022-08-11 06:19:35 +02:00
return $data;
2023-02-16 02:36:09 +01:00
}
2022-08-11 04:39:43 +02:00
2023-02-16 02:36:09 +01:00
foreach ($transaction->transaction as $transaction) {
2023-02-12 22:31:17 +01:00
//do not store duplicate / pending transactions
2023-10-26 04:57:44 +02:00
if (property_exists($transaction, 'status') && $transaction->status == 'PENDING') {
continue;
2023-10-26 04:57:44 +02:00
}
//some object do no store amounts ignore these
2023-10-26 04:57:44 +02:00
if(!property_exists($transaction, 'amount')) {
2023-02-12 22:31:17 +01:00
continue;
2023-10-26 04:57:44 +02:00
}
2023-02-12 22:31:17 +01:00
2022-08-08 04:46:41 +02:00
$data[] = $this->transformTransaction($transaction);
}
return $data;
}
public function transformTransaction($transaction)
{
return [
2022-08-12 05:41:55 +02:00
'transaction_id' => $transaction->id,
2022-08-08 04:46:41 +02:00
'amount' => $transaction->amount->amount,
2022-09-22 07:54:58 +02:00
'currency_id' => $this->convertCurrency($transaction->amount->currency),
2022-08-08 04:46:41 +02:00
'account_type' => $transaction->CONTAINER,
2022-09-15 08:15:57 +02:00
'category_id' => $transaction->highLevelCategoryId,
2022-08-08 04:46:41 +02:00
'category_type' => $transaction->categoryType,
'date' => $transaction->date,
2022-08-12 05:41:55 +02:00
'bank_account_id' => $transaction->accountId,
'description' => $transaction?->description?->original ?? '',
2022-10-24 10:11:37 +02:00
'base_type' => property_exists($transaction, 'baseType') ? $transaction->baseType : $this->calculateBaseType($transaction),
2022-08-08 04:46:41 +02:00
];
}
2022-10-24 10:11:37 +02:00
private function calculateBaseType($transaction)
{
//CREDIT / DEBIT
2023-02-16 02:36:09 +01:00
if (property_exists($transaction, 'highLevelCategoryId') && $transaction->highLevelCategoryId == 10000012) {
2022-10-24 10:11:37 +02:00
return 'CREDIT';
2023-02-16 02:36:09 +01:00
}
2022-10-24 10:11:37 +02:00
return 'DEBIT';
}
2022-09-22 07:54:58 +02:00
private function convertCurrency(string $code)
{
$currencies = Cache::get('currencies');
if (! $currencies) {
$this->buildCache(true);
}
2023-02-16 02:36:09 +01:00
$currency = $currencies->filter(function ($item) use ($code) {
2022-09-22 07:54:58 +02:00
return $item->code == $code;
})->first();
2023-02-16 02:36:09 +01:00
if ($currency) {
2022-09-22 07:54:58 +02:00
return $currency->id;
2023-02-16 02:36:09 +01:00
}
2022-09-22 07:54:58 +02:00
return 1;
}
2022-08-05 05:45:53 +02:00
}