Account search fixed.

This commit is contained in:
James Cole
2020-08-28 11:24:55 +02:00
parent 63794cab07
commit 093f34b7a8
26 changed files with 157 additions and 996 deletions

View File

@@ -337,7 +337,7 @@ class OperatorQuerySearch implements SearchInterface
$parts = explode(',', $value);
$collection = new Collection;
foreach ($parts as $accountId) {
$account = $this->accountRepository->findNull((int) $value);
$account = $this->accountRepository->findNull((int) $accountId);
if (null !== $account) {
$collection->push($account);
}

View File

@@ -37,16 +37,6 @@ interface ActionInterface
*/
public function __construct(RuleAction $action);
/**
* Execute the action.
* @deprecated
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool;
/**
* Execute the action on an array.
*

View File

@@ -46,42 +46,6 @@ class AddTag implements ActionInterface
$this->action = $action;
}
/**
* @inheritDoc
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
// journal has this tag maybe?
/** @var TagFactory $factory */
$factory = app(TagFactory::class);
$factory->setUser($journal->user);
// TODO explode value on comma?
$tag = $factory->findOrCreate($this->action->action_value);
if (null === $tag) {
// could not find, could not create tag.
Log::error(sprintf('RuleAction AddTag. Could not find or create tag "%s"', $this->action->action_value));
return false;
}
$count = $journal->tags()->where('tag_id', $tag->id)->count();
if (0 === $count) {
$journal->tags()->save($tag);
$journal->touch();
Log::debug(sprintf('RuleAction AddTag. Added tag #%d ("%s") to journal %d.', $tag->id, $tag->tag, $journal->id));
return true;
}
Log::debug(sprintf('RuleAction AddTag fired but tag %d ("%s") was already added to journal %d.', $tag->id, $tag->tag, $journal->id));
return false;
}
/**
* @inheritDoc
*/

View File

@@ -44,23 +44,6 @@ class AppendDescription implements ActionInterface
$this->action = $action;
}
/**
* Append description with X
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
* @deprecated
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction AppendDescription appended "%s" to "%s".', $this->action->action_value, $journal->description));
$journal->description .= $this->action->action_value;
$journal->save();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -45,30 +45,6 @@ class AppendNotes implements ActionInterface
$this->action = $action;
}
/**
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$dbNote = $journal->notes()->first();
if (null === $dbNote) {
$dbNote = new Note;
$dbNote->noteable()->associate($journal);
}
$notes = $dbNote->text;
Log::debug(sprintf('RuleAction AppendNotes appended "%s" to "%s".', $this->action->action_value, $notes));
$notes .= $this->action->action_value;
$dbNote->text = $notes;
$dbNote->save();
$journal->save();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -41,30 +41,6 @@ class ClearBudget implements ActionInterface
{
}
/**
* Clear all budgets
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
* @return bool
* @deprecated
*/
public function act(TransactionJournal $journal): bool
{
$journal->budgets()->detach();
$journal->touch();
// also remove budgets from transactions (although no longer necessary)
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
$transaction->budgets()->detach();
}
Log::debug(sprintf('RuleAction ClearBudget removed all budgets from journal %d.', $journal->id));
return true;
}
/**
* @inheritDoc
*/

View File

@@ -41,31 +41,6 @@ class ClearCategory implements ActionInterface
{
}
/**
* Clear all categories
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
* @deprecated
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$journal->categories()->detach();
$journal->touch();
// also remove categories from transactions:
/** @var Transaction $transaction */
foreach ($journal->transactions as $transaction) {
$transaction->categories()->detach();
}
Log::debug(sprintf('RuleAction ClearCategory removed all categories from journal %d.', $journal->id));
return true;
}
/**
* @inheritDoc
*/

View File

@@ -42,28 +42,6 @@ class ClearNotes implements ActionInterface
{
}
/**
* Remove notes
*
* @param TransactionJournal $journal
* @codeCoverageIgnore
* @deprecated
* @return bool
* @throws Exception
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction ClearNotes removed all notes.'));
$notes = $journal->notes()->get();
/** @var Note $note */
foreach ($notes as $note) {
$note->delete();
}
$journal->touch();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -53,112 +53,6 @@ class ConvertToDeposit implements ActionInterface
$this->action = $action;
}
/**
* Execute the action.
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
* @throws FireflyException
*/
public function act(TransactionJournal $journal): bool
{
$type = $journal->transactionType->type;
if (TransactionType::DEPOSIT === $type) {
// @codeCoverageIgnoreStart
Log::error(sprintf('Journal #%d is already a deposit (rule "%s").', $journal->id, $this->action->rule->title));
return false;
// @codeCoverageIgnoreEnd
}
$destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
$sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
// break if count is zero:
if (1 !== $sourceTransactions->count()) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has %d source transactions. ConvertToDeposit failed. (rule "%s").',
[$journal->id, $sourceTransactions->count(), $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
if (0 === $destTransactions->count()) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has %d dest transactions. ConvertToDeposit failed. (rule "%s").',
[$journal->id, $destTransactions->count(), $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
if (TransactionType::WITHDRAWAL === $type) {
Log::debug('Going to transform a withdrawal to a deposit.');
return $this->convertWithdrawal($journal);
}
if (TransactionType::TRANSFER === $type) {
Log::debug('Going to transform a transfer to a deposit.');
return $this->convertTransfer($journal);
}
return false; // @codeCoverageIgnore
}
/**
* Input is a transfer from A to B.
* Output is a deposit from C to B.
*
* @param TransactionJournal $journal
* @return bool
* @throws FireflyException
* @deprecated
*/
private function convertTransfer(TransactionJournal $journal): bool
{
// find or create revenue account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($journal->user);
$sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
// 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.
/** @var Account $source */
$source = $sourceTransactions->first()->account;
$revenueName = '' === $this->action->action_value ? $source->name : $this->action->action_value;
$revenue = $factory->findOrCreate($revenueName, AccountType::REVENUE);
Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $source->name));
unset($source);
// update source transaction(s) to be revenue account
$journal->transactions()
->where('amount', '<', 0)
->update(['account_id' => $revenue->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
$journal->transaction_type_id = $newType->id;
$journal->save();
Log::debug('Converted transfer to deposit.');
return true;
}
/**
* Input is a transfer from A to B.
* Output is a deposit from C to B.
@@ -202,59 +96,6 @@ class ConvertToDeposit implements ActionInterface
return true;
}
/**
* Input is a withdrawal from A to B
* Is converted to a deposit from C to A.
*
* @param TransactionJournal $journal
* @deprecated
* @return bool
* @throws FireflyException
*/
private function convertWithdrawal(TransactionJournal $journal): bool
{
// find or create revenue account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($journal->user);
$destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
$sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
// get the action value, or use the original destination name in case the action value is empty:
// this becomes a new or existing revenue account.
/** @var Account $destination */
$destination = $destTransactions->first()->account;
$revenueName = '' === $this->action->action_value ? $destination->name : $this->action->action_value;
$revenue = $factory->findOrCreate($revenueName, AccountType::REVENUE);
Log::debug(sprintf('ConvertToDeposit. Action value is "%s", revenue name is "%s"', $this->action->action_value, $destination->name));
// get source account from transaction(s).
/** @var Account $source */
$source = $sourceTransactions->first()->account;
// update source transaction(s) to be revenue account
$journal->transactions()
->where('amount', '<', 0)
->update(['account_id' => $revenue->id]);
// update destination transaction(s) to be original source account(s).
$journal->transactions()
->where('amount', '>', 0)
->update(['account_id' => $source->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
$journal->transaction_type_id = $newType->id;
$journal->save();
Log::debug('Converted withdrawal to deposit.');
return true;
}
/**
* Input is a withdrawal from A to B
* Is converted to a deposit from C to A.

View File

@@ -52,166 +52,6 @@ class ConvertToTransfer implements ActionInterface
$this->action = $action;
}
/**
* Execute the action.
* @deprecated
* @codeCoverageIgnore
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$type = $journal->transactionType->type;
if (TransactionType::TRANSFER === $type) {
// @codeCoverageIgnoreStart
Log::error(sprintf('Journal #%d is already a transfer so cannot be converted (rule "%s").', $journal->id, $this->action->rule->title));
return false;
// @codeCoverageIgnoreEnd
}
// find the asset account in the action value.
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$repository->setUser($journal->user);
$asset = $repository->findByName(
$this->action->action_value, [AccountType::ASSET, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]
);
if (null === $asset) {
// @codeCoverageIgnoreStart
Log::error(
sprintf(
'Journal #%d cannot be converted because no asset with name "%s" exists (rule "%s").', $journal->id, $this->action->action_value,
$this->action->rule->title
)
);
return false;
// @codeCoverageIgnoreEnd
}
$destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
$sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
// break if count is zero:
if (1 !== $sourceTransactions->count()) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has %d source transactions. ConvertToTransfer failed. (rule "%s").',
[$journal->id, $sourceTransactions->count(), $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
if (0 === $destTransactions->count()) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has %d dest transactions. ConvertToTransfer failed. (rule "%s").',
[$journal->id, $destTransactions->count(), $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
if (TransactionType::WITHDRAWAL === $type) {
Log::debug('Going to transform a withdrawal to a transfer.');
return $this->convertWithdrawal($journal, $asset);
}
if (TransactionType::DEPOSIT === $type) {
Log::debug('Going to transform a deposit to a transfer.');
return $this->convertDeposit($journal, $asset);
}
return false; // @codeCoverageIgnore
}
/**
* A deposit is from Revenue to Asset.
* We replace the Revenue with another asset.
* @deprecated
* @codeCoverageIgnore
* @param TransactionJournal $journal
* @param Account $assetAccount
*
* @return bool
*/
private function convertDeposit(TransactionJournal $journal, Account $assetAccount): bool
{
/** @var Account $destinationAsset */
$destinationAsset = $journal->transactions()->where('amount', '>', 0)->first()->account;
if ($destinationAsset->id === $assetAccount->id) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has already has "%s" as a destination asset. ConvertToTransfer failed. (rule "%s").',
[$journal->id, $assetAccount->name, $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
// update source transactions
$journal->transactions()->where('amount', '<', 0)
->update(['account_id' => $assetAccount->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
$journal->transaction_type_id = $newType->id;
$journal->save();
Log::debug('Converted deposit to transfer.');
return true;
}
/**
* A withdrawal is from Asset to Expense.
* We replace the Expense with another asset.
* @deprecated
* @codeCoverageIgnore
* @param TransactionJournal $journal
* @param Account $assetAccount
*
* @return bool
*/
private function convertWithdrawal(TransactionJournal $journal, Account $assetAccount): bool
{
/** @var Account $sourceAsset */
$sourceAsset = $journal->transactions()->where('amount', '<', 0)->first()->account;
if ($sourceAsset->id === $assetAccount->id) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has already has "%s" as a source asset. ConvertToTransfer failed. (rule "%s").',
[$journal->id, $assetAccount->name, $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
// update destination transactions
$journal->transactions()->where('amount', '>', 0)
->update(['account_id' => $assetAccount->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
$journal->transaction_type_id = $newType->id;
$journal->save();
Log::debug('Converted withdrawal to transfer.');
return true;
}
/**
* @inheritDoc
*/

View File

@@ -53,168 +53,6 @@ class ConvertToWithdrawal implements ActionInterface
$this->action = $action;
}
/**
* Execute the action.
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
* @throws FireflyException
*/
public function act(TransactionJournal $journal): bool
{
$type = $journal->transactionType->type;
if (TransactionType::WITHDRAWAL === $type) {
// @codeCoverageIgnoreStart
Log::error(sprintf('Journal #%d is already a withdrawal (rule "%s").', $journal->id, $this->action->rule->title));
return false;
// @codeCoverageIgnoreEnd
}
$destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
$sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
// break if count is zero:
if (1 !== $sourceTransactions->count()) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has %d source transactions. ConvertToWithdrawal failed. (rule "%s").',
[$journal->id, $sourceTransactions->count(), $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
if (0 === $destTransactions->count()) {
// @codeCoverageIgnoreStart
Log::error(
vsprintf(
'Journal #%d has %d dest transactions. ConvertToWithdrawal failed. (rule "%s").',
[$journal->id, $destTransactions->count(), $this->action->rule->title]
)
);
return false;
// @codeCoverageIgnoreEnd
}
if (TransactionType::DEPOSIT === $type) {
Log::debug('Going to transform a deposit to a withdrawal.');
return $this->convertDeposit($journal);
}
if (TransactionType::TRANSFER === $type) {
Log::debug('Going to transform a transfer to a withdrawal.');
return $this->convertTransfer($journal);
}
return false; // @codeCoverageIgnore
}
/**
* Input is a deposit from A to B
* Is converted to a withdrawal from B to C.
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
* @throws FireflyException
*/
private function convertDeposit(TransactionJournal $journal): bool
{
// find or create expense account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($journal->user);
$destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
$sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
// get the action value, or use the original source revenue name in case the action value is empty:
// this becomes a new or existing expense account.
/** @var Account $source */
$source = $sourceTransactions->first()->account;
$expenseName = '' === $this->action->action_value ? $source->name : $this->action->action_value;
$expense = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $this->action->action_value, $source->name));
unset($source);
// get destination asset account from transaction(s).
/** @var Account $destination */
$destination = $destTransactions->first()->account;
// update source transaction(s) to be the original destination account
$journal->transactions()
->where('amount', '<', 0)
->update(['account_id' => $destination->id]);
// update destination transaction(s) to be new expense account.
$journal->transactions()
->where('amount', '>', 0)
->update(['account_id' => $expense->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
$journal->transaction_type_id = $newType->id;
$journal->save();
Log::debug('Converted deposit to withdrawal.');
return true;
}
/**
* Input is a transfer from A to B.
* Output is a withdrawal from A to C.
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
* @throws FireflyException
*/
private function convertTransfer(TransactionJournal $journal): bool
{
// find or create expense account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($journal->user);
$destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
// get the action value, or use the original destination name in case the action value is empty:
// this becomes a new or existing expense account.
/** @var Account $destination */
$destination = $destTransactions->first()->account;
$expenseName = '' === $this->action->action_value ? $destination->name : $this->action->action_value;
$expense = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", revenue name is "%s"', $this->action->action_value, $destination->name));
unset($source);
// update destination transaction(s) to be the expense account
$journal->transactions()
->where('amount', '>', 0)
->update(['account_id' => $expense->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
$journal->transaction_type_id = $newType->id;
$journal->save();
Log::debug('Converted transfer to withdrawal.');
return true;
}
/**
* Input is a transfer from A to B.
* Output is a withdrawal from A to C.

View File

@@ -44,43 +44,6 @@ class DeleteTransaction implements ActionInterface
{
}
/**
* Will delete transaction journal. Also the group if no other journals are in the group.
* @param TransactionJournal $journal
*
* @return bool
* @throws Exception
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
$count = $journal->transactionGroup->transactionJournals()->count();
// destroy entire group.
if (1 === $count) {
Log::debug(
sprintf(
'RuleAction DeleteTransaction DELETED the entire transaction group of journal #%d ("%s").',
$journal->id, $journal->description
)
);
$service = app(TransactionGroupDestroyService::class);
$service->destroy($journal->transactionGroup);
return true;
}
Log::debug(sprintf('RuleAction DeleteTransaction DELETED transaction journal #%d ("%s").', $journal->id, $journal->description));
// trigger delete factory:
/** @var JournalDestroyService $service */
$service = app(JournalDestroyService::class);
$service->destroy($journal);
return true;
}
/**
* @inheritDoc
*/

View File

@@ -50,36 +50,6 @@ class LinkToBill implements ActionInterface
$this->action = $action;
}
/**
* Set bill to be X.
* @param TransactionJournal $journal
*
* @return bool
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($this->action->rule->user);
$billName = (string) $this->action->action_value;
$bill = $repository->findByName($billName);
if (null !== $bill && $journal->transactionType->type === TransactionType::WITHDRAWAL) {
$journal->bill()->associate($bill);
$journal->save();
Log::debug(sprintf('RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").', $journal->id, $bill->id, $bill->name));
return true;
}
Log::error(sprintf('RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found!', $journal->id, $billName));
return false;
}
/**
* @inheritDoc
*/

View File

@@ -45,24 +45,6 @@ class PrependDescription implements ActionInterface
$this->action = $action;
}
/**
* Prepend description with X
* @codeCoverageIgnore
* @deprecated
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction PrependDescription prepended "%s" to "%s".', $this->action->action_value, $journal->description));
$journal->description = $this->action->action_value . $journal->description;
$journal->save();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -45,30 +45,6 @@ class PrependNotes implements ActionInterface
$this->action = $action;
}
/**
* Prepend notes with X
*
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$dbNote = $journal->notes()->first();
if (null === $dbNote) {
$dbNote = new Note;
$dbNote->noteable()->associate($journal);
}
$notes = $dbNote->text;
Log::debug(sprintf('RuleAction PrependNotes prepended "%s" with "%s".', $notes, $this->action->action_value));
$notes = $this->action->action_value . $notes;
$dbNote->text = $notes;
$dbNote->save();
$journal->save();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -42,23 +42,6 @@ class RemoveAllTags implements ActionInterface
{
}
/**
* Remove all tags
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('RuleAction ClearCategory removed all tags from journal %d.', $journal->id));
$journal->tags()->detach();
$journal->touch();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -44,32 +44,6 @@ class RemoveTag implements ActionInterface
$this->action = $action;
}
/**
* Remove tag X
* @deprecated
* @codeCoverageIgnore
* @param TransactionJournal $journal
*
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
// if tag does not exist, no need to continue:
$name = $this->action->action_value;
$tag = $journal->user->tags()->where('tag', $name)->first();
if (null !== $tag) {
Log::debug(sprintf('RuleAction RemoveTag removed tag #%d ("%s") from journal #%d.', $tag->id, $tag->tag, $journal->id));
$journal->tags()->detach([$tag->id]);
$journal->touch();
return true;
}
Log::debug(sprintf('RuleAction RemoveTag tried to remove tag "%s" from journal #%d but no such tag exists.', $name, $journal->id));
return true;
}
/**
* @inheritDoc
*/

View File

@@ -46,46 +46,6 @@ class SetBudget implements ActionInterface
$this->action = $action;
}
/**
* Set budget.
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$search = $this->action->action_value;
$budget = $journal->user->budgets()->where('name', $search)->first();
if (null === $budget) {
Log::debug(sprintf('RuleAction SetBudget could not set budget of journal #%d to "%s" because no such budget exists.', $journal->id, $search));
return false;
}
if (TransactionType::WITHDRAWAL !== $journal->transactionType->type) {
Log::debug(
sprintf(
'RuleAction SetBudget could not set budget of journal #%d to "%s" because journal is a %s.',
$journal->id,
$search,
$journal->transactionType->type
)
);
return true;
}
Log::debug(sprintf('RuleAction SetBudget set the budget of journal #%d to budget #%d ("%s").', $journal->id, $budget->id, $budget->name));
$journal->budgets()->sync([$budget->id]);
$journal->touch();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -47,35 +47,6 @@ class SetCategory implements ActionInterface
$this->action = $action;
}
/**
* Set category X
*
* @param TransactionJournal $journal
* @deprecated
* @codeCoverageIgnore
* @return bool
*/
public function act(TransactionJournal $journal): bool
{
$name = $this->action->action_value;
/** @var CategoryFactory $factory */
$factory = app(CategoryFactory::class);
$factory->setUser($journal->user);
$category = $factory->findOrCreate(null, $name);
if (null === $category) {
Log::error(sprintf('Action SetCategory did not fire because "%s" did not result in a valid category.', $name));
return false;
}
$journal->categories()->sync([$category->id]);
Log::debug(sprintf('RuleAction SetCategory set the category of journal #%d to category #%d ("%s").', $journal->id, $category->id, $category->name));
return true;
}
/**
* @inheritDoc
*/

View File

@@ -44,33 +44,6 @@ class SetDescription implements ActionInterface
$this->action = $action;
}
/**
* Set description to X
* @param TransactionJournal $journal
*
* @return bool
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
$oldDescription = $journal->description;
$journal->description = $this->action->action_value;
$journal->save();
Log::debug(
sprintf(
'RuleAction SetDescription changed the description of journal #%d from "%s" to "%s".',
$journal->id,
$oldDescription,
$this->action->action_value
)
);
$journal->touch();
return true;
}
/**
* @inheritDoc
*/

View File

@@ -49,19 +49,6 @@ class SetDestinationAccount implements ActionInterface
$this->action = $action;
}
/**
* Set destination account to X
* @param TransactionJournal $journal
*
* @return bool
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
return false;
}
/**
* @return Account|null
*/

View File

@@ -44,31 +44,6 @@ class SetNotes implements ActionInterface
$this->action = $action;
}
/**
* Set notes to X
*
* @param TransactionJournal $journal
* @return bool
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
$dbNote = $journal->notes()->first();
if (null === $dbNote) {
$dbNote = new Note;
$dbNote->noteable()->associate($journal);
}
$oldNotes = $dbNote->text;
$dbNote->text = $this->action->action_value;
$dbNote->save();
$journal->save();
Log::debug(sprintf('RuleAction SetNotes changed the notes of journal #%d from "%s" to "%s".', $journal->id, $oldNotes, $this->action->action_value));
return true;
}
/**
* @inheritDoc
*/

View File

@@ -49,19 +49,6 @@ class SetSourceAccount implements ActionInterface
$this->action = $action;
}
/**
* Set source account to X
*
* @param TransactionJournal $journal
* @return bool
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
return false;
}
/**
* @param string $type
*

View File

@@ -55,60 +55,14 @@ class UpdatePiggybank implements ActionInterface
}
/**
* @inheritDoc
* @param array $journalArray
* @param PiggyBank $piggyBank
* @param string $amount
*/
public function act(TransactionJournal $journal): bool
{
Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal->id));
if (TransactionType::TRANSFER !== $journal->transactionType->type) {
Log::info(sprintf('Journal #%d is a "%s" so skip this action.', $journal->id, $journal->transactionType->type));
return false;
}
$piggyBank = $this->findPiggybank($journal->user);
if (null === $piggyBank) {
Log::info(
sprintf(
'No piggy bank names "%s", cant execute action #%d of rule #%d ("%s")',
$this->action->value, $this->action->id, $this->action->rule_id, $this->action->rule->title,
)
);
return false;
}
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
/** @var Transaction $source */
$source = $journal->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $destination */
$destination = $journal->transactions()->where('amount', '>', 0)->first();
if ((int) $source->account_id === (int) $piggyBank->account_id) {
Log::debug('Piggy bank account is linked to source, so remove amount.');
$this->removeAmount($journal, $piggyBank, $destination->amount);
return true;
}
if ((int) $destination->account_id === (int) $piggyBank->account_id) {
Log::debug('Piggy bank account is linked to source, so add amount.');
$this->addAmount($journal, $piggyBank, $destination->amount);
return true;
}
Log::info('Piggy bank is not linked to source or destination, so no action will be taken.');
return true;
}
/**
* @param TransactionJournal $journal
* @param PiggyBank $piggyBank
* @param string $amount
*/
private function addAmount(TransactionJournal $journal, PiggyBank $piggyBank, string $amount): void
private function addAmount(array $journalArray, PiggyBank $piggyBank, string $amount): void
{
$user = User::find($journalArray['user_id']);
$journal = $user->transactionJournals()->find($journalArray['transaction_journal_id']);
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
@@ -150,12 +104,14 @@ class UpdatePiggybank implements ActionInterface
}
/**
* @param TransactionJournal $journal
* @param PiggyBank $piggyBank
* @param string $amount
* @param array $journalArray
* @param PiggyBank $piggyBank
* @param string $amount
*/
private function removeAmount(TransactionJournal $journal, PiggyBank $piggyBank, string $amount): void
private function removeAmount(array $journalArray, PiggyBank $piggyBank, string $amount): void
{
$user = User::find($journalArray['user_id']);
$journal = $user->transactionJournals()->find($journalArray['transaction_journal_id']);
$repository = app(PiggyBankRepositoryInterface::class);
$repository->setUser($journal->user);
@@ -190,6 +146,42 @@ class UpdatePiggybank implements ActionInterface
*/
public function actOnArray(array $journal): bool
{
// TODO: Implement actOnArray() method.
Log::debug(sprintf('Triggered rule action UpdatePiggybank on journal #%d', $journal['transaction_journal_id']));
if (TransactionType::TRANSFER !== $journal['transaction_type_type']) {
Log::info(sprintf('Journal #%d is a "%s" so skip this action.', $journal['transaction_journal_id'], $journal['transaction_type_type']));
return false;
}
$user = User::find($journal['user_id']);
$piggyBank = $this->findPiggybank($user);
if (null === $piggyBank) {
Log::info(sprintf('No piggy bank names "%s", cant execute action #%d of rule #%d', $this->action->action_value, $this->action->id, $this->action->rule_id));
return false;
}
Log::debug(sprintf('Found piggy bank #%d ("%s")', $piggyBank->id, $piggyBank->name));
/** @var Transaction $source */
$source = Transaction::where('transaction_journal_id', $journal['transaction_journal_id'])->where('amount', '<', 0)->first();
/** @var Transaction $destination */
$destination = Transaction::where('transaction_journal_id', $journal['transaction_journal_id'])->where('amount', '>', 0)->first();
if ((int) $source->account_id === (int) $piggyBank->account_id) {
Log::debug('Piggy bank account is linked to source, so remove amount.');
$this->removeAmount($journal, $piggyBank, $destination->amount);
return true;
}
if ((int) $destination->account_id === (int) $piggyBank->account_id) {
Log::debug('Piggy bank account is linked to source, so add amount.');
$this->addAmount($journal, $piggyBank, $destination->amount);
return true;
}
Log::info('Piggy bank is not linked to source or destination, so no action will be taken.');
return true;
}
}