Skip to content

Firefly III: a personal finances manager

Cleaned up a lot of source, finished the ChartController. [skip ci]

James Cole9af2b47463fb
diff --git a/app/controllers/ChartController.php b/app/controllers/ChartController.php
index d6870547f9..20abf95400 100644
--- a/app/controllers/ChartController.php
+++ b/app/controllers/ChartController.php
@@ -1,6 +1,7 @@
<?php
use Firefly\Helper\Controllers\ChartInterface;
+use Firefly\Storage\Account\AccountRepositoryInterface;
/**
* Class ChartController
@@ -9,29 +10,58 @@ class ChartController extends BaseController
{
protected $_chart;
+ protected $_accounts;
/**
* @param ChartInterface $chart
*/
- public function __construct(ChartInterface $chart)
+ public function __construct(ChartInterface $chart, AccountRepositoryInterface $accounts)
{
$this->_chart = $chart;
+ $this->_accounts = $accounts;
}
/**
* @param Account $account
+ *
* @return mixed
*/
public function homeAccount(Account $account = null)
{
-
-
- if (!is_null($account)) {
- $data = $this->_chart->account($account);
+ // get preferences and accounts (if necessary):
+ $data = [];
+ $start = Session::get('start');
+ $end = Session::get('end');
+
+ if (is_null($account)) {
+ // get, depending on preferences:
+ /** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $prefs */
+ $prefs = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');
+ $pref = $prefs->get('frontpageAccounts', []);
+
+ /** @var \Firefly\Storage\Account\AccountRepositoryInterface $acct */
+ $acct = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
+ $accounts = $acct->getByIds($pref->data);
} else {
- $data = $this->_chart->accounts();
+ $accounts = [$account];
+ }
+ // loop and get array data.
+
+ $url = count($accounts) == 1
+ ? '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>'
+ :
+ '<a href="' . route('accounts.index') . '">View more</a>';
+ $data = [
+ 'chart_title' => count($accounts) == 1 ? $accounts[0]->name : 'All accounts',
+ 'subtitle' => $url,
+ 'series' => []
+ ];
+
+ foreach ($accounts as $account) {
+ $data['series'][] = $this->_chart->account($account, $start, $end);
}
+
return Response::json($data);
}
@@ -47,120 +77,38 @@ class ChartController extends BaseController
*/
public function homeAccountInfo($name, $day, $month, $year)
{
+
$account = $this->_accounts->findByName($name);
- $result = [];
- $sum = 0;
+ $date = Carbon::createFromDate($year, $month, $day);
if ($account) {
- $date = \Carbon\Carbon::createFromDate($year, $month, $day);
- $journals = $this->_journals->getByAccountAndDate($account, $date);
- // loop all journals:
- foreach ($journals as $journal) {
- foreach ($journal->transactions as $transaction) {
- $name = $transaction->account->name;
- if ($transaction->account->id != $account->id) {
- $result[$name] = isset($result[$name]) ? $result[$name] + floatval($transaction->amount)
- : floatval($transaction->amount);
- $sum += floatval($transaction->amount);
- }
- }
- }
- }
+ $result = $this->_chart->accountDailySummary($account, $date);
- return View::make('charts.info')->with('rows', $result)->with('sum', $sum);
+ return View::make('charts.info')->with('rows', $result['rows'])->with('sum', $result['sum']);
+ } else {
+ return View::make('error')->with('message', 'No account!');
+ }
}
/**
* @return \Illuminate\Http\JsonResponse
- * @throws Firefly\Exception\FireflyException
*/
- public function homeCategories()
+ public function homeBudgets()
{
- list($start, $end) = $this->_tk->getDateRangeDates();
- $result = [];
- // grab all transaction journals in this period:
- $journals = $this->_journals->getByDateRange($start, $end);
-
- foreach ($journals as $journal) {
- // has to be one:
-
- if (!isset($journal->transactions[0])) {
- throw new FireflyException('Journal #' . $journal->id . ' has ' . count($journal->transactions)
- . ' transactions!');
- }
- $transaction = $journal->transactions[0];
- $amount = floatval($transaction->amount);
-
- // get budget from journal:
- $budget = $journal->categories()->first();
- $budgetName = is_null($budget) ? '(no category)' : $budget->name;
-
- $result[$budgetName] = isset($result[$budgetName]) ? $result[$budgetName] + floatval($amount) : $amount;
-
- }
- unset($journal, $transaction, $budget, $amount);
-
- // sort
- arsort($result);
- $chartData = [
- ];
- foreach ($result as $name => $value) {
- $chartData[] = [$name, $value];
- }
-
-
- return Response::json($chartData);
+ $start = \Session::get('start');
+ return Response::json($this->_chart->budgets($start));
}
/**
* @return \Illuminate\Http\JsonResponse
- * @throws Firefly\Exception\FireflyException
*/
- public function homeBudgets()
+ public function homeCategories()
{
- // grab all budgets in the time period, like the index does:
- // get the budgets for this period:
- $data = [];
-
- list($start) = $this->_tk->getDateRangeDates();
- $budgets = $this->_budgets->getWithRepetitionsInPeriod($start, \Session::get('range'));
+ $start = Session::get('start');
+ $end = Session::get('end');
- $repeatFreq = Config::get('firefly.range_to_repeat_freq.' . Session::get('range'));
+ return Response::json($this->_chart->categories($start, $end));
- $limitInPeriod = 'Envelope for XXX';
- $spentInPeriod = 'Spent in XXX';
-
- $data['series'] = [
- [
- 'name' => $limitInPeriod,
- 'data' => []
- ],
- [
- 'name' => $spentInPeriod,
- 'data' => []
- ],
- ];
-
-
- foreach ($budgets as $budget) {
- if ($budget->count > 0) {
- $data['labels'][] = wordwrap($budget->name, 12, "<br>");
- }
- foreach ($budget->limits as $limit) {
- foreach ($limit->limitrepetitions as $rep) {
- //0: envelope for period:
- $amount = floatval($rep->amount);
- $spent = $rep->spent;
- $color = $spent > $amount ? '#FF0000' : null;
- $data['series'][0]['data'][] = $amount;
- $data['series'][1]['data'][] = ['y' => $rep->spent, 'color' => $color];
- }
- }
-
-
- }
-
- return Response::json($data);
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Database/SingleTableInheritanceEntity.php b/app/lib/Firefly/Database/SingleTableInheritanceEntity.php
index 7a6df521b4..40d31c755b 100644
--- a/app/lib/Firefly/Database/SingleTableInheritanceEntity.php
+++ b/app/lib/Firefly/Database/SingleTableInheritanceEntity.php
@@ -35,6 +35,7 @@ abstract class SingleTableInheritanceEntity extends Ardent
{
$instance = $this->mapData((array)$attributes)->newInstance([], true);
$instance->setRawAttributes((array)$attributes, true);
+
return $instance;
}
@@ -117,6 +118,7 @@ abstract class SingleTableInheritanceEntity extends Ardent
if ($this->subclassField) {
$this->attributes[$this->subclassField] = get_class($this);
}
+
return parent::save($rules, $customMessages, $options, $beforeSave, $afterSave);
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Helper/Controllers/Account.php b/app/lib/Firefly/Helper/Controllers/Account.php
index bdc18aff7a..fb79b1a388 100644
--- a/app/lib/Firefly/Helper/Controllers/Account.php
+++ b/app/lib/Firefly/Helper/Controllers/Account.php
@@ -127,8 +127,8 @@ class Account implements AccountInterface
// statistics
$stats['period']['in'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '>', 0)->leftJoin(
- 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
- )->leftJoin(
+ 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
+ )->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->whereIn('transaction_types.type', ['Deposit', 'Withdrawal'])->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
@@ -138,8 +138,8 @@ class Account implements AccountInterface
$stats['period']['out'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '<', 0)->leftJoin(
- 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
- )->leftJoin(
+ 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
+ )->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->whereIn('transaction_types.type', ['Deposit', 'Withdrawal'])->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
@@ -149,8 +149,8 @@ class Account implements AccountInterface
$stats['period']['t_in'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '>', 0)->leftJoin(
- 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
- )->leftJoin(
+ 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
+ )->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Transfer')->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
@@ -159,8 +159,8 @@ class Account implements AccountInterface
$stats['period']['t_out'] = floatval(
\Transaction::where('account_id', $account->id)->where('amount', '<', 0)->leftJoin(
- 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
- )->leftJoin(
+ 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
+ )->leftJoin(
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
)->where('transaction_types.type', 'Transfer')->where(
'transaction_journals.date', '>=', $start->format('Y-m-d')
diff --git a/app/lib/Firefly/Helper/Controllers/Budget.php b/app/lib/Firefly/Helper/Controllers/Budget.php
index 5aa5b4b78b..b698c12b47 100644
--- a/app/lib/Firefly/Helper/Controllers/Budget.php
+++ b/app/lib/Firefly/Helper/Controllers/Budget.php
@@ -214,7 +214,8 @@ class Budget implements BudgetInterface
}
$paginator = \Paginator::make($items, $totalItems, $perPage);
$result = [0 => ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true,
- 'journals' => $paginator]];
+ 'journals' => $paginator]];
+
return $result;
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Helper/Controllers/BudgetInterface.php b/app/lib/Firefly/Helper/Controllers/BudgetInterface.php
index 99aaf03ff7..6f0051c0f9 100644
--- a/app/lib/Firefly/Helper/Controllers/BudgetInterface.php
+++ b/app/lib/Firefly/Helper/Controllers/BudgetInterface.php
@@ -1,12 +1,15 @@
<?php
namespace Firefly\Helper\Controllers;
+
use Illuminate\Database\Eloquent\Collection;
+
/**
* Interface BudgetInterface
*
* @package Firefly\Helper\Controllers
*/
-interface BudgetInterface {
+interface BudgetInterface
+{
/**
* @param Collection $budgets
diff --git a/app/lib/Firefly/Helper/Controllers/Chart.php b/app/lib/Firefly/Helper/Controllers/Chart.php
index afbaf7d271..5b4976f0f0 100644
--- a/app/lib/Firefly/Helper/Controllers/Chart.php
+++ b/app/lib/Firefly/Helper/Controllers/Chart.php
@@ -9,63 +9,208 @@
namespace Firefly\Helper\Controllers;
use Carbon\Carbon;
+use Firefly\Exception\FireflyException;
class Chart implements ChartInterface
{
- public function account(\Account $account)
+ public function account(\Account $account, Carbon $start, Carbon $end)
{
- $data = [
- 'chart_title' => $account->name,
- 'subtitle' => '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>',
- 'series' => [$this->_account($account)]
- ];
- return $data;
+ $current = clone $start;
+ $today = new Carbon;
+ $return = ['name' => $account->name, 'id' => $account->id, 'data' => []];
+ while ($current <= $end) {
+ if ($current > $today) {
+ $return['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
+ } else {
+ $return['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];
+ }
+
+ $current->addDay();
+ }
+
+ return $return;
}
- public function accounts()
+ public function accountDailySummary(\Account $account, Carbon $date)
{
- $data = [
- 'chart_title' => 'All accounts',
- 'subtitle' => '<a href="' . route('accounts.index') . '">View more</a>',
- 'series' => []
+ $result = [
+ 'rows' => [],
+ 'sum' => 0
];
- /** @var \Firefly\Helper\Preferences\PreferencesHelperInterface $prefs */
- $prefs = \App::make('Firefly\Helper\Preferences\PreferencesHelperInterface');
- $pref = $prefs->get('frontpageAccounts', []);
+ if ($account) {
+ // get journals in range:
+ $journals = \Auth::user()->transactionjournals()->with(
+ [
+ 'transactions',
+ 'transactions.account',
+ 'transactioncurrency',
+ 'transactiontype'
+ ]
+ )
+ ->distinct()
+ ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
+ ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
+ ->where('transactions.account_id', $account->id)
+ ->where('transaction_journals.date', $date->format('Y-m-d'))
+ ->orderBy('transaction_journals.date', 'DESC')
+ ->orderBy('transaction_journals.id', 'DESC')
+ ->get(['transaction_journals.*']);
+
+ // loop all journals:
+ foreach ($journals as $journal) {
+ foreach ($journal->transactions as $transaction) {
+ $name = $transaction->account->name;
+ if ($transaction->account->id != $account->id) {
+ $result['rows'][$name] = isset($result[$name]) ? $result[$name] + floatval($transaction->amount)
+ : floatval($transaction->amount);
+ $result['sum'] += floatval($transaction->amount);
+ }
+ }
+ }
+ }
+
+ return $result;
+
+ }
+
+ /**
+ * @return array
+ */
+ public function budgets(Carbon $start)
+ {
+ // grab all budgets in the time period, like the index does:
+ // get the budgets for this period:
- /** @var \Firefly\Storage\Account\AccountRepositoryInterface $acct */
- $acct = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
+ $data = [];
- if ($pref->data == []) {
- $accounts = $acct->getActiveDefault();
- } else {
- $accounts = $acct->getByIds($pref->data);
+ $budgets = \Auth::user()->budgets()->with(
+ ['limits' => function ($q) {
+ $q->orderBy('limits.startdate', 'ASC');
+ }, 'limits.limitrepetitions' => function ($q) use ($start) {
+ $q->orderBy('limit_repetitions.startdate', 'ASC');
+ $q->where('startdate', $start->format('Y-m-d'));
+ }]
+ )->orderBy('name', 'ASC')->get();
+
+ foreach ($budgets as $budget) {
+ $budget->count = 0;
+ foreach ($budget->limits as $limit) {
+ /** @var $rep \LimitRepetition */
+ foreach ($limit->limitrepetitions as $rep) {
+ $rep->left = $rep->left();
+ // overspent:
+ if ($rep->left < 0) {
+ $rep->spent = ($rep->left * -1) + $rep->amount;
+ $rep->overspent = $rep->left * -1;
+ $total = $rep->spent + $rep->overspent;
+ $rep->spent_pct = round(($rep->spent / $total) * 100);
+ $rep->overspent_pct = 100 - $rep->spent_pct;
+ } else {
+ $rep->spent = $rep->amount - $rep->left;
+ $rep->spent_pct = round(($rep->spent / $rep->amount) * 100);
+ $rep->left_pct = 100 - $rep->spent_pct;
+
+
+ }
+ }
+ $budget->count += count($limit->limitrepetitions);
+ }
}
- foreach($accounts as $account) {
- $data['series'][] = $this->_account($account);
+
+ $limitInPeriod = 'Envelope for XXX';
+ $spentInPeriod = 'Spent in XXX';
+
+ $data['series'] = [
+ [
+ 'name' => $limitInPeriod,
+ 'data' => []
+ ],
+ [
+ 'name' => $spentInPeriod,
+ 'data' => []
+ ],
+ ];
+
+
+ foreach ($budgets as $budget) {
+ if ($budget->count > 0) {
+ $data['labels'][] = wordwrap($budget->name, 12, "<br>");
+ }
+ foreach ($budget->limits as $limit) {
+ foreach ($limit->limitrepetitions as $rep) {
+ //0: envelope for period:
+ $amount = floatval($rep->amount);
+ $spent = $rep->spent;
+ $color = $spent > $amount ? '#FF0000' : null;
+ $data['series'][0]['data'][] = $amount;
+ $data['series'][1]['data'][] = ['y' => $rep->spent, 'color' => $color];
+ }
+ }
+
+
}
- return $data;
+ return $data;
}
- protected function _account(\Account $account)
+ public function categories(Carbon $start, Carbon $end)
{
- $start = \Session::get('start');
- $end = \Session::get('end');
- $current = clone $start;
- $today = new Carbon;
- $return = ['name' => $account->name, 'id' => $account->id, 'data' => []];
- while ($current <= $end) {
- if ($current > $today) {
- $return['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
- } else {
- $return['data'][] = [$current->timestamp * 1000, $account->balance(clone $current)];
+
+ $result = [];
+ // grab all transaction journals in this period:
+ $journals = \TransactionJournal::
+ with(
+ ['components', 'transactions' => function ($q) {
+ $q->where('amount', '>', 0);
+ }]
+ )
+ ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
+ ->where('transaction_types.type', 'Withdrawal')
+ ->after($start)->before($end)
+ ->where('completed', 1)
+ ->get(['transaction_journals.*']);
+ foreach ($journals as $journal) {
+ // has to be one:
+
+ if (!isset($journal->transactions[0])) {
+ throw new FireflyException('Journal #' . $journal->id . ' has ' . count($journal->transactions)
+ . ' transactions!');
}
+ $transaction = $journal->transactions[0];
+ $amount = floatval($transaction->amount);
+
+ // get budget from journal:
+ $category = $journal->categories()->first();
+ $categoryName = is_null($category) ? '(no category)' : $category->name;
+
+ $result[$categoryName] = isset($result[$categoryName]) ? $result[$categoryName] + floatval($amount)
+ : $amount;
- $current->addDay();
}
- return $return;
+ unset($journal, $transaction, $category, $amount);
+
+ // sort
+ arsort($result);
+ $chartData = [
+ ];
+ foreach ($result as $name => $value) {
+ $chartData[] = [$name, $value];
+ }
+
+
+ return $chartData;
+ }
+
+ public function accountXX(\Account $account)
+ {
+ $data = [
+ 'chart_title' => $account->name,
+ 'subtitle' => '<a href="' . route('accounts.show', [$account->id]) . '">View more</a>',
+ 'series' => [$this->_account($account)]
+ ];
+
+ return $data;
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Helper/Controllers/ChartInterface.php b/app/lib/Firefly/Helper/Controllers/ChartInterface.php
index 5ad025d894..3839b546f9 100644
--- a/app/lib/Firefly/Helper/Controllers/ChartInterface.php
+++ b/app/lib/Firefly/Helper/Controllers/ChartInterface.php
@@ -9,9 +9,16 @@
namespace Firefly\Helper\Controllers;
+use Carbon\Carbon;
+
interface ChartInterface
{
- public function account(\Account $account);
- public function accounts();
+ public function account(\Account $account, Carbon $start, Carbon $end);
+
+ public function categories(Carbon $start, Carbon $end);
+
+ public function budgets(Carbon $start);
+
+ public function accountDailySummary(\Account $account, Carbon $date);
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Helper/Form/FormTrigger.php b/app/lib/Firefly/Helper/Form/FormTrigger.php
index 4c95b8b678..16e3636bfd 100644
--- a/app/lib/Firefly/Helper/Form/FormTrigger.php
+++ b/app/lib/Firefly/Helper/Form/FormTrigger.php
@@ -9,17 +9,21 @@
namespace Firefly\Helper\Form;
use Illuminate\Events\Dispatcher;
+
/**
* Class FormTrigger
*
* @package Firefly\Helper\Form
*/
-class FormTrigger {
+class FormTrigger
+{
- public function registerFormExtensions() {
+ public function registerFormExtensions()
+ {
\Form::macro(
'budget', function () {
$helper = new \Firefly\Helper\Form\FormHelper;
+
return $helper->budget();
}
);
diff --git a/app/lib/Firefly/Helper/Migration/MigrationHelper.php b/app/lib/Firefly/Helper/Migration/MigrationHelper.php
index 32416ff4d6..477072829f 100644
--- a/app/lib/Firefly/Helper/Migration/MigrationHelper.php
+++ b/app/lib/Firefly/Helper/Migration/MigrationHelper.php
@@ -35,6 +35,7 @@ class MigrationHelper implements MigrationHelperInterface
// file does not exist:
if (!file_exists($this->path)) {
\Log::error('Migration file ' . $this->path . ' does not exist!');
+
return false;
}
@@ -50,6 +51,7 @@ class MigrationHelper implements MigrationHelperInterface
return false;
}
\Log::info('Migration file ' . $this->path . ' is valid!');
+
return true;
}
@@ -83,11 +85,13 @@ class MigrationHelper implements MigrationHelperInterface
\DB::rollBack();
\Log::error('Rollback because of error!');
\Log::error($e->getMessage());
+
return false;
}
\DB::commit();
\Log::info('Done!');
+
return true;
}
@@ -164,6 +168,7 @@ class MigrationHelper implements MigrationHelperInterface
{
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
+
return $accounts->store(
[
'name' => $component->name,
@@ -181,6 +186,7 @@ class MigrationHelper implements MigrationHelperInterface
{
/** @var \Firefly\Storage\Component\ComponentRepositoryInterface $components */
$components = \App::make('Firefly\Storage\Component\ComponentRepositoryInterface');
+
return $components->store(['name' => $component->name, 'class' => 'Category']);
}
@@ -193,6 +199,7 @@ class MigrationHelper implements MigrationHelperInterface
{
/** @var \Firefly\Storage\Component\ComponentRepositoryInterface $components */
$components = \App::make('Firefly\Storage\Component\ComponentRepositoryInterface');
+
return $components->store(['name' => $component->name, 'class' => 'Budget']);
}
diff --git a/app/lib/Firefly/Helper/Preferences/PreferencesHelper.php b/app/lib/Firefly/Helper/Preferences/PreferencesHelper.php
index 2dc2fa2f25..e212fca757 100644
--- a/app/lib/Firefly/Helper/Preferences/PreferencesHelper.php
+++ b/app/lib/Firefly/Helper/Preferences/PreferencesHelper.php
@@ -28,6 +28,7 @@ class PreferencesHelper implements PreferencesHelperInterface
// create preference, return that:
return $this->set($name, $default);
}
+
return null;
}
diff --git a/app/lib/Firefly/Helper/Toolkit/Toolkit.php b/app/lib/Firefly/Helper/Toolkit/Toolkit.php
index bfc6b13d8d..b2a102b38f 100644
--- a/app/lib/Firefly/Helper/Toolkit/Toolkit.php
+++ b/app/lib/Firefly/Helper/Toolkit/Toolkit.php
@@ -149,6 +149,7 @@ class Toolkit implements ToolkitInterface
return \Redirect::to($request->url());
}
+
return null;
diff --git a/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php b/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php
index d804d5e3de..40263785b9 100644
--- a/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php
+++ b/app/lib/Firefly/Helper/Toolkit/ToolkitInterface.php
@@ -3,6 +3,7 @@
namespace Firefly\Helper\Toolkit;
use Illuminate\Http\Request;
+
/**
* Interface ToolkitInterface
*
diff --git a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
index 9bb8490e59..80e45c868f 100644
--- a/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
+++ b/app/lib/Firefly/Storage/Account/AccountRepositoryInterface.php
@@ -77,7 +77,7 @@ interface AccountRepositoryInterface
*
* @return mixed
*/
- public function getByIds($ids);
+ public function getByIds(array $ids);
/**
* @return mixed
diff --git a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
index 35301ff1fa..8f4497e4b1 100644
--- a/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
+++ b/app/lib/Firefly/Storage/Account/EloquentAccountRepository.php
@@ -22,48 +22,86 @@ class EloquentAccountRepository implements AccountRepositoryInterface
/**
* @return mixed
*/
- public function get()
+ public function count()
{
- return \Auth::user()->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
+ return \Auth::user()->accounts()->count();
+
}
/**
- * @return mixed
+ * @param $name
+ * @param \AccountType $type
+ *
+ * @return \Account|mixed
*/
- public function getBeneficiaries()
+ public function createOrFind($name, \AccountType $type)
{
- $list = \Auth::user()->accounts()->leftJoin(
- 'account_types', 'account_types.id', '=', 'accounts.account_type_id'
- )
- ->where('account_types.description', 'Beneficiary account')->where('accounts.active', 1)
+ $beneficiary = $this->findByName($name);
+ if (!$beneficiary) {
+ $data = [
+ 'name' => $name,
+ 'account_type' => $type
+ ];
- ->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
- return $list;
+ return $this->store($data);
+ }
+
+ return $beneficiary;
}
/**
- * @param $ids
+ * @param $name
*
- * @return array|mixed
+ * @return \Account|mixed|null
*/
- public function getByIds($ids)
+ public function createOrFindBeneficiary($name)
{
- if (count($ids) > 0) {
- return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();
- } else {
- return [];
+ if (is_null($name) || strlen($name) == 0) {
+ return null;
+ }
+ $type = \AccountType::where('description', 'Beneficiary account')->first();
+
+ return $this->createOrFind($name, $type);
+ }
+
+ public function destroy($accountId)
+ {
+ $account = $this->find($accountId);
+ if ($account) {
+ $account->delete();
+
+ return true;
}
+
+ return false;
+ }
+
+ /**
+ * @param $accountId
+ *
+ * @return mixed
+ */
+ public function find($accountId)
+ {
+ return \Auth::user()->accounts()->where('id', $accountId)->first();
}
/**
+ * @param $name
+ *
* @return mixed
*/
- public function getDefault()
+ public function findByName($name)
{
- return \Auth::user()->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
- ->where('account_types.description', 'Default account')
+ return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
+ }
- ->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
+ /**
+ * @return mixed
+ */
+ public function get()
+ {
+ return \Auth::user()->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
}
/**
@@ -92,59 +130,60 @@ class EloquentAccountRepository implements AccountRepositoryInterface
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
}
+
return $return;
}
/**
* @return mixed
*/
- public function count()
+ public function getBeneficiaries()
{
- return \Auth::user()->accounts()->count();
+ $list = \Auth::user()->accounts()->leftJoin(
+ 'account_types', 'account_types.id', '=', 'accounts.account_type_id'
+ )
+ ->where('account_types.description', 'Beneficiary account')->where('accounts.active', 1)
+ ->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
+
+ return $list;
}
/**
- * @param $name
+ * @param $ids
*
- * @return \Account|mixed|null
+ * @return array|mixed
*/
- public function createOrFindBeneficiary($name)
+ public function getByIds(array $ids)
{
- if (is_null($name) || strlen($name) == 0) {
- return null;
+ if (count($ids) > 0) {
+ return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();
+ } else {
+ return $this->getActiveDefault();
}
- $type = \AccountType::where('description', 'Beneficiary account')->first();
- return $this->createOrFind($name, $type);
}
/**
- * @param $name
- * @param \AccountType $type
- *
- * @return \Account|mixed
+ * @return mixed
*/
- public function createOrFind($name, \AccountType $type)
+ public function getCashAccount()
{
- $beneficiary = $this->findByName($name);
- if (!$beneficiary) {
- $data = [
- 'name' => $name,
- 'account_type' => $type
- ];
- return $this->store($data);
- }
- return $beneficiary;
+ $type = \AccountType::where('description', 'Cash account')->first();
+ $cash = \Auth::user()->accounts()->where('account_type_id', $type->id)->first();
+
+ return $cash;
+
}
/**
- * @param $name
- *
* @return mixed
*/
- public function findByName($name)
+ public function getDefault()
{
- return \Auth::user()->accounts()->where('name', 'like', '%' . $name . '%')->first();
+ return \Auth::user()->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
+ ->where('account_types.description', 'Default account')
+
+ ->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
/**
@@ -183,40 +222,6 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return $account;
}
- /**
- * @param \Account $account
- * @param int $amount
- * @param Carbon $date
- *
- * @return bool
- */
- protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
- {
- // get account type:
- $initialBalanceAT = \AccountType::where('description', 'Initial balance account')->first();
-
- // create new account:
- $initial = new \Account;
- $initial->accountType()->associate($initialBalanceAT);
- $initial->user()->associate(\Auth::user());
- $initial->name = $account->name . ' initial balance';
- $initial->active = 0;
- if ($initial->validate()) {
- $initial->save();
- // create new transaction journal (and transactions):
- /** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
- $transactionJournal = \App::make(
- 'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
- );
-
- $transactionJournal->createSimpleJournal(
- $initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
- );
- return true;
- }
- return false;
- }
-
/**
* @param $data
*
@@ -242,37 +247,44 @@ class EloquentAccountRepository implements AccountRepositoryInterface
$journal->save();
}
}
- return $account;
- }
- public function destroy($accountId) {
- $account = $this->find($accountId);
- if($account) {
- $account->delete();
- return true;
- }
- return false;
+ return $account;
}
/**
- * @param $accountId
+ * @param \Account $account
+ * @param int $amount
+ * @param Carbon $date
*
- * @return mixed
+ * @return bool
*/
- public function find($accountId)
+ protected function _createInitialBalance(\Account $account, $amount = 0, Carbon $date)
{
- return \Auth::user()->accounts()->where('id', $accountId)->first();
- }
+ // get account type:
+ $initialBalanceAT = \AccountType::where('description', 'Initial balance account')->first();
- /**
- * @return mixed
- */
- public function getCashAccount()
- {
- $type = \AccountType::where('description', 'Cash account')->first();
- $cash = \Auth::user()->accounts()->where('account_type_id', $type->id)->first();
- return $cash;
+ // create new account:
+ $initial = new \Account;
+ $initial->accountType()->associate($initialBalanceAT);
+ $initial->user()->associate(\Auth::user());
+ $initial->name = $account->name . ' initial balance';
+ $initial->active = 0;
+ if ($initial->validate()) {
+ $initial->save();
+ // create new transaction journal (and transactions):
+ /** @var \Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface $transactionJournal */
+ $transactionJournal = \App::make(
+ 'Firefly\Storage\TransactionJournal\TransactionJournalRepositoryInterface'
+ );
+ $transactionJournal->createSimpleJournal(
+ $initial, $account, 'Initial Balance for ' . $account->name, $amount, $date
+ );
+
+ return true;
+ }
+
+ return false;
}
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php
index 0d4fc68d39..9decd1ca5b 100644
--- a/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php
+++ b/app/lib/Firefly/Storage/Budget/BudgetRepositoryInterface.php
@@ -23,6 +23,7 @@ interface BudgetRepositoryInterface
/**
* @param $data
+ *
* @return mixed
*/
public function update($data);
@@ -36,6 +37,7 @@ interface BudgetRepositoryInterface
/**
* @param $data
+ *
* @return mixed
*/
public function destroy($data);
diff --git a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php
index 9b245d1ad3..135f6444dc 100644
--- a/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php
+++ b/app/lib/Firefly/Storage/Budget/EloquentBudgetRepository.php
@@ -29,7 +29,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
public function get()
{
$set = \Auth::user()->budgets()->with(
- ['limits' => function ($q) {
+ ['limits' => function ($q) {
$q->orderBy('limits.startdate', 'ASC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate', 'ASC');
@@ -48,9 +48,11 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
/**
* @param $data
+ *
* @return mixed
*/
- public function update($data) {
+ public function update($data)
+ {
$budget = $this->find($data['id']);
if ($budget) {
// update account accordingly:
@@ -59,15 +61,19 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
$budget->save();
}
}
+
return $budget;
}
- public function destroy($budgetId) {
+ public function destroy($budgetId)
+ {
$budget = $this->find($budgetId);
- if($budget) {
+ if ($budget) {
$budget->delete();
+
return true;
}
+
return false;
}
@@ -96,41 +102,8 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
public function getWithRepetitionsInPeriod(Carbon $date, $range)
{
- $set = \Auth::user()->budgets()->with(
- ['limits' => function ($q) use ($date) {
- $q->orderBy('limits.startdate', 'ASC');
- }, '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) {
- /** @var $rep \LimitRepetition */
- foreach ($limit->limitrepetitions as $rep) {
- $rep->left = $rep->left();
- // overspent:
- if ($rep->left < 0) {
- $rep->spent = ($rep->left * -1) + $rep->amount;
- $rep->overspent = $rep->left * -1;
- $total = $rep->spent + $rep->overspent;
- $rep->spent_pct = round(($rep->spent / $total) * 100);
- $rep->overspent_pct = 100 - $rep->spent_pct;
- } else {
- $rep->spent = $rep->amount - $rep->left;
- $rep->spent_pct = round(($rep->spent / $rep->amount) * 100);
- $rep->left_pct = 100 - $rep->spent_pct;
-
-
- }
- }
- $budget->count += count($limit->limitrepetitions);
- }
- }
-
- return $set;
+ //return $set;
}
/**
@@ -184,6 +157,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
if ($budget->validate()) {
$budget->save();
}
+
return $budget;
}
diff --git a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php
index 5fdd144f39..f432d58d0e 100644
--- a/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php
+++ b/app/lib/Firefly/Storage/Category/EloquentCategoryRepository.php
@@ -28,6 +28,7 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
if (!$category) {
return $this->store($name);
}
+
return $category;
@@ -40,9 +41,10 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
*/
public function findByName($name)
{
- if($name == '') {
+ if ($name == '') {
return null;
}
+
return \Auth::user()->categories()->where('name', 'LIKE', '%' . $name . '%')->first();
}
@@ -58,6 +60,7 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
$category->name = $name;
$category->user()->associate(\Auth::user());
$category->save();
+
return $category;
}
diff --git a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php
index 96bcc3d607..bb90022b55 100644
--- a/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php
+++ b/app/lib/Firefly/Storage/Limit/EloquentLimitRepository.php
@@ -37,6 +37,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
$budget = \Budget::find($data['budget_id']);
if (is_null($budget)) {
\Session::flash('error', 'No such budget.');
+
return new \Limit;
}
// set the date to the correct start period:
@@ -76,6 +77,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
)->count();
if ($count > 0) {
\Session::flash('error', 'There already is an entry for these parameters.');
+
return new \Limit;
}
// create new limit:
@@ -88,6 +90,7 @@ class EloquentLimitRepository implements LimitRepositoryInterface
if (!$limit->save()) {
Session::flash('error', 'Could not save: ' . $limit->errors()->first());
}
+
return $limit;
}
diff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
index 0b9d0ad1b8..5e37cf683e 100644
--- a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
+++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
@@ -171,6 +171,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$journal->completed = true;
$journal->save();
+
return $journal;
}
@@ -209,6 +210,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
->orderBy('transaction_journals.id', 'DESC')
->take($count)
->get(['transaction_journals.*']);
+
return $query;
}
@@ -234,6 +236,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
->orderBy('transaction_journals.id', 'DESC')
->take($count)
->paginate($count);
+
return $query;
}
@@ -245,26 +248,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
*/
public function getByDateRange(Carbon $start, Carbon $end)
{
- // lets make this simple.
- $types = [];
- foreach (\TransactionType::whereIn('type', ['Withdrawal'])->get() as $t) {
- $types[] = $t->id;
- }
- unset($t);
-
- // get all journals, partly filtered:
- $journals = \TransactionJournal::
- with(
- ['components', 'transactions' => function ($q) {
- $q->where('amount', '>', 0);
- }]
- )
- ->after($start)->before($end)
- ->where('completed', 1)
- ->whereIn('transaction_type_id', $types)
- ->get(['transaction_journals.*']);
- unset($types);
- return $journals;
+ die('no impl');
}
/**
@@ -292,6 +276,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
+
return $query;
}
diff --git a/app/lib/Firefly/Storage/User/EloquentUserRepository.php b/app/lib/Firefly/Storage/User/EloquentUserRepository.php
index a63f4046e3..30fac28df2 100644
--- a/app/lib/Firefly/Storage/User/EloquentUserRepository.php
+++ b/app/lib/Firefly/Storage/User/EloquentUserRepository.php
@@ -33,9 +33,11 @@ class EloquentUserRepository implements UserRepositoryInterface
if (!$user->save()) {
\Log::error('Invalid user');
\Session::flash('error', 'Input invalid, please try again: ' . $user->errors()->first());
+
return false;
}
$user->save();
+
return $user;
}
@@ -51,6 +53,7 @@ class EloquentUserRepository implements UserRepositoryInterface
if (\Hash::check($array['password'], $user->password)) {
}
}
+
return false;
}
@@ -87,6 +90,7 @@ class EloquentUserRepository implements UserRepositoryInterface
$user->password = $password;
/** @noinspection PhpUndefinedMethodInspection */
$user->save();
+
return true;
}