1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Update account balance information when processing for new transactions

This commit is contained in:
David Bomba 2023-07-23 13:16:15 +10:00
parent 79d50d5686
commit 66230a390d
4 changed files with 55 additions and 3 deletions

View File

@ -81,6 +81,19 @@ class AccountTransformer implements AccountTransformerInterface
public function transformAccount($account)
{
$current_balance = 0;
$account_currency = '';
if(property_exists($account, 'currentBalance')) {
$current_balance = $account->currentBalance->amount ?? 0;
$account_currency = $account->currentBalance->currency ?? '';
}
elseif(property_exists($account, 'balance')){
$current_balance = $account->balance->amount ?? 0;
$account_currency = $account->balance->currency ?? '';
}
return [
'id' => $account->id,
'account_type' => $account->CONTAINER,
@ -92,8 +105,8 @@ class AccountTransformer implements AccountTransformerInterface
'provider_id' => $account->providerId,
'provider_name' => $account->providerName,
'nickname' => property_exists($account, 'nickname') ? $account->nickname : '',
'current_balance' => property_exists($account, 'currentBalance') ? $account->currentBalance->amount : 0,
'account_currency' => property_exists($account, 'currency') ? $account->currentBalance->currency : '',
'current_balance' => $current_balance,
'account_currency' => $account_currency,
];
}
}

View File

@ -185,6 +185,21 @@ class Yodlee
}
}
public function getAccountSummary($account_id)
{
$token = $this->getAccessToken();
$response = Http::withHeaders($this->getHeaders(["Authorization" => "Bearer {$token}"]))->get($this->getEndpoint(). "/accounts/{$account_id}", []);
if ($response->successful()) {
return $response->object();
}
if ($response->failed()) {
return false;
}
}
public function deleteAccount($account_id)
{
$token = $this->getAccessToken();

View File

@ -209,7 +209,12 @@ class BankIntegrationController extends BaseController
$accounts = $yodlee->getAccounts();
foreach ($accounts as $account) {
if (!BankIntegration::withTrashed()->where('bank_account_id', $account['id'])->where('company_id', $user->company()->id)->exists()) {
if ($bi = BankIntegration::withTrashed()->where('bank_account_id', $account['id'])->where('company_id', $user->company()->id)->first()){
$bi->balance = $account['current_balance'];
$bi->currency = $account['account_currency'];
$bi->save();
}
else {
$bank_integration = new BankIntegration();
$bank_integration->company_id = $user->company()->id;
$bank_integration->account_id = $user->account_id;

View File

@ -24,6 +24,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use App\Notifications\Ninja\GenericNinjaAdminNotification;
use App\Helpers\Bank\Yodlee\Transformer\AccountTransformer;
class ProcessBankTransactions implements ShouldQueue
{
@ -99,6 +100,24 @@ class ProcessBankTransactions implements ShouldQueue
return;
}
try {
$account_summary = $yodlee->getAccountSummary($this->bank_integration->bank_account_id);
if($account_summary) {
$at = new AccountTransformer();
$account = $at->transform($account_summary);
$this->bank_integration->balance = $account['current_balance'];
$this->bank_integration->currency = $account['account_currency'];
$this->bank_integration->save();
}
}
catch(\Exception $e) {
nlog("YODLEE: unable to update account summary for {$this->bank_integration->bank_account_id} => ". $e->getMessage());
}
$data = [
'top' => 500,
'fromDate' => $this->from_date,