mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Wallets\Services;
|
|
|
|
use App\Classes\General\Eloquent\AbstractUpdateRecord;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Classes\Modules\Wallets\Services\UpdatesWalletBalance;
|
|
use App\Models\Wallet;
|
|
|
|
class RecalculatesWalletBalance extends AbstractUpdateRecord
|
|
{
|
|
|
|
private $updatesWalletBalance;
|
|
|
|
public function __construct(UpdatesWalletBalance $updatesWalletBalance)
|
|
{
|
|
$this->updatesWalletBalance = $updatesWalletBalance;
|
|
}
|
|
/**
|
|
* @param Wallet $model
|
|
* @param $amount
|
|
* @return \Illuminate\Database\Eloquent\Model
|
|
* @throws \App\Classes\Exceptions\MalformedRequestException
|
|
*/
|
|
public function execute(Wallet $wallet)
|
|
{
|
|
|
|
$i = 0;
|
|
$topups = 0;
|
|
$credit = 0;
|
|
$payments = 0;
|
|
$debit = 0;
|
|
|
|
foreach ($wallet->transactions as $transaction) {
|
|
if (!in_array((int) $transaction->status, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])) continue;
|
|
if ((int) $transaction->type === TransactionType::TOP_UP) {
|
|
$topups += (float) $transaction->amount;
|
|
}
|
|
if ((int) $transaction->type === TransactionType::CREDIT_NOTE) $credit += (float) $transaction->amount;
|
|
if ((int) $transaction->type === TransactionType::PAYMENT) $payments += (float) $transaction->amount;
|
|
if ((int) $transaction->type === TransactionType::DEBIT_NOTE) $debit += (float) $transaction->amount;
|
|
}
|
|
$auditBalance = ($topups + $credit) - ($payments + $debit);
|
|
|
|
return $auditBalance;
|
|
}
|
|
}
|