Files
firefly-iii/app/models/Bill.php

117 lines
3.4 KiB
PHP
Raw Normal View History

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