mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-26 13:36:15 +00:00
Improve account CRUD and tests.
This commit is contained in:
@@ -26,11 +26,15 @@ namespace FireflyIII\Services\Internal\Support;
|
||||
use Exception;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Factory\AccountMetaFactory;
|
||||
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||
use FireflyIII\Factory\TransactionGroupFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\Note;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
|
||||
use Log;
|
||||
@@ -75,6 +79,32 @@ trait AccountServiceTrait
|
||||
return $iban;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update meta data for account. Depends on type which fields are valid.
|
||||
*
|
||||
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function updateMetaData(Account $account, array $data): void
|
||||
{
|
||||
$fields = $this->validFields;
|
||||
|
||||
if ($account->accountType->type === AccountType::ASSET) {
|
||||
$fields = $this->validAssetFields;
|
||||
}
|
||||
if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['account_role']) {
|
||||
$fields = $this->validCCFields;
|
||||
}
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
foreach ($fields as $field) {
|
||||
$factory->crud($account, $field, (string)($data[$field] ?? ''));
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @param User $user
|
||||
// * @param string $name
|
||||
@@ -126,29 +156,34 @@ trait AccountServiceTrait
|
||||
// }
|
||||
|
||||
/**
|
||||
* Update meta data for account. Depends on type which fields are valid.
|
||||
*
|
||||
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function updateMetaData(Account $account, array $data): void
|
||||
public function updateNote(Account $account, string $note): bool
|
||||
{
|
||||
$fields = $this->validFields;
|
||||
if ('' === $note) {
|
||||
$dbNote = $account->notes()->first();
|
||||
if (null !== $dbNote) {
|
||||
try {
|
||||
$dbNote->delete();
|
||||
} catch (Exception $e) {
|
||||
Log::debug($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($account->accountType->type === AccountType::ASSET) {
|
||||
$fields = $this->validAssetFields;
|
||||
return true;
|
||||
}
|
||||
if ($account->accountType->type === AccountType::ASSET && 'ccAsset' === $data['account_role']) {
|
||||
$fields = $this->validCCFields;
|
||||
}
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
foreach ($fields as $field) {
|
||||
$factory->crud($account, $field, (string)($data[$field] ?? ''));
|
||||
$dbNote = $account->notes()->first();
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note;
|
||||
$dbNote->noteable()->associate($account);
|
||||
}
|
||||
$dbNote->text = trim($note);
|
||||
$dbNote->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// /**
|
||||
@@ -262,37 +297,6 @@ trait AccountServiceTrait
|
||||
// return $journal;
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function updateNote(Account $account, string $note): bool
|
||||
{
|
||||
if ('' === $note) {
|
||||
$dbNote = $account->notes()->first();
|
||||
if (null !== $dbNote) {
|
||||
try {
|
||||
$dbNote->delete();
|
||||
} catch (Exception $e) {
|
||||
Log::debug($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
$dbNote = $account->notes()->first();
|
||||
if (null === $dbNote) {
|
||||
$dbNote = new Note;
|
||||
$dbNote->noteable()->associate($account);
|
||||
}
|
||||
$dbNote->text = trim($note);
|
||||
$dbNote->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if array contains valid data to possibly store or update the opening balance.
|
||||
*
|
||||
@@ -313,6 +317,29 @@ trait AccountServiceTrait
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currencyId
|
||||
* @param string $currencyCode
|
||||
* @return TransactionCurrency
|
||||
*/
|
||||
protected function getCurrency(int $currencyId, string $currencyCode): TransactionCurrency
|
||||
{
|
||||
// find currency, or use default currency instead.
|
||||
/** @var TransactionCurrencyFactory $factory */
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
/** @var TransactionCurrency $currency */
|
||||
$currency = $factory->find($currencyId, $currencyCode);
|
||||
|
||||
if (null === $currency) {
|
||||
// use default currency:
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($this->user);
|
||||
}
|
||||
$currency->enabled = true;
|
||||
$currency->save();
|
||||
|
||||
return $currency;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @param Account $account
|
||||
// * @param TransactionJournal $journal
|
||||
@@ -446,6 +473,7 @@ trait AccountServiceTrait
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
}
|
||||
|
||||
// @codeCoverageIgnoreEnd
|
||||
|
||||
return $group;
|
||||
@@ -462,12 +490,46 @@ trait AccountServiceTrait
|
||||
*/
|
||||
protected function updateOBGroup(Account $account, array $data): ?TransactionGroup
|
||||
{
|
||||
if (null === $this->getOBGroup($account)) {
|
||||
$obGroup = $this->getOBGroup($account);
|
||||
if (null === $obGroup) {
|
||||
return $this->createOBGroup($account, $data);
|
||||
}
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $obGroup->transactionJournals()->first();
|
||||
$journal->date = $data['opening_balance_date'] ?? $journal->date;
|
||||
$journal->transaction_currency_id = $data['currency_id'];
|
||||
|
||||
// edit in this method
|
||||
die('cannot handle edit');
|
||||
/** @var Transaction $obTransaction */
|
||||
$obTransaction = $journal->transactions()->where('account_id', '!=', $account->id)->first();
|
||||
/** @var Transaction $accountTransaction */
|
||||
$accountTransaction = $journal->transactions()->where('account_id', $account->id)->first();
|
||||
|
||||
// if amount is negative:
|
||||
if (1 === bccomp('0', $data['opening_balance'])) {
|
||||
// account transaction loses money:
|
||||
$accountTransaction->amount = app('steam')->negative($data['opening_balance']);
|
||||
$accountTransaction->transaction_currency_id = $data['currency_id'];
|
||||
|
||||
// OB account transaction gains money
|
||||
$obTransaction->amount = app('steam')->positive($data['opening_balance']);
|
||||
$obTransaction->transaction_currency_id = $data['currency_id'];
|
||||
}
|
||||
if (-1 === bccomp('0', $data['opening_balance'])) {
|
||||
// account gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($data['opening_balance']);
|
||||
$accountTransaction->transaction_currency_id = $data['currency_id'];
|
||||
|
||||
// OB account loses money:
|
||||
$obTransaction->amount = app('steam')->negative($data['opening_balance']);
|
||||
$obTransaction->transaction_currency_id = $data['currency_id'];
|
||||
}
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$obTransaction->save();
|
||||
$journal->save();
|
||||
$obGroup->refresh();
|
||||
|
||||
return $obGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,9 @@ namespace FireflyIII\Services\Internal\Update;
|
||||
|
||||
use FireflyIII\Factory\TransactionCurrencyFactory;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\AccountType;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
|
||||
use Log;
|
||||
|
||||
@@ -36,6 +38,11 @@ class AccountUpdateService
|
||||
{
|
||||
use AccountServiceTrait;
|
||||
|
||||
/** @var array */
|
||||
private $canHaveVirtual;
|
||||
/** @var AccountRepositoryInterface */
|
||||
protected $accountRepository;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@@ -44,6 +51,9 @@ class AccountUpdateService
|
||||
if ('testing' === config('app.env')) {
|
||||
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
||||
}
|
||||
// TODO move to configuration.
|
||||
$this->canHaveVirtual = [AccountType::ASSET, AccountType::DEBT, AccountType::LOAN, AccountType::MORTGAGE, AccountType::CREDITCARD];
|
||||
$this->accountRepository = app(AccountRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,12 +63,11 @@ class AccountUpdateService
|
||||
* @param array $data
|
||||
*
|
||||
* @return Account
|
||||
* @throws \FireflyIII\Exceptions\FireflyException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function update(Account $account, array $data): Account
|
||||
{
|
||||
$this->accountRepository->setUser($account->user);
|
||||
|
||||
// update the account itself:
|
||||
$account->name = $data['name'];
|
||||
$account->active = $data['active'];
|
||||
@@ -69,32 +78,26 @@ class AccountUpdateService
|
||||
if (isset($data['currency_id']) && 0 === $data['currency_id']) {
|
||||
unset($data['currency_id']);
|
||||
}
|
||||
// find currency, or use default currency instead.
|
||||
/** @var TransactionCurrencyFactory $factory */
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
/** @var TransactionCurrency $currency */
|
||||
$currency = $factory->find($data['currency_id'] ?? null, $data['currency_code'] ?? null);
|
||||
|
||||
if (null === $currency) {
|
||||
// use default currency:
|
||||
$currency = app('amount')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
$currency->enabled = true;
|
||||
$currency->save();
|
||||
// find currency, or use default currency instead.
|
||||
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
|
||||
unset($data['currency_code']);
|
||||
$data['currency_id'] = $currency->id;
|
||||
|
||||
|
||||
// update all meta data:
|
||||
$this->updateMetaData($account, $data);
|
||||
|
||||
// has valid initial balance (IB) data?
|
||||
if ($this->validOBData($data)) {
|
||||
// then do update!
|
||||
$this->updateIB($account, $data);
|
||||
}
|
||||
|
||||
// if not, delete it when exist.
|
||||
if (!$this->validOBData($data)) {
|
||||
$this->deleteIB($account);
|
||||
$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)) {
|
||||
if ($this->validOBData($data)) {
|
||||
$this->updateOBGroup($account, $data);
|
||||
}
|
||||
if (!$this->validOBData($data)) {
|
||||
$this->deleteOBGroup($account);
|
||||
}
|
||||
}
|
||||
|
||||
// update note:
|
||||
|
||||
Reference in New Issue
Block a user