Lots of cleaning up.

This commit is contained in:
James Cole
2015-01-02 12:38:13 +01:00
parent 3be1cdb249
commit 5707dc7579
20 changed files with 201 additions and 121 deletions

View File

@@ -83,6 +83,12 @@ class BillController extends BaseController
public function index()
{
$bills = $this->_repository->get();
$bills->each(
function (Bill $bill) {
$bill->nextExpectedMatch = $this->_repository->nextExpectedMatch($bill);
$bill->lastFoundMatch = $this->_repository->lastFoundMatch($bill);
}
);
return View::make('bills.index', compact('bills'));
}
@@ -115,6 +121,7 @@ class BillController extends BaseController
public function show(Bill $bill)
{
$journals = $bill->transactionjournals()->withRelevantData()->orderBy('date', 'DESC')->get();
$bill->nextExpectedMatch = $this->_repository->nextExpectedMatch($bill);
$hideBill = true;

View File

@@ -262,11 +262,9 @@ class PiggyBankController extends BaseController
* Number of reminders:
*/
$amountPerReminder = $piggyBank->amountPerReminder();
$remindersCount = $piggyBank->countFutureReminders();
$subTitle = e($piggyBank->name);
return View::make('piggy_banks.show', compact('amountPerReminder', 'remindersCount', 'piggyBank', 'events', 'subTitle'));
return View::make('piggy_banks.show', compact('piggyBank', 'events', 'subTitle'));
}

View File

@@ -249,7 +249,7 @@ class TransactionController extends BaseController
$data['transaction_currency_id'] = $transactionCurrency->id;
$data['completed'] = 0;
$data['what'] = $what;
$data['currency'] = 'EUR'; // TODO allow custom currency
$data['currency'] = 'EUR';
// always validate:
$messages = $this->_repository->validate($data);

View File

