From 30553ed7a376015360df2076d364f4cde281bcb4 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 23 Jul 2014 16:44:20 +0200 Subject: [PATCH] Small fixes and updates. [skip ci] --- app/controllers/HomeController.php | 26 +++++++++----- .../2014_07_17_183717_create_limits_table.php | 2 ++ .../Helper/Migration/MigrationHelper.php | 6 ++-- .../Budget/BudgetRepositoryInterface.php | 2 ++ .../Budget/EloquentBudgetRepository.php | 35 +++++++++++++++++-- app/views/index.blade.php | 29 ++++++++++++++- 6 files changed, 84 insertions(+), 16 deletions(-) diff --git a/app/controllers/HomeController.php b/app/controllers/HomeController.php index acc37b57ca..67912e60d6 100644 --- a/app/controllers/HomeController.php +++ b/app/controllers/HomeController.php @@ -2,6 +2,8 @@ use Firefly\Helper\Preferences\PreferencesHelperInterface as PHI; use Firefly\Storage\Account\AccountRepositoryInterface as ARI; use Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface as TJRI; +use Firefly\Helper\Toolkit\ToolkitInterface as Toolkit; +use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI; /** * Class HomeController @@ -11,21 +13,24 @@ class HomeController extends BaseController protected $_accounts; protected $_preferences; protected $_journal; + protected $_budgets; + protected $_tk; /** * @param ARI $accounts * @param PHI $preferences * @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->_preferences = $preferences; $this->_journal = $journal; + $this->_tk = $toolkit; + $this->_budgets = $budgets; View::share('menu', 'home'); - } /** @@ -33,33 +38,36 @@ class HomeController extends BaseController */ 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(); // get the preference for the home accounts to show: $frontpage = $this->_preferences->get('frontpageAccounts', []); - if($frontpage->data == []) { + if ($frontpage->data == []) { $accounts = $this->_accounts->getActiveDefault(); } else { $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 = []; - foreach($accounts as $account) { - $transactions[] = [$this->_journal->getByAccount($account,15),$account]; + foreach ($accounts as $account) { + $transactions[] = [$this->_journal->getByAccount($account, 15), $account]; } - if(count($transactions) % 2 == 0) { + if (count($transactions) % 2 == 0) { $transactions = array_chunk($transactions, 2); - } elseif(count($transactions) == 1) { + } elseif (count($transactions) == 1) { $transactions = array_chunk($transactions, 3); } else { $transactions = array_chunk($transactions, 3); } // 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); } } \ No newline at end of file diff --git a/app/database/migrations/2014_07_17_183717_create_limits_table.php b/app/database/migrations/2014_07_17_183717_create_limits_table.php index fc23784b3f..ce330aecc3 100644 --- a/app/database/migrations/2014_07_17_183717_create_limits_table.php +++ b/app/database/migrations/2014_07_17_183717_create_limits_table.php @@ -22,6 +22,8 @@ class CreateLimitsTable extends Migration { $table->boolean('repeats'); $table->enum('repeat_freq', ['daily', 'weekly','monthly','quarterly','half-year','yearly']); + $table->unique(['component_id','startdate','repeat_freq']); + // connect component $table->foreign('component_id') ->references('id')->on('components') diff --git a/app/lib/Firefly/Helper/Migration/MigrationHelper.php b/app/lib/Firefly/Helper/Migration/MigrationHelper.php index 5aaff61b8b..fa68887a22 100644 --- a/app/lib/Firefly/Helper/Migration/MigrationHelper.php +++ b/app/lib/Firefly/Helper/Migration/MigrationHelper.php @@ -173,9 +173,9 @@ class MigrationHelper implements MigrationHelperInterface $limit->amount = floatval($entry->amount); $limit->repeats = 0; $limit->repeat_freq = 'monthly'; - if (!$limit->save()) { - \Log::error('MigrationException!'); - throw new MigrationException('Importing limits failed: ' . $limit->errors()->first()); + try { + $limit->save(); + } catch (\Exception $e) { } } else { \Log::warning('No budget for this limit!'); diff --git a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php index 1271ee6103..3af17ffe4f 100644 --- a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php +++ b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php @@ -12,4 +12,6 @@ interface BudgetRepositoryInterface public function find($id); + public function getWithRepetitionsInPeriod(\Carbon\Carbon $date, $range); + } \ No newline at end of file diff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php index d43118b8f6..aadb3dbc71 100644 --- a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php +++ b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php @@ -18,6 +18,35 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface 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) { $budget = new \Budget; @@ -67,10 +96,10 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface public function get() { return \Auth::user()->budgets()->with( - ['limits' => function ($q) { - $q->orderBy('limits.startdate','ASC'); + ['limits' => function ($q) { + $q->orderBy('limits.startdate', 'ASC'); }, 'limits.limitrepetitions' => function ($q) { - $q->orderBy('limit_repetitions.startdate','ASC'); + $q->orderBy('limit_repetitions.startdate', 'ASC'); }] )->orderBy('name', 'ASC')->get(); } diff --git a/app/views/index.blade.php b/app/views/index.blade.php index 00a6b34f4d..3ec780f72c 100644 --- a/app/views/index.blade.php +++ b/app/views/index.blade.php @@ -77,9 +77,36 @@ @endforeach @endif + +
- +

Budgets

+ + @foreach($budgets as $budget) + + + @if($budget->count == 0) + + @else + @foreach($budget->limits as $limit) + @foreach($limit->limitrepetitions as $rep) + + + @endforeach + @endforeach + @endif + + @endforeach +
+ {{{$budget->name}}} + + No budget set for this period. + + {{mf($rep->amount)}} + + {{mf($rep->left())}} +