mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Factory seems to work for update and create
This commit is contained in:
@@ -39,12 +39,7 @@ class AccountUpdateService
|
||||
{
|
||||
use AccountServiceTrait;
|
||||
|
||||
/** @var array */
|
||||
private $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
|
||||
/** @var array */
|
||||
private $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
|
||||
/** @var array */
|
||||
private $validFields = ['accountNumber', 'currency_id', 'BIC'];
|
||||
|
||||
|
||||
/**
|
||||
* Update account data.
|
||||
@@ -86,44 +81,4 @@ class AccountUpdateService
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update meta data for account. Depends on type which fields are valid.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*/
|
||||
protected function updateMetaData(Account $account, array $data)
|
||||
{
|
||||
$fields = $this->validFields;
|
||||
|
||||
if ($account->accountType->type === AccountType::ASSET) {
|
||||
$fields = $this->validAssetFields;
|
||||
}
|
||||
if ($account->accountType->type === AccountType::ASSET && $data['accountRole'] === 'ccAsset') {
|
||||
$fields = $this->validCCFields;
|
||||
}
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
foreach ($fields as $field) {
|
||||
/** @var AccountMeta $entry */
|
||||
$entry = $account->accountMeta()->where('name', $field)->first();
|
||||
|
||||
// if $data has field and $entry is null, create new one:
|
||||
if (isset($data[$field]) && null === $entry) {
|
||||
Log::debug(sprintf('Created meta-field "%s":"%s" for account #%d ("%s") ', $field, $data[$field], $account->id, $account->name));
|
||||
$factory->create(['account_id' => $account->id, 'name' => $field, 'data' => $data[$field],]);
|
||||
}
|
||||
|
||||
// if $data has field and $entry is not null, update $entry:
|
||||
// let's not bother with a service.
|
||||
if (isset($data[$field]) && null !== $entry) {
|
||||
$entry->data = $data[$field];
|
||||
$entry->save();
|
||||
Log::debug(sprintf('Updated meta-field "%s":"%s" for #%d ("%s") ', $field, $data[$field], $account->id, $account->name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user