mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Some comment cleanup in the libraries.
This commit is contained in:
@@ -14,7 +14,9 @@ use Illuminate\Events\Dispatcher;
|
||||
class EloquentPiggybankTrigger
|
||||
{
|
||||
/**
|
||||
*
|
||||
* This method checks every repeating piggy bank the user has (these are called repeated expenses) and makes
|
||||
* sure each repeated expense has a "repetition" for the current time period. For example, if the user has
|
||||
* a weekly repeated expense of E 40,- this method will fire every week and create a new repetition.
|
||||
*/
|
||||
public function checkRepeatingPiggies()
|
||||
{
|
||||
@@ -25,26 +27,29 @@ class EloquentPiggybankTrigger
|
||||
$piggies = [];
|
||||
}
|
||||
|
||||
\Log::debug('Now in checkRepeatingPiggies with ' . count($piggies) . ' piggies');
|
||||
|
||||
\Log::debug('Now in checkRepeatingPiggies with ' . count($piggies) . ' piggies found.');
|
||||
|
||||
/** @var \Piggybank $piggyBank */
|
||||
foreach ($piggies as $piggyBank) {
|
||||
\Log::debug('Now working on ' . $piggyBank->name);
|
||||
|
||||
// get the latest repetition, see if we need to "append" more:
|
||||
/*
|
||||
* Get the latest repetition, see if Firefly needs to create more.
|
||||
*/
|
||||
/** @var \PiggybankRepetition $primer */
|
||||
$primer = $piggyBank->piggybankrepetitions()->orderBy('targetdate', 'DESC')->first();
|
||||
\Log::debug('Last target date is: ' . $primer->targetdate);
|
||||
|
||||
// for repeating piggy banks, the target date is mandatory:
|
||||
|
||||
$today = new Carbon;
|
||||
$end = clone $primer->targetdate;
|
||||
|
||||
// the next repetition must be created starting at the day after the target date:
|
||||
// the next repetition must be created starting at the day after the target date of the previous one.
|
||||
/*
|
||||
* A repeated expense runs from day 1 to day X. Since it repeats, the next repetition starts at day X+1
|
||||
* until however often the repeated expense is set to repeat: a month, a week, a year.
|
||||
*/
|
||||
$start = clone $primer->targetdate;
|
||||
$start->addDay();
|
||||
|
||||
while ($start <= $today) {
|
||||
\Log::debug('Looping! Start is: ' . $start);
|
||||
|
||||
@@ -104,15 +109,26 @@ class EloquentPiggybankTrigger
|
||||
|
||||
/**
|
||||
* Whenever a repetition is made, the decision is there to make reminders for it. Or not.
|
||||
*
|
||||
* Some combinations are "invalid" or impossible and will never trigger reminders. Others do.
|
||||
*
|
||||
* @param \PiggybankRepetition $rep
|
||||
* The numbers below refer to a small list I made in a text-file (it no longer exists) which contained the eight
|
||||
* binary combinations that can be made of three properties each piggy bank has (among others):
|
||||
*
|
||||
* - Whether or not it has a start date.
|
||||
* - Whether or not it has an end date.
|
||||
* - Whether or not the piggy bank repeats itself.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*
|
||||
* @param \PiggybankRepetition $repetition
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
public function createdRepetition(\PiggybankRepetition $repetition)
|
||||
{
|
||||
\Log::debug('TRIGGER on createdRepetition() for repetition #' . $repetition->id);
|
||||
|
||||
$piggyBank = $repetition->piggybank;
|
||||
|
||||
@@ -120,49 +136,70 @@ class EloquentPiggybankTrigger
|
||||
|
||||
// no reminders needed (duh)
|
||||
if (is_null(($piggyBank->reminder))) {
|
||||
\Log::debug('No reminders because no reminder needed.');
|
||||
return null;
|
||||
}
|
||||
|
||||
// no start, no target, no repeat (#1):
|
||||
if (is_null($piggyBank->startdate) && is_null($piggyBank->targetdate) && $piggyBank->repeats == 0) {
|
||||
\Log::debug('No reminders because no start, no target, no repeat (#1)');
|
||||
return null;
|
||||
}
|
||||
|
||||
// no start, but repeats (#5):
|
||||
if (is_null($piggyBank->startdate) && $piggyBank->repeats == 1) {
|
||||
\Log::debug('No reminders because no start, but repeats (#5)');
|
||||
return null;
|
||||
}
|
||||
|
||||
// no start, no end, but repeats (#6)
|
||||
if (is_null($piggyBank->startdate) && is_null($piggyBank->targetdate) && $piggyBank->repeats == 1) {
|
||||
\Log::debug('No reminders because no start, no end, but repeats (#6)');
|
||||
return null;
|
||||
}
|
||||
|
||||
// no end, but repeats (#7)
|
||||
if (is_null($piggyBank->targetdate) && $piggyBank->repeats == 1) {
|
||||
\Log::debug('No reminders because no end, but repeats (#7)');
|
||||
return null;
|
||||
}
|
||||
|
||||
// #2, #3, #4 and #8 are valid combo's.
|
||||
\Log::debug('Will continue...');
|
||||
/*
|
||||
* #2, #3, #4 and #8 are valid combo's.
|
||||
*
|
||||
* We add two years to the end when the repetition has no target date; we "pretend" there is a target date.
|
||||
*
|
||||
*/
|
||||
if (is_null($repetition->targetdate)) {
|
||||
$end = new Carbon;
|
||||
$end->addYears(2);
|
||||
} else {
|
||||
$end = $repetition->targetdate;
|
||||
}
|
||||
/*
|
||||
* If there is no start date, the start dat becomes right now.
|
||||
*/
|
||||
if (is_null($repetition->startdate)) {
|
||||
$start = new Carbon;
|
||||
} else {
|
||||
$start = $repetition->startdate;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Firefly checks every period X between $start and $end and if necessary creates a reminder. Firefly
|
||||
* only creates reminders if the $current date is after today. Piggy banks may have their start in the past.
|
||||
*
|
||||
* This loop will jump a month when the reminder is set monthly, a week when it's set weekly, etcetera.
|
||||
*/
|
||||
$current = $start;
|
||||
$today = new Carbon;
|
||||
$today = new Carbon;
|
||||
$today->startOfDay();
|
||||
while ($current <= $end) {
|
||||
|
||||
// when do we start reminding?
|
||||
// X days before $current:
|
||||
\Log::debug('Looping reminder dates; now at ' . $current);
|
||||
/*
|
||||
* Piggy bank reminders start X days before the actual date of the event.
|
||||
*/
|
||||
$reminderStart = clone $current;
|
||||
switch ($piggyBank->reminder) {
|
||||
case 'day':
|
||||
@@ -179,20 +216,32 @@ class EloquentPiggybankTrigger
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the date is past today we create a reminder, otherwise we don't. The end date is the date
|
||||
* the reminder is due; after that it is invalid.
|
||||
*/
|
||||
if ($current >= $today) {
|
||||
$reminder = new \PiggybankReminder;
|
||||
$reminder->piggybank()->associate($piggyBank);
|
||||
$reminder->user()->associate(\Auth::user());
|
||||
$reminder->startdate = $reminderStart;
|
||||
$reminder->enddate = $current;
|
||||
$reminder->enddate = $current;
|
||||
$reminder->active = 1;
|
||||
\Log::debug('Will create a reminder. Is it valid?');
|
||||
\Log::debug($reminder->validate());
|
||||
try {
|
||||
$reminder->save();
|
||||
|
||||
$reminder->save();
|
||||
} catch (QueryException $e) {
|
||||
\Log::error('Could not save reminder: ' . $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
\Log::debug('Current is before today, will not make a reminder.');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Here Firefly jumps ahead to the next reminder period.
|
||||
*/
|
||||
switch ($piggyBank->reminder) {
|
||||
case 'day':
|
||||
$current->addDays($piggyBank->reminder_skip);
|
||||
@@ -234,12 +283,12 @@ class EloquentPiggybankTrigger
|
||||
*/
|
||||
public function modifyAmountAdd(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
$rep = $piggyBank->currentRelevantRep();
|
||||
$rep = $piggyBank->currentRelevantRep();
|
||||
$today = new Carbon;
|
||||
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
|
||||
@@ -259,8 +308,8 @@ class EloquentPiggybankTrigger
|
||||
public function modifyAmountRemove(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
$event->save();
|
||||
@@ -359,7 +408,7 @@ class EloquentPiggybankTrigger
|
||||
if (!is_null($rep->targetdate)) {
|
||||
$eventSumQuery->where('date', '<=', $rep->targetdate->format('Y-m-d'));
|
||||
}
|
||||
$eventSum = floatval($eventSumQuery->sum('amount'));
|
||||
$eventSum = floatval($eventSumQuery->sum('amount'));
|
||||
$rep->currentamount = floatval($sum) + $eventSum;
|
||||
$rep->save();
|
||||
|
||||
|
Reference in New Issue
Block a user