firefly-iii/app/models/LimitRepetition.php

57 lines
1.9 KiB
PHP
Raw Normal View History

<?php
use FireflyIII\Exception\FireflyException;
2014-12-06 05:12:55 -06:00
use Watson\Validating\ValidatingTrait;
2014-12-20 08:00:53 -06:00
use \Illuminate\Database\Eloquent\Model as Eloquent;
2014-12-13 14:59:02 -06:00
/**
* Class LimitRepetition
*/
2014-12-06 05:12:55 -06:00
class LimitRepetition extends Eloquent
{
2014-12-06 05:12:55 -06:00
use ValidatingTrait;
public static $rules
2014-11-18 02:47:50 -06:00
= [
2014-12-13 15:54:52 -06:00
'budget_limit_id' => 'required|exists:budgetlimits,id',
2014-12-13 14:59:02 -06:00
'startdate' => 'required|date',
'enddate' => 'required|date',
'amount' => 'numeric|required|min:0.01',
2014-11-18 02:47:50 -06:00
];
2014-08-10 11:22:42 -05:00
/**
2014-12-13 14:59:02 -06:00
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2014-08-10 11:22:42 -05:00
*/
2014-12-13 14:59:02 -06:00
public function budgetLimit()
{
2014-12-13 14:59:02 -06:00
return $this->belongsTo('BudgetLimit');
}
2014-12-06 05:12:55 -06:00
/**
2014-12-13 14:59:02 -06:00
* @return array
2014-12-06 05:12:55 -06:00
*/
2014-12-13 14:59:02 -06:00
public function getDates()
2014-12-06 05:12:55 -06:00
{
2014-12-13 14:59:02 -06:00
return ['created_at', 'updated_at', 'startdate', 'enddate'];
2014-12-06 05:12:55 -06:00
}
2014-11-17 13:45:55 -06:00
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
2014-11-18 02:47:50 -06:00
*
2014-11-17 13:45:55 -06:00
* @return float
*/
2014-11-12 15:37:44 -06:00
public function spentInRepetition()
{
$sum = \DB::table('transactions')
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->where('transaction_journals.date', '>=', $this->startdate->format('Y-m-d'))
->where('transaction_journals.date', '<=', $this->enddate->format('Y-m-d'))
->where('transactions.amount', '>', 0)
->where('limit_repetitions.id', '=', $this->id)
->sum('transactions.amount');
2014-11-12 15:37:44 -06:00
return floatval($sum);
}
2015-01-01 23:16:49 -06:00
}