mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 06:31:22 +00:00
Merge remote-tracking branch 'upstream/main' into feat/expression-engine
This commit is contained in:
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\TransactionRules\Actions;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
|
||||
use FireflyIII\Events\Model\Rule\RuleActionFailedOnObject;
|
||||
use FireflyIII\Events\TriggeredAuditLog;
|
||||
@@ -36,10 +35,8 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\TransactionRules\Expressions\ActionExpressionEvaluator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConvertToTransfer
|
||||
*/
|
||||
class ConvertToTransfer implements ActionInterface
|
||||
@@ -49,42 +46,43 @@ class ConvertToTransfer implements ActionInterface
|
||||
|
||||
/**
|
||||
* TriggerInterface constructor.
|
||||
*
|
||||
* @param RuleAction $action
|
||||
*/
|
||||
public function __construct(RuleAction $action, ActionExpressionEvaluator $evaluator)
|
||||
{
|
||||
$this->action = $action;
|
||||
$this->action = $action;
|
||||
$this->evaluator = $evaluator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
public function actOnArray(array $journal): bool
|
||||
{
|
||||
$actionValue = $this->evaluator->evaluate($journal);
|
||||
|
||||
// make object from array (so the data is fresh).
|
||||
/** @var TransactionJournal|null $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
/** @var null|TransactionJournal $object */
|
||||
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
|
||||
if (null === $object) {
|
||||
Log::error(sprintf('Cannot find journal #%d, cannot convert to transfer.', $journal['transaction_journal_id']));
|
||||
app('log')->error(sprintf('Cannot find journal #%d, cannot convert to transfer.', $journal['transaction_journal_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_not_found')));
|
||||
|
||||
return false;
|
||||
}
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
$groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count();
|
||||
if ($groupCount > 1) {
|
||||
Log::error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id']));
|
||||
app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id']));
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group')));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$type = $object->transactionType->type;
|
||||
$user = $object->user;
|
||||
$journalId = (int)$object->id;
|
||||
$type = $object->transactionType->type;
|
||||
$user = $object->user;
|
||||
$journalId = $object->id;
|
||||
if (TransactionType::TRANSFER === $type) {
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf('Journal #%d is already a transfer so cannot be converted (rule #%d).', $object->id, $this->action->rule_id)
|
||||
);
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.is_already_transfer')));
|
||||
@@ -93,12 +91,13 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
if (TransactionType::DEPOSIT !== $type && TransactionType::WITHDRAWAL !== $type) {
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_transfer', ['type' => $type])));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// find the asset account in the action value.
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$repository->setUser($user);
|
||||
$expectedType = null;
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
@@ -109,10 +108,10 @@ class ConvertToTransfer implements ActionInterface
|
||||
$expectedType = $this->getDestinationType($journalId);
|
||||
// Deposit? Replace source with account with same type as destination.
|
||||
}
|
||||
$opposing = $repository->findByName($actionValue, [$expectedType]);
|
||||
$opposing = $repository->findByName($actionValue, [$expectedType]);
|
||||
|
||||
if (null === $opposing) {
|
||||
Log::error(
|
||||
app('log')->error(
|
||||
sprintf(
|
||||
'Journal #%d cannot be converted because no valid %s account with name "%s" exists (rule #%d).',
|
||||
$expectedType,
|
||||
@@ -127,68 +126,65 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
if (TransactionType::WITHDRAWAL === $type) {
|
||||
Log::debug('Going to transform a withdrawal to a transfer.');
|
||||
app('log')->debug('Going to transform a withdrawal to a transfer.');
|
||||
|
||||
try {
|
||||
$res = $this->convertWithdrawalArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
Log::debug('Could not convert withdrawal to transfer.');
|
||||
Log::error($e->getMessage());
|
||||
app('log')->debug('Could not convert withdrawal to transfer.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
if (false !== $res) {
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::WITHDRAWAL, TransactionType::TRANSFER));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
if (TransactionType::DEPOSIT === $type) {
|
||||
Log::debug('Going to transform a deposit to a transfer.');
|
||||
try {
|
||||
$res = $this->convertDepositArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
Log::debug('Could not convert deposit to transfer.');
|
||||
Log::error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
return false;
|
||||
}
|
||||
if (false !== $res) {
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER));
|
||||
}
|
||||
return $res;
|
||||
// can only be a deposit at this point.
|
||||
app('log')->debug('Going to transform a deposit to a transfer.');
|
||||
|
||||
try {
|
||||
$res = $this->convertDepositArray($object, $opposing);
|
||||
} catch (FireflyException $e) {
|
||||
app('log')->debug('Could not convert deposit to transfer.');
|
||||
app('log')->error($e->getMessage());
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.complex_error')));
|
||||
|
||||
return false;
|
||||
}
|
||||
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.unsupported_transaction_type_transfer', ['type' => $type])));
|
||||
return false;
|
||||
if (false !== $res) {
|
||||
event(new TriggeredAuditLog($this->action->rule, $object, 'update_transaction_type', TransactionType::DEPOSIT, TransactionType::TRANSFER));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $journalId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getSourceType(int $journalId): string
|
||||
{
|
||||
/** @var TransactionJournal $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = TransactionJournal::find($journalId);
|
||||
if (null === $journal) {
|
||||
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
|
||||
app('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
|
||||
*/
|
||||
private function getDestinationType(int $journalId): string
|
||||
{
|
||||
/** @var TransactionJournal $journal */
|
||||
/** @var null|TransactionJournal $journal */
|
||||
$journal = TransactionJournal::find($journalId);
|
||||
if (null === $journal) {
|
||||
Log::error(sprintf('Journal #%d does not exist. Cannot convert to transfer.', $journalId));
|
||||
app('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;
|
||||
}
|
||||
|
||||
@@ -197,17 +193,13 @@ class ConvertToTransfer implements ActionInterface
|
||||
* We replace the Expense with another asset.
|
||||
* So this replaces the destination
|
||||
*
|
||||
* @param TransactionJournal $journal
|
||||
* @param Account $opposing
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function convertWithdrawalArray(TransactionJournal $journal, Account $opposing): bool
|
||||
{
|
||||
$sourceAccount = $this->getSourceAccount($journal);
|
||||
if ((int)$sourceAccount->id === (int)$opposing->id) {
|
||||
Log::error(
|
||||
if ($sourceAccount->id === $opposing->id) {
|
||||
app('log')->error(
|
||||
vsprintf(
|
||||
'Journal #%d has already has "%s" as a source asset. ConvertToTransfer failed. (rule #%d).',
|
||||
[$journal->id, $opposing->name, $this->action->rule_id]
|
||||
@@ -219,36 +211,34 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
// update destination transaction:
|
||||
DB::table('transactions')
|
||||
\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();
|
||||
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
|
||||
Log::debug('Converted withdrawal to transfer.');
|
||||
app('log')->debug('Converted withdrawal to transfer.');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionJournal $journal
|
||||
*
|
||||
* @return Account
|
||||
* @throws FireflyException
|
||||
*/
|
||||
private function getSourceAccount(TransactionJournal $journal): Account
|
||||
{
|
||||
/** @var Transaction|null $sourceTransaction */
|
||||
/** @var null|Transaction $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;
|
||||
}
|
||||
|
||||
@@ -256,17 +246,13 @@ 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
|
||||
*/
|
||||
private function convertDepositArray(TransactionJournal $journal, Account $opposing): bool
|
||||
{
|
||||
$destAccount = $this->getDestinationAccount($journal);
|
||||
if ((int)$destAccount->id === (int)$opposing->id) {
|
||||
Log::error(
|
||||
if ($destAccount->id === $opposing->id) {
|
||||
app('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]
|
||||
@@ -278,36 +264,34 @@ class ConvertToTransfer implements ActionInterface
|
||||
}
|
||||
|
||||
// update source transaction:
|
||||
DB::table('transactions')
|
||||
\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();
|
||||
$newType = TransactionType::whereType(TransactionType::TRANSFER)->first();
|
||||
|
||||
DB::table('transaction_journals')
|
||||
\DB::table('transaction_journals')
|
||||
->where('id', '=', $journal->id)
|
||||
->update(['transaction_type_id' => $newType->id, 'bill_id' => null]);
|
||||
|
||||
Log::debug('Converted deposit to transfer.');
|
||||
app('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 */
|
||||
/** @var null|Transaction $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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user