Factory seems to work for update and create

This commit is contained in:
James Cole
2018-02-21 21:06:59 +01:00
parent 085eb650e7
commit 35d0bd1985
5 changed files with 172 additions and 95 deletions

View File

@@ -25,9 +25,11 @@ namespace FireflyIII\Services\Internal\Support;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Factory\AccountMetaFactory;
use FireflyIII\Factory\TransactionFactory;
use FireflyIII\Factory\TransactionJournalFactory;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Note;
use FireflyIII\Models\Transaction;
@@ -44,6 +46,13 @@ use Validator;
*/
trait AccountServiceTrait
{
/** @var array */
public $validAssetFields = ['accountRole', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */
public $validCCFields = ['accountRole', 'ccMonthlyPaymentDate', 'ccType', 'accountNumber', 'currency_id', 'BIC'];
/** @var array */
public $validFields = ['accountNumber', 'currency_id', 'BIC'];
/**
* @param Account $account
*
@@ -56,7 +65,7 @@ trait AccountServiceTrait
$openingBalance = $this->getIBJournal($account);
// opening balance data? update it!
if (null !== $openingBalance->id) {
if (null !== $openingBalance) {
Log::debug('Opening balance journal found, delete journal.');
$openingBalance->delete();
@@ -216,7 +225,6 @@ trait AccountServiceTrait
$factory = app(AccountFactory::class);
$factory->setUser($user);
return $factory->findOrCreate($name, AccountType::INITIAL_BALANCE);
}
@@ -252,27 +260,6 @@ trait AccountServiceTrait
return true;
}
/**
* Verify if array contains valid data to possibly store or update the opening balance.
*
* @param array $data
*
* @return bool
*/
public function validIBData(array $data): bool
{
$data['openingBalance'] = strval($data['openingBalance'] ?? '');
if (isset($data['openingBalance']) && null !== $data['openingBalance'] && strlen($data['openingBalance']) > 0
&& isset($data['openingBalanceDate'])) {
Log::debug('Array has valid opening balance data.');
return true;
}
Log::debug('Array does not have valid opening balance data.');
return false;
}
/**
* @param Account $account
* @param TransactionJournal $journal
@@ -282,7 +269,7 @@ trait AccountServiceTrait
*
* @throws \Exception
*/
protected function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
public function updateIBJournal(Account $account, TransactionJournal $journal, array $data): bool
{
$date = $data['openingBalanceDate'];
$amount = strval($data['openingBalance']);
@@ -323,13 +310,51 @@ trait AccountServiceTrait
return true;
}
/**
* Update meta data for account. Depends on type which fields are valid.
*
* @param Account $account
* @param array $data
*/
public 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));
}
}
}
/**
* @param Account $account
* @param string $note
*
* @return bool
*/
protected function updateNote(Account $account, string $note): bool
public function updateNote(Account $account, string $note): bool
{
if (0 === strlen($note)) {
$dbNote = $account->notes()->first();
@@ -349,4 +374,25 @@ trait AccountServiceTrait
return true;
}
/**
* Verify if array contains valid data to possibly store or update the opening balance.
*
* @param array $data
*
* @return bool
*/
public function validIBData(array $data): bool
{
$data['openingBalance'] = strval($data['openingBalance'] ?? '');
if (isset($data['openingBalance']) && null !== $data['openingBalance'] && strlen($data['openingBalance']) > 0
&& isset($data['openingBalanceDate'])) {
Log::debug('Array has valid opening balance data.');
return true;
}
Log::debug('Array does not have valid opening balance data.');
return false;
}
}

View File

@@ -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));
}
}
}
}