2023-08-11 06:16:40 +02:00
< ? php
2024-11-25 04:18:55 +01:00
2023-08-11 06:16:40 +02:00
/**
* ConvertToWithdrawal . php
* Copyright ( c ) 2019 james @ firefly - iii . org
*
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
declare ( strict_types = 1 );
namespace FireflyIII\TransactionRules\Actions ;
2023-08-13 15:01:12 +02:00
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray ;
2023-08-11 06:16:40 +02:00
use FireflyIII\Events\TriggeredAuditLog ;
use FireflyIII\Models\RuleAction ;
use FireflyIII\Models\Transaction ;
use FireflyIII\Models\TransactionJournal ;
use FireflyIII\Models\TransactionType ;
/**
* Class SwitchAccounts
*/
class SwitchAccounts implements ActionInterface
{
private RuleAction $action ;
/**
* TriggerInterface constructor .
*/
public function __construct ( RuleAction $action )
{
$this -> action = $action ;
}
public function actOnArray ( array $journal ) : bool
{
// make object from array (so the data is fresh).
2023-12-20 19:35:52 +01:00
/** @var null|TransactionJournal $object */
2024-01-01 14:43:56 +01:00
$object = TransactionJournal :: where ( 'user_id' , $journal [ 'user_id' ]) -> find ( $journal [ 'transaction_journal_id' ]);
2023-08-11 06:16:40 +02:00
if ( null === $object ) {
2023-10-29 06:32:00 +01:00
app ( 'log' ) -> error ( sprintf ( 'Cannot find journal #%d, cannot switch accounts.' , $journal [ 'transaction_journal_id' ]));
2023-08-13 15:01:12 +02:00
event ( new RuleActionFailedOnArray ( $this -> action , $journal , trans ( 'rules.no_such_journal' )));
2023-12-20 19:35:52 +01:00
2023-08-11 06:16:40 +02:00
return false ;
}
2024-01-01 14:43:56 +01:00
$groupCount = TransactionJournal :: where ( 'transaction_group_id' , $journal [ 'transaction_group_id' ]) -> count ();
2023-08-11 06:16:40 +02:00
if ( $groupCount > 1 ) {
2023-10-29 06:32:00 +01:00
app ( 'log' ) -> error ( sprintf ( 'Group #%d has more than one transaction in it, cannot switch accounts.' , $journal [ 'transaction_group_id' ]));
2023-08-13 15:01:12 +02:00
event ( new RuleActionFailedOnArray ( $this -> action , $journal , trans ( 'rules.split_group' )));
2023-12-20 19:35:52 +01:00
2023-08-11 06:16:40 +02:00
return false ;
}
2024-01-01 14:43:56 +01:00
$type = $object -> transactionType -> type ;
2023-08-11 06:16:40 +02:00
if ( TransactionType :: TRANSFER !== $type ) {
2023-10-29 06:32:00 +01:00
app ( 'log' ) -> error ( sprintf ( 'Journal #%d is NOT a transfer (rule #%d), cannot switch accounts.' , $journal [ 'transaction_journal_id' ], $this -> action -> rule_id ));
2023-08-13 15:01:12 +02:00
event ( new RuleActionFailedOnArray ( $this -> action , $journal , trans ( 'rules.is_not_transfer' )));
2023-12-20 19:35:52 +01:00
2023-08-11 06:16:40 +02:00
return false ;
}
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $sourceTransaction */
2024-01-01 14:43:56 +01:00
$sourceTransaction = $object -> transactions () -> where ( 'amount' , '<' , 0 ) -> first ();
2023-12-20 19:35:52 +01:00
/** @var null|Transaction $destTransaction */
2024-01-01 14:43:56 +01:00
$destTransaction = $object -> transactions () -> where ( 'amount' , '>' , 0 ) -> first ();
2023-08-11 06:16:40 +02:00
if ( null === $sourceTransaction || null === $destTransaction ) {
2023-10-29 06:32:00 +01:00
app ( 'log' ) -> error ( sprintf ( 'Journal #%d has no source or destination transaction (rule #%d), cannot switch accounts.' , $journal [ 'transaction_journal_id' ], $this -> action -> rule_id ));
2023-08-13 15:01:12 +02:00
event ( new RuleActionFailedOnArray ( $this -> action , $journal , trans ( 'rules.cannot_find_accounts' )));
2023-12-20 19:35:52 +01:00
2023-08-11 06:16:40 +02:00
return false ;
}
2023-11-05 19:41:37 +01:00
$sourceAccountId = $sourceTransaction -> account_id ;
2023-08-11 06:16:40 +02:00
$destinationAccountId = $destTransaction -> account_id ;
$sourceTransaction -> account_id = $destinationAccountId ;
$destTransaction -> account_id = $sourceAccountId ;
$sourceTransaction -> save ();
$destTransaction -> save ();
event ( new TriggeredAuditLog ( $this -> action -> rule , $object , 'switch_accounts' , $sourceAccountId , $destinationAccountId ));
return true ;
}
}