mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-12 09:52:20 +00:00
chore: reformat code.
This commit is contained in:
@@ -51,7 +51,7 @@ trait AccountServiceTrait
|
||||
protected AccountRepositoryInterface $accountRepository;
|
||||
|
||||
/**
|
||||
* @param null|string $iban
|
||||
* @param null|string $iban
|
||||
*
|
||||
* @return null|string
|
||||
*/
|
||||
@@ -76,7 +76,7 @@ trait AccountServiceTrait
|
||||
/**
|
||||
* Returns true if the data in the array is submitted but empty.
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -104,8 +104,8 @@ trait AccountServiceTrait
|
||||
*
|
||||
* TODO this method treats expense accounts and liabilities the same way (tries to save interest)
|
||||
*
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
*/
|
||||
public function updateMetaData(Account $account, array $data): void
|
||||
@@ -164,8 +164,8 @@ trait AccountServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $note
|
||||
* @param Account $account
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -192,7 +192,7 @@ trait AccountServiceTrait
|
||||
/**
|
||||
* Verify if array contains valid data to possibly store or update the opening balance.
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -214,99 +214,8 @@ trait AccountServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @param Account $account
|
||||
* @param array $data
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
@@ -398,9 +307,369 @@ trait AccountServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $openingBalance
|
||||
* @param Carbon $openingBalanceDate
|
||||
* 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
|
||||
* @param Carbon $openingBalanceDate
|
||||
*
|
||||
* @return TransactionGroup
|
||||
* @throws FireflyException
|
||||
@@ -488,273 +757,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,9 +36,9 @@ use Illuminate\Support\Facades\Log;
|
||||
trait BillServiceTrait
|
||||
{
|
||||
/**
|
||||
* @param Bill $bill
|
||||
* @param string $oldName
|
||||
* @param string $newName
|
||||
* @param Bill $bill
|
||||
* @param string $oldName
|
||||
* @param string $newName
|
||||
*/
|
||||
public function updateBillActions(Bill $bill, string $oldName, string $newName): void
|
||||
{
|
||||
@@ -59,8 +59,8 @@ trait BillServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Bill $bill
|
||||
* @param string $note
|
||||
* @param Bill $bill
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
@@ -73,23 +73,23 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
*
|
||||
*/
|
||||
public function setAccount(?Account $account): void
|
||||
private function processGroup(): void
|
||||
{
|
||||
$this->account = $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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionGroup $group
|
||||
*/
|
||||
public function setGroup(TransactionGroup $group): void
|
||||
{
|
||||
$this->group = $group;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @throws FireflyException
|
||||
*/
|
||||
@@ -109,8 +109,19 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $direction
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
return $this->getAccountByDirection($journal, '<');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string $direction
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
@@ -131,7 +142,7 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
@@ -141,17 +152,6 @@ class CreditRecalculateService
|
||||
return $this->getAccountByDirection($journal, '>');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
return $this->getAccountByDirection($journal, '<');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -166,24 +166,47 @@ class CreditRecalculateService
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function processGroup(): void
|
||||
private function processWork(): 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));
|
||||
}
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
foreach ($this->work as $account) {
|
||||
$this->processWorkAccount($account);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $direction
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
* @param Account $account
|
||||
*/
|
||||
private function processWorkAccount(Account $account): void
|
||||
{
|
||||
// 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();
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param string $direction
|
||||
* @param Transaction $transaction
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
@@ -277,41 +300,18 @@ class CreditRecalculateService
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param Account|null $account
|
||||
*/
|
||||
private function processWork(): void
|
||||
public function setAccount(?Account $account): void
|
||||
{
|
||||
$this->repository = app(AccountRepositoryInterface::class);
|
||||
foreach ($this->work as $account) {
|
||||
$this->processWorkAccount($account);
|
||||
}
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param TransactionGroup $group
|
||||
*/
|
||||
private function processWorkAccount(Account $account): void
|
||||
public function setGroup(TransactionGroup $group): void
|
||||
{
|
||||
// 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();
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($transactions as $transaction) {
|
||||
$leftOfDebt = $this->processTransaction($account, $direction, $transaction, $leftOfDebt);
|
||||
}
|
||||
$factory->crud($account, 'current_debt', $leftOfDebt);
|
||||
$this->group = $group;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,9 +49,9 @@ trait JournalServiceTrait
|
||||
private TagFactory $tagFactory;
|
||||
|
||||
/**
|
||||
* @param string $transactionType
|
||||
* @param string $direction
|
||||
* @param array $data
|
||||
* @param string $transactionType
|
||||
* @param string $direction
|
||||
* @param array $data
|
||||
*
|
||||
* @return Account|null
|
||||
* @throws FireflyException
|
||||
@@ -118,159 +118,152 @@ trait JournalServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $amount
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
* @return Account|null
|
||||
*/
|
||||
protected function getAmount(string $amount): string
|
||||
private function findAccountById(array $data, array $types): ?Account
|
||||
{
|
||||
if ('' === $amount) {
|
||||
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount));
|
||||
}
|
||||
Log::debug(sprintf('Now in getAmount("%s")', $amount));
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
|
||||
}
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $amount
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getForeignAmount(?string $amount): ?string
|
||||
{
|
||||
if (null === $amount) {
|
||||
Log::debug('No foreign amount info in array. Return NULL');
|
||||
|
||||
return null;
|
||||
}
|
||||
if ('' === $amount) {
|
||||
Log::debug('Foreign amount is empty string, return NULL.');
|
||||
|
||||
return null;
|
||||
}
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
Log::debug('Foreign amount is 0.0, return NULL.');
|
||||
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('Foreign amount is %s', $amount));
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeBudget(TransactionJournal $journal, NullArrayObject $data): void
|
||||
{
|
||||
if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) {
|
||||
$journal->budgets()->sync([]);
|
||||
|
||||
return;
|
||||
}
|
||||
$budget = $this->budgetRepository->findBudget($data['budget_id'], $data['budget_name']);
|
||||
if (null !== $budget) {
|
||||
Log::debug(sprintf('Link budget #%d to journal #%d', $budget->id, $journal->id));
|
||||
$journal->budgets()->sync([$budget->id]);
|
||||
|
||||
return;
|
||||
}
|
||||
// if the budget is NULL, sync empty.
|
||||
$journal->budgets()->sync([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeCategory(TransactionJournal $journal, NullArrayObject $data): void
|
||||
{
|
||||
$category = $this->categoryRepository->findCategory($data['category_id'], $data['category_name']);
|
||||
if (null !== $category) {
|
||||
Log::debug(sprintf('Link category #%d to journal #%d', $category->id, $journal->id));
|
||||
$journal->categories()->sync([$category->id]);
|
||||
|
||||
return;
|
||||
}
|
||||
// if the category is NULL, sync empty.
|
||||
$journal->categories()->sync([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string|null $notes
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeNotes(TransactionJournal $journal, ?string $notes): void
|
||||
{
|
||||
$notes = (string)$notes;
|
||||
$note = $journal->notes()->first();
|
||||
if ('' !== $notes) {
|
||||
if (null === $note) {
|
||||
$note = new Note();
|
||||
$note->noteable()->associate($journal);
|
||||
// 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;
|
||||
}
|
||||
$note->text = $notes;
|
||||
$note->save();
|
||||
Log::debug(sprintf('Stored notes for journal #%d', $journal->id));
|
||||
|
||||
return;
|
||||
}
|
||||
if ('' === $notes && null !== $note) {
|
||||
// try to delete existing notes.
|
||||
$note->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Link tags to journal.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param array|null $tags
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeTags(TransactionJournal $journal, ?array $tags): void
|
||||
{
|
||||
Log::debug('Now in storeTags()', $tags ?? []);
|
||||
$this->tagFactory->setUser($journal->user);
|
||||
$set = [];
|
||||
if (!is_array($tags)) {
|
||||
Log::debug('Tags is not an array, break.');
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug('Start of loop.');
|
||||
foreach ($tags as $string) {
|
||||
$string = (string)$string;
|
||||
Log::debug(sprintf('Now at tag "%s"', $string));
|
||||
if ('' !== $string) {
|
||||
$tag = $this->tagFactory->findOrCreate($string);
|
||||
if (null !== $tag) {
|
||||
$set[] = $tag->id;
|
||||
}
|
||||
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('End of loop.');
|
||||
Log::debug(sprintf('Total nr. of tags: %d', count($tags)), $tags);
|
||||
$journal->tags()->sync($set);
|
||||
Log::debug(sprintf('Found no account by ID #%d of types', $data['id']), $types);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param string $preferredType
|
||||
* @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
|
||||
@@ -341,134 +334,9 @@ trait JournalServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @param Account|null $account
|
||||
* @param array $data
|
||||
* @param array $types
|
||||
*
|
||||
* @return Account|null
|
||||
*/
|
||||
@@ -484,20 +352,152 @@ trait JournalServiceTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
* @return null|string
|
||||
* @param string $amount
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getCreatableType(array $types): ?string
|
||||
protected function getAmount(string $amount): 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;
|
||||
if ('' === $amount) {
|
||||
throw new FireflyException(sprintf('The amount cannot be an empty string: "%s"', $amount));
|
||||
}
|
||||
Log::debug(sprintf('Now in getAmount("%s")', $amount));
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
throw new FireflyException(sprintf('The amount seems to be zero: "%s"', $amount));
|
||||
}
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $amount
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function getForeignAmount(?string $amount): ?string
|
||||
{
|
||||
if (null === $amount) {
|
||||
Log::debug('No foreign amount info in array. Return NULL');
|
||||
|
||||
return null;
|
||||
}
|
||||
if ('' === $amount) {
|
||||
Log::debug('Foreign amount is empty string, return NULL.');
|
||||
|
||||
return null;
|
||||
}
|
||||
if (0 === bccomp('0', $amount)) {
|
||||
Log::debug('Foreign amount is 0.0, return NULL.');
|
||||
|
||||
return null;
|
||||
}
|
||||
Log::debug(sprintf('Foreign amount is %s', $amount));
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeBudget(TransactionJournal $journal, NullArrayObject $data): void
|
||||
{
|
||||
if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) {
|
||||
$journal->budgets()->sync([]);
|
||||
|
||||
return;
|
||||
}
|
||||
$budget = $this->budgetRepository->findBudget($data['budget_id'], $data['budget_name']);
|
||||
if (null !== $budget) {
|
||||
Log::debug(sprintf('Link budget #%d to journal #%d', $budget->id, $journal->id));
|
||||
$journal->budgets()->sync([$budget->id]);
|
||||
|
||||
return;
|
||||
}
|
||||
// if the budget is NULL, sync empty.
|
||||
$journal->budgets()->sync([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param NullArrayObject $data
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeCategory(TransactionJournal $journal, NullArrayObject $data): void
|
||||
{
|
||||
$category = $this->categoryRepository->findCategory($data['category_id'], $data['category_name']);
|
||||
if (null !== $category) {
|
||||
Log::debug(sprintf('Link category #%d to journal #%d', $category->id, $journal->id));
|
||||
$journal->categories()->sync([$category->id]);
|
||||
|
||||
return;
|
||||
}
|
||||
// if the category is NULL, sync empty.
|
||||
$journal->categories()->sync([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
* @param string|null $notes
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeNotes(TransactionJournal $journal, ?string $notes): void
|
||||
{
|
||||
$notes = (string)$notes;
|
||||
$note = $journal->notes()->first();
|
||||
if ('' !== $notes) {
|
||||
if (null === $note) {
|
||||
$note = new Note();
|
||||
$note->noteable()->associate($journal);
|
||||
}
|
||||
$note->text = $notes;
|
||||
$note->save();
|
||||
Log::debug(sprintf('Stored notes for journal #%d', $journal->id));
|
||||
|
||||
return;
|
||||
}
|
||||
if ('' === $notes && null !== $note) {
|
||||
// try to delete existing notes.
|
||||
$note->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Link tags to journal.
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param array|null $tags
|
||||
*
|
||||
|
||||
*/
|
||||
protected function storeTags(TransactionJournal $journal, ?array $tags): void
|
||||
{
|
||||
Log::debug('Now in storeTags()', $tags ?? []);
|
||||
$this->tagFactory->setUser($journal->user);
|
||||
$set = [];
|
||||
if (!is_array($tags)) {
|
||||
Log::debug('Tags is not an array, break.');
|
||||
|
||||
return;
|
||||
}
|
||||
Log::debug('Start of loop.');
|
||||
foreach ($tags as $string) {
|
||||
$string = (string)$string;
|
||||
Log::debug(sprintf('Now at tag "%s"', $string));
|
||||
if ('' !== $string) {
|
||||
$tag = $this->tagFactory->findOrCreate($string);
|
||||
if (null !== $tag) {
|
||||
$set[] = $tag->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
Log::debug('End of loop.');
|
||||
Log::debug(sprintf('Total nr. of tags: %d', count($tags)), $tags);
|
||||
$journal->tags()->sync($set);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
trait LocationServiceTrait
|
||||
{
|
||||
/**
|
||||
* @param Model $model
|
||||
* @param array $data
|
||||
* @param Model $model
|
||||
* @param array $data
|
||||
*
|
||||
* @return Location|null
|
||||
*/
|
||||
|
||||
@@ -50,8 +50,8 @@ use JsonException;
|
||||
trait RecurringTransactionTrait
|
||||
{
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param string $note
|
||||
* @param Recurrence $recurrence
|
||||
* @param string $note
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -77,8 +77,8 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $repetitions
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $repetitions
|
||||
*/
|
||||
protected function createRepetitions(Recurrence $recurrence, array $repetitions): void
|
||||
{
|
||||
@@ -99,8 +99,8 @@ trait RecurringTransactionTrait
|
||||
/**
|
||||
* Store transactions of a recurring transactions. It's complex but readable.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $transactions
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $transactions
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @throws JsonException
|
||||
@@ -180,35 +180,9 @@ 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
|
||||
* @param string|null $accountName
|
||||
* @param array $expectedTypes
|
||||
* @param int|null $accountId
|
||||
* @param string|null $accountName
|
||||
*
|
||||
* @return Account
|
||||
* @throws JsonException
|
||||
@@ -257,78 +231,8 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $piggyId
|
||||
*/
|
||||
protected function updatePiggyBank(RecurrenceTransaction $transaction, int $piggyId): void
|
||||
{
|
||||
/** @var PiggyBankFactory $factory */
|
||||
$factory = app(PiggyBankFactory::class);
|
||||
$factory->setUser($transaction->recurrence->user);
|
||||
$piggyBank = $factory->find($piggyId, null);
|
||||
if (null !== $piggyBank) {
|
||||
/** @var RecurrenceMeta|null $entry */
|
||||
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'piggy_bank_id')->first();
|
||||
if (null === $entry) {
|
||||
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'piggy_bank_id', 'value' => $piggyBank->id]);
|
||||
}
|
||||
$entry->value = $piggyBank->id;
|
||||
$entry->save();
|
||||
}
|
||||
if (null === $piggyBank) {
|
||||
// delete if present
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'piggy_bank_id')->delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param array $tags
|
||||
*/
|
||||
protected function updateTags(RecurrenceTransaction $transaction, array $tags): void
|
||||
{
|
||||
if (0 !== count($tags)) {
|
||||
/** @var RecurrenceMeta|null $entry */
|
||||
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'tags')->first();
|
||||
if (null === $entry) {
|
||||
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'tags', 'value' => json_encode($tags)]);
|
||||
}
|
||||
$entry->value = json_encode($tags);
|
||||
$entry->save();
|
||||
}
|
||||
if (0 === count($tags)) {
|
||||
// delete if present
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'tags')->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 $budgetId
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $budgetId
|
||||
*/
|
||||
private function setBudget(RecurrenceTransaction $transaction, int $budgetId): void
|
||||
{
|
||||
@@ -350,8 +254,31 @@ trait RecurringTransactionTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $categoryId
|
||||
* @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
|
||||
*/
|
||||
private function setCategory(RecurrenceTransaction $transaction, int $categoryId): void
|
||||
{
|
||||
@@ -375,4 +302,76 @@ trait RecurringTransactionTrait
|
||||
$meta->value = $category->id;
|
||||
$meta->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param int $piggyId
|
||||
*/
|
||||
protected function updatePiggyBank(RecurrenceTransaction $transaction, int $piggyId): void
|
||||
{
|
||||
/** @var PiggyBankFactory $factory */
|
||||
$factory = app(PiggyBankFactory::class);
|
||||
$factory->setUser($transaction->recurrence->user);
|
||||
$piggyBank = $factory->find($piggyId, null);
|
||||
if (null !== $piggyBank) {
|
||||
/** @var RecurrenceMeta|null $entry */
|
||||
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'piggy_bank_id')->first();
|
||||
if (null === $entry) {
|
||||
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'piggy_bank_id', 'value' => $piggyBank->id]);
|
||||
}
|
||||
$entry->value = $piggyBank->id;
|
||||
$entry->save();
|
||||
}
|
||||
if (null === $piggyBank) {
|
||||
// delete if present
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'piggy_bank_id')->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
* @param array $tags
|
||||
*/
|
||||
protected function updateTags(RecurrenceTransaction $transaction, array $tags): void
|
||||
{
|
||||
if (0 !== count($tags)) {
|
||||
/** @var RecurrenceMeta|null $entry */
|
||||
$entry = $transaction->recurrenceTransactionMeta()->where('name', 'tags')->first();
|
||||
if (null === $entry) {
|
||||
$entry = RecurrenceTransactionMeta::create(['rt_id' => $transaction->id, 'name' => 'tags', 'value' => json_encode($tags)]);
|
||||
}
|
||||
$entry->value = json_encode($tags);
|
||||
$entry->save();
|
||||
}
|
||||
if (0 === count($tags)) {
|
||||
// delete if present
|
||||
$transaction->recurrenceTransactionMeta()->where('name', 'tags')->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ trait TransactionTypeTrait
|
||||
* Get the transaction type. Since this is mandatory, will throw an exception when nothing comes up. Will always
|
||||
* use TransactionType repository.
|
||||
*
|
||||
* @param string $type
|
||||
* @param string $type
|
||||
*
|
||||
* @return TransactionType
|
||||
* @throws FireflyException
|
||||
|
||||
Reference in New Issue
Block a user