Simplify code.

This commit is contained in:
James Cole 2020-10-23 18:26:18 +02:00
parent a8fcde1459
commit cf540bf173
No known key found for this signature in database
GPG Key ID: B5669F9493CDE38D

View File

@ -86,8 +86,31 @@ class AccountUpdateService
$this->updateMetaData($account, $data);
// update, delete or create location:
$this->updateLocation($account, $data);
// update opening balance.
$this->updateOpeningBalance($account, $data);
// update note:
if (isset($data['notes']) && null !== $data['notes']) {
$this->updateNote($account, (string)$data['notes']);
}
// update preferences if inactive:
$this->updatePreferences($account, $data);
return $account;
}
/**
* @param Account $account
* @param array $data
*/
private function updateLocation(Account $account, array $data): void {
$updateLocation = $data['update_location'] ?? false;
// location must be updated?
if (true === $updateLocation) {
// if all set to NULL, delete
@ -109,33 +132,6 @@ class AccountUpdateService
$location->save();
}
}
// has valid initial balance (IB) data?
$type = $account->accountType;
// if it can have a virtual balance, it can also have an opening balance.
if (in_array($type->type, $this->canHaveVirtual, true)) {
// check if is submitted as empty, that makes it valid:
if ($this->validOBData($data) && !$this->isEmptyOBData($data)) {
$this->updateOBGroup($account, $data);
}
if (!$this->validOBData($data) && $this->isEmptyOBData($data)) {
$this->deleteOBGroup($account);
}
}
// update note:
if (isset($data['notes']) && null !== $data['notes']) {
$this->updateNote($account, (string)$data['notes']);
}
// update preferences if inactive:
$this->updatePreferences($account, $data);
return $account;
}
/**
@ -205,13 +201,40 @@ class AccountUpdateService
if (null !== $preference) {
$removeAccountId = (int)$account->id;
$array = $preference->data;
Log::debug('Current list of accounts: ', $array);
Log::debug(sprintf('Going to remove account #%d', $removeAccountId));
$filtered = array_filter(
$array, function ($accountId) use ($removeAccountId) {
return (int)$accountId !== $removeAccountId;
}
);
Log::debug('Left with accounts', array_values($filtered));
app('preferences')->setForUser($account->user, 'frontpageAccounts', array_values($filtered));
}
}
}
/**
* @param Account $account
* @param array $data
*/
private function updateOpeningBalance(Account $account, array $data): void
{
// has valid initial balance (IB) data?
$type = $account->accountType;
// if it can have a virtual balance, it can also have an opening balance.
if (in_array($type->type, $this->canHaveVirtual, true)) {
// check if is submitted as empty, that makes it valid:
if ($this->validOBData($data) && !$this->isEmptyOBData($data)) {
$this->updateOBGroup($account, $data);
}
if (!$this->validOBData($data) && $this->isEmptyOBData($data)) {
$this->deleteOBGroup($account);
}
}
}
}