Small fixes and updates. [skip ci]

This commit is contained in:
James Cole 2014-07-23 16:44:20 +02:00
parent 1a9c44f7f9
commit 30553ed7a3
6 changed files with 84 additions and 16 deletions

View File

@ -2,6 +2,8 @@
use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI; use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI;
use Firefly\Storage\Account\AccountRepositoryInterface as ARI; use Firefly\Storage\Account\AccountRepositoryInterface as ARI;
use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI; use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI;
use Firefly\Helper\Toolkit\ToolkitInterface as Toolkit;
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
/** /**
* Class HomeController * Class HomeController
@ -11,21 +13,24 @@ class HomeController extends BaseController
protected $_accounts; protected $_accounts;
protected $_preferences; protected $_preferences;
protected $_journal; protected $_journal;
protected $_budgets;
protected $_tk;
/** /**
* @param ARI $accounts * @param ARI $accounts
* @param PHI $preferences * @param PHI $preferences
* @param TJRI $journal * @param TJRI $journal
*/ */
public function __construct(ARI $accounts, PHI $preferences, TJRI $journal) public function __construct(ARI $accounts, PHI $preferences, TJRI $journal, Toolkit $toolkit, BRI $budgets)
{ {
$this->_accounts = $accounts; $this->_accounts = $accounts;
$this->_preferences = $preferences; $this->_preferences = $preferences;
$this->_journal = $journal; $this->_journal = $journal;
$this->_tk = $toolkit;
$this->_budgets = $budgets;
View::share('menu', 'home'); View::share('menu', 'home');
} }
/** /**
@ -33,33 +38,36 @@ class HomeController extends BaseController
*/ */
public function index() public function index()
{ {
// count, maybe we need some introductionary text to show: // count, maybe we need some introducing text to show:
$count = $this->_accounts->count(); $count = $this->_accounts->count();
// get the preference for the home accounts to show: // get the preference for the home accounts to show:
$frontpage = $this->_preferences->get('frontpageAccounts', []); $frontpage = $this->_preferences->get('frontpageAccounts', []);
if($frontpage->data == []) { if ($frontpage->data == []) {
$accounts = $this->_accounts->getActiveDefault(); $accounts = $this->_accounts->getActiveDefault();
} else { } else {
$accounts = $this->_accounts->getByIds($frontpage->data); $accounts = $this->_accounts->getByIds($frontpage->data);
} }
// get the budgets for this period:
$dates = $this->_tk->getDateRange();
$budgets = $this->_budgets->getWithRepetitionsInPeriod($dates[0],\Session::get('range'));
$transactions = []; $transactions = [];
foreach($accounts as $account) { foreach ($accounts as $account) {
$transactions[] = [$this->_journal->getByAccount($account,15),$account]; $transactions[] = [$this->_journal->getByAccount($account, 15), $account];
} }
if(count($transactions) % 2 == 0) { if (count($transactions) % 2 == 0) {
$transactions = array_chunk($transactions, 2); $transactions = array_chunk($transactions, 2);
} elseif(count($transactions) == 1) { } elseif (count($transactions) == 1) {
$transactions = array_chunk($transactions, 3); $transactions = array_chunk($transactions, 3);
} else { } else {
$transactions = array_chunk($transactions, 3); $transactions = array_chunk($transactions, 3);
} }
// build the home screen: // build the home screen:
return View::make('index')->with('count', $count)->with('transactions',$transactions); return View::make('index')->with('count', $count)->with('transactions', $transactions)->with('budgets',$budgets);
} }
} }

View File

@ -22,6 +22,8 @@ class CreateLimitsTable extends Migration {
$table->boolean('repeats'); $table->boolean('repeats');
$table->enum('repeat_freq', ['daily', 'weekly','monthly','quarterly','half-year','yearly']); $table->enum('repeat_freq', ['daily', 'weekly','monthly','quarterly','half-year','yearly']);
$table->unique(['component_id','startdate','repeat_freq']);
// connect component // connect component
$table->foreign('component_id') $table->foreign('component_id')
->references('id')->on('components') ->references('id')->on('components')

View File

@ -173,9 +173,9 @@ class MigrationHelper implements MigrationHelperInterface
$limit->amount = floatval($entry->amount); $limit->amount = floatval($entry->amount);
$limit->repeats = 0; $limit->repeats = 0;
$limit->repeat_freq = 'monthly'; $limit->repeat_freq = 'monthly';
if (!$limit->save()) { try {
\Log::error('MigrationException!'); $limit->save();
throw new MigrationException('Importing limits failed: ' . $limit->errors()->first()); } catch (\Exception $e) {
} }
} else { } else {
\Log::warning('No budget for this limit!'); \Log::warning('No budget for this limit!');

View File

@ -12,4 +12,6 @@ interface BudgetRepositoryInterface
public function find($id); public function find($id);
public function getWithRepetitionsInPeriod(\Carbon\Carbon $date, $range);
} }

View File

@ -18,6 +18,35 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $return; return $return;
} }
public function getWithRepetitionsInPeriod(\Carbon\Carbon $date, $range)
{
/** @var \Firefly\Helper\Toolkit\ToolkitInterface $toolkit */
$toolkit = \App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$dates = $toolkit->getDateRange();
$start = $dates[0];
$result = [];
$set = \Auth::user()->budgets()->with(
['limits' => function ($q) use ($date) {
$q->orderBy('limits.startdate', 'ASC');
// $q->where('startdate',$date->format('Y-m-d'));
}, 'limits.limitrepetitions' => function ($q) use ($date) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
$q->where('startdate',$date->format('Y-m-d'));
}]
)->orderBy('name', 'ASC')->get();
foreach ($set as $budget) {
$budget->count = 0;
foreach($budget->limits as $limit) {
$budget->count += count($limit->limitrepetitions);
}
}
return $set;
}
public function store($data) public function store($data)
{ {
$budget = new \Budget; $budget = new \Budget;
@ -67,10 +96,10 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
public function get() public function get()
{ {
return \Auth::user()->budgets()->with( return \Auth::user()->budgets()->with(
['limits' => function ($q) { ['limits' => function ($q) {
$q->orderBy('limits.startdate','ASC'); $q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) { }, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate','ASC'); $q->orderBy('limit_repetitions.startdate', 'ASC');
}] }]
)->orderBy('name', 'ASC')->get(); )->orderBy('name', 'ASC')->get();
} }

View File

@ -77,9 +77,36 @@
@endforeach @endforeach
@endif @endif
<div class="row"> <div class="row">
<div class="col-lg-12 col-md-12 col-sm-12"> <div class="col-lg-12 col-md-12 col-sm-12">
<h4>Budgets</h4>
<table class="table table-bordered">
@foreach($budgets as $budget)
<tr>
<td>
<a href="{{route('budgets.show',$budget->id)}}">{{{$budget->name}}}</a>
</td>
@if($budget->count == 0)
<td colspan="2">
<em>No budget set for this period.</em>
</td>
@else
@foreach($budget->limits as $limit)
@foreach($limit->limitrepetitions as $rep)
<td>
{{mf($rep->amount)}}
</td>
<td>
{{mf($rep->left())}}
</td>
@endforeach
@endforeach
@endif
</tr>
@endforeach
</table>
</div> </div>
</div> </div>