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:
@@ -23,8 +23,11 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Factory;
|
namespace FireflyIII\Factory;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
|
use FireflyIII\Services\Internal\Support\AccountServiceTrait;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,17 +37,84 @@ use FireflyIII\User;
|
|||||||
*/
|
*/
|
||||||
class AccountFactory
|
class AccountFactory
|
||||||
{
|
{
|
||||||
|
use AccountServiceTrait;
|
||||||
/** @var User */
|
/** @var User */
|
||||||
private $user;
|
private $user;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
|
* @throws FireflyException
|
||||||
|
* @throws Exception
|
||||||
* @return Account
|
* @return Account
|
||||||
*/
|
*/
|
||||||
public function create(array $data): Account
|
public function create(array $data): Account
|
||||||
{
|
{
|
||||||
return Account::create($data);
|
$type = $this->getAccountType($data['account_type_id'], $data['accountType']);
|
||||||
|
$data['iban'] = $this->filterIban($data['iban']);
|
||||||
|
|
||||||
|
|
||||||
|
// account may exist already:
|
||||||
|
$existingAccount = $this->find($data['name'], $type->type);
|
||||||
|
if (null !== $existingAccount) {
|
||||||
|
return $existingAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// create it:
|
||||||
|
$databaseData
|
||||||
|
= [
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'account_type_id' => $type->id,
|
||||||
|
'name' => $data['name'],
|
||||||
|
'virtual_balance' => strlen(strval($data['virtualBalance'])) === 0 ? '0' : $data['virtualBalance'],
|
||||||
|
'active' => true === $data['active'] ? true : false,
|
||||||
|
'iban' => $data['iban'],
|
||||||
|
];
|
||||||
|
|
||||||
|
// remove virtual balance when not an asset account:
|
||||||
|
if ($type->type !== AccountType::ASSET) {
|
||||||
|
$databaseData['virtual_balance'] = '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
$newAccount = Account::create($databaseData);
|
||||||
|
$this->updateMetadata($newAccount, $data);
|
||||||
|
|
||||||
|
if ($this->validIBData($data)) {
|
||||||
|
$this->updateIB($newAccount, $data);
|
||||||
|
}
|
||||||
|
if (!$this->validIBData($data)) {
|
||||||
|
$this->deleteIB($newAccount);
|
||||||
|
}
|
||||||
|
// update note:
|
||||||
|
if (isset($data['notes'])) {
|
||||||
|
$this->updateNote($newAccount, $data['notes']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $newAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $accountName
|
||||||
|
* @param string $accountType
|
||||||
|
*
|
||||||
|
* @return Account|null
|
||||||
|
* @throws Exception
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
public function find(string $accountName, string $accountType): ?Account
|
||||||
|
{
|
||||||
|
$type = AccountType::whereType($accountType)->first();
|
||||||
|
$accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']);
|
||||||
|
|
||||||
|
/** @var Account $object */
|
||||||
|
foreach ($accounts as $object) {
|
||||||
|
if ($object->name === $accountName) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -52,6 +122,8 @@ class AccountFactory
|
|||||||
* @param string $accountType
|
* @param string $accountType
|
||||||
*
|
*
|
||||||
* @return Account
|
* @return Account
|
||||||
|
* @throws Exception
|
||||||
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function findOrCreate(string $accountName, string $accountType): Account
|
public function findOrCreate(string $accountName, string $accountType): Account
|
||||||
{
|
{
|
||||||
@@ -70,7 +142,8 @@ class AccountFactory
|
|||||||
'user_id' => $this->user->id,
|
'user_id' => $this->user->id,
|
||||||
'name' => $accountName,
|
'name' => $accountName,
|
||||||
'account_type_id' => $type->id,
|
'account_type_id' => $type->id,
|
||||||
'virtual_balance' => '0',
|
'accountType' => null,
|
||||||
|
'virtualBalance' => '0',
|
||||||
'iban' => null,
|
'iban' => null,
|
||||||
'active' => true,
|
'active' => true,
|
||||||
]
|
]
|
||||||
@@ -85,4 +158,22 @@ class AccountFactory
|
|||||||
$this->user = $user;
|
$this->user = $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int|null $accountTypeId
|
||||||
|
* @param null|string $accountType
|
||||||
|
*
|
||||||
|
* @return AccountType|null
|
||||||
|
*/
|
||||||
|
protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType
|
||||||
|
{
|
||||||
|
$accountTypeId = intval($accountTypeId);
|
||||||
|
if ($accountTypeId > 0) {
|
||||||
|
return AccountType::find($accountTypeId);
|
||||||
|
}
|
||||||
|
$type = config('firefly.accountTypeByIdentifier.' . strval($accountType));
|
||||||
|
|
||||||
|
return AccountType::whereType($type)->first();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -47,6 +47,7 @@ class AccountFormRequest extends Request
|
|||||||
'name' => $this->string('name'),
|
'name' => $this->string('name'),
|
||||||
'active' => $this->boolean('active'),
|
'active' => $this->boolean('active'),
|
||||||
'accountType' => $this->string('what'),
|
'accountType' => $this->string('what'),
|
||||||
|
'account_type_id' => 0,
|
||||||
'currency_id' => $this->integer('currency_id'),
|
'currency_id' => $this->integer('currency_id'),
|
||||||
'virtualBalance' => $this->string('virtualBalance'),
|
'virtualBalance' => $this->string('virtualBalance'),
|
||||||
'iban' => $this->string('iban'),
|
'iban' => $this->string('iban'),
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ namespace FireflyIII\Repositories\Account;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use DB;
|
use DB;
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
|
use FireflyIII\Factory\AccountFactory;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountMeta;
|
use FireflyIII\Models\AccountMeta;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
@@ -241,32 +242,15 @@ class AccountRepository implements AccountRepositoryInterface
|
|||||||
* @param array $data
|
* @param array $data
|
||||||
*
|
*
|
||||||
* @return Account
|
* @return Account
|
||||||
*
|
|
||||||
* @throws FireflyException
|
|
||||||
*/
|
*/
|
||||||
public function store(array $data): Account
|
public function store(array $data): Account
|
||||||
{
|
{
|
||||||
$newAccount = $this->storeAccount($data);
|
/** @var AccountFactory $factory */
|
||||||
$this->updateMetadata($newAccount, $data);
|
$factory = app(AccountFactory::class);
|
||||||
|
$factory->setUser($this->user);
|
||||||
|
$account = $factory->create($data);
|
||||||
|
|
||||||
if ($this->validOpeningBalanceData($data)) {
|
return $account;
|
||||||
$this->updateInitialBalance($newAccount, $data);
|
|
||||||
|
|
||||||
// update note:
|
|
||||||
if (isset($data['notes'])) {
|
|
||||||
$this->updateNote($newAccount, $data['notes']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $newAccount;
|
|
||||||
}
|
|
||||||
$this->deleteInitialBalance($newAccount);
|
|
||||||
|
|
||||||
// update note:
|
|
||||||
if (isset($data['notes'])) {
|
|
||||||
$this->updateNote($newAccount, $data['notes']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $newAccount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,9 +25,11 @@ namespace FireflyIII\Services\Internal\Support;
|
|||||||
|
|
||||||
use FireflyIII\Exceptions\FireflyException;
|
use FireflyIII\Exceptions\FireflyException;
|
||||||
use FireflyIII\Factory\AccountFactory;
|
use FireflyIII\Factory\AccountFactory;
|
||||||
|
use FireflyIII\Factory\AccountMetaFactory;
|
||||||
use FireflyIII\Factory\TransactionFactory;
|
use FireflyIII\Factory\TransactionFactory;
|
||||||
use FireflyIII\Factory\TransactionJournalFactory;
|
use FireflyIII\Factory\TransactionJournalFactory;
|
||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\Models\AccountMeta;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Models\Note;
|
use FireflyIII\Models\Note;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
@@ -44,6 +46,13 @@ use Validator;
|
|||||||
*/
|
*/
|
||||||
trait AccountServiceTrait
|
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
|
* @param Account $account
|
||||||
*
|
*
|
||||||
@@ -56,7 +65,7 @@ trait AccountServiceTrait
|
|||||||
$openingBalance = $this->getIBJournal($account);
|
$openingBalance = $this->getIBJournal($account);
|
||||||
|
|
||||||
// opening balance data? update it!
|
// opening balance data? update it!
|
||||||
if (null !== $openingBalance->id) {
|
if (null !== $openingBalance) {
|
||||||
Log::debug('Opening balance journal found, delete journal.');
|
Log::debug('Opening balance journal found, delete journal.');
|
||||||
$openingBalance->delete();
|
$openingBalance->delete();
|
||||||
|
|
||||||
@@ -216,7 +225,6 @@ trait AccountServiceTrait
|
|||||||
$factory = app(AccountFactory::class);
|
$factory = app(AccountFactory::class);
|
||||||
$factory->setUser($user);
|
$factory->setUser($user);
|
||||||
|
|
||||||
|
|
||||||
return $factory->findOrCreate($name, AccountType::INITIAL_BALANCE);
|
return $factory->findOrCreate($name, AccountType::INITIAL_BALANCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,27 +260,6 @@ trait AccountServiceTrait
|
|||||||
return true;
|
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 Account $account
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
@@ -282,7 +269,7 @@ trait AccountServiceTrait
|
|||||||
*
|
*
|
||||||
* @throws \Exception
|
* @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'];
|
$date = $data['openingBalanceDate'];
|
||||||
$amount = strval($data['openingBalance']);
|
$amount = strval($data['openingBalance']);
|
||||||
@@ -323,13 +310,51 @@ trait AccountServiceTrait
|
|||||||
return true;
|
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 Account $account
|
||||||
* @param string $note
|
* @param string $note
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function updateNote(Account $account, string $note): bool
|
public function updateNote(Account $account, string $note): bool
|
||||||
{
|
{
|
||||||
if (0 === strlen($note)) {
|
if (0 === strlen($note)) {
|
||||||
$dbNote = $account->notes()->first();
|
$dbNote = $account->notes()->first();
|
||||||
@@ -349,4 +374,25 @@ trait AccountServiceTrait
|
|||||||
|
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -39,12 +39,7 @@ class AccountUpdateService
|
|||||||
{
|
{
|
||||||
use AccountServiceTrait;
|
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.
|
* Update account data.
|
||||||
@@ -86,44 +81,4 @@ class AccountUpdateService
|
|||||||
|
|
||||||
return $account;
|
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