2016-01-08 09:00:07 -06:00
< ? php
2016-05-20 05:41:23 -05:00
/**
* EventServiceProvider . php
2020-02-16 06:55:52 -06:00
* Copyright ( c ) 2019 james @ firefly - iii . org
2016-05-20 05:41:23 -05:00
*
2019-10-01 23:37:26 -05:00
* This file is part of Firefly III ( https :// github . com / firefly - iii ) .
2016-10-04 23:52:15 -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-05-20 05:41:23 -05:00
*/
2017-04-09 00:44:22 -05:00
declare ( strict_types = 1 );
2016-01-08 09:00:07 -06:00
namespace FireflyIII\Providers ;
2015-02-05 21:39:52 -06:00
2018-05-26 06:55:11 -05:00
use Exception ;
2021-10-12 22:57:11 -05:00
use FireflyIII\Events\ActuallyLoggedIn ;
2018-04-02 08:10:40 -05:00
use FireflyIII\Events\AdminRequestedTestMessage ;
2020-12-01 23:54:13 -06:00
use FireflyIII\Events\DestroyedTransactionGroup ;
2020-08-28 14:58:03 -05:00
use FireflyIII\Events\DetectedNewIPAddress ;
2017-12-05 13:50:04 -06:00
use FireflyIII\Events\RegisteredUser ;
2018-04-02 08:10:40 -05:00
use FireflyIII\Events\RequestedNewPassword ;
2018-06-25 09:01:45 -05:00
use FireflyIII\Events\RequestedReportOnJournals ;
2020-12-02 12:30:09 -06:00
use FireflyIII\Events\RequestedSendWebhookMessages ;
2017-12-28 12:03:15 -06:00
use FireflyIII\Events\RequestedVersionCheckStatus ;
2021-04-10 10:25:48 -05:00
use FireflyIII\Events\StoredAccount ;
2019-03-30 05:03:39 -05:00
use FireflyIII\Events\StoredTransactionGroup ;
2021-04-10 10:25:48 -05:00
use FireflyIII\Events\UpdatedAccount ;
2019-03-30 05:03:39 -05:00
use FireflyIII\Events\UpdatedTransactionGroup ;
2018-04-02 08:10:40 -05:00
use FireflyIII\Events\UserChangedEmail ;
2022-03-28 05:23:46 -05:00
use FireflyIII\Events\WarnUserAboutBill ;
2018-05-26 06:55:11 -05:00
use FireflyIII\Mail\OAuthTokenCreatedMail ;
2022-09-18 09:28:04 -05:00
use FireflyIII\Models\BudgetLimit ;
2015-02-25 12:32:33 -06:00
use FireflyIII\Models\PiggyBank ;
use FireflyIII\Models\PiggyBankRepetition ;
2022-09-22 23:05:22 -05:00
use FireflyIII\Notifications\Admin\TestNotification ;
2022-09-18 09:28:04 -05:00
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface ;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface ;
2018-05-26 06:55:11 -05:00
use FireflyIII\Repositories\User\UserRepositoryInterface ;
2017-12-05 13:50:04 -06:00
use Illuminate\Auth\Events\Login ;
2015-02-05 21:39:52 -06:00
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider ;
2022-09-22 23:05:22 -05:00
use Illuminate\Support\Facades\Notification ;
2018-05-26 06:55:11 -05:00
use Laravel\Passport\Client ;
use Laravel\Passport\Events\AccessTokenCreated ;
use Log ;
use Mail ;
use Session ;
2022-09-07 23:22:02 -05:00
use TypeError ;
2015-04-07 11:26:14 -05:00
2016-01-09 01:20:55 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class EventServiceProvider .
2021-03-21 03:15:40 -05:00
*
2019-07-31 09:53:09 -05:00
* @ codeCoverageIgnore
2016-01-09 01:20:55 -06:00
*/
2015-02-11 00:35:10 -06:00
class EventServiceProvider extends ServiceProvider
{
/**
2016-01-08 09:00:07 -06:00
* The event listener mappings for the application .
2015-02-11 00:35:10 -06:00
*
* @ var array
*/
protected $listen
= [
2017-09-26 01:52:16 -05:00
// is a User related event.
2021-03-21 03:15:40 -05:00
RegisteredUser :: class => [
2017-11-15 05:25:49 -06:00
'FireflyIII\Handlers\Events\UserEventHandler@sendRegistrationMail' ,
'FireflyIII\Handlers\Events\UserEventHandler@attachUserRole' ,
2021-08-28 08:47:33 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@createGroupMembership' ,
2022-06-06 07:40:19 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@createExchangeRates' ,
2017-11-15 05:25:49 -06:00
],
2017-12-05 13:50:04 -06:00
// is a User related event.
2021-03-21 03:15:40 -05:00
Login :: class => [
2017-12-05 13:50:04 -06:00
'FireflyIII\Handlers\Events\UserEventHandler@checkSingleUserIsAdmin' ,
2018-06-12 14:41:58 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@demoUserBackToEnglish' ,
2021-10-12 22:57:11 -05:00
],
2022-03-29 07:59:58 -05:00
ActuallyLoggedIn :: class => [
2020-08-28 14:58:03 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@storeUserIPAddress' ,
],
2021-03-21 03:15:40 -05:00
DetectedNewIPAddress :: class => [
2020-08-28 14:58:03 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@notifyNewIPAddress' ,
2017-12-28 12:03:15 -06:00
],
2021-03-21 03:15:40 -05:00
RequestedVersionCheckStatus :: class => [
2017-12-28 04:38:40 -06:00
'FireflyIII\Handlers\Events\VersionCheckEventHandler@checkForUpdates' ,
2017-12-05 13:50:04 -06:00
],
2021-03-21 03:15:40 -05:00
RequestedReportOnJournals :: class => [
2018-06-25 09:01:45 -05:00
'FireflyIII\Handlers\Events\AutomationHandler@reportJournals' ,
],
2017-12-05 13:50:04 -06:00
2017-09-26 01:52:16 -05:00
// is a User related event.
2021-03-21 03:15:40 -05:00
RequestedNewPassword :: class => [
2017-09-26 01:52:16 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@sendNewPassword' ,
2016-11-22 14:21:11 -06:00
],
2017-09-26 01:52:16 -05:00
// is a User related event.
2021-03-21 03:15:40 -05:00
UserChangedEmail :: class => [
2017-09-26 01:52:16 -05:00
'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeConfirmMail' ,
'FireflyIII\Handlers\Events\UserEventHandler@sendEmailChangeUndoMail' ,
],
2017-09-27 08:45:55 -05:00
// admin related
2021-03-21 03:15:40 -05:00
AdminRequestedTestMessage :: class => [
2017-09-27 08:45:55 -05:00
'FireflyIII\Handlers\Events\AdminEventHandler@sendTestMessage' ,
],
2017-09-26 01:52:16 -05:00
// is a Transaction Journal related event.
2021-03-21 03:15:40 -05:00
StoredTransactionGroup :: class => [
2019-03-30 05:03:39 -05:00
'FireflyIII\Handlers\Events\StoredGroupEventHandler@processRules' ,
2021-04-10 00:58:13 -05:00
'FireflyIII\Handlers\Events\StoredGroupEventHandler@recalculateCredit' ,
2020-12-01 23:54:13 -06:00
'FireflyIII\Handlers\Events\StoredGroupEventHandler@triggerWebhooks' ,
2017-11-15 05:25:49 -06:00
],
2017-09-26 01:52:16 -05:00
// is a Transaction Journal related event.
2021-03-21 03:15:40 -05:00
UpdatedTransactionGroup :: class => [
2020-06-30 22:51:15 -05:00
'FireflyIII\Handlers\Events\UpdatedGroupEventHandler@unifyAccounts' ,
2019-03-30 05:03:39 -05:00
'FireflyIII\Handlers\Events\UpdatedGroupEventHandler@processRules' ,
2021-04-10 00:58:13 -05:00
'FireflyIII\Handlers\Events\UpdatedGroupEventHandler@recalculateCredit' ,
2020-12-01 23:54:13 -06:00
'FireflyIII\Handlers\Events\UpdatedGroupEventHandler@triggerWebhooks' ,
],
2021-03-21 03:15:40 -05:00
DestroyedTransactionGroup :: class => [
2020-12-01 23:54:13 -06:00
'FireflyIII\Handlers\Events\DestroyedGroupEventHandler@triggerWebhooks' ,
2017-11-15 05:25:49 -06:00
],
2018-05-26 06:55:11 -05:00
// API related events:
2021-03-21 03:15:40 -05:00
AccessTokenCreated :: class => [
2018-05-26 06:55:11 -05:00
'FireflyIII\Handlers\Events\APIEventHandler@accessTokenCreated' ,
],
2020-12-02 12:30:09 -06:00
// Webhook related event:
RequestedSendWebhookMessages :: class => [
'FireflyIII\Handlers\Events\WebhookEventHandler@sendWebhookMessages' ,
],
2021-04-10 10:25:48 -05:00
// account related events:
2021-09-18 03:26:12 -05:00
StoredAccount :: class => [
2021-04-10 10:25:48 -05:00
'FireflyIII\Handlers\Events\StoredAccountEventHandler@recalculateCredit' ,
2021-09-18 03:26:12 -05:00
],
UpdatedAccount :: class => [
2021-04-10 10:25:48 -05:00
'FireflyIII\Handlers\Events\UpdatedAccountEventHandler@recalculateCredit' ,
],
2022-03-28 05:23:46 -05:00
// bill related events:
2022-03-29 07:59:58 -05:00
WarnUserAboutBill :: class => [
2022-03-28 05:23:46 -05:00
'FireflyIII\Handlers\Events\BillEventHandler@warnAboutBill' ,
],
2015-02-11 00:35:10 -06:00
];
2015-02-05 21:39:52 -06:00
2015-02-11 00:35:10 -06:00
/**
2016-09-16 00:05:34 -05:00
* Register any events for your application .
2015-02-11 00:35:10 -06:00
*/
2018-07-25 23:10:17 -05:00
public function boot () : void
2015-02-11 00:35:10 -06:00
{
2016-09-16 00:05:34 -05:00
parent :: boot ();
2015-05-17 03:30:18 -05:00
$this -> registerCreateEvents ();
2022-09-18 09:28:04 -05:00
$this -> registerBudgetEvents ();
}
/**
2022-09-24 00:03:03 -05:00
* TODO needs a dedicated method .
2022-09-18 09:28:04 -05:00
*/
protected function registerBudgetEvents () : void
{
$func = static function ( BudgetLimit $limit ) {
Log :: debug ( 'Trigger budget limit event.' );
// find available budget with same period and same currency or create it.
// then set it or add money:
$user = $limit -> budget -> user ;
$availableBudget = $user
-> availableBudgets ()
-> where ( 'start_date' , $limit -> start_date -> format ( 'Y-m-d' ))
-> where ( 'end_date' , $limit -> end_date -> format ( 'Y-m-d' ))
-> where ( 'transaction_currency_id' , $limit -> transaction_currency_id )
-> first ();
// update!
if ( null !== $availableBudget ) {
$repository = app ( BudgetLimitRepositoryInterface :: class );
$repository -> setUser ( $user );
$set = $repository -> getAllBudgetLimitsByCurrency ( $limit -> transactionCurrency , $limit -> start_date , $limit -> end_date );
$sum = ( string ) $set -> sum ( 'amount' );
Log :: debug ( sprintf ( 'Because budget limit #%d had its amount changed to %s, available budget limit #%d will be updated.' , $limit -> id , $limit -> amount , $availableBudget -> id ));
$availableBudget -> amount = $sum ;
$availableBudget -> save ();
return ;
}
Log :: debug ( 'Does not exist, create it.' );
// create it.
$data = [
'amount' => $limit -> amount ,
'start' => $limit -> start_date ,
'end' => $limit -> end_date ,
'currency_id' => $limit -> transaction_currency_id ,
];
$repository = app ( AvailableBudgetRepositoryInterface :: class );
$repository -> setUser ( $user );
$repository -> store ( $data );
};
BudgetLimit :: created ( $func );
BudgetLimit :: updated ( $func );
2015-05-17 03:30:18 -05:00
}
2016-03-19 10:24:47 -05:00
/**
2022-09-24 00:03:03 -05:00
* TODO needs a dedicated ( static ) method .
2016-03-19 10:24:47 -05:00
*/
2018-05-26 06:55:11 -05:00
protected function registerCreateEvents () : void
2016-03-19 10:24:47 -05:00
{
PiggyBank :: created (
2019-07-31 09:53:09 -05:00
static function ( PiggyBank $piggyBank ) {
2016-03-19 10:24:47 -05:00
$repetition = new PiggyBankRepetition ;
$repetition -> piggyBank () -> associate ( $piggyBank );
2018-03-25 06:30:55 -05:00
$repetition -> startdate = $piggyBank -> startdate ;
$repetition -> targetdate = $piggyBank -> targetdate ;
2016-03-19 10:24:47 -05:00
$repetition -> currentamount = 0 ;
$repetition -> save ();
}
);
2015-05-17 03:30:18 -05:00
}
2018-05-26 06:55:11 -05:00
2015-02-05 21:39:52 -06:00
}