mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Alright for issue #6 a lot of things have been updated. Piggy banks can now be made "repeated" in a different form, viewed and edited; Firefly will properly trigger to make the "instances". Also the show-view is finished which doesn't do much but helps in seeing how it works. [skip ci]
This commit is contained in:
@@ -5,63 +5,66 @@ use LaravelBook\Ardent\Ardent as Ardent;
|
||||
/**
|
||||
* Piggybank
|
||||
*
|
||||
* @property integer $id
|
||||
* @property integer $id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
* @property integer $account_id
|
||||
* @property integer $account_id
|
||||
* @property string $name
|
||||
* @property float $targetamount
|
||||
* @property \Carbon\Carbon $targetdate
|
||||
* @property string $name
|
||||
* @property float $amount
|
||||
* @property float $target
|
||||
* @property integer $order
|
||||
* @property-read \Account $account
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereAmount($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTarget($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value)
|
||||
* @property float $targetamount
|
||||
* @property string $startdate
|
||||
* @property boolean $repeats
|
||||
* @property string $rep_length
|
||||
* @property integer $rep_times
|
||||
* @property string $reminder
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value)
|
||||
* @property \Carbon\Carbon $startdate
|
||||
* @property boolean $repeats
|
||||
* @property string $rep_length
|
||||
* @property integer $rep_every
|
||||
* @property integer $rep_times
|
||||
* @property string $reminder
|
||||
* @property integer $reminder_skip
|
||||
* @property integer $order
|
||||
* @property-read \Account $account
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\PiggybankRepetition[] $piggybankrepetitions
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereAccountId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereName($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetamount($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereTargetdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereStartdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepeats($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepLength($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepEvery($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereRepTimes($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminder($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereReminderSkip($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\Piggybank whereOrder($value)
|
||||
*/
|
||||
class Piggybank extends Ardent
|
||||
{
|
||||
public static $rules
|
||||
= [
|
||||
'account_id' => 'required|exists:accounts,id',
|
||||
'name' => 'required|between:1,255',
|
||||
'targetamount' => 'required|min:0',
|
||||
'targetdate' => 'date',
|
||||
'startdate' => 'date',
|
||||
'repeats' => 'required|between:0,1',
|
||||
'rep_length' => 'in:day,week,month,year',
|
||||
'rep_times' => 'required|min:0|max:100',
|
||||
'reminder' => 'in:day,week,month,year',
|
||||
'reminder_skip' => 'required|min:0|max:100',
|
||||
'order' => 'required:min:1',
|
||||
'account_id' => 'required|exists:accounts,id', // link to Account
|
||||
'name' => 'required|between:1,255', // name
|
||||
'targetamount' => 'required|min:0', // amount you want to save
|
||||
'startdate' => 'date', // when you started
|
||||
'targetdate' => 'date', // when its due
|
||||
'repeats' => 'required|between:0,1', // does it repeat?
|
||||
'rep_length' => 'in:day,week,month,year', // how long is the period?
|
||||
'rep_every' => 'required|min:1|max:100', // how often does it repeat? every 3 years.
|
||||
'rep_times' => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times
|
||||
'reminder' => 'in:day,week,month,year', // want a reminder to put money in this?
|
||||
'reminder_skip' => 'required|min:0|max:100', // every week? every 2 months?
|
||||
'order' => 'required:min:1', // not yet used.
|
||||
];
|
||||
public $fillable
|
||||
= [
|
||||
'account_id',
|
||||
'name',
|
||||
'targetamount',
|
||||
'targetdate',
|
||||
'startdate',
|
||||
'targetdate',
|
||||
'repeats',
|
||||
'rep_length',
|
||||
'rep_every',
|
||||
'rep_times',
|
||||
'reminder',
|
||||
'reminder_skip',
|
||||
@@ -78,17 +81,18 @@ class Piggybank extends Ardent
|
||||
$today = new Carbon;
|
||||
|
||||
return [
|
||||
'account_id' => 'factory|Account',
|
||||
'name' => 'string',
|
||||
'targetamount' => 'required|min:0',
|
||||
'targetdate' => $start,
|
||||
'startdate' => $today,
|
||||
'repeats' => 0,
|
||||
'rep_length' => null,
|
||||
'rep_times' => 0,
|
||||
'reminder' => null,
|
||||
'reminder_skip' => 0,
|
||||
'order' => 1,
|
||||
'account_id' => 'factory|Account',
|
||||
'name' => 'string',
|
||||
'targetamount' => 'required|min:0',
|
||||
'startdate' => $today,
|
||||
'targetdate' => $start,
|
||||
'repeats' => 0,
|
||||
'rep_length' => null,
|
||||
'rep_times' => 0,
|
||||
'rep_every' => 0,
|
||||
'reminder' => null,
|
||||
'reminder_skip' => 0,
|
||||
'order' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -105,9 +109,95 @@ class Piggybank extends Ardent
|
||||
*/
|
||||
public function getDates()
|
||||
{
|
||||
return ['created_at', 'updated_at', 'targetdate'];
|
||||
return ['created_at', 'updated_at', 'targetdate','startdate'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Firefly shouldn't create piggybank repetions that completely
|
||||
* lie in the future, so we should be able to safely grab the "latest"
|
||||
* one and use that to calculate when the user will be reminded.
|
||||
*/
|
||||
public function nextReminderDate()
|
||||
{
|
||||
if(is_null($this->reminder)) {
|
||||
return null;
|
||||
}
|
||||
/** @var \PiggybankRepetition $rep */
|
||||
$rep = $this->currentRelevantRep();
|
||||
if($rep) {
|
||||
$today = new Carbon;
|
||||
if(is_null($rep->startdate)) {
|
||||
switch($this->reminder) {
|
||||
case 'day':
|
||||
return $today;
|
||||
break;
|
||||
case 'week':
|
||||
return $today->endOfWeek();
|
||||
break;
|
||||
case 'month':
|
||||
return $today->endOfMonth();
|
||||
break;
|
||||
case 'year':
|
||||
return $today->endOfYear();
|
||||
break;
|
||||
|
||||
}
|
||||
} else {
|
||||
// start with the start date
|
||||
// when its bigger than today, return it:
|
||||
$start = clone $rep->startdate;
|
||||
while($start <= $today) {
|
||||
switch($this->reminder) {
|
||||
case 'day':
|
||||
$start->addDay();
|
||||
break;
|
||||
case 'week':
|
||||
$start->addWeek();
|
||||
break;
|
||||
case 'month':
|
||||
$start->addMonth();
|
||||
break;
|
||||
case 'year':
|
||||
$start->addYear();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
return $start;
|
||||
}
|
||||
// if start date is null, simple switch on
|
||||
// the reminder period (if any) and go to the end of the period.
|
||||
|
||||
// otherwise, keep jumping until we are past today.
|
||||
}
|
||||
|
||||
|
||||
return new Carbon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Grabs the PiggyBankRepetition that's currently relevant / active
|
||||
*/
|
||||
public function currentRelevantRep() {
|
||||
return $this->piggybankrepetitions()
|
||||
->where(function ($q) {
|
||||
$today = new Carbon;
|
||||
$q->whereNull('startdate');
|
||||
$q->orWhere('startdate','<=',$today->format('Y-m-d'));
|
||||
})
|
||||
->where(function ($q) {
|
||||
$today = new Carbon;
|
||||
$q->whereNull('targetdate');
|
||||
$q->orWhere('targetdate','>=',$today->format('Y-m-d'));
|
||||
})
|
||||
->first();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
*/
|
||||
public function piggybankrepetitions()
|
||||
{
|
||||
return $this->hasMany('PiggybankRepetition');
|
||||
|
@@ -14,13 +14,13 @@ use LaravelBook\Ardent\Ardent as Ardent;
|
||||
* @property \Carbon\Carbon $startdate
|
||||
* @property float $currentamount
|
||||
* @property-read \Piggybank $piggybank
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition wherePiggybankId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereTargetdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereStartdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCurrentamount($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCreatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereUpdatedAt($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition wherePiggybankId($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereTargetdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereStartdate($value)
|
||||
* @method static \Illuminate\Database\Query\Builder|\PiggybankRepetition whereCurrentamount($value)
|
||||
*/
|
||||
class PiggybankRepetition extends Ardent
|
||||
{
|
||||
@@ -63,4 +63,5 @@ class PiggybankRepetition extends Ardent
|
||||
return $this->belongsTo('Piggybank');
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user