Expanded reminders.

This commit is contained in:
James Cole
2015-03-07 09:21:06 +01:00
parent a60671d4c1
commit 93996ada96
13 changed files with 304 additions and 214 deletions

View File

@@ -88,117 +88,6 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $part;
}
/**
* @param PiggyBank $piggyBank
* @param Carbon $currentStart
* @param Carbon $currentEnd
*
* @return Reminder
*/
public function createReminder(PiggyBank $piggyBank, Carbon $currentStart, Carbon $currentEnd)
{
$reminder = Auth::user()->reminders()
->where('remindersable_id', $piggyBank->id)
->onDates($currentStart, $currentEnd)
->first();
if (is_null($reminder)) {
// create one:
$reminder = new Reminder;
$reminder->user()->associate(Auth::user());
$reminder->startdate = $currentStart;
$reminder->enddate = $currentEnd;
$reminder->active = true;
$reminder->notnow = false;
$reminder->remindersable()->associate($piggyBank);
$reminder->save();
return $reminder;
} else {
return $reminder;
}
}
/**
* This routine will return an array consisting of two dates which indicate the start
* and end date for each reminder that this piggy bank will have, if the piggy bank has
* any reminders. For example:
*
* [12 mar - 15 mar]
* [15 mar - 18 mar]
*
* etcetera.
*
* Array is filled with tiny arrays with Carbon objects in them.
*
* @param PiggyBank $piggyBank
*
* @return array
*/
public function getReminderRanges(PiggyBank $piggyBank)
{
$ranges = [];
$today = new Carbon;
if ($piggyBank->remind_me === false) {
return $ranges;
}
if (!is_null($piggyBank->targetdate)) {
// count back until now.
// echo 'Count back!<br>';
$start = $piggyBank->targetdate;
$end = $piggyBank->startdate;
while ($start >= $end) {
$currentEnd = clone $start;
$start = Navigation::subtractPeriod($start, $piggyBank->reminder, 1);
$currentStart = clone $start;
$ranges[] = ['start' => clone $currentStart, 'end' => clone $currentEnd];
}
} else {
$start = clone $piggyBank->startdate;
while ($start < $today) {
$currentStart = clone $start;
$start = Navigation::addPeriod($start, $piggyBank->reminder, 0);
$currentEnd = clone $start;
$ranges[] = ['start' => clone $currentStart, 'end' => clone $currentEnd];
}
}
return $ranges;
}
/**
* Takes a reminder, finds the piggy bank and tells you what to do now.
* Aka how much money to put in.
*
*
* @param Reminder $reminder
*
* @return string
*/
public function getReminderText(Reminder $reminder)
{
/** @var PiggyBank $piggyBank */
$piggyBank = $reminder->remindersable;
if (is_null($piggyBank->targetdate)) {
return 'Add money to this piggy bank to reach your target of ' . Amount::format($piggyBank->targetamount);
}
$currentRep = $piggyBank->currentRelevantRep();
$ranges = $this->getReminderRanges($piggyBank);
// calculate number of reminders:
$left = $piggyBank->targetamount - $currentRep->currentamount;
$perReminder = $left / count($ranges);
return 'Add '.Amount::format($perReminder).' to fill this piggy bank on '.$piggyBank->targetdate->format('jS F Y');
}
/**
* @param array $data
*
@@ -243,4 +132,5 @@ class PiggyBankRepository implements PiggyBankRepositoryInterface
return $piggyBank;
}
}