@@ -239,7 +239,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
$q->orWhere(
function (QueryBuilder $q) use ($model) {
$q->where('accounts.name', 'LIKE', '%' . $model->name . '%');
// TODO magic number!
$q->where('accounts.account_type_id', 3);
$q->where('accounts.active', 0);
}
@@ -280,7 +279,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
$q->orWhere(
function (EloquentBuilder $q) use ($model) {
$q->where('accounts.name', 'LIKE', '%' . $model->name . '%');
// TODO magic number!
$q->where('accounts.account_type_id', 3);
$q->where('accounts.active', 0);
}
@@ -324,7 +322,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
$this->storeInitialBalance($account, $data);
}
// TODO this needs cleaning up and thinking over.
switch ($account->accountType->type) {
case 'Asset account':
case 'Default account':
@@ -352,7 +349,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
$model->name = $data['name'];
$model->active = isset($data['active']) ? intval($data['active']) : 0;
// TODO this needs cleaning up and thinking over.
switch ($model->accountType->type) {
case 'Asset account':
case 'Default account':
@@ -365,7 +361,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
if (isset($data['openingbalance']) && isset($data['openingbalancedate']) && strlen($data['openingbalancedate']) > 0) {
/** @noinspection PhpParamsInspection */
$openingBalance = $this->openingBalanceTransaction($model);
// TODO this needs cleaning up and thinking over.
if (is_null($openingBalance)) {
$this->storeInitialBalance($model, $data);
} else {
@@ -483,7 +478,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
*/
public function findByWhat($what)
{
// TODO: Implement findByWhat() method.
throw new NotImplementedException;
}
@@ -495,7 +489,6 @@ class Account implements CUDInterface, CommonDatabaseCallsInterface, AccountInte
*/
public function get()
{
// TODO: Implement get() method.
throw new NotImplementedException;
}

View File

@@ -202,6 +202,73 @@ class Bill implements CUDInterface, CommonDatabaseCallsInterface, BillInterface
}
/**
* @param \Bill $bill
*
* @return Carbon|null
*/
public function lastFoundMatch(\Bill $bill)
{
$last = $bill->transactionjournals()->orderBy('date', 'DESC')->first();
if ($last) {
return $last->date;
}
return null;
}
/**
* @param \Bill $bill
*
* @return Carbon|null
*/
public function nextExpectedMatch(\Bill $bill)
{
/*
* The date Firefly tries to find. If this stays null, it's "unknown".
*/
$finalDate = null;
if ($bill->active == 0) {
return $finalDate;
}
/*
* $today is the start of the next period, to make sure FF3 won't miss anything
* when the current period has a transaction journal.
*/
$today = \DateKit::addPeriod(new Carbon, $bill->repeat_freq, 0);
/*
* FF3 loops from the $start of the bill, and to make sure
* $skip works, it adds one (for modulo).
*/
$skip = $bill->skip + 1;
$start = \DateKit::startOfPeriod(new Carbon, $bill->repeat_freq);
/*
* 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;
while ($start <= $today) {
if (($counter % $skip) == 0) {
// do something.
$end = \DateKit::endOfPeriod(clone $start, $bill->repeat_freq);
$journalCount = $bill->transactionjournals()->before($end)->after($start)->count();
if ($journalCount == 0) {
$finalDate = clone $start;
break;
}
}
// add period for next round!
$start = \DateKit::addPeriod($start, $bill->repeat_freq, 0);
$counter++;
}
return $finalDate;
}
/**
* @param \Bill $bill
* @param \TransactionJournal $journal

View File

@@ -23,6 +23,20 @@ interface BillInterface
*/
public function getJournalForBillInRange(\Bill $bill, Carbon $start, Carbon $end);
/**
* @param \Bill $bill
*
* @return Carbon|null
*/
public function lastFoundMatch(\Bill $bill);
/**
* @param \Bill $bill
*
* @return Carbon|null
*/
public function nextExpectedMatch(\Bill $bill);
/**
* @param \Bill $bill
* @param \TransactionJournal $journal

View File

@@ -16,7 +16,6 @@ class Steam
{
/**
* TODO find a way to reliably remove cache entries for accounts.
*
* @param \Account $account
* @param Carbon $date

View File

@@ -30,21 +30,6 @@ class Bill extends Eloquent
return ['created_at', 'updated_at', 'date'];
}
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
*
* @return null
*/
public function lastFoundMatch()
{
$last = $this->transactionjournals()->orderBy('date', 'DESC')->first();
if ($last) {
return $last->date;
}
return null;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
@@ -54,58 +39,6 @@ class Bill extends Eloquent
}
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
*
* Find the next expected match based on the set journals and the date stuff from the bill.
*/
public function nextExpectedMatch()
{
/*
* The date Firefly tries to find. If this stays null, it's "unknown".
*/
$finalDate = null;
if ($this->active == 0) {
return $finalDate;
}
/*
* $today is the start of the next period, to make sure FF3 won't miss anything
* when the current period has a transaction journal.
*/
$today = DateKit::addPeriod(new Carbon, $this->repeat_freq, 0);
/*
* FF3 loops from the $start of the bill, and to make sure
* $skip works, it adds one (for modulo).
*/
$skip = $this->skip + 1;
$start = DateKit::startOfPeriod(new Carbon, $this->repeat_freq);
/*
* 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;
while ($start <= $today) {
if (($counter % $skip) == 0) {
// do something.
$end = DateKit::endOfPeriod(clone $start, $this->repeat_freq);
$journalCount = $this->transactionjournals()->before($end)->after($start)->count();
if ($journalCount == 0) {
$finalDate = clone $start;
break;
}
}
// add period for next round!
$start = DateKit::addPeriod($start, $this->repeat_freq, 0);
$counter++;
}
return $finalDate;
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo

View File

@@ -37,27 +37,6 @@ class PiggyBank extends Eloquent
return $this->belongsTo('Account');
}
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
*
* @return int
*/
public function amountPerReminder()
{
return 0;
}
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
*
* @return int
*/
public function countFutureReminders()
{
return 0;
}
/**
* TODO remove this method in favour of something in the FireflyIII libraries.
*

View File

@@ -70,8 +70,6 @@ class TransactionJournal extends Eloquent
return floatval($t->amount);
}
}
return -0.01;
}
/**

View File

@@ -258,11 +258,11 @@ Route::group(
Route::get('/profile/change-password', ['uses' => 'ProfileController@changePassword', 'as' => 'change-password']);
// related controller:
Route::get('/related/alreadyRelated/{tj}', ['uses' => 'RelatedController@alreadyRelated','as' => 'related.alreadyRelated']);
Route::post('/related/relate/{tj}/{tjSecond}', ['uses' => 'RelatedController@relate','as' => 'related.relate']);
Route::get('/related/removeRelation/{tj}/{tjSecond}', ['uses' => 'RelatedController@removeRelation','as' => 'related.removeRelation']);
Route::get('/related/related/{tj}', ['uses' => 'RelatedController@related','as' => 'related.related']);
Route::post('/related/search/{tj}', ['uses' => 'RelatedController@search','as' => 'related.search']);
Route::post('/related/relate/{tj}/{tjSecond}', ['uses' => 'RelatedController@relate','as' => 'related.relate']);
Route::get('/related/alreadyRelated/{tj}', ['uses' => 'RelatedController@alreadyRelated','as' => 'related.alreadyRelated']);
// bills controller
Route::get('/bills', ['uses' => 'BillController@index', 'as' => 'bills.index']);

View File

@@ -6,7 +6,6 @@
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa {{$mainTitleIcon}}"></i> {{{$title}}}
<!-- TODO add menu -->
</div>
<div class="panel-body">
@include('list.bills')

View File

@@ -49,9 +49,8 @@
<tr>
<td>Next expected match</td>
<td>
<?php $nextExpectedMatch = $bill->nextExpectedMatch();?>
@if($nextExpectedMatch)
{{$nextExpectedMatch->format('j F Y')}}
@if($bill->nextExpectedMatch)
{{$bill->nextExpectedMatch->format('j F Y')}}
@else
<em>Unknown</em>
@endif

View File

@@ -22,7 +22,6 @@
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-5">
<!-- TODO do this -->
BLa bla something here.
</div>
</div>

View File

@@ -32,17 +32,15 @@
{{Amount::format($entry->amount_max)}}
</td>
<td>
<?php $lastMatch = $entry->lastFoundMatch();?>
@if($lastMatch)
{{$lastMatch->format('j F Y')}}
@if($entry->lastFoundMatch)
{{$entry->lastFoundMatch->format('j F Y')}}
@else
<em>Unknown</em>
@endif
</td>
<td>
<?php $nextExpectedMatch = $entry->nextExpectedMatch();?>
@if($nextExpectedMatch)
{{$nextExpectedMatch->format('j F Y')}}
@if($entry->nextExpectedMatch)
{{$entry->nextExpectedMatch->format('j F Y')}}
@else
<em>Unknown</em>
@endif

View File

@@ -88,16 +88,14 @@
</td>
</tr>
@endif
@if($remindersCount > 0)
<tr>
<td>Reminders left</td>
<td>{{$remindersCount}}</td>
<td>(in progress...)</td>
</tr>
<tr>
<td>Expected amount per reminder</td>
<td>{{Amount::format($amountPerReminder)}}</td>
<td>(in progress...)</td>
</tr>
@endif
</table>
</div>
</div>