2018-04-07 06:20:45 +02:00
< ? php
2022-12-29 19:42:26 +01:00
2018-04-07 06:20:45 +02:00
/**
* LinkToBill . php
2020-02-16 13:57:05 +01:00
* Copyright ( c ) 2019 james @ firefly - iii . org
2018-04-07 06:20:45 +02:00
*
2019-10-02 06:37:26 +02:00
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
2018-04-07 06:20:45 +02:00
*
2019-10-02 06:37:26 +02: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 .
2018-04-07 06:20:45 +02:00
*
2019-10-02 06:37:26 +02:00
* This program is distributed in the hope that it will be useful ,
2018-04-07 06:20:45 +02:00
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2019-10-02 06:37:26 +02:00
* GNU Affero General Public License for more details .
2018-04-07 06:20:45 +02:00
*
2019-10-02 06:37:26 +02: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 />.
2018-04-07 06:20:45 +02:00
*/
declare ( strict_types = 1 );
namespace FireflyIII\TransactionRules\Actions ;
2025-01-03 09:05:19 +01:00
use FireflyIII\Enums\TransactionTypeEnum ;
2023-08-13 15:01:12 +02:00
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray ;
2022-10-02 06:23:31 +02:00
use FireflyIII\Events\TriggeredAuditLog ;
2018-04-07 06:20:45 +02:00
use FireflyIII\Models\RuleAction ;
2022-10-02 06:23:31 +02:00
use FireflyIII\Models\TransactionJournal ;
2018-04-07 06:20:45 +02:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface ;
2020-08-23 08:51:58 +02:00
use FireflyIII\User ;
2025-02-23 12:47:04 +01:00
use Illuminate\Support\Facades\DB ;
2025-11-02 14:00:55 +01:00
use Illuminate\Support\Facades\Log ;
2018-04-07 06:20:45 +02:00
/**
* Class LinkToBill .
*/
class LinkToBill implements ActionInterface
{
/**
* TriggerInterface constructor .
*/
2025-05-04 13:55:42 +02:00
public function __construct ( private readonly RuleAction $action ) {}
2018-04-07 06:20:45 +02:00
2020-08-23 07:42:14 +02:00
public function actOnArray ( array $journal ) : bool
{
2023-11-28 17:18:31 +01:00
/** @var User $user */
2024-01-01 14:43:56 +01:00
$user = User :: find ( $journal [ 'user_id' ]);
2023-12-20 19:35:52 +01:00
2020-08-23 08:51:58 +02:00
/** @var BillRepositoryInterface $repository */
$repository = app ( BillRepositoryInterface :: class );
$repository -> setUser ( $user );
2024-03-07 17:18:46 -05:00
$billName = $this -> action -> getValue ( $journal );
2024-01-01 14:43:56 +01:00
$bill = $repository -> findByName ( $billName );
2020-08-23 08:51:58 +02:00
2025-01-03 09:05:19 +01:00
if ( null !== $bill && TransactionTypeEnum :: WITHDRAWAL -> value === $journal [ 'transaction_type_type' ]) {
2025-11-02 14:00:55 +01:00
$count = DB :: table ( 'transaction_journals' ) -> where ( 'id' , '=' , $journal [ 'transaction_journal_id' ]) -> where ( 'bill_id' , $bill -> id ) -> count ();
2022-10-02 14:37:50 +02:00
if ( 0 !== $count ) {
2025-11-02 14:00:55 +01:00
Log :: error ( sprintf ( 'RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": already set.' , $journal [ 'transaction_journal_id' ], $billName ));
// event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_subscription', ['name' => $billName])));
2023-12-20 19:35:52 +01:00
2022-10-02 14:37:50 +02:00
return false ;
}
2025-11-02 14:00:55 +01:00
DB :: table ( 'transaction_journals' ) -> where ( 'id' , '=' , $journal [ 'transaction_journal_id' ]) -> update ([ 'bill_id' => $bill -> id ]);
Log :: debug ( sprintf ( 'RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").' , $journal [ 'transaction_journal_id' ], $bill -> id , $bill -> name ));
2020-08-23 08:51:58 +02:00
2023-11-28 17:18:31 +01:00
/** @var TransactionJournal $object */
2022-10-02 20:23:11 +02:00
$object = TransactionJournal :: where ( 'user_id' , $journal [ 'user_id' ]) -> find ( $journal [ 'transaction_journal_id' ]);
event ( new TriggeredAuditLog ( $this -> action -> rule , $object , 'set_bill' , null , $bill -> name ));
2022-10-02 06:23:31 +02:00
2020-08-23 08:51:58 +02:00
return true ;
}
2025-11-02 14:00:55 +01:00
Log :: error ( sprintf ( 'RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found or not a withdrawal.' , $journal [ 'transaction_journal_id' ], $billName ));
2023-08-13 15:01:12 +02:00
event ( new RuleActionFailedOnArray ( $this -> action , $journal , trans ( 'rules.cannot_find_subscription' , [ 'name' => $billName ])));
2023-12-20 19:35:52 +01:00
2020-08-23 08:51:58 +02:00
return false ;
2020-08-23 07:42:14 +02:00
}
2018-04-07 06:20:45 +02:00
}