chore: reformat code.

This commit is contained in:
James Cole
2023-06-21 12:34:58 +02:00
parent 8d87abde64
commit 3dcb35710b
799 changed files with 23319 additions and 22173 deletions

View File

@@ -32,7 +32,7 @@ interface ActionInterface
* Execute the action on an array. Returns "true" if the action was a success and the action
* was applied. Returns false if otherwise.
*
* @param array $journal
* @param array $journal
*
* @return bool
*/

View File

@@ -41,7 +41,7 @@ class AddTag implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -38,7 +38,7 @@ class AppendDescription implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -40,7 +40,7 @@ class AppendDescriptionToNotes implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class AppendNotes implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -43,7 +43,7 @@ class AppendNotesToDescription implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class ClearBudget implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class ClearCategory implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class ClearNotes implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -48,7 +48,7 @@ class ConvertToDeposit implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
@@ -86,7 +86,7 @@ class ConvertToDeposit implements ActionInterface
try {
$res = $this->convertWithdrawalArray($object);
} catch (JsonException|FireflyException $e) {
} catch (JsonException | FireflyException $e) {
Log::debug('Could not convert withdrawal to deposit.');
Log::error($e->getMessage());
return false;
@@ -101,7 +101,7 @@ class ConvertToDeposit implements ActionInterface
try {
$res = $this->convertTransferArray($object);
} catch (JsonException|FireflyException $e) {
} catch (JsonException | FireflyException $e) {
Log::debug('Could not convert transfer to deposit.');
Log::error($e->getMessage());
return false;
@@ -114,65 +114,11 @@ 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.
*
* @param TransactionJournal $journal
* @param TransactionJournal $journal
*
* @return bool
* @throws FireflyException
@@ -229,7 +175,7 @@ class ConvertToDeposit implements ActionInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
@@ -244,7 +190,7 @@ class ConvertToDeposit implements ActionInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
@@ -257,4 +203,58 @@ class ConvertToDeposit implements ActionInterface
}
return $sourceTransaction->account;
}
/**
* 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;
}
}

View File

@@ -45,7 +45,7 @@ class ConvertToTransfer implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
@@ -140,45 +140,33 @@ class ConvertToTransfer implements ActionInterface
}
/**
* A deposit is from Revenue to Asset.
* We replace the Revenue with another asset.
*
* @param TransactionJournal $journal
* @param Account $opposing
*
* @return bool
* @throws FireflyException
* @param int $journalId
* @return string
*/
private function convertDepositArray(TransactionJournal $journal, Account $opposing): bool
private function getSourceType(int $journalId): string
{
$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;
/** @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;
}
// 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 int $journalId
* @return string
*/
private function getDestinationType(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;
}
/**
@@ -186,8 +174,8 @@ class ConvertToTransfer implements ActionInterface
* We replace the Expense with another asset.
* So this replaces the destination
*
* @param TransactionJournal $journal
* @param Account $opposing
* @param TransactionJournal $journal
* @param Account $opposing
*
* @return bool
* @throws FireflyException
@@ -225,37 +213,7 @@ class ConvertToTransfer implements ActionInterface
}
/**
* @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;
}
/**
* @param int $journalId
* @return string
*/
private function getDestinationType(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 TransactionJournal $journal
* @param TransactionJournal $journal
* @return Account
* @throws FireflyException
*/
@@ -270,17 +228,59 @@ class ConvertToTransfer implements ActionInterface
}
/**
* @param int $journalId
* @return string
* 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 getSourceType(int $journalId): string
private function convertDepositArray(TransactionJournal $journal, Account $opposing): bool
{
/** @var TransactionJournal $journal */
$journal = TransactionJournal::find($journalId);
if (null === $journal) {
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
return '';
$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;
}
return (string)$journal->transactions()->where('amount', '<', 0)->first()?->account?->accountType?->type;
// 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 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

@@ -48,7 +48,7 @@ class ConvertToWithdrawal implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
@@ -84,7 +84,7 @@ class ConvertToWithdrawal implements ActionInterface
Log::debug('Going to transform a deposit to a withdrawal.');
try {
$res = $this->convertDepositArray($object);
} catch (JsonException|FireflyException $e) {
} catch (JsonException | FireflyException $e) {
Log::debug('Could not convert transfer to deposit.');
Log::error($e->getMessage());
return false;
@@ -98,7 +98,7 @@ class ConvertToWithdrawal implements ActionInterface
try {
$res = $this->convertTransferArray($object);
} catch (JsonException|FireflyException $e) {
} catch (JsonException | FireflyException $e) {
Log::debug('Could not convert transfer to deposit.');
Log::error($e->getMessage());
return false;
@@ -112,7 +112,7 @@ class ConvertToWithdrawal implements ActionInterface
}
/**
* @param TransactionJournal $journal
* @param TransactionJournal $journal
* @return bool
* @throws FireflyException
* @throws JsonException
@@ -165,11 +165,41 @@ 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
* @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;
}
/**
* 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
@@ -216,34 +246,4 @@ class ConvertToWithdrawal implements ActionInterface
return true;
}
/**
* @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;
}
/**
* @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

@@ -41,7 +41,7 @@ class DeleteTransaction implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -43,7 +43,7 @@ class LinkToBill implements ActionInterface
* TriggerInterface constructor.
*
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -41,7 +41,7 @@ class MoveDescriptionToNotes implements ActionInterface
* TriggerInterface constructor.
*
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -47,7 +47,7 @@ class MoveNotesToDescription implements ActionInterface
* TriggerInterface constructor.
*
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -38,7 +38,7 @@ class PrependDescription implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class PrependNotes implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class RemoveAllTags implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -40,7 +40,7 @@ class RemoveTag implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -41,7 +41,7 @@ class SetBudget implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -41,7 +41,7 @@ class SetCategory implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -39,7 +39,7 @@ class SetDescription implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -45,7 +45,7 @@ class SetDestinationAccount implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
@@ -133,7 +133,7 @@ class SetDestinationAccount implements ActionInterface
}
/**
* @param string $type
* @param string $type
*
* @return Account|null
*/

View File

@@ -39,7 +39,7 @@ class SetNotes implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{

View File

@@ -45,7 +45,7 @@ class SetSourceAccount implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
@@ -128,7 +128,7 @@ class SetSourceAccount implements ActionInterface
}
/**
* @param string $type
* @param string $type
*
* @return Account|null
*/

View File

@@ -44,7 +44,7 @@ class UpdatePiggybank implements ActionInterface
/**
* TriggerInterface constructor.
*
* @param RuleAction $action
* @param RuleAction $action
*/
public function __construct(RuleAction $action)
{
@@ -135,9 +135,56 @@ class UpdatePiggybank implements ActionInterface
}
/**
* @param PiggyBank $piggyBank
* @param TransactionJournal $journal
* @param string $amount
* @param User $user
*
* @return PiggyBank|null
*/
private function findPiggyBank(User $user): ?PiggyBank
{
return $user->piggyBanks()->where('piggy_banks.name', $this->action->action_value)->first();
}
/**
* @param PiggyBank $piggyBank
* @param TransactionJournal $journal
* @param string $amount
* @return void
*/
private function removeAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
{
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
// how much can we remove from this piggy bank?
$toRemove = $repository->getCurrentAmount($piggyBank);
Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
// if $amount is bigger than $toRemove, shrink it.
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
Log::debug(sprintf('Amount is now %s', $amount));
// if amount is zero, stop.
if (0 === bccomp('0', $amount)) {
app('log')->warning('Amount left is zero, stop.');
return;
}
// make sure we can remove amount:
if (false === $repository->canRemoveAmount($piggyBank, $amount)) {
app('log')->warning(sprintf('Cannot remove %s from piggy bank.', $amount));
return;
}
Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
$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
@@ -176,51 +223,4 @@ class UpdatePiggybank implements ActionInterface
$repository->addAmount($piggyBank, $amount, $journal);
}
/**
* @param User $user
*
* @return PiggyBank|null
*/
private function findPiggyBank(User $user): ?PiggyBank
{
return $user->piggyBanks()->where('piggy_banks.name', $this->action->action_value)->first();
}
/**
* @param PiggyBank $piggyBank
* @param TransactionJournal $journal
* @param string $amount
* @return void
*/
private function removeAmount(PiggyBank $piggyBank, TransactionJournal $journal, string $amount): void
{
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
// how much can we remove from this piggy bank?
$toRemove = $repository->getCurrentAmount($piggyBank);
Log::debug(sprintf('Amount is %s, max to remove is %s', $amount, $toRemove));
// if $amount is bigger than $toRemove, shrink it.
$amount = -1 === bccomp($amount, $toRemove) ? $amount : $toRemove;
Log::debug(sprintf('Amount is now %s', $amount));
// if amount is zero, stop.
if (0 === bccomp('0', $amount)) {
app('log')->warning('Amount left is zero, stop.');
return;
}
// make sure we can remove amount:
if (false === $repository->canRemoveAmount($piggyBank, $amount)) {
app('log')->warning(sprintf('Cannot remove %s from piggy bank.', $amount));
return;
}
Log::debug(sprintf('Will now remove %s from piggy bank.', $amount));
$repository->removeAmount($piggyBank, $amount, $journal);
}
}