2016-10-29 02:22:51 -05:00
< ? php
/**
* SetDestinationAccount . php
2020-02-16 06:57:05 -06:00
* Copyright ( c ) 2019 james @ firefly - iii . org
2016-10-29 02:22:51 -05:00
*
2019-10-01 23:37:26 -05:00
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
2016-10-29 02:22:51 -05:00
*
2019-10-01 23:37:26 -05:00
* 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 .
2017-10-21 01:40:00 -05:00
*
2019-10-01 23:37:26 -05:00
* This program is distributed in the hope that it will be useful ,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2019-10-01 23:37:26 -05:00
* GNU Affero General Public License for more details .
2017-10-21 01:40:00 -05:00
*
2019-10-01 23:37:26 -05:00
* 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 />.
2016-10-29 02:22:51 -05:00
*/
2017-04-09 00:44:22 -05:00
declare ( strict_types = 1 );
2016-10-29 02:22:51 -05:00
2017-09-13 00:49:58 -05:00
namespace FireflyIII\TransactionRules\Actions ;
2016-10-29 02:22:51 -05:00
2020-08-23 09:12:16 -05:00
use DB ;
2016-10-29 02:22:51 -05:00
use FireflyIII\Models\Account ;
use FireflyIII\Models\AccountType ;
use FireflyIII\Models\RuleAction ;
use FireflyIII\Repositories\Account\AccountRepositoryInterface ;
2020-08-23 09:12:16 -05:00
use FireflyIII\User ;
2016-10-29 02:22:51 -05:00
use Log ;
/**
2017-11-15 05:25:49 -06:00
* Class SetDestinationAccount .
2016-10-29 02:22:51 -05:00
*/
class SetDestinationAccount implements ActionInterface
{
2020-08-23 09:12:16 -05:00
private RuleAction $action ;
private AccountRepositoryInterface $repository ;
2016-10-29 02:22:51 -05:00
/**
* TriggerInterface constructor .
*
* @ param RuleAction $action
*/
public function __construct ( RuleAction $action )
{
$this -> action = $action ;
}
/**
2020-08-23 09:12:16 -05:00
* @ return Account | null
2016-10-29 02:22:51 -05:00
*/
2020-08-23 09:12:16 -05:00
private function findExpenseAccount () : ? Account
2016-10-29 02:22:51 -05:00
{
2016-11-08 14:34:13 -06:00
$account = $this -> repository -> findByName ( $this -> action -> action_value , [ AccountType :: EXPENSE ]);
2018-02-16 08:19:19 -06:00
if ( null === $account ) {
2016-10-29 02:22:51 -05:00
$data = [
2018-02-23 09:21:28 -06:00
'name' => $this -> action -> action_value ,
2019-08-17 03:47:29 -05:00
'account_type' => 'expense' ,
2018-02-23 09:21:28 -06:00
'account_type_id' => null ,
2019-08-17 03:47:29 -05:00
'virtual_balance' => 0 ,
2018-02-23 09:21:28 -06:00
'active' => true ,
'iban' => null ,
2016-10-29 02:22:51 -05:00
];
$account = $this -> repository -> store ( $data );
}
Log :: debug ( sprintf ( 'Found or created expense account #%d ("%s")' , $account -> id , $account -> name ));
2020-08-23 09:12:16 -05:00
return $account ;
2016-10-29 02:22:51 -05:00
}
2020-08-23 00:42:14 -05:00
/**
* @ inheritDoc
*/
public function actOnArray ( array $journal ) : bool
{
2020-08-23 09:12:16 -05:00
$user = User :: find ( $journal [ 'user_id' ]);
$type = $journal [ 'transaction_type_type' ];
$this -> repository = app ( AccountRepositoryInterface :: class );
$this -> repository -> setUser ( $user );
// it depends on the type what kind of destination account is expected.
$expectedTypes = config ( sprintf ( 'firefly.source_dests.%s.%s' , $type , $journal [ 'source_account_type' ]));
if ( null === $expectedTypes ) {
Log :: error ( sprintf ( 'Configuration line "%s" is unexpectedly empty. Stopped.' , sprintf ( 'firefly.source_dests.%s.%s' , $type , $journal [ 'source_account_type' ])));
return false ;
}
// try to find an account with the destination name and these types:
$destination = $this -> findAccount ( $expectedTypes );
if ( null !== $destination ) {
// update account of destination transaction.
DB :: table ( 'transactions' )
-> where ( 'transaction_journal_id' , '=' , $journal [ 'transaction_journal_id' ])
-> where ( 'amount' , '>' , 0 )
-> update ([ 'account_id' => $destination -> id ]);
Log :: debug ( sprintf ( 'Updated journal #%d and gave it new account ID.' , $journal [ 'transaction_journal_id' ]));
return true ;
}
Log :: info ( sprintf ( 'Expected destination account "%s" not found.' , $this -> action -> action_value ));
if ( in_array ( AccountType :: EXPENSE , $expectedTypes )) {
// does not exist, but can be created.
Log :: debug ( 'Expected type is expense, lets create it.' );
$expense = $this -> findExpenseAccount ();
if ( null === $expense ) {
Log :: error ( 'Could not create expense account.' );
return false ;
}
DB :: table ( 'transactions' )
-> where ( 'transaction_journal_id' , '=' , $journal [ 'transaction_journal_id' ])
-> where ( 'amount' , '>' , 0 )
-> update ([ 'account_id' => $expense -> id ]);
Log :: debug ( sprintf ( 'Updated journal #%d and gave it new account ID.' , $journal [ 'transaction_journal_id' ]));
return true ;
}
return false ;
}
/**
* @ param array $types
* @ return Account | null
*/
private function findAccount ( array $types ) : ? Account
{
return $this -> repository -> findByName ( $this -> action -> action_value , $types );
2020-08-23 00:42:14 -05:00
}
2016-10-29 02:22:51 -05:00
}