Skip to content

Firefly III: a personal finances manager

And now also make sure that old meta data isn't changed.

James Cole7c5e10de3357
diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php
index 8f555f57aa..fcc56335fc 100644
--- a/app/Factory/AccountFactory.php
+++ b/app/Factory/AccountFactory.php
@@ -49,6 +49,13 @@ class AccountFactory
/** @var array */
private $canHaveVirtual;
+ /** @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'];
+
/**
* AccountFactory constructor.
*
diff --git a/app/Services/Internal/Support/AccountServiceTrait.php b/app/Services/Internal/Support/AccountServiceTrait.php
index 7e5b438963..29f65669e9 100644
--- a/app/Services/Internal/Support/AccountServiceTrait.php
+++ b/app/Services/Internal/Support/AccountServiceTrait.php
@@ -35,7 +35,6 @@ 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;
use Validator;
@@ -46,16 +45,6 @@ use Validator;
*/
trait AccountServiceTrait
{
- /** @var AccountRepositoryInterface */
- protected $accountRepository;
-
- /** @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'];
-
/**
* @param null|string $iban
*
@@ -85,7 +74,7 @@ trait AccountServiceTrait
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
*
* @param Account $account
- * @param array $data
+ * @param array $data
*
*/
public function updateMetaData(Account $account, array $data): void
@@ -101,14 +90,17 @@ trait AccountServiceTrait
/** @var AccountMetaFactory $factory */
$factory = app(AccountMetaFactory::class);
foreach ($fields as $field) {
-
- $factory->crud($account, $field, (string)($data[$field] ?? ''));
+ // if the field is set but NULL, skip it.
+ // if the field is set but "", update it.
+ if (isset($data[$field]) && null !== $data[$field]) {
+ $factory->crud($account, $field, (string)($data[$field] ?? ''));
+ }
}
}
/**
* @param Account $account
- * @param string $note
+ * @param string $note
*
* @codeCoverageIgnore
* @return bool
@@ -159,50 +151,9 @@ trait AccountServiceTrait
}
/**
- * @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;
- }
-
- /**
- * Delete TransactionGroup with opening balance in it.
- *
* @param Account $account
- */
- protected function deleteOBGroup(Account $account): void
- {
- Log::debug(sprintf('deleteOB() for account #%d', $account->id));
- $openingBalanceGroup = $this->getOBGroup($account);
-
- // opening balance data? update it!
- if (null !== $openingBalanceGroup) {
- Log::debug('Opening balance journal found, delete journal.');
- /** @var TransactionGroupDestroyService $service */
- $service = app(TransactionGroupDestroyService::class);
- $service->destroy($openingBalanceGroup);
- }
- }
-
- /**
- * @param Account $account
- * @param array $data
+ * @param array $data
+ *
* @return TransactionGroup|null
*/
protected function createOBGroup(Account $account, array $data): ?TransactionGroup
@@ -280,13 +231,68 @@ trait AccountServiceTrait
return $group;
}
+ /**
+ * Delete TransactionGroup with opening balance in it.
+ *
+ * @param Account $account
+ */
+ protected function deleteOBGroup(Account $account): void
+ {
+ Log::debug(sprintf('deleteOB() for account #%d', $account->id));
+ $openingBalanceGroup = $this->getOBGroup($account);
+
+ // opening balance data? update it!
+ if (null !== $openingBalanceGroup) {
+ Log::debug('Opening balance journal found, delete journal.');
+ /** @var TransactionGroupDestroyService $service */
+ $service = app(TransactionGroupDestroyService::class);
+ $service->destroy($openingBalanceGroup);
+ }
+ }
+
+ /**
+ * @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;
+ }
+
+ /**
+ * Returns the opening balance group, or NULL if it does not exist.
+ *
+ * @param Account $account
+ *
+ * @return TransactionGroup|null
+ */
+ protected function getOBGroup(Account $account): ?TransactionGroup
+ {
+ return $this->accountRepository->getOpeningBalanceGroup($account);
+ }
+
/**
* Update or create the opening balance group. Assumes valid data in $data.
*
* Returns null if this fails.
*
* @param Account $account
- * @param array $data
+ * @param array $data
*
* @return TransactionGroup|null
* @codeCoverageIgnore
@@ -334,15 +340,4 @@ trait AccountServiceTrait
return $obGroup;
}
-
- /**
- * Returns the opening balance group, or NULL if it does not exist.
- *
- * @param Account $account
- * @return TransactionGroup|null
- */
- protected function getOBGroup(Account $account): ?TransactionGroup
- {
- return $this->accountRepository->getOpeningBalanceGroup($account);
- }
}
diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php
index 5755923d7b..0ca74e82f1 100644
--- a/app/Services/Internal/Update/AccountUpdateService.php
+++ b/app/Services/Internal/Update/AccountUpdateService.php
@@ -44,6 +44,13 @@ class AccountUpdateService
/** @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'];
+
/**
* Constructor.
*/
@@ -71,20 +78,16 @@ class AccountUpdateService
$this->user = $account->user;
// update the account itself:
-
$account->name = $data['name'] ?? $account->name;
$account->active = $data['active'] ?? $account->active;
- if (null !== $data['virtual_balance']) {
- $account->virtual_balance = '' === trim($data['virtual_balance']) ? '0' : $data['virtual_balance'];
- }
$account->iban = $data['iban'] ?? $account->iban;
- $account->save();
-
- if (isset($data['currency_id']) && null !== $data['currency_id'] && 0 === $data['currency_id']) {
- unset($data['currency_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'];
}
+ $account->save();
// find currency, or use default currency instead.
if (null !== $data['currency_id'] || null !== $data['currency_code']) {
@@ -93,13 +96,6 @@ class AccountUpdateService
$data['currency_id'] = $currency->id;
}
- if (null === $data['currency_id']) {
- $data['currency_id'] = $this->accountRepository->getMetaValue($account, 'currency_id');
- }
-
- if (null === $data['account_role']) {
- $data['account_role'] = $this->accountRepository->getMetaValue($account, 'account_role');
- }
// update all meta data:
$this->updateMetaData($account, $data);