chore: code cleanup.

This commit is contained in:
James Cole
2023-05-29 13:56:55 +02:00
parent 7f7644c92f
commit 1b52147a05
295 changed files with 12418 additions and 12324 deletions

View File

@@ -72,7 +72,7 @@ class AppendNotesToDescription implements ActionInterface
// only append if there is something to append
if ('' !== $note->text) {
$before = $object->description;
$object->description = trim(sprintf("%s %s", $object->description, (string)$this->clearString($note->text, false)));
$object->description = trim(sprintf('%s %s', $object->description, (string)$this->clearString($note->text, false)));
$object->save();
Log::debug(sprintf('Journal description is updated to "%s".', $object->description));

View File

@@ -114,6 +114,60 @@ class ConvertToDeposit implements ActionInterface
return false;
}
/**
* Input is a transfer from A to B.
* Output is a deposit from C to B.
* The source account is replaced.
*
* @param TransactionJournal $journal
*
* @return bool
* @throws FireflyException
* @throws JsonException
*/
private function convertTransferArray(TransactionJournal $journal): bool
{
$user = $journal->user;
// find or create revenue account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($user);
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($user);
$sourceAccount = $this->getSourceAccount($journal);
// get the action value, or use the original source name in case the action value is empty:
// this becomes a new or existing (revenue) account, which is the source of the new deposit.
$opposingName = '' === $this->action->action_value ? $sourceAccount->name : $this->action->action_value;
// we check all possible source account types if one exists:
$validTypes = config('firefly.expected_source_types.source.Deposit');
$opposingAccount = $repository->findByName($opposingName, $validTypes);
if (null === $opposingAccount) {
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::REVENUE);
}
Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $opposingAccount->name));
// update source transaction(s) to be revenue account
DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0)
->update(['account_id' => $opposingAccount->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
DB::table('transaction_journals')
->where('id', '=', $journal->id)
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
Log::debug('Converted transfer to deposit.');
return true;
}
/**
* Input is a withdrawal from A to B
* Is converted to a deposit from C to A.
@@ -175,57 +229,18 @@ class ConvertToDeposit implements ActionInterface
}
/**
* Input is a transfer from A to B.
* Output is a deposit from C to B.
* The source account is replaced.
*
* @param TransactionJournal $journal
*
* @return bool
* @return Account
* @throws FireflyException
* @throws JsonException
*/
private function convertTransferArray(TransactionJournal $journal): bool
private function getDestinationAccount(TransactionJournal $journal): Account
{
$user = $journal->user;
// find or create revenue account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($user);
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($user);
$sourceAccount = $this->getSourceAccount($journal);
// get the action value, or use the original source name in case the action value is empty:
// this becomes a new or existing (revenue) account, which is the source of the new deposit.
$opposingName = '' === $this->action->action_value ? $sourceAccount->name : $this->action->action_value;
// we check all possible source account types if one exists:
$validTypes = config('firefly.expected_source_types.source.Deposit');
$opposingAccount = $repository->findByName($opposingName, $validTypes);
if (null === $opposingAccount) {
$opposingAccount = $factory->findOrCreate($opposingName, AccountType::REVENUE);
/** @var Transaction|null $destAccount */
$destAccount = $journal->transactions()->where('amount', '>', 0)->first();
if (null === $destAccount) {
throw new FireflyException(sprintf('Cannot find destination transaction for journal #%d', $journal->id));
}
Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $opposingAccount->name));
// update source transaction(s) to be revenue account
DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0)
->update(['account_id' => $opposingAccount->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
DB::table('transaction_journals')
->where('id', '=', $journal->id)
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
Log::debug('Converted transfer to deposit.');
return true;
return $destAccount->account;
}
/**
@@ -242,19 +257,4 @@ class ConvertToDeposit implements ActionInterface
}
return $sourceTransaction->account;
}
/**
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
private function getDestinationAccount(TransactionJournal $journal): Account
{
/** @var Transaction|null $destAccount */
$destAccount = $journal->transactions()->where('amount', '>', 0)->first();
if (null === $destAccount) {
throw new FireflyException(sprintf('Cannot find destination transaction for journal #%d', $journal->id));
}
return $destAccount->account;
}
}

