mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 20:22:07 +00:00
Up to budgets now!
This commit is contained in:
@@ -2,10 +2,16 @@
|
||||
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Models\LimitRepetition;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Support\Facades\Navigation;
|
||||
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Class EventServiceProvider
|
||||
*
|
||||
@@ -61,6 +67,46 @@ class EventServiceProvider extends ServiceProvider
|
||||
}
|
||||
);
|
||||
|
||||
BudgetLimit::saved(function(BudgetLimit $budgetLimit) {
|
||||
|
||||
$end = Navigation::addPeriod(clone $budgetLimit->startdate, $budgetLimit->repeat_freq, 0);
|
||||
$end->subDay();
|
||||
|
||||
$set = $budgetLimit->limitrepetitions()->where('startdate', $budgetLimit->startdate->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'))->get();
|
||||
/*
|
||||
* Create new LimitRepetition:
|
||||
*/
|
||||
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) {
|
||||
\Log::error('Trying to save new LimitRepetition failed!');
|
||||
\Log::error($e->getMessage());
|
||||
}
|
||||
} else {
|
||||
if ($set->count() == 1) {
|
||||
/*
|
||||
* Update existing one.
|
||||
*/
|
||||
$repetition = $set->first();
|
||||
$repetition->amount = $budgetLimit->amount;
|
||||
$repetition->save();
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user