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

This commit is contained in:
James Cole 2019-08-24 08:22:46 +02:00
parent d836c8217d
commit 7c5e10de33
3 changed files with 83 additions and 85 deletions

View File

@ -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.
*

View File

@ -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
* @param array $data
*
* @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
* @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);
}
}

View File

@ -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,21 +78,17 @@ class AccountUpdateService
$this->user = $account->user;
// update the account itself:
$account->name = $data['name'] ?? $account->name;
$account->active = $data['active'] ?? $account->active;
$account->iban = $data['iban'] ?? $account->iban;
// 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->iban = $data['iban'] ?? $account->iban;
$account->save();
if (isset($data['currency_id']) && null !== $data['currency_id'] && 0 === $data['currency_id']) {
unset($data['currency_id']);
}
// find currency, or use default currency instead.
if (null !== $data['currency_id'] || null !== $data['currency_code']) {
$currency = $this->getCurrency((int)($data['currency_id'] ?? null), (string)($data['currency_code'] ?? null));
@ -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);