View File

@@ -139,6 +139,48 @@ class ConvertToTransfer implements ActionInterface
return false;
}
/**
* A deposit is from Revenue to Asset.
* We replace the Revenue with another asset.
*
* @param TransactionJournal $journal
* @param Account $opposing
*
* @return bool
* @throws FireflyException
*/
private function convertDepositArray(TransactionJournal $journal, Account $opposing): bool
{
$destAccount = $this->getDestinationAccount($journal);
if ((int)$destAccount->id === (int)$opposing->id) {
Log::error(
vsprintf(
'Journal #%d has already has "%s" as a destination asset. ConvertToTransfer failed. (rule #%d).',
[$journal->id, $opposing->name, $this->action->rule_id]
)
);
return false;
}
// update source transaction:
DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0)
->update(['account_id' => $opposing->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
DB::table('transaction_journals')
->where('id', '=', $journal->id)
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
Log::debug('Converted deposit to transfer.');
return true;
}
/**
* A withdrawal is from Asset to Expense.
* We replace the Expense with another asset.
@@ -182,63 +224,6 @@ class ConvertToTransfer implements ActionInterface
return true;
}
/**
* A deposit is from Revenue to Asset.
* We replace the Revenue with another asset.
*
* @param TransactionJournal $journal
* @param Account $opposing
*
* @return bool
* @throws FireflyException
*/
private function convertDepositArray(TransactionJournal $journal, Account $opposing): bool
{
$destAccount = $this->getDestinationAccount($journal);
if ((int)$destAccount->id === (int)$opposing->id) {
Log::error(
vsprintf(
'Journal #%d has already has "%s" as a destination asset. ConvertToTransfer failed. (rule #%d).',
[$journal->id, $opposing->name, $this->action->rule_id]
)
);
return false;
}
// update source transaction:
DB::table('transactions')
->where('transaction_journal_id', '=', $journal->id)
->where('amount', '<', 0)
->update(['account_id' => $opposing->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
DB::table('transaction_journals')
->where('id', '=', $journal->id)
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
Log::debug('Converted deposit to transfer.');
return true;
}
/**
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
private function getSourceAccount(TransactionJournal $journal): Account
{
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $sourceTransaction) {
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
}
return $sourceTransaction->account;
}
/**
* @param TransactionJournal $journal
* @return Account
@@ -254,21 +239,6 @@ class ConvertToTransfer implements ActionInterface
return $destAccount->account;
}
/**
* @param int $journalId
* @return string
*/
private function getSourceType(int $journalId): string
{
/** @var TransactionJournal $journal */
$journal = TransactionJournal::find($journalId);
if (null === $journal) {
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
return '';
}
return (string)$journal->transactions()->where('amount', '<', 0)->first()?->account?->accountType?->type;
}
/**
* @param int $journalId
* @return string
@@ -283,4 +253,34 @@ class ConvertToTransfer implements ActionInterface
}
return (string)$journal->transactions()->where('amount', '>', 0)->first()?->account?->accountType?->type;
}
/**
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
private function getSourceAccount(TransactionJournal $journal): Account
{
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $sourceTransaction) {
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
}
return $sourceTransaction->account;
}
/**
* @param int $journalId
* @return string
*/
private function getSourceType(int $journalId): string
{
/** @var TransactionJournal $journal */
$journal = TransactionJournal::find($journalId);
if (null === $journal) {
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
return '';
}
return (string)$journal->transactions()->where('amount', '<', 0)->first()?->account?->accountType?->type;
}
}

View File

@@ -34,7 +34,6 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Illuminate\Support\Facades\Log;
use JsonException;
@@ -113,7 +112,7 @@ class ConvertToWithdrawal implements ActionInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
* @return bool
* @throws FireflyException
* @throws JsonException
@@ -170,7 +169,7 @@ class ConvertToWithdrawal implements ActionInterface
* Input is a transfer from A to B.
* Output is a withdrawal from A to C.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return bool
* @throws FireflyException
@@ -187,7 +186,7 @@ class ConvertToWithdrawal implements ActionInterface
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($user);
$destAccount = $this->getDestinationAccount($journal);
$destAccount = $this->getDestinationAccount($journal);
// get the action value, or use the original source name in case the action value is empty:
// this becomes a new or existing (expense) account, which is the destination of the new withdrawal.
@@ -218,21 +217,6 @@ class ConvertToWithdrawal implements ActionInterface
return true;
}
/**
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
private function getSourceAccount(TransactionJournal $journal): Account
{
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $sourceTransaction) {
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
}
return $sourceTransaction->account;
}
/**
* @param TransactionJournal $journal
* @return Account
@@ -247,4 +231,19 @@ class ConvertToWithdrawal implements ActionInterface
}
return $destAccount->account;
}
/**
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
private function getSourceAccount(TransactionJournal $journal): Account
{
/** @var Transaction|null $sourceTransaction */
$sourceTransaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $sourceTransaction) {
throw new FireflyException(sprintf('Cannot find source transaction for journal #%d', $journal->id));
}
return $sourceTransaction->account;
}
}

View File

@@ -134,6 +134,49 @@ class UpdatePiggybank implements ActionInterface
return false;
}
/**
* @param PiggyBank $piggyBank
* @param TransactionJournal $journal
* @param string $amount
* @return void
*/
private function addAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
{
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
// how much can we add to the piggy bank?
if (0 !== bccomp($piggyBank->targetamount, '0')) {
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
Log::debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
// update amount to fit:
$amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
Log::debug(sprintf('Amount is now %s', $amount));
}
if (0 === bccomp($piggyBank->targetamount, '0')) {
Log::debug('Target amount is zero, can add anything.');
}
// if amount is zero, stop.
if (0 === bccomp('0', $amount)) {
app('log')->warning('Amount left is zero, stop.');
return;
}
// make sure we can add amount:
if (false === $repository->canAddAmount($piggyBank, $amount)) {
app('log')->warning(sprintf('Cannot add %s to piggy bank.', $amount));
return;
}
Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
$repository->addAmount($piggyBank, $amount, $journal);
}
/**
* @param User $user
*
@@ -180,47 +223,4 @@ class UpdatePiggybank implements ActionInterface
$repository->removeAmount($piggyBank, $amount, $journal);
}
/**
* @param PiggyBank $piggyBank
* @param TransactionJournal $journal
* @param string $amount
* @return void
*/
private function addAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
{
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
// how much can we add to the piggy bank?
if (0 !== bccomp($piggyBank->targetamount, '0')) {
$toAdd = bcsub($piggyBank->targetamount, $repository->getCurrentAmount($piggyBank));
Log::debug(sprintf('Max amount to add to piggy bank is %s, amount is %s', $toAdd, $amount));
// update amount to fit:
$amount = -1 === bccomp($amount, $toAdd) ? $amount : $toAdd;
Log::debug(sprintf('Amount is now %s', $amount));
}
if (0 === bccomp($piggyBank->targetamount, '0')) {
Log::debug('Target amount is zero, can add anything.');
}
// if amount is zero, stop.
if (0 === bccomp('0', $amount)) {
app('log')->warning('Amount left is zero, stop.');
return;
}
// make sure we can add amount:
if (false === $repository->canAddAmount($piggyBank, $amount)) {
app('log')->warning(sprintf('Cannot add %s to piggy bank.', $amount));
return;
}
Log::debug(sprintf('Will now add %s to piggy bank.', $amount));
$repository->addAmount($piggyBank, $amount, $journal);
}
}