firefly-iii/app/models/Bill.php

117 lines
3.4 KiB
PHP
Raw Normal View History

<?php
2014-08-09 06:21:24 -05:00
use Carbon\Carbon;
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
/**
2014-12-29 13:28:17 -06:00
* Class Bill
2014-12-13 14:59:02 -06:00
*/
2014-12-29 13:28:17 -06:00
class Bill extends Eloquent
{
2014-12-06 05:12:55 -06:00
use ValidatingTrait;
2014-12-20 08:00:53 -06:00
protected $rules
2014-11-18 02:47:50 -06:00
= [
'user_id' => 'required|exists:users,id',
2014-12-20 08:00:53 -06:00
'name' => 'required|between:1,255|min:1',
2014-11-18 02:47:50 -06:00
'match' => 'required',
'amount_max' => 'required|between:0,65536',
'amount_min' => 'required|between:0,65536',
'date' => 'required|date',
2014-12-20 08:00:53 -06:00
'active' => 'between:0,1',
'automatch' => 'between:0,1',
2014-11-18 02:47:50 -06:00
'repeat_freq' => 'required|in:daily,weekly,monthly,quarterly,half-year,yearly',
'skip' => 'required|between:0,31',];
2014-11-12 15:37:44 -06:00
protected $fillable = ['user_id', 'name', 'match', 'amount_min', 'amount_max', 'date', 'repeat_freq', 'skip', 'active', 'automatch'];
2014-08-10 11:22:42 -05:00
/**
* @return array
*/
public function getDates()
{
return ['created_at', 'updated_at', 'date'];
}
2014-11-18 02:47:50 -06:00
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
2014-12-06 05:12:55 -06:00
*
2014-11-18 02:47:50 -06:00
* @return null
*/
2014-11-17 00:33:18 -06:00
public function lastFoundMatch()
{
$last = $this->transactionjournals()->orderBy('date', 'DESC')->first();
if ($last) {
2014-11-15 00:46:01 -06:00
return $last->date;
}
2014-11-17 00:33:18 -06:00
2014-11-15 00:46:01 -06:00
return null;
}
2014-08-28 00:53:54 -05:00
2014-11-17 00:33:18 -06:00
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function transactionjournals()
{
return $this->hasMany('TransactionJournal');
}
2014-11-21 04:12:22 -06:00
2014-08-10 11:22:42 -05:00
/**
2014-11-17 13:45:55 -06:00
* TODO remove this method in favour of something in the FireflyIII libraries.
*
2014-12-29 13:28:17 -06:00
* Find the next expected match based on the set journals and the date stuff from the bill.
2014-08-10 11:22:42 -05:00
*/
2014-11-15 00:46:01 -06:00
public function nextExpectedMatch()
{
2014-11-15 00:46:01 -06:00
/*
* The date Firefly tries to find. If this stays null, it's "unknown".
*/
$finalDate = null;
2014-12-15 11:16:48 -06:00
if ($this->active == 0) {
return $finalDate;
}
2014-11-15 00:46:01 -06:00
/*
* $today is the start of the next period, to make sure FF3 won't miss anything
* when the current period has a transaction journal.
*/
2014-11-21 04:12:22 -06:00
$today = DateKit::addPeriod(new Carbon, $this->repeat_freq, 0);
2014-11-15 00:46:01 -06:00
/*
2014-12-29 13:28:17 -06:00
* FF3 loops from the $start of the bill, and to make sure
2014-11-15 00:46:01 -06:00
* $skip works, it adds one (for modulo).
*/
$skip = $this->skip + 1;
2014-11-21 04:12:22 -06:00
$start = DateKit::startOfPeriod(new Carbon, $this->repeat_freq);
2014-11-15 00:46:01 -06:00
/*
* go back exactly one month/week/etc because FF3 does not care about 'next'
2014-12-29 13:28:17 -06:00
* bills if they're too far into the past.
2014-11-15 00:46:01 -06:00
*/
$counter = 0;
while ($start <= $today) {
if (($counter % $skip) == 0) {
// do something.
2014-11-21 04:12:22 -06:00
$end = DateKit::endOfPeriod(clone $start, $this->repeat_freq);
2014-11-15 00:46:01 -06:00
$journalCount = $this->transactionjournals()->before($end)->after($start)->count();
if ($journalCount == 0) {
$finalDate = clone $start;
break;
}
}
2014-11-15 00:46:01 -06:00
// add period for next round!
2014-11-21 04:12:22 -06:00
$start = DateKit::addPeriod($start, $this->repeat_freq, 0);
2014-11-15 00:46:01 -06:00
$counter++;
}
2014-11-17 00:33:18 -06:00
return $finalDate;
2014-11-13 09:13:32 -06:00
}
2014-08-10 11:22:42 -05:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('User');
}
}