This commit is contained in:
James Cole
2020-02-19 20:13:09 +01:00
parent ce29beaf55
commit a5d0658241

View File

@@ -40,17 +40,16 @@ class AccountUpdateService
/** @var AccountRepositoryInterface */
protected $accountRepository;
/** @var array */
private $canHaveVirtual;
/** @var User */
private $user;
/** @var array */
protected $validAssetFields = ['account_role', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
/** @var array */
protected $validCCFields = ['account_role', 'cc_monthly_payment_date', 'cc_type', 'account_number', 'currency_id', 'BIC', 'include_net_worth'];
/** @var array */
protected $validFields = ['account_number', 'currency_id', 'BIC', 'interest', 'interest_period', 'include_net_worth'];
/** @var array */
private $canHaveVirtual;
/** @var User */
private $user;
/**
* Constructor.
@@ -83,6 +82,12 @@ class AccountUpdateService
$account->active = $data['active'] ?? $account->active;
$account->iban = $data['iban'] ?? $account->iban;
// if account type is a liability, the liability type (account type)
// can be updated to another one.
if ($this->isLiability($account) && $this->isLiabilityTypeId((int)($data['account_type_id'] ?? 0))) {
$account->account_type_id = (int)$data['account_type_id'];
}
// update virtual balance (could be set to zero if empty string).
if (null !== $data['virtual_balance']) {
$account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
@@ -147,4 +152,30 @@ class AccountUpdateService
return $account;
}
/**
* @param Account $account
*
* @return bool
*/
private function isLiability(Account $account): bool
{
$type = $account->accountType->type;
return in_array($type, [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE], true);
}
/**
* @param int $accountTypeId
*
* @return bool
*/
private function isLiabilityTypeId(int $accountTypeId): bool
{
if (0 === $accountTypeId) {
return false;
}
return 1 === AccountType::whereIn('type', [AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE])->where('id', $accountTypeId)->count();
}
}