mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
chore: code cleanup.
This commit is contained in:
@@ -38,8 +38,8 @@ use FireflyIII\Models\TransactionGroup;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Services\Internal\Destroy\TransactionGroupDestroyService;
|
||||
use JsonException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
use Validator;
|
||||
|
||||
/**
|
||||
@@ -154,7 +154,7 @@ trait AccountServiceTrait
|
||||
if (is_bool($data[$field]) && true === $data[$field]) {
|
||||
$data[$field] = 1;
|
||||
}
|
||||
if($data[$field] instanceof Carbon) {
|
||||
if ($data[$field] instanceof Carbon) {
|
||||
$data[$field] = $data[$field]->toAtomString();
|
||||
}
|
||||
|
||||
@@ -214,6 +214,97 @@ trait AccountServiceTrait
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function createCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug('Now going to create an createCreditTransaction.');
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not make an liability credit group.');
|
||||
throw new FireflyException('Amount for new liability credit was unexpectedly 0.');
|
||||
}
|
||||
|
||||
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
|
||||
|
||||
// set source and/or destination based on whether the amount is positive or negative.
|
||||
// first, assume the amount is positive and go from there:
|
||||
// if amount is positive ("I am owed this debt"), source is special account, destination is the liability.
|
||||
$sourceId = null;
|
||||
$sourceName = trans('firefly.liability_credit_description', ['account' => $account->name], $language);
|
||||
$destId = $account->id;
|
||||
$destName = null;
|
||||
if (-1 === bccomp($openingBalance, '0')) {
|
||||
// amount is negative, reverse it
|
||||
$sourceId = $account->id;
|
||||
$sourceName = null;
|
||||
$destId = null;
|
||||
$destName = trans('firefly.liability_credit_description', ['account' => $account->name], $language);
|
||||
}
|
||||
|
||||
// amount must be positive for the transaction to work.
|
||||
$amount = app('steam')->positive($openingBalance);
|
||||
|
||||
// get or grab currency:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// submit to factory:
|
||||
$submission = [
|
||||
'group_title' => null,
|
||||
'user' => $account->user_id,
|
||||
'transactions' => [
|
||||
[
|
||||
'type' => 'Liability credit',
|
||||
'date' => $openingBalanceDate,
|
||||
'source_id' => $sourceId,
|
||||
'source_name' => $sourceName,
|
||||
'destination_id' => $destId,
|
||||
'destination_name' => $destName,
|
||||
'user' => $account->user_id,
|
||||
'currency_id' => $currency->id,
|
||||
'order' => 0,
|
||||
'amount' => $amount,
|
||||
'foreign_amount' => null,
|
||||
'description' => trans('firefly.liability_credit_description', ['account' => $account->name]),
|
||||
'budget_id' => null,
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => null,
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'reconciled' => false,
|
||||
'notes' => null,
|
||||
'tags' => [],
|
||||
],
|
||||
],
|
||||
];
|
||||
Log::debug('Going for submission in createCreditTransaction', $submission);
|
||||
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
@@ -307,366 +398,6 @@ trait AccountServiceTrait
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete TransactionGroup with liability credit in it.
|
||||
*
|
||||
* @param Account $account
|
||||
*/
|
||||
protected function deleteCreditTransaction(Account $account): void
|
||||
{
|
||||
Log::debug(sprintf('deleteCreditTransaction() for account #%d', $account->id));
|
||||
$creditGroup = $this->getCreditTransaction($account);
|
||||
|
||||
if (null !== $creditGroup) {
|
||||
Log::debug('Credit journal found, delete journal.');
|
||||
/** @var TransactionGroupDestroyService $service */
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
$service->destroy($creditGroup);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the credit transaction group, or NULL if it does not exist.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return TransactionGroup|null
|
||||
*/
|
||||
protected function getCreditTransaction(Account $account): ?TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now at %s', __METHOD__));
|
||||
|
||||
return $this->accountRepository->getCreditTransactionGroup($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currencyId
|
||||
* @param string $currencyCode
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function getCurrency(int $currencyId, string $currencyCode): TransactionCurrency
|
||||
{
|
||||
// find currency, or use default currency instead.
|
||||
/** @var TransactionCurrencyFactory $factory */
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
/** @var TransactionCurrency|null $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the opposing "credit liability" transaction for credit liabilities.
|
||||
*
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function updateCreditTransaction(Account $account, string $direction, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not update liability credit/debit group.');
|
||||
throw new FireflyException('Amount for update liability credit/debit was unexpectedly 0.');
|
||||
}
|
||||
// if direction is "debit" (i owe this debt), amount is negative.
|
||||
// which means the liability will have a negative balance which the user must fill.
|
||||
$openingBalance = app('steam')->negative($openingBalance);
|
||||
|
||||
// if direction is "credit" (I am owed this debt), amount is positive.
|
||||
// which means the liability will have a positive balance which is drained when its paid back into any asset.
|
||||
if ('credit' === $direction) {
|
||||
$openingBalance = app('steam')->positive($openingBalance);
|
||||
}
|
||||
|
||||
// create if not exists:
|
||||
$clGroup = $this->getCreditTransaction($account);
|
||||
if (null === $clGroup) {
|
||||
return $this->createCreditTransaction($account, $openingBalance, $openingBalanceDate);
|
||||
}
|
||||
// if exists, update:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// simply grab the first journal and change it:
|
||||
$journal = $this->getObJournal($clGroup);
|
||||
$clTransaction = $this->getOBTransaction($journal, $account);
|
||||
$accountTransaction = $this->getNotOBTransaction($journal, $account);
|
||||
$journal->date = $openingBalanceDate;
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
|
||||
// account always gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// CL account always loses money:
|
||||
$clTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$clTransaction->transaction_currency_id = $currency->id;
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$clTransaction->save();
|
||||
$journal->save();
|
||||
$clGroup->refresh();
|
||||
|
||||
return $clGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function createCreditTransaction(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug('Now going to create an createCreditTransaction.');
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not make an liability credit group.');
|
||||
throw new FireflyException('Amount for new liability credit was unexpectedly 0.');
|
||||
}
|
||||
|
||||
$language = app('preferences')->getForUser($account->user, 'language', 'en_US')->data;
|
||||
|
||||
// set source and/or destination based on whether the amount is positive or negative.
|
||||
// first, assume the amount is positive and go from there:
|
||||
// if amount is positive ("I am owed this debt"), source is special account, destination is the liability.
|
||||
$sourceId = null;
|
||||
$sourceName = trans('firefly.liability_credit_description', ['account' => $account->name], $language);
|
||||
$destId = $account->id;
|
||||
$destName = null;
|
||||
if (-1 === bccomp($openingBalance, '0')) {
|
||||
// amount is negative, reverse it
|
||||
$sourceId = $account->id;
|
||||
$sourceName = null;
|
||||
$destId = null;
|
||||
$destName = trans('firefly.liability_credit_description', ['account' => $account->name], $language);
|
||||
}
|
||||
|
||||
// amount must be positive for the transaction to work.
|
||||
$amount = app('steam')->positive($openingBalance);
|
||||
|
||||
// get or grab currency:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// submit to factory:
|
||||
$submission = [
|
||||
'group_title' => null,
|
||||
'user' => $account->user_id,
|
||||
'transactions' => [
|
||||
[
|
||||
'type' => 'Liability credit',
|
||||
'date' => $openingBalanceDate,
|
||||
'source_id' => $sourceId,
|
||||
'source_name' => $sourceName,
|
||||
'destination_id' => $destId,
|
||||
'destination_name' => $destName,
|
||||
'user' => $account->user_id,
|
||||
'currency_id' => $currency->id,
|
||||
'order' => 0,
|
||||
'amount' => $amount,
|
||||
'foreign_amount' => null,
|
||||
'description' => trans('firefly.liability_credit_description', ['account' => $account->name]),
|
||||
'budget_id' => null,
|
||||
'budget_name' => null,
|
||||
'category_id' => null,
|
||||
'category_name' => null,
|
||||
'piggy_bank_id' => null,
|
||||
'piggy_bank_name' => null,
|
||||
'reconciled' => false,
|
||||
'notes' => null,
|
||||
'tags' => [],
|
||||
],
|
||||
],
|
||||
];
|
||||
Log::debug('Going for submission in createCreditTransaction', $submission);
|
||||
|
||||
/** @var TransactionGroupFactory $factory */
|
||||
$factory = app(TransactionGroupFactory::class);
|
||||
$factory->setUser($account->user);
|
||||
|
||||
try {
|
||||
$group = $factory->create($submission);
|
||||
} catch (DuplicateTransactionException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::error($e->getTraceAsString());
|
||||
throw new FireflyException($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO refactor to "getfirstjournal"
|
||||
*
|
||||
* @param TransactionGroup $group
|
||||
*
|
||||
* @return TransactionJournal
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getObJournal(TransactionGroup $group): TransactionJournal
|
||||
{
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $group->transactionJournals()->first();
|
||||
if (null === $journal) {
|
||||
throw new FireflyException(sprintf('Group #%d has no OB journal', $group->id));
|
||||
}
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Rename to getOpposingTransaction
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', '!=', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getNotOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get non-OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update or create the opening balance group.
|
||||
* Since opening balance and date can still be empty strings, it may fail.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function updateOBGroupV2(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
// create if not exists:
|
||||
$obGroup = $this->getOBGroup($account);
|
||||
if (null === $obGroup) {
|
||||
return $this->createOBGroupV2($account, $openingBalance, $openingBalanceDate);
|
||||
}
|
||||
Log::debug('Update OB group');
|
||||
|
||||
// if exists, update:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// simply grab the first journal and change it:
|
||||
$journal = $this->getObJournal($obGroup);
|
||||
$obTransaction = $this->getOBTransaction($journal, $account);
|
||||
$accountTransaction = $this->getNotOBTransaction($journal, $account);
|
||||
$journal->date = $openingBalanceDate;
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
|
||||
|
||||
// if amount is negative:
|
||||
if (1 === bccomp('0', $openingBalance)) {
|
||||
Log::debug('Amount is negative.');
|
||||
// account transaction loses money:
|
||||
$accountTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// OB account transaction gains money
|
||||
$obTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$obTransaction->transaction_currency_id = $currency->id;
|
||||
}
|
||||
if (-1 === bccomp('0', $openingBalance)) {
|
||||
Log::debug('Amount is positive.');
|
||||
// account gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// OB account loses money:
|
||||
$obTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$obTransaction->transaction_currency_id = $currency->id;
|
||||
}
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$obTransaction->save();
|
||||
$journal->save();
|
||||
$obGroup->refresh();
|
||||
|
||||
return $obGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
@@ -758,4 +489,273 @@ trait AccountServiceTrait
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete TransactionGroup with liability credit in it.
|
||||
*
|
||||
* @param Account $account
|
||||
*/
|
||||
protected function deleteCreditTransaction(Account $account): void
|
||||
{
|
||||
Log::debug(sprintf('deleteCreditTransaction() for account #%d', $account->id));
|
||||
$creditGroup = $this->getCreditTransaction($account);
|
||||
|
||||
if (null !== $creditGroup) {
|
||||
Log::debug('Credit journal found, delete journal.');
|
||||
/** @var TransactionGroupDestroyService $service */
|
||||
$service = app(TransactionGroupDestroyService::class);
|
||||
$service->destroy($creditGroup);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the credit transaction group, or NULL if it does not exist.
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return TransactionGroup|null
|
||||
*/
|
||||
protected function getCreditTransaction(Account $account): ?TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now at %s', __METHOD__));
|
||||
|
||||
return $this->accountRepository->getCreditTransactionGroup($account);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $currencyId
|
||||
* @param string $currencyCode
|
||||
*
|
||||
* @return TransactionCurrency
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
*/
|
||||
protected function getCurrency(int $currencyId, string $currencyCode): TransactionCurrency
|
||||
{
|
||||
// find currency, or use default currency instead.
|
||||
/** @var TransactionCurrencyFactory $factory */
|
||||
$factory = app(TransactionCurrencyFactory::class);
|
||||
/** @var TransactionCurrency|null $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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the opposing "credit liability" transaction for credit liabilities.
|
||||
*
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function updateCreditTransaction(Account $account, string $direction, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
|
||||
if (0 === bccomp($openingBalance, '0')) {
|
||||
Log::debug('Amount is zero, so will not update liability credit/debit group.');
|
||||
throw new FireflyException('Amount for update liability credit/debit was unexpectedly 0.');
|
||||
}
|
||||
// if direction is "debit" (i owe this debt), amount is negative.
|
||||
// which means the liability will have a negative balance which the user must fill.
|
||||
$openingBalance = app('steam')->negative($openingBalance);
|
||||
|
||||
// if direction is "credit" (I am owed this debt), amount is positive.
|
||||
// which means the liability will have a positive balance which is drained when its paid back into any asset.
|
||||
if ('credit' === $direction) {
|
||||
$openingBalance = app('steam')->positive($openingBalance);
|
||||
}
|
||||
|
||||
// create if not exists:
|
||||
$clGroup = $this->getCreditTransaction($account);
|
||||
if (null === $clGroup) {
|
||||
return $this->createCreditTransaction($account, $openingBalance, $openingBalanceDate);
|
||||
}
|
||||
// if exists, update:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// simply grab the first journal and change it:
|
||||
$journal = $this->getObJournal($clGroup);
|
||||
$clTransaction = $this->getOBTransaction($journal, $account);
|
||||
$accountTransaction = $this->getNotOBTransaction($journal, $account);
|
||||
$journal->date = $openingBalanceDate;
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
|
||||
// account always gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// CL account always loses money:
|
||||
$clTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$clTransaction->transaction_currency_id = $currency->id;
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$clTransaction->save();
|
||||
$journal->save();
|
||||
$clGroup->refresh();
|
||||
|
||||
return $clGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update or create the opening balance group.
|
||||
* Since opening balance and date can still be empty strings, it may fail.
|
||||
*
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function updateOBGroupV2(Account $account, string $openingBalance, Carbon $openingBalanceDate): TransactionGroup
|
||||
{
|
||||
Log::debug(sprintf('Now in %s', __METHOD__));
|
||||
// create if not exists:
|
||||
$obGroup = $this->getOBGroup($account);
|
||||
if (null === $obGroup) {
|
||||
return $this->createOBGroupV2($account, $openingBalance, $openingBalanceDate);
|
||||
}
|
||||
Log::debug('Update OB group');
|
||||
|
||||
// if exists, update:
|
||||
$currency = $this->accountRepository->getAccountCurrency($account);
|
||||
if (null === $currency) {
|
||||
$currency = app('default')->getDefaultCurrencyByUser($account->user);
|
||||
}
|
||||
|
||||
// simply grab the first journal and change it:
|
||||
$journal = $this->getObJournal($obGroup);
|
||||
$obTransaction = $this->getOBTransaction($journal, $account);
|
||||
$accountTransaction = $this->getNotOBTransaction($journal, $account);
|
||||
$journal->date = $openingBalanceDate;
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
|
||||
|
||||
// if amount is negative:
|
||||
if (1 === bccomp('0', $openingBalance)) {
|
||||
Log::debug('Amount is negative.');
|
||||
// account transaction loses money:
|
||||
$accountTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// OB account transaction gains money
|
||||
$obTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$obTransaction->transaction_currency_id = $currency->id;
|
||||
}
|
||||
if (-1 === bccomp('0', $openingBalance)) {
|
||||
Log::debug('Amount is positive.');
|
||||
// account gains money:
|
||||
$accountTransaction->amount = app('steam')->positive($openingBalance);
|
||||
$accountTransaction->transaction_currency_id = $currency->id;
|
||||
|
||||
// OB account loses money:
|
||||
$obTransaction->amount = app('steam')->negative($openingBalance);
|
||||
$obTransaction->transaction_currency_id = $currency->id;
|
||||
}
|
||||
// save both
|
||||
$accountTransaction->save();
|
||||
$obTransaction->save();
|
||||
$journal->save();
|
||||
$obGroup->refresh();
|
||||
|
||||
return $obGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getNotOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get non-OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Rename to getOpposingTransaction
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $account
|
||||
*
|
||||
* @return Transaction
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getOBTransaction(TransactionJournal $journal, Account $account): Transaction
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->where('account_id', '!=', $account->id)->first();
|
||||
if (null === $transaction) {
|
||||
throw new FireflyException(sprintf('Could not get OB transaction for journal #%d', $journal->id));
|
||||
}
|
||||
|
||||
return $transaction;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO refactor to "getfirstjournal"
|
||||
*
|
||||
* @param TransactionGroup $group
|
||||
*
|
||||
* @return TransactionJournal
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getObJournal(TransactionGroup $group): TransactionJournal
|
||||
{
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $group->transactionJournals()->first();
|
||||
if (null === $journal) {
|
||||
throw new FireflyException(sprintf('Group #%d has no OB journal', $group->id));
|
||||
}
|
||||
|
||||
return $journal;
|
||||
}
|
||||
}
|
||||
|
@@ -80,20 +80,19 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Account|null $account
|
||||
*/
|
||||
private function processGroup(): void
|
||||
public function setAccount(?Account $account): void
|
||||
{
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($this->group->transactionJournals as $journal) {
|
||||
try {
|
||||
$this->findByJournal($journal);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getTraceAsString());
|
||||
Log::error(sprintf('Could not find work account for transaction group #%d.', $this->group->id));
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('Done with %s', __METHOD__));
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
*/
|
||||
public function setGroup(TransactionGroup $group): void
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,17 +117,6 @@ class CreditRecalculateService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
return $this->getAccountByDirection($journal, '<');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $direction
|
||||
@@ -162,6 +150,17 @@ class CreditRecalculateService
|
||||
return $this->getAccountByDirection($journal, '>');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
return $this->getAccountByDirection($journal, '<');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -177,49 +176,20 @@ class CreditRecalculateService
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function processWork(): void
|
||||
private function processGroup(): void
|
||||
{
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
foreach ($this->work as $account) {
|
||||
$this->processWorkAccount($account);
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($this->group->transactionJournals as $journal) {
|
||||
try {
|
||||
$this->findByJournal($journal);
|
||||
} catch (FireflyException $e) {
|
||||
Log::error($e->getTraceAsString());
|
||||
Log::error(sprintf('Could not find work account for transaction group #%d.', $this->group->id));
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('Done with %s', __METHOD__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
private function processWorkAccount(Account $account): void
|
||||
{
|
||||
Log::debug(sprintf('Now in %s(#%d)', __METHOD__, $account->id));
|
||||
|
||||
// get opening balance (if present)
|
||||
$this->repository->setUser($account->user);
|
||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
|
||||
// amount is positive or negative, doesn't matter.
|
||||
$factory->crud($account, 'start_of_debt', $startOfDebt);
|
||||
|
||||
// get direction of liability:
|
||||
$direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
|
||||
|
||||
// now loop all transactions (except opening balance and credit thing)
|
||||
$transactions = $account->transactions()->get();
|
||||
Log::debug(sprintf('Going to process %d transaction(s)', $transactions->count()));
|
||||
Log::debug(sprintf('Account currency is #%d (%s)', $account->id, $this->repository->getAccountCurrency($account)?->code));
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
|
||||
Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $direction
|
||||
@@ -341,18 +311,48 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
*
|
||||
*/
|
||||
public function setAccount(?Account $account): void
|
||||
private function processWork(): void
|
||||
{
|
||||
$this->account = $account;
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
foreach ($this->work as $account) {
|
||||
$this->processWorkAccount($account);
|
||||
}
|
||||
Log::debug(sprintf('Done with %s', __METHOD__));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
* @param Account $account
|
||||
*/
|
||||
public function setGroup(TransactionGroup $group): void
|
||||
private function processWorkAccount(Account $account): void
|
||||
{
|
||||
$this->group = $group;
|
||||
Log::debug(sprintf('Now in %s(#%d)', __METHOD__, $account->id));
|
||||
|
||||
// get opening balance (if present)
|
||||
$this->repository->setUser($account->user);
|
||||
$startOfDebt = $this->repository->getOpeningBalanceAmount($account) ?? '0';
|
||||
$leftOfDebt = app('steam')->positive($startOfDebt);
|
||||
|
||||
/** @var AccountMetaFactory $factory */
|
||||
$factory = app(AccountMetaFactory::class);
|
||||
|
||||
// amount is positive or negative, doesn't matter.
|
||||
$factory->crud($account, 'start_of_debt', $startOfDebt);
|
||||
|
||||
// get direction of liability:
|
||||
$direction = (string)$this->repository->getMetaValue($account, 'liability_direction');
|
||||
|
||||
// now loop all transactions (except opening balance and credit thing)
|
||||
$transactions = $account->transactions()->get();
|
||||
Log::debug(sprintf('Going to process %d transaction(s)', $transactions->count()));
|
||||
Log::debug(sprintf('Account currency is #%d (%s)', $account->id, $this->repository->getAccountCurrency($account)?->code));
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
|
||||
Log::debug(sprintf('Done with %s(#%d)', __METHOD__, $account->id));
|
||||
}
|
||||
}
|
||||
|
@@ -117,240 +117,6 @@ trait JournalServiceTrait
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountById(array $data, array $types): ?Account
|
||||
{
|
||||
// first attempt, find by ID.
|
||||
if (null !== $data['id']) {
|
||||
$search = $this->accountRepository->find((int)$data['id']);
|
||||
if (null !== $search && in_array($search->accountType->type, $types, true)) {
|
||||
Log::debug(
|
||||
sprintf('Found "account_id" object: #%d, "%s" of type %s (1)', $search->id, $search->name, $search->accountType->type)
|
||||
);
|
||||
return $search;
|
||||
}
|
||||
if (null !== $search && 0 === count($types)) {
|
||||
Log::debug(
|
||||
sprintf('Found "account_id" object: #%d, "%s" of type %s (2)', $search->id, $search->name, $search->accountType->type)
|
||||
);
|
||||
return $search;
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('Found no account by ID #%d of types', $data['id']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountByIban(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
|
||||
return $account;
|
||||
}
|
||||
if (null === $data['iban'] || '' === $data['iban']) {
|
||||
Log::debug('IBAN is empty, will not search for IBAN.');
|
||||
return null;
|
||||
}
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByIbanNull($data['iban'], [$types[0]]);
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByIbanNull($data['iban'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found "account_iban" object: #%d, %s', $source->id, $source->name));
|
||||
|
||||
return $source;
|
||||
}
|
||||
Log::debug(sprintf('Found no account with IBAN "%s" of expected types', $data['iban']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountByNumber(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
|
||||
return $account;
|
||||
}
|
||||
if (null === $data['number'] || '' === $data['number']) {
|
||||
Log::debug('Account number is empty, will not search for account number.');
|
||||
return null;
|
||||
}
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByAccountNumber((string)$data['number'], [$types[0]]);
|
||||
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByAccountNumber((string)$data['number'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found account: #%d, %s', $source->id, $source->name));
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('Found no account with account number "%s" of expected types', $data['number']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountByName(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
|
||||
return $account;
|
||||
}
|
||||
if (null === $data['name'] || '' === $data['name']) {
|
||||
Log::debug('Account name is empty, will not search for account name.');
|
||||
return null;
|
||||
}
|
||||
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByName($data['name'], [$types[0]]);
|
||||
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByName($data['name'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found "account_name" object: #%d, %s', $source->id, $source->name));
|
||||
|
||||
return $source;
|
||||
}
|
||||
Log::debug(sprintf('Found no account with account name "%s" of expected types', $data['name']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
* @return null|string
|
||||
*/
|
||||
private function getCreatableType(array $types): ?string
|
||||
{
|
||||
$result = null;
|
||||
$list = config('firefly.dynamic_creation_allowed');
|
||||
/** @var string $type */
|
||||
foreach ($types as $type) {
|
||||
if (true === in_array($type, $list, true)) {
|
||||
$result = $type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param string $preferredType
|
||||
*
|
||||
* @return Account|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function createAccount(?Account $account, array $data, string $preferredType): ?Account
|
||||
{
|
||||
Log::debug('Now in createAccount()', $data);
|
||||
// return new account.
|
||||
if (null !== $account) {
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Was given %s account #%d ("%s") so will simply return that.',
|
||||
$account->accountType->type,
|
||||
$account->id,
|
||||
$account->name
|
||||
)
|
||||
);
|
||||
}
|
||||
if (null === $account) {
|
||||
// final attempt, create it.
|
||||
if (AccountType::ASSET === $preferredType) {
|
||||
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s', json_encode($data)));
|
||||
}
|
||||
// fix name of account if only IBAN is given:
|
||||
if ('' === (string)$data['name'] && '' !== (string)$data['iban']) {
|
||||
Log::debug(sprintf('Account name is now IBAN ("%s")', $data['iban']));
|
||||
$data['name'] = $data['iban'];
|
||||
}
|
||||
// fix name of account if only number is given:
|
||||
if ('' === (string)$data['name'] && '' !== (string)$data['number']) {
|
||||
Log::debug(sprintf('Account name is now account number ("%s")', $data['number']));
|
||||
$data['name'] = $data['number'];
|
||||
}
|
||||
// if name is still NULL, return NULL.
|
||||
if ('' === (string)$data['name']) {
|
||||
Log::debug('Account name is still NULL, return NULL.');
|
||||
return null;
|
||||
}
|
||||
//$data['name'] = $data['name'] ?? '(no name)';
|
||||
|
||||
$account = $this->accountRepository->store(
|
||||
[
|
||||
'account_type_id' => null,
|
||||
'account_type_name' => $preferredType,
|
||||
'name' => $data['name'],
|
||||
'virtual_balance' => null,
|
||||
'active' => true,
|
||||
'iban' => $data['iban'],
|
||||
'currency_id' => $data['currency_id'] ?? null,
|
||||
'order' => $this->accountRepository->maxOrder($preferredType),
|
||||
]
|
||||
);
|
||||
// store BIC
|
||||
if (null !== $data['bic']) {
|
||||
/** @var AccountMetaFactory $metaFactory */
|
||||
$metaFactory = app(AccountMetaFactory::class);
|
||||
$metaFactory->create(['account_id' => $account->id, 'name' => 'BIC', 'data' => $data['bic']]);
|
||||
}
|
||||
// store account number
|
||||
if (null !== $data['number']) {
|
||||
/** @var AccountMetaFactory $metaFactory */
|
||||
$metaFactory = app(AccountMetaFactory::class);
|
||||
$metaFactory->create(['account_id' => $account->id, 'name' => 'account_number', 'data' => $data['number']]);
|
||||
}
|
||||
}
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function getCashAccount(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
// return cash account.
|
||||
if (null === $account && '' === (string)$data['name']
|
||||
&& in_array(AccountType::CASH, $types, true)) {
|
||||
$account = $this->accountRepository->getCashAccount();
|
||||
}
|
||||
Log::debug('Cannot return cash account, return input instead.');
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
*
|
||||
@@ -501,4 +267,238 @@ trait JournalServiceTrait
|
||||
$journal->tags()->sync($set);
|
||||
Log::debug('Done!');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param string $preferredType
|
||||
*
|
||||
* @return Account|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function createAccount(?Account $account, array $data, string $preferredType): ?Account
|
||||
{
|
||||
Log::debug('Now in createAccount()', $data);
|
||||
// return new account.
|
||||
if (null !== $account) {
|
||||
Log::debug(
|
||||
sprintf(
|
||||
'Was given %s account #%d ("%s") so will simply return that.',
|
||||
$account->accountType->type,
|
||||
$account->id,
|
||||
$account->name
|
||||
)
|
||||
);
|
||||
}
|
||||
if (null === $account) {
|
||||
// final attempt, create it.
|
||||
if (AccountType::ASSET === $preferredType) {
|
||||
throw new FireflyException(sprintf('TransactionFactory: Cannot create asset account with these values: %s', json_encode($data)));
|
||||
}
|
||||
// fix name of account if only IBAN is given:
|
||||
if ('' === (string)$data['name'] && '' !== (string)$data['iban']) {
|
||||
Log::debug(sprintf('Account name is now IBAN ("%s")', $data['iban']));
|
||||
$data['name'] = $data['iban'];
|
||||
}
|
||||
// fix name of account if only number is given:
|
||||
if ('' === (string)$data['name'] && '' !== (string)$data['number']) {
|
||||
Log::debug(sprintf('Account name is now account number ("%s")', $data['number']));
|
||||
$data['name'] = $data['number'];
|
||||
}
|
||||
// if name is still NULL, return NULL.
|
||||
if ('' === (string)$data['name']) {
|
||||
Log::debug('Account name is still NULL, return NULL.');
|
||||
return null;
|
||||
}
|
||||
//$data['name'] = $data['name'] ?? '(no name)';
|
||||
|
||||
$account = $this->accountRepository->store(
|
||||
[
|
||||
'account_type_id' => null,
|
||||
'account_type_name' => $preferredType,
|
||||
'name' => $data['name'],
|
||||
'virtual_balance' => null,
|
||||
'active' => true,
|
||||
'iban' => $data['iban'],
|
||||
'currency_id' => $data['currency_id'] ?? null,
|
||||
'order' => $this->accountRepository->maxOrder($preferredType),
|
||||
]
|
||||
);
|
||||
// store BIC
|
||||
if (null !== $data['bic']) {
|
||||
/** @var AccountMetaFactory $metaFactory */
|
||||
$metaFactory = app(AccountMetaFactory::class);
|
||||
$metaFactory->create(['account_id' => $account->id, 'name' => 'BIC', 'data' => $data['bic']]);
|
||||
}
|
||||
// store account number
|
||||
if (null !== $data['number']) {
|
||||
/** @var AccountMetaFactory $metaFactory */
|
||||
$metaFactory = app(AccountMetaFactory::class);
|
||||
$metaFactory->create(['account_id' => $account->id, 'name' => 'account_number', 'data' => $data['number']]);
|
||||
}
|
||||
}
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountByIban(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
|
||||
return $account;
|
||||
}
|
||||
if (null === $data['iban'] || '' === $data['iban']) {
|
||||
Log::debug('IBAN is empty, will not search for IBAN.');
|
||||
return null;
|
||||
}
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByIbanNull($data['iban'], [$types[0]]);
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByIbanNull($data['iban'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found "account_iban" object: #%d, %s', $source->id, $source->name));
|
||||
|
||||
return $source;
|
||||
}
|
||||
Log::debug(sprintf('Found no account with IBAN "%s" of expected types', $data['iban']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountById(array $data, array $types): ?Account
|
||||
{
|
||||
// first attempt, find by ID.
|
||||
if (null !== $data['id']) {
|
||||
$search = $this->accountRepository->find((int)$data['id']);
|
||||
if (null !== $search && in_array($search->accountType->type, $types, true)) {
|
||||
Log::debug(
|
||||
sprintf('Found "account_id" object: #%d, "%s" of type %s (1)', $search->id, $search->name, $search->accountType->type)
|
||||
);
|
||||
return $search;
|
||||
}
|
||||
if (null !== $search && 0 === count($types)) {
|
||||
Log::debug(
|
||||
sprintf('Found "account_id" object: #%d, "%s" of type %s (2)', $search->id, $search->name, $search->accountType->type)
|
||||
);
|
||||
return $search;
|
||||
}
|
||||
}
|
||||
Log::debug(sprintf('Found no account by ID #%d of types', $data['id']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountByName(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
|
||||
return $account;
|
||||
}
|
||||
if (null === $data['name'] || '' === $data['name']) {
|
||||
Log::debug('Account name is empty, will not search for account name.');
|
||||
return null;
|
||||
}
|
||||
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByName($data['name'], [$types[0]]);
|
||||
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByName($data['name'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found "account_name" object: #%d, %s', $source->id, $source->name));
|
||||
|
||||
return $source;
|
||||
}
|
||||
Log::debug(sprintf('Found no account with account name "%s" of expected types', $data['name']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function findAccountByNumber(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
if (null !== $account) {
|
||||
Log::debug(sprintf('Already have account #%d ("%s"), return that.', $account->id, $account->name));
|
||||
return $account;
|
||||
}
|
||||
if (null === $data['number'] || '' === $data['number']) {
|
||||
Log::debug('Account number is empty, will not search for account number.');
|
||||
return null;
|
||||
}
|
||||
// find by preferred type.
|
||||
$source = $this->accountRepository->findByAccountNumber((string)$data['number'], [$types[0]]);
|
||||
|
||||
// or any expected type.
|
||||
$source = $source ?? $this->accountRepository->findByAccountNumber((string)$data['number'], $types);
|
||||
|
||||
if (null !== $source) {
|
||||
Log::debug(sprintf('Found account: #%d, %s', $source->id, $source->name));
|
||||
|
||||
return $source;
|
||||
}
|
||||
|
||||
Log::debug(sprintf('Found no account with account number "%s" of expected types', $data['number']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
private function getCashAccount(?Account $account, array $data, array $types): ?Account
|
||||
{
|
||||
// return cash account.
|
||||
if (null === $account && '' === (string)$data['name']
|
||||
&& in_array(AccountType::CASH, $types, true)) {
|
||||
$account = $this->accountRepository->getCashAccount();
|
||||
}
|
||||
Log::debug('Cannot return cash account, return input instead.');
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
* @return null|string
|
||||
*/
|
||||
private function getCreatableType(array $types): ?string
|
||||
{
|
||||
$result = null;
|
||||
$list = config('firefly.dynamic_creation_allowed');
|
||||
/** @var string $type */
|
||||
foreach ($types as $type) {
|
||||
if (true === in_array($type, $list, true)) {
|
||||
$result = $type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
@@ -40,8 +40,8 @@ use FireflyIII\Models\RecurrenceTransaction;
|
||||
use FireflyIII\Models\RecurrenceTransactionMeta;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Validation\AccountValidator;
|
||||
use JsonException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use JsonException;
|
||||
|
||||
/**
|
||||
* Trait RecurringTransactionTrait
|
||||
@@ -179,6 +179,32 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
|
||||
*/
|
||||
protected function deleteRepetitions(Recurrence $recurrence): void
|
||||
{
|
||||
$recurrence->recurrenceRepetitions()->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
|
||||
*/
|
||||
protected function deleteTransactions(Recurrence $recurrence): void
|
||||
{
|
||||
Log::debug('deleteTransactions()');
|
||||
/** @var RecurrenceTransaction $transaction */
|
||||
foreach ($recurrence->recurrenceTransactions as $transaction) {
|
||||
$transaction->recurrenceTransactionMeta()->delete();
|
||||
|
||||
$transaction->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $expectedTypes
|
||||
* @param int|null $accountId
|
||||
@@ -230,81 +256,6 @@ trait RecurringTransactionTrait
|
||||
return $result ?? $repository->getCashAccount();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $budgetId
|
||||
*/
|
||||
private function setBudget(RecurrenceTransaction $transaction, int $budgetId): void
|
||||
{
|
||||
$budgetFactory = app(BudgetFactory::class);
|
||||
$budgetFactory->setUser($transaction->recurrence->user);
|
||||
$budget = $budgetFactory->find($budgetId, null);
|
||||
if (null === $budget) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'budget_id')->first();
|
||||
if (null === $meta) {
|
||||
$meta = new RecurrenceTransactionMeta();
|
||||
$meta->rt_id = $transaction->id;
|
||||
$meta->name = 'budget_id';
|
||||
}
|
||||
$meta->value = $budget->id;
|
||||
$meta->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $billId
|
||||
*/
|
||||
private function setBill(RecurrenceTransaction $transaction, int $billId): void
|
||||
{
|
||||
$billFactory = app(BillFactory::class);
|
||||
$billFactory->setUser($transaction->recurrence->user);
|
||||
$bill = $billFactory->find($billId, null);
|
||||
if (null === $bill) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'bill_id')->first();
|
||||
if (null === $meta) {
|
||||
$meta = new RecurrenceTransactionMeta();
|
||||
$meta->rt_id = $transaction->id;
|
||||
$meta->name = 'bill_id';
|
||||
}
|
||||
$meta->value = $bill->id;
|
||||
$meta->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $categoryId
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function setCategory(RecurrenceTransaction $transaction, int $categoryId): void
|
||||
{
|
||||
$categoryFactory = app(CategoryFactory::class);
|
||||
$categoryFactory->setUser($transaction->recurrence->user);
|
||||
$category = $categoryFactory->findOrCreate($categoryId, null);
|
||||
if (null === $category) {
|
||||
// remove category:
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'category_id')->delete();
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'category_name')->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'category_name')->delete();
|
||||
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'category_id')->first();
|
||||
if (null === $meta) {
|
||||
$meta = new RecurrenceTransactionMeta();
|
||||
$meta->rt_id = $transaction->id;
|
||||
$meta->name = 'category_id';
|
||||
}
|
||||
$meta->value = $category->id;
|
||||
$meta->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $piggyId
|
||||
@@ -352,28 +303,77 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $billId
|
||||
*/
|
||||
protected function deleteRepetitions(Recurrence $recurrence): void
|
||||
private function setBill(RecurrenceTransaction $transaction, int $billId): void
|
||||
{
|
||||
$recurrence->recurrenceRepetitions()->delete();
|
||||
$billFactory = app(BillFactory::class);
|
||||
$billFactory->setUser($transaction->recurrence->user);
|
||||
$bill = $billFactory->find($billId, null);
|
||||
if (null === $bill) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'bill_id')->first();
|
||||
if (null === $meta) {
|
||||
$meta = new RecurrenceTransactionMeta();
|
||||
$meta->rt_id = $transaction->id;
|
||||
$meta->name = 'bill_id';
|
||||
}
|
||||
$meta->value = $bill->id;
|
||||
$meta->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
*
|
||||
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $budgetId
|
||||
*/
|
||||
protected function deleteTransactions(Recurrence $recurrence): void
|
||||
private function setBudget(RecurrenceTransaction $transaction, int $budgetId): void
|
||||
{
|
||||
Log::debug('deleteTransactions()');
|
||||
/** @var RecurrenceTransaction $transaction */
|
||||
foreach ($recurrence->recurrenceTransactions as $transaction) {
|
||||
$transaction->recurrenceTransactionMeta()->delete();
|
||||
|
||||
$transaction->delete();
|
||||
$budgetFactory = app(BudgetFactory::class);
|
||||
$budgetFactory->setUser($transaction->recurrence->user);
|
||||
$budget = $budgetFactory->find($budgetId, null);
|
||||
if (null === $budget) {
|
||||
return;
|
||||
}
|
||||
|
||||
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'budget_id')->first();
|
||||
if (null === $meta) {
|
||||
$meta = new RecurrenceTransactionMeta();
|
||||
$meta->rt_id = $transaction->id;
|
||||
$meta->name = 'budget_id';
|
||||
}
|
||||
$meta->value = $budget->id;
|
||||
$meta->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $categoryId
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function setCategory(RecurrenceTransaction $transaction, int $categoryId): void
|
||||
{
|
||||
$categoryFactory = app(CategoryFactory::class);
|
||||
$categoryFactory->setUser($transaction->recurrence->user);
|
||||
$category = $categoryFactory->findOrCreate($categoryId, null);
|
||||
if (null === $category) {
|
||||
// remove category:
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'category_id')->delete();
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'category_name')->delete();
|
||||
|
||||
return;
|
||||
}
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'category_name')->delete();
|
||||
$meta = $transaction->recurrenceTransactionMeta()->where('name', 'category_id')->first();
|
||||
if (null === $meta) {
|
||||
$meta = new RecurrenceTransactionMeta();
|
||||
$meta->rt_id = $transaction->id;
|
||||
$meta->name = 'category_id';
|
||||
}
|
||||
$meta->value = $category->id;
|
||||
$meta->save();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user