Implemented a proper way of generating reminders (we hope).

This commit is contained in:
James Cole 2014-11-17 22:32:55 +01:00
parent 4f4e6fac16
commit 9918410954
20 changed files with 294 additions and 209 deletions

View File

@ -27,7 +27,7 @@ class PiggybankController extends BaseController
*/
public function add(Piggybank $piggybank)
{
/** @var \FireflyIII\Database\Piggybank $acct */
/** @var \FireflyIII\Database\Piggybank $repos */
$repos = App::make('FireflyIII\Database\Piggybank');
$leftOnAccount = $repos->leftOnAccount($piggybank->account);
@ -109,9 +109,13 @@ class PiggybankController extends BaseController
/*
* Flash some data to fill the form.
*/
$prefilled = ['name' => $piggybank->name, 'account_id' => $piggybank->account_id, 'targetamount' => $piggybank->targetamount,
'targetdate' => !is_null($piggybank->targetdate) ? $piggybank->targetdate->format('Y-m-d') : null, 'reminder' => $piggybank->reminder,
'remind_me' => intval($piggybank->remind_me) == 1 ? true : false];
$prefilled = ['name' => $piggybank->name,
'account_id' => $piggybank->account_id,
'targetamount' => $piggybank->targetamount,
'targetdate' => !is_null($piggybank->targetdate) ? $piggybank->targetdate->format('Y-m-d') : null,
'reminder' => $piggybank->reminder,
'remind_me' => intval($piggybank->remind_me) == 1 || !is_null($piggybank->reminder) ? true : false
];
Session::flash('prefilled', $prefilled);
return View::make('piggybanks.edit', compact('piggybank', 'accounts', 'periods', 'prefilled'))->with('title', 'Piggybanks')->with(

View File

@ -51,7 +51,6 @@ class Form
$options['autocomplete'] = 'off';
$label = self::label($name, $options);
/*
* Make label and placeholder look nice.
*/
@ -62,7 +61,8 @@ class Form
*/
if (\Session::has('prefilled')) {
$prefilled = \Session::get('prefilled');
$value = isset($prefilled[$name]) && is_null($value) ? $prefilled[$name] : $value;
//$value = isset($prefilled[$name]) && is_null($value) ? $prefilled[$name] : $value;
}
/*
@ -132,7 +132,7 @@ class Form
break;
case 'checkbox':
$checked = $options['checked'];
unset($options['checked'], $options['placeholder'], $options['autocomplete'], $options['class']);
unset($options['placeholder'], $options['autocomplete'], $options['class']);
$html .= '<div class="checkbox"><label>';
$html .= \Form::checkbox($name, $value, $checked, $options);
$html .= '</label></div>';
@ -237,7 +237,7 @@ class Form
*/
public static function ffCheckbox($name, $value = 1, $checked = null, $options = [])
{
$options['checked'] = $checked ? true : null;
$options['checked'] = $checked === true ? true : null;
return self::ffInput('checkbox', $name, $value, $options);
}

View File

@ -69,6 +69,7 @@ class Date
case 'daily':
$currentEnd->addDay();
break;
case 'week':
case 'weekly':
$currentEnd->addWeek()->subDay();
break;
@ -105,6 +106,7 @@ class Date
case 'daily':
$date->startOfDay();
break;
case 'week':
case 'weekly':
$date->startOfWeek();
break;

View File

@ -3,6 +3,7 @@
namespace FireflyIII\Shared\Toolkit;
use Carbon\Carbon;
use Illuminate\Support\Collection;
/**
* Class Reminders
@ -39,17 +40,6 @@ class Reminders
// return $return;
}
public function updateReminders()
{
// $today = Carbon::now()->format('Y-m-d');
// $reminders = \Auth::user()->reminders()->where('startdate', '<=', $today)->where('enddate', '>=', $today)->where('active', '=', 1)->get();
//
// /*
// * Find all piggy banks in the current set of reminders.
// */
// $this->updatePiggyBankReminders();
}
/**
*
*/
@ -112,4 +102,57 @@ class Reminders
// }
}
public function updateReminders()
{
/*
* Reminder capable objects are (so far) only piggy banks.
*/
/** @var \FireflyIII\Database\Piggybank $repository */
$repository = \App::make('FireflyIII\Database\Piggybank');
/** @var \FireflyIII\Shared\Toolkit\Date $dateKit */
$dateKit = \App::make('FireflyIII\Shared\Toolkit\Date');
/** @var Collection $piggybanks */
$piggybanks = $repository->get();
$set = $piggybanks->filter(
function (\Piggybank $piggybank) {
if (!is_null($piggybank->reminder)) {
return $piggybank;
}
}
);
/** @var \Piggybank $piggybank */
foreach ($set as $piggybank) {
/*
* Try to find a reminder that is valid in the current [period]
* aka between [start of period] and [end of period] as denoted
* by the piggy's repeat_freq.
*/
/** @var \PiggybankRepetition $repetition */
$repetition = $piggybank->currentRelevantRep();
$start = $dateKit->startOfPeriod(Carbon::now(), $piggybank->reminder);
if ($repetition->targetdate && $repetition->targetdate <= Carbon::now()) {
// break when no longer relevant:
continue;
}
$end = $dateKit->endOfPeriod(Carbon::now(), $piggybank->reminder);
// should have a reminder for this period:
/** @var \Collection $reminders */
$reminders = $piggybank->reminders()->dateIs($start, $end)->get();
if ($reminders->count() == 0) {
// create new!
$reminder = new \Reminder;
$reminder->startdate = $start;
$reminder->enddate = $end;
$reminder->user()->associate($repository->getUser());
$reminder->remindersable()->associate($piggybank);
$reminder->save();
}
}
}
}

View File

@ -42,6 +42,7 @@ use LaravelBook\Ardent\Ardent as Ardent;
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminderSkip($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value)
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRemindMe($value)
* @property-read \Illuminate\Database\Eloquent\Collection|\Reminder[] $reminders
*/
class Piggybank extends Ardent
{
@ -83,7 +84,7 @@ class Piggybank extends Ardent
public function reminders()
{
return $this->morphMany('Reminder', 'remembersable');
return $this->morphMany('Reminder', 'remindersable');
}
/**

View File

@ -1,8 +1,35 @@
<?php
use Carbon\Carbon;
use LaravelBook\Ardent\Ardent;
class Reminder extends Ardent
/**
* Reminder
*
* @property integer $id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property integer $user_id
* @property \Carbon\Carbon $startdate
* @property \Carbon\Carbon $enddate
* @property boolean $active
* @property integer $remembersable_id
* @property string $remembersable_type
* @property-read \ $remindersable
* @property-read \User $user
* @property mixed $data
* @method static \Illuminate\Database\Query\Builder|\Reminder whereId($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereUpdatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereUserId($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereStartdate($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereEnddate($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereActive($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereRemembersableId($value)
* @method static \Illuminate\Database\Query\Builder|\Reminder whereRemembersableType($value)
* @method static \Reminder dateIs($start, $end)
*/
class Reminder extends Eloquent
{
protected $table = 'reminders';
@ -35,6 +62,10 @@ class Reminder extends Ardent
{
return $this->belongsTo('User');
}
public function scopeDateIs($query, Carbon $start, Carbon $end)
{
return $query->where('startdate', $start->format('Y-m-d'))->where('enddate', $end->format('Y-m-d'));
}
public function getDataAttribute($value)
{

View File

@ -48,6 +48,10 @@ use LaravelBook\Ardent\Builder;
* @method static \TransactionJournal onDate($date)
* @method static \TransactionJournal transactionTypes($types)
* @method static \TransactionJournal withRelevantData()
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Budget[] $budgets
* @property-read \Illuminate\Database\Eloquent\Collection|\
* 'Category[] $categories
*/
class TransactionJournal extends Ardent
{