Start testing new rule actions.

This commit is contained in:
James Cole
2020-08-23 07:42:14 +02:00
parent d89a4d8a54
commit 6e074d9b8b
34 changed files with 608 additions and 246 deletions

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use DB;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\AccountFactory;
use FireflyIII\Models\Account;
@@ -31,6 +32,7 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
use Log;
/**
@@ -39,8 +41,7 @@ use Log;
*/
class ConvertToDeposit implements ActionInterface
{
/** @var RuleAction The rule action */
private $action;
private RuleAction $action;
/**
* TriggerInterface constructor.
@@ -56,7 +57,7 @@ class ConvertToDeposit implements ActionInterface
* Execute the action.
*
* @param TransactionJournal $journal
*
* @deprecated
* @return bool
* @throws FireflyException
*/
@@ -120,9 +121,9 @@ class ConvertToDeposit implements ActionInterface
* Output is a deposit from C to B.
*
* @param TransactionJournal $journal
*
* @return bool
* @throws FireflyException
* @deprecated
*/
private function convertTransfer(TransactionJournal $journal): bool
{
@@ -157,12 +158,55 @@ class ConvertToDeposit implements ActionInterface
return true;
}
/**
* Input is a transfer from A to B.
* Output is a deposit from C to B.
*
* @param array $journal
*
* @return bool
* @throws FireflyException
*/
private function convertTransferArray(array $journal): bool
{
$user = User::find($journal['user_id']);
// find or create revenue account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($user);
// 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.
$revenueName = '' === $this->action->action_value ? $journal['source_account_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, $journal['source_account_name']));
unset($source);
// update source transaction(s) to be revenue account
DB::table('transactions')
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
->where('amount', '<', 0)
->update(['account_id' => $revenue->id]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
DB::table('transaction_journals')
->where('id', '=', $journal['transaction_journal_id'])
->update(['transaction_type_id' => $newType->id]);
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
*
* @deprecated
* @return bool
* @throws FireflyException
*/
@@ -209,4 +253,79 @@ class ConvertToDeposit implements ActionInterface
return true;
}
/**
* Input is a withdrawal from A to B
* Is converted to a deposit from C to A.
*
* @param array $journal
*
* @return bool
* @throws FireflyException
*/
private function convertWithdrawalArray(array $journal): bool
{
$user = User::find($journal['user_id']);
// find or create revenue account.
/** @var AccountFactory $factory */
$factory = app(AccountFactory::class);
$factory->setUser($user);
// 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.
$revenueName = '' === $this->action->action_value ? $journal['destination_account_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, $journal['destination_account_name']));
// update the source transaction and put in the new revenue ID.
DB::table('transactions')
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
->where('amount', '<', 0)
->update(['account_id' => $revenue->id]);
// update the destination transaction and put in the original source account ID.
DB::table('transactions')
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
->where('amount', '>', 0)
->update(['account_id' => $journal['source_account_id']]);
// change transaction type of journal:
$newType = TransactionType::whereType(TransactionType::DEPOSIT)->first();
DB::table('transaction_journals')
->where('id', '=', $journal['transaction_journal_id'])
->update(['transaction_type_id' => $newType->id]);
Log::debug('Converted withdrawal to deposit.');
return true;
}
/**
* @inheritDoc
* @throws FireflyException
*/
public function actOnArray(array $journal): bool
{
Log::debug(sprintf('Convert journal #%d to deposit.', $journal['transaction_journal_id']));
$type = $journal['transaction_type_type'];
if (TransactionType::DEPOSIT === $type) {
Log::error(sprintf('Journal #%d is already a deposit (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
return false;
}
if (TransactionType::WITHDRAWAL === $type) {
Log::debug('Going to transform a withdrawal to a deposit.');
return $this->convertWithdrawalArray($journal);
}
if (TransactionType::TRANSFER === $type) {
Log::debug('Going to transform a transfer to a deposit.');
return $this->convertTransferArray($journal);
}
return false;
}
}