mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Update conversion actions.
This commit is contained in:
parent
6e074d9b8b
commit
fc519c41bc
@ -31,8 +31,9 @@ use FireflyIII\Models\AccountType;
|
|||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
|
use FireflyIII\User;
|
||||||
use Log;
|
use Log;
|
||||||
|
use DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -40,8 +41,7 @@ use Log;
|
|||||||
*/
|
*/
|
||||||
class ConvertToWithdrawal implements ActionInterface
|
class ConvertToWithdrawal implements ActionInterface
|
||||||
{
|
{
|
||||||
/** @var RuleAction The rule action */
|
private RuleAction $action;
|
||||||
private $action;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TriggerInterface constructor.
|
* TriggerInterface constructor.
|
||||||
@ -57,7 +57,8 @@ class ConvertToWithdrawal implements ActionInterface
|
|||||||
* Execute the action.
|
* Execute the action.
|
||||||
*
|
*
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
* @deprecated
|
||||||
|
* @codeCoverageIgnore
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
@ -121,7 +122,8 @@ class ConvertToWithdrawal implements ActionInterface
|
|||||||
* Is converted to a withdrawal from B to C.
|
* Is converted to a withdrawal from B to C.
|
||||||
*
|
*
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
* @deprecated
|
||||||
|
* @codeCoverageIgnore
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
@ -174,7 +176,8 @@ class ConvertToWithdrawal implements ActionInterface
|
|||||||
* Output is a withdrawal from A to C.
|
* Output is a withdrawal from A to C.
|
||||||
*
|
*
|
||||||
* @param TransactionJournal $journal
|
* @param TransactionJournal $journal
|
||||||
*
|
* @deprecated
|
||||||
|
* @codeCoverageIgnore
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
@ -211,11 +214,108 @@ class ConvertToWithdrawal implements ActionInterface
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input is a transfer from A to B.
|
||||||
|
* Output is a withdrawal from A to C.
|
||||||
|
*
|
||||||
|
* @param array $journal
|
||||||
|
* @return bool
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function convertTransferArray(array $journal): bool
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// find or create expense account.
|
||||||
|
$user = User::find($journal['user_id']);
|
||||||
|
/** @var AccountFactory $factory */
|
||||||
|
$factory = app(AccountFactory::class);
|
||||||
|
$factory->setUser($user);
|
||||||
|
|
||||||
|
|
||||||
|
$expenseName = '' === $this->action->action_value ? $journal['destination_account_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, $expenseName));
|
||||||
|
|
||||||
|
// update destination transaction(s) to be new expense account.
|
||||||
|
DB::table('transactions')
|
||||||
|
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
|
||||||
|
->where('amount', '>', 0)
|
||||||
|
->update(['account_id' => $expense->id]);
|
||||||
|
|
||||||
|
// change transaction type of journal:
|
||||||
|
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||||
|
DB::table('transaction_journals')
|
||||||
|
->where('id', '=', $journal['transaction_journal_id'])
|
||||||
|
->update(['transaction_type_id' => $newType->id]);
|
||||||
|
|
||||||
|
Log::debug('Converted transfer to withdrawal.');
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
public function actOnArray(array $journal): bool
|
public function actOnArray(array $journal): bool
|
||||||
{
|
{
|
||||||
// TODO: Implement actOnArray() method.
|
$type = $journal['transaction_type_type'];
|
||||||
|
if (TransactionType::WITHDRAWAL === $type) {
|
||||||
|
Log::error(sprintf('Journal #%d is already a withdrawal (rule #%d).', $journal['transaction_journal_id'], $this->action->rule_id));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (TransactionType::DEPOSIT === $type) {
|
||||||
|
Log::debug('Going to transform a deposit to a withdrawal.');
|
||||||
|
|
||||||
|
return $this->convertDepositArray($journal);
|
||||||
|
}
|
||||||
|
if (TransactionType::TRANSFER === $type) {
|
||||||
|
Log::debug('Going to transform a transfer to a withdrawal.');
|
||||||
|
|
||||||
|
return $this->convertTransferArray($journal);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false; // @codeCoverageIgnore
|
||||||
|
}
|
||||||
|
|
||||||
|
private function convertDepositArray(array $journal): bool
|
||||||
|
{
|
||||||
|
$user = User::find($journal['user_id']);
|
||||||
|
/** @var AccountFactory $factory */
|
||||||
|
$factory = app(AccountFactory::class);
|
||||||
|
$factory->setUser($user);
|
||||||
|
|
||||||
|
$expenseName = '' === $this->action->action_value ? $journal['source_account_name'] : $this->action->action_value;
|
||||||
|
$expense = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
|
||||||
|
$destinationId = $journal['destination_account_id'];
|
||||||
|
|
||||||
|
|
||||||
|
Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $this->action->action_value, $expenseName));
|
||||||
|
|
||||||
|
// update source transaction(s) to be the original destination account
|
||||||
|
DB::table('transactions')
|
||||||
|
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
|
||||||
|
->where('amount', '<', 0)
|
||||||
|
->update(['account_id' => $destinationId]);
|
||||||
|
|
||||||
|
// update destination transaction(s) to be new expense account.
|
||||||
|
DB::table('transactions')
|
||||||
|
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
|
||||||
|
->where('amount', '>', 0)
|
||||||
|
->update(['account_id' => $expense->id]);
|
||||||
|
|
||||||
|
// change transaction type of journal:
|
||||||
|
$newType = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
|
||||||
|
DB::table('transaction_journals')
|
||||||
|
->where('id', '=', $journal['transaction_journal_id'])
|
||||||
|
->update(['transaction_type_id' => $newType->id]);
|
||||||
|
|
||||||
|
Log::debug('Converted deposit to withdrawal.');
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,8 +26,10 @@ namespace Tests\Unit\TransactionRules\Actions;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use FireflyIII\Factory\AccountFactory;
|
use FireflyIII\Factory\AccountFactory;
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
use FireflyIII\Models\RuleAction;
|
use FireflyIII\Models\RuleAction;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\TransactionRules\Actions\ConvertToWithdrawal;
|
use FireflyIII\TransactionRules\Actions\ConvertToWithdrawal;
|
||||||
use Log;
|
use Log;
|
||||||
@ -47,9 +49,6 @@ class ConvertToWithdrawalTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
self::markTestIncomplete('Incomplete for refactor.');
|
|
||||||
|
|
||||||
return;
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
Log::info(sprintf('Now in %s.', get_class($this)));
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
||||||
}
|
}
|
||||||
@ -61,25 +60,33 @@ class ConvertToWithdrawalTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testActDeposit()
|
public function testActDeposit()
|
||||||
{
|
{
|
||||||
$expense = $this->getRandomExpense();
|
/** @var TransactionJournal $deposit */
|
||||||
$name = 'Random expense #' . $this->randomInt();
|
$deposit = $this->user()->transactionJournals()->where('description', 'Deposit for ConvertToWithdrawalTest.')->first();
|
||||||
$deposit = $this->getRandomDeposit();
|
|
||||||
|
// new expense account:
|
||||||
|
$name = sprintf('Random expense #%d', $this->randomInt());
|
||||||
|
|
||||||
|
// original destination:
|
||||||
|
$destination = Account::where('name','Checking Account')->first();
|
||||||
|
|
||||||
// journal is a deposit:
|
// journal is a deposit:
|
||||||
$this->assertEquals(TransactionType::DEPOSIT, $deposit->transactionType->type);
|
$this->assertEquals(TransactionType::DEPOSIT, $deposit->transactionType->type);
|
||||||
|
|
||||||
// mock used stuff:
|
// array:
|
||||||
$factory = $this->mock(AccountFactory::class);
|
$array = [
|
||||||
$factory->shouldReceive('setUser')->once();
|
'user_id' => $this->user()->id,
|
||||||
$factory->shouldReceive('findOrCreate')->once()->withArgs([$name, AccountType::EXPENSE])->andReturn($expense);
|
'transaction_journal_id' => $deposit->id,
|
||||||
|
'transaction_type_type' => $deposit->transactionType->type,
|
||||||
|
'source_account_name' => 'Boss',
|
||||||
|
'destination_account_id' => $destination->id
|
||||||
|
];
|
||||||
|
|
||||||
// fire the action:
|
// fire the action:
|
||||||
$ruleAction = new RuleAction;
|
$ruleAction = new RuleAction;
|
||||||
$ruleAction->action_value = $name;
|
$ruleAction->action_value = $name;
|
||||||
$action = new ConvertToWithdrawal($ruleAction);
|
$action = new ConvertToWithdrawal($ruleAction);
|
||||||
try {
|
try {
|
||||||
$result = $action->act($deposit);
|
$result = $action->actOnArray($array);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
Log::error($e->getTraceAsString());
|
Log::error($e->getTraceAsString());
|
||||||
@ -93,28 +100,39 @@ class ConvertToWithdrawalTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a transfer to a deposit.
|
* Convert a transfer to a withdrawal.
|
||||||
*
|
*
|
||||||
* @covers \FireflyIII\TransactionRules\Actions\ConvertToWithdrawal
|
* @covers \FireflyIII\TransactionRules\Actions\ConvertToWithdrawal
|
||||||
*/
|
*/
|
||||||
public function testActTransfer()
|
public function testActTransfer()
|
||||||
{
|
{
|
||||||
$expense = $this->getRandomExpense();
|
/** @var TransactionJournal $transfer */
|
||||||
$name = 'Random expense #' . $this->randomInt();
|
$transfer = $this->user()->transactionJournals()->where('description', 'Transfer for ConvertToWithdrawalTest.')->first();
|
||||||
$transfer = $this->getRandomTransfer();
|
|
||||||
|
|
||||||
// mock used stuff:
|
// new expense account:
|
||||||
$factory = $this->mock(AccountFactory::class);
|
$name = sprintf('Random new expense #%d', $this->randomInt());
|
||||||
$factory->shouldReceive('setUser')->once();
|
|
||||||
$factory->shouldReceive('findOrCreate')->once()->withArgs([$name, AccountType::EXPENSE])->andReturn($expense);
|
|
||||||
|
|
||||||
|
$destination = Account::whereName('Checking Account')->first();
|
||||||
|
|
||||||
|
// journal is a transfer:
|
||||||
|
$this->assertEquals(TransactionType::TRANSFER, $transfer->transactionType->type);
|
||||||
|
|
||||||
|
// array:
|
||||||
|
$array = [
|
||||||
|
'user_id' => $this->user()->id,
|
||||||
|
'transaction_journal_id' => $transfer->id,
|
||||||
|
'transaction_type_type' => $transfer->transactionType->type,
|
||||||
|
'destination_account_name' => $destination->name,
|
||||||
|
//'source_account_name' => 'Boss',
|
||||||
|
//'destination_account_id' => $destination->id
|
||||||
|
];
|
||||||
|
|
||||||
// fire the action:
|
// fire the action:
|
||||||
$ruleAction = new RuleAction;
|
$ruleAction = new RuleAction;
|
||||||
$ruleAction->action_value = $name;
|
$ruleAction->action_value = $name;
|
||||||
$action = new ConvertToWithdrawal($ruleAction);
|
$action = new ConvertToWithdrawal($ruleAction);
|
||||||
try {
|
try {
|
||||||
$result = $action->act($transfer);
|
$result = $action->actOnArray($array);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error($e->getMessage());
|
Log::error($e->getMessage());
|
||||||
Log::error($e->getTraceAsString());
|
Log::error($e->getTraceAsString());
|
||||||
@ -122,7 +140,7 @@ class ConvertToWithdrawalTest extends TestCase
|
|||||||
}
|
}
|
||||||
$this->assertTrue($result);
|
$this->assertTrue($result);
|
||||||
|
|
||||||
// journal is now a deposit.
|
// journal is now a withdrawal.
|
||||||
$transfer->refresh();
|
$transfer->refresh();
|
||||||
$this->assertEquals(TransactionType::WITHDRAWAL, $transfer->transactionType->type);
|
$this->assertEquals(TransactionType::WITHDRAWAL, $transfer->transactionType->type);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user