firefly-iii/app/Models/PiggyBank.php

112 lines
3.2 KiB
PHP
Raw Normal View History

2015-02-05 21:52:16 -06:00
<?php namespace FireflyIII\Models;
2015-02-24 14:10:25 -06:00
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
2015-02-05 21:52:16 -06:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
2015-02-05 21:52:16 -06:00
2015-02-11 00:35:10 -06:00
/**
* Class PiggyBank
*
* @package FireflyIII\Models
*/
2015-02-05 22:04:06 -06:00
class PiggyBank extends Model
{
use SoftDeletes;
2015-02-05 21:52:16 -06:00
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2015-02-05 22:14:27 -06:00
public function account()
{
2015-02-05 22:35:00 -06:00
return $this->belongsTo('FireflyIII\Models\Account');
2015-02-05 22:14:27 -06:00
}
2015-02-05 21:52:16 -06:00
2015-02-11 00:35:10 -06:00
/**
* @return array
*/
2015-02-07 06:15:40 -06:00
public function getDates()
{
return ['created_at', 'updated_at', 'deleted_at', 'startdate', 'targetdate'];
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-02-05 22:14:27 -06:00
public function piggyBankEvents()
{
2015-02-05 22:35:00 -06:00
return $this->hasMany('FireflyIII\Models\PiggyBankEvent');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2015-02-05 22:14:27 -06:00
public function piggyBankRepetitions()
{
2015-02-05 22:35:00 -06:00
return $this->hasMany('FireflyIII\Models\PiggyBankRepetition');
2015-02-05 22:14:27 -06:00
}
2015-02-11 00:35:10 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
2015-02-05 22:14:27 -06:00
public function reminders()
{
2015-02-05 22:35:00 -06:00
return $this->morphMany('FireflyIII\Models\Reminder', 'remindersable');
2015-02-05 22:14:27 -06:00
}
2015-02-24 14:10:25 -06:00
/**
* Grabs the PiggyBankRepetition that's currently relevant / active
*
* @returns PiggyBankRepetition
*/
public function currentRelevantRep()
{
if ($this->currentRep) {
return $this->currentRep;
}
if ($this->repeats == 0) {
$rep = $this->piggyBankRepetitions()->first(['piggy_bank_repetitions.*']);
$this->currentRep = $rep;
return $rep;
} else {
$query = $this->piggyBankRepetitions()->where(
function (EloquentBuilder $q) {
$q->where(
function (EloquentBuilder $q) {
$q->where(
function (EloquentBuilder $q) {
$today = new Carbon;
$q->whereNull('startdate');
$q->orWhere('startdate', '<=', $today->format('Y-m-d 00:00:00'));
}
)->where(
function (EloquentBuilder $q) {
$today = new Carbon;
$q->whereNull('targetdate');
$q->orWhere('targetdate', '>=', $today->format('Y-m-d 00:00:00'));
}
);
}
)->orWhere(
function (EloquentBuilder $q) {
$today = new Carbon;
$q->where('startdate', '>=', $today->format('Y-m-d 00:00:00'));
$q->where('targetdate', '>=', $today->format('Y-m-d 00:00:00'));
}
);
}
)->orderBy('startdate', 'ASC');
$result = $query->first(['piggy_bank_repetitions.*']);
$this->currentRep = $result;
return $result;
}
}
2015-02-05 21:52:16 -06:00
}