Improve bill things for issue #341

This commit is contained in:
James Cole 2016-10-20 21:40:45 +02:00
parent d92768ecbf
commit f83aaf77f1
6 changed files with 126 additions and 59 deletions

View File

@ -137,7 +137,7 @@ class BillController extends Controller
$bills = $repository->getBills(); $bills = $repository->getBills();
$bills->each( $bills->each(
function (Bill $bill) use ($repository, $start, $end) { function (Bill $bill) use ($repository, $start, $end) {
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill); $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, new Carbon);
$bill->lastFoundMatch = $repository->lastFoundMatch($bill); $bill->lastFoundMatch = $repository->lastFoundMatch($bill);
$journals = $repository->getJournalsInRange($bill, $start, $end); $journals = $repository->getJournalsInRange($bill, $start, $end);
// loop journals, find average: // loop journals, find average:
@ -204,7 +204,7 @@ class BillController extends Controller
$yearAverage = $repository->getYearAverage($bill, $date); $yearAverage = $repository->getYearAverage($bill, $date);
$overallAverage = $repository->getOverallAverage($bill); $overallAverage = $repository->getOverallAverage($bill);
$journals->setPath('/bills/show/' . $bill->id); $journals->setPath('/bills/show/' . $bill->id);
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill); $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, new Carbon);
$hideBill = true; $hideBill = true;
$subTitle = e($bill->name); $subTitle = e($bill->name);

View File

@ -18,10 +18,12 @@ use DB;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
use FireflyIII\User; use FireflyIII\User;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log;
use Navigation; use Navigation;
/** /**
@ -461,52 +463,44 @@ class BillRepository implements BillRepositoryInterface
} }
/** /**
* @param Bill $bill * @param Bill $bill
* @param Carbon $date
* *
* @return \Carbon\Carbon * @return Carbon
*/ */
public function nextExpectedMatch(Bill $bill): Carbon public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon
{ {
$cache = new CacheProperties;
$finalDate = Carbon::now(); $cache->addProperty($bill->id);
$finalDate->year = 1900; $cache->addProperty('nextExpectedMatch');
if ($bill->active == 0) { if ($cache->has()) {
return $finalDate; return $cache->get();
} }
// find the most recent date for this bill NOT in the future. Cache this date:
$start = clone $bill->date;
$now = new Carbon;
Log::debug('Start is ' . $start->format('Y-m-d'));
/* do {
* $today is the start of the next period, to make sure FF3 won't miss anything $start = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
* when the current period has a transaction journal. Log::debug('Start is now ' . $start->format('Y-m-d'));
*/ } while ($start <= $now);
/** @var \Carbon\Carbon $obj */
$obj = new Carbon;
$today = Navigation::addPeriod($obj, $bill->repeat_freq, 0);
$skip = $bill->skip + 1; $end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
$start = Navigation::startOfPeriod($obj, $bill->repeat_freq); Log::debug('Final start is ' . $start->format('Y-m-d'));
/* Log::debug('Matching end is ' . $end->format('Y-m-d'));
* go back exactly one month/week/etc because FF3 does not care about 'next'
* bills if they're too far into the past.
*/
$counter = 0; // see if the bill was paid in this period.
while ($start <= $today) { $journalCount = $bill->transactionJournals()->before($end)->after($start)->count();
if (($counter % $skip) == 0) {
// do something.
$end = Navigation::endOfPeriod(clone $start, $bill->repeat_freq);
$journalCount = $bill->transactionJournals()->before($end)->after($start)->count();
if ($journalCount == 0) {
$finalDate = new Carbon($start->format('Y-m-d'));
break;
}
}
// add period for next round! if ($journalCount > 0) {
$start = Navigation::addPeriod($start, $bill->repeat_freq, 0); // this period had in fact a bill. The new start is the current end, and we create a new end.
$counter++; $start = clone $end;
$end = Navigation::addPeriod($start, $bill->repeat_freq, $bill->skip);
} }
$cache->store($start);
return $finalDate; return $start;
} }
/** /**

View File

@ -168,11 +168,12 @@ interface BillRepositoryInterface
/** /**
* @param Bill $bill * @param Bill $bill
* @param Carbon $date
* *
* @return \Carbon\Carbon * @return \Carbon\Carbon
*/ */
public function nextExpectedMatch(Bill $bill): Carbon; public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon;
/** /**
* @param Bill $bill * @param Bill $bill

View File

@ -6,7 +6,7 @@
<th class="hidden-sm hidden-xs">{{ trans('list.matchesOn') }}</th> <th class="hidden-sm hidden-xs">{{ trans('list.matchesOn') }}</th>
<th colspan="2">{{ trans('list.matchingAmount') }}</th> <th colspan="2">{{ trans('list.matchingAmount') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.paid_current_period') }}</th> <th class="hidden-sm hidden-xs">{{ trans('list.paid_current_period') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.expectedMatch') }}</th> <th class="hidden-sm hidden-xs">{{ trans('list.next_expected_match') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.active') }}</th> <th class="hidden-sm hidden-xs">{{ trans('list.active') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.automatch') }}</th> <th class="hidden-sm hidden-xs">{{ trans('list.automatch') }}</th>
<th class="hidden-sm hidden-xs">{{ trans('list.repeat_freq') }}</th> <th class="hidden-sm hidden-xs">{{ trans('list.repeat_freq') }}</th>

View File

@ -0,0 +1,84 @@
{
"users": [
{
"email": "thegrumpydictator@gmail.com",
"password": "james"
},
{
"email": "thegrumpydictator+empty@gmail.com",
"password": "james"
},
{
"email": "thegrumpydictator+deleteme@gmail.com",
"password": "james"
}
],
"roles": [
{
"user_id": 1,
"role": 1
}
],
"accounts": [
{
"user_id": 1,
"account_type_id": 3,
"name": "Checking Account",
"iban": "NL11XOLA6707795988"
}
],
"account-meta": [
],
"bills": [
{
"name": "Some weird weekly bill",
"match": "weird,weekly,bill",
"amount_min": 30,
"amount_max": 35,
"user_id": 1,
"date": "2016-06-12",
"active": 1,
"automatch": 1,
"repeat_freq": "weekly",
"skip": 0
}
],
"budgets": [
],
"budget-limits": [
],
"monthly-limits": [
],
"categories": [
],
"piggy-banks": [
],
"piggy-events": [
],
"rule-groups": [
],
"rules": [
],
"rule-triggers": [
],
"rule-actions": [
],
"tags": [
],
"monthly-deposits": [
],
"monthly-transfers": [
],
"monthly-withdrawals": [
],
"attachments": [
],
"multi-withdrawals": [
],
"multi-deposits": [
],
"multi-transfers": [
],
"import-jobs": [],
"currencies": []
}

View File

@ -245,27 +245,15 @@
], ],
"bills": [ "bills": [
{ {
"name": "Rent", "name": "Some weird weekly bill",
"match": "rent,land,lord", "match": "weird,weekly,bill",
"amount_min": 795, "amount_min": 30,
"amount_max": 805, "amount_max": 35,
"user_id": 1, "user_id": 1,
"date": "2015-01-01", "date": "2016-06-12",
"active": 1, "active": 1,
"automatch": 1, "automatch": 1,
"repeat_freq": "monthly", "repeat_freq": "weekly",
"skip": 0
},
{
"name": "Health insurance",
"match": "insurer,insurance,health",
"amount_min": 120,
"amount_max": 140,
"user_id": 1,
"date": "2015-01-01",
"active": 1,
"automatch": 1,
"repeat_freq": "monthly",
"skip": 0 "skip": 0
} }
], ],