2016-01-08 16:00:07 +01:00
|
|
|
<?php
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2016-01-08 16:00:07 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Providers;
|
2015-02-06 04:39:52 +01:00
|
|
|
|
2015-02-14 14:25:29 +01:00
|
|
|
use FireflyIII\Models\Account;
|
2015-02-22 09:46:21 +01:00
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2016-01-09 08:20:55 +01:00
|
|
|
use FireflyIII\Models\LimitRepetition;
|
2015-02-25 19:32:33 +01:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\Models\PiggyBankRepetition;
|
2015-03-01 08:15:24 +01:00
|
|
|
use FireflyIII\Models\Transaction;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-02-06 04:39:52 +01:00
|
|
|
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
|
2015-02-22 09:46:21 +01:00
|
|
|
use Illuminate\Database\QueryException;
|
2015-02-06 04:39:52 +01:00
|
|
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
2015-03-01 08:25:12 +01:00
|
|
|
use Log;
|
2016-01-08 16:00:07 +01:00
|
|
|
use Navigation;
|
2015-04-07 18:26:14 +02:00
|
|
|
|
2016-01-09 08:20:55 +01:00
|
|
|
/**
|
|
|
|
* Class EventServiceProvider
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Providers
|
|
|
|
*/
|
2015-02-11 07:35:10 +01:00
|
|
|
class EventServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
2016-01-08 16:00:07 +01:00
|
|
|
* The event listener mappings for the application.
|
2015-02-11 07:35:10 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $listen
|
|
|
|
= [
|
2016-01-15 23:12:52 +01:00
|
|
|
'FireflyIII\Events\TransactionJournalUpdated' => [
|
2016-01-13 07:16:29 +01:00
|
|
|
'FireflyIII\Handlers\Events\ScanForBillsAfterUpdate',
|
2015-03-02 20:05:28 +01:00
|
|
|
'FireflyIII\Handlers\Events\UpdateJournalConnection',
|
2016-01-12 21:41:19 +01:00
|
|
|
'FireflyIII\Handlers\Events\FireRulesForUpdate',
|
2015-03-02 20:05:28 +01:00
|
|
|
|
2015-02-14 14:25:29 +01:00
|
|
|
],
|
2016-01-15 23:12:52 +01:00
|
|
|
'FireflyIII\Events\TransactionJournalStored' => [
|
2016-01-13 07:16:29 +01:00
|
|
|
'FireflyIII\Handlers\Events\ScanForBillsAfterStore',
|
2015-03-02 20:05:28 +01:00
|
|
|
'FireflyIII\Handlers\Events\ConnectJournalToPiggyBank',
|
2016-01-12 21:41:19 +01:00
|
|
|
'FireflyIII\Handlers\Events\FireRulesForStore',
|
2016-01-15 23:12:52 +01:00
|
|
|
],
|
2016-03-19 16:24:47 +01:00
|
|
|
'Illuminate\Auth\Events\Logout' => [
|
|
|
|
'FireflyIII\Handlers\Events\UserEventListener@onUserLogout',
|
|
|
|
],
|
2016-03-28 19:50:24 +02:00
|
|
|
'FireflyIII\Events\UserRegistration' => [
|
|
|
|
'FireflyIII\Handlers\Events\SendRegistrationMail',
|
|
|
|
'FireflyIII\Handlers\Events\AttachUserRole',
|
2016-03-29 11:55:25 +02:00
|
|
|
'FireflyIII\Handlers\Events\UserConfirmation@sendConfirmation',
|
|
|
|
],
|
|
|
|
'FireflyIII\Events\ResendConfirmation' => [
|
|
|
|
'FireflyIII\Handlers\Events\UserConfirmation@resendConfirmation',
|
2016-03-28 19:50:24 +02:00
|
|
|
],
|
2015-02-11 07:35:10 +01:00
|
|
|
];
|
2015-02-06 04:39:52 +01:00
|
|
|
|
2015-02-11 07:35:10 +01:00
|
|
|
/**
|
|
|
|
* Register any other events for your application.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot(DispatcherContract $events)
|
|
|
|
{
|
|
|
|
parent::boot($events);
|
2015-05-17 10:30:18 +02:00
|
|
|
$this->registerDeleteEvents();
|
|
|
|
$this->registerCreateEvents();
|
|
|
|
BudgetLimit::saved(
|
2015-06-06 23:09:12 +02:00
|
|
|
function (BudgetLimit $budgetLimit) {
|
2015-05-17 10:30:18 +02:00
|
|
|
$end = Navigation::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0);
|
|
|
|
$end->subDay();
|
2015-06-06 15:36:12 +02:00
|
|
|
$set = $budgetLimit->limitrepetitions()
|
2015-06-06 23:09:12 +02:00
|
|
|
->where('startdate', $budgetLimit->startdate->format('Y-m-d 00:00:00'))
|
|
|
|
->where('enddate', $end->format('Y-m-d 00:00:00'))
|
|
|
|
->get();
|
2015-05-17 10:30:18 +02:00
|
|
|
if ($set->count() == 0) {
|
|
|
|
$repetition = new LimitRepetition;
|
|
|
|
$repetition->startdate = $budgetLimit->startdate;
|
|
|
|
$repetition->enddate = $end;
|
|
|
|
$repetition->amount = $budgetLimit->amount;
|
|
|
|
$repetition->budgetLimit()->associate($budgetLimit);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$repetition->save();
|
|
|
|
} catch (QueryException $e) {
|
2016-04-06 09:27:45 +02:00
|
|
|
Log::error('Trying to save new LimitRepetition failed: ' . $e->getMessage());
|
2015-05-17 10:30:18 +02:00
|
|
|
}
|
2016-04-28 05:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($set->count() == 1) {
|
|
|
|
$repetition = $set->first();
|
|
|
|
$repetition->amount = $budgetLimit->amount;
|
|
|
|
$repetition->save();
|
2015-05-17 10:30:18 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2016-01-08 16:00:07 +01:00
|
|
|
//
|
2015-05-17 10:30:18 +02:00
|
|
|
}
|
|
|
|
|
2016-03-19 16:24:47 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function registerCreateEvents()
|
|
|
|
{
|
|
|
|
|
|
|
|
// move this routine to a filter
|
|
|
|
// in case of repeated piggy banks and/or other problems.
|
|
|
|
PiggyBank::created(
|
|
|
|
function (PiggyBank $piggyBank) {
|
|
|
|
$repetition = new PiggyBankRepetition;
|
|
|
|
$repetition->piggyBank()->associate($piggyBank);
|
|
|
|
$repetition->startdate = is_null($piggyBank->startdate) ? null : $piggyBank->startdate;
|
|
|
|
$repetition->targetdate = is_null($piggyBank->targetdate) ? null : $piggyBank->targetdate;
|
|
|
|
$repetition->currentamount = 0;
|
|
|
|
$repetition->save();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-05-17 10:30:18 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
protected function registerDeleteEvents()
|
|
|
|
{
|
2015-02-14 14:25:29 +01:00
|
|
|
TransactionJournal::deleted(
|
2015-06-06 23:09:12 +02:00
|
|
|
function (TransactionJournal $journal) {
|
2015-02-14 14:25:29 +01:00
|
|
|
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($journal->transactions()->get() as $transaction) {
|
|
|
|
$transaction->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2015-03-31 20:22:51 +02:00
|
|
|
|
2015-02-14 14:25:29 +01:00
|
|
|
Account::deleted(
|
2015-06-06 23:09:12 +02:00
|
|
|
function (Account $account) {
|
2015-02-14 14:25:29 +01:00
|
|
|
|
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($account->transactions()->get() as $transaction) {
|
|
|
|
$journal = $transaction->transactionJournal()->first();
|
|
|
|
$journal->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-05-17 10:30:18 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 04:39:52 +01:00
|
|
|
}
|