Skip to content

Firefly III: a personal finances manager

Remove some often used long calls with shorter ones.

James Cole6381408fba10
diff --git a/app/controllers/GoogleChartController.php b/app/controllers/GoogleChartController.php
index 1aec3d005c..700b29f575 100644
--- a/app/controllers/GoogleChartController.php
+++ b/app/controllers/GoogleChartController.php
@@ -29,7 +29,7 @@ class GoogleChartController extends BaseController
if ($current > Carbon::now()) {
$row[] = null;
} else {
- $row[] = $account->balance($current);
+ $row[] = Steam::balance($account, $current);
}
$chart->addRowArray($row);
@@ -203,12 +203,7 @@ class GoogleChartController extends BaseController
$row = [clone $current];
foreach ($accounts as $account) {
- //if ($current > Carbon::now()) {
- // $row[] = 0;
- //} else {
- $row[] = $account->balance($current);
- //}
-
+ $row[] = Steam::balance($account, $current);
}
$chart->addRowArray($row);
@@ -573,8 +568,7 @@ class GoogleChartController extends BaseController
/*
* Get end of period for $current:
*/
- $currentEnd = clone $current;
- DateKit::endOfPeriod($currentEnd, $entry->repeat_freq);
+ $currentEnd = DateKit::endOfPeriod($current, $entry->repeat_freq);
/*
* In the current session range?
@@ -599,7 +593,7 @@ class GoogleChartController extends BaseController
/*
* Add some time for the next loop!
*/
- DateKit::addPeriod($current, $entry->repeat_freq, intval($entry->skip));
+ $current = DateKit::addPeriod($current, $entry->repeat_freq, intval($entry->skip));
}
diff --git a/app/controllers/PiggybankController.php b/app/controllers/PiggybankController.php
index 15bc390f62..08ba47d26a 100644
--- a/app/controllers/PiggybankController.php
+++ b/app/controllers/PiggybankController.php
@@ -137,7 +137,7 @@ class PiggybankController extends BaseController
*/
$account = $piggybank->account;
if (!isset($accounts[$account->id])) {
- $accounts[$account->id] = ['name' => $account->name, 'balance' => $account->balance(),
+ $accounts[$account->id] = ['name' => $account->name, 'balance' => Steam::balance($account),
'leftForPiggybanks' => $repos->leftOnAccount($account), 'sumOfSaved' => $piggybank->savedSoFar,
'sumOfTargets' => floatval($piggybank->targetamount), 'leftToSave' => $piggybank->leftToSave];
} else {
diff --git a/app/lib/FireflyIII/Database/Piggybank.php b/app/lib/FireflyIII/Database/Piggybank.php
index a05bcbe7cd..7534796a7d 100644
--- a/app/lib/FireflyIII/Database/Piggybank.php
+++ b/app/lib/FireflyIII/Database/Piggybank.php
@@ -264,7 +264,7 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
*/
public function leftOnAccount(\Account $account)
{
- $balance = $account->balance();
+ $balance = Steam::balance($account);
/** @var \Piggybank $p */
foreach ($account->piggybanks()->get() as $p) {
$balance -= $p->currentRelevantRep()->currentamount;
diff --git a/app/lib/FireflyIII/FF3ServiceProvider.php b/app/lib/FireflyIII/FF3ServiceProvider.php
index 65231c2352..5069de9eae 100644
--- a/app/lib/FireflyIII/FF3ServiceProvider.php
+++ b/app/lib/FireflyIII/FF3ServiceProvider.php
@@ -66,6 +66,15 @@ class FF3ServiceProvider extends ServiceProvider
}
);
+ /*
+ * For models, various stuff:
+ */
+ $this->app->bind(
+ 'steam', function() {
+ return new \FireflyIII\Shared\Toolkit\Steam;
+ }
+ );
+
// preferences:
$this->app->bind('FireflyIII\Shared\Preferences\PreferencesInterface', 'FireflyIII\Shared\Preferences\Preferences');
@@ -81,6 +90,7 @@ class FF3ServiceProvider extends ServiceProvider
$loader->alias('DateKit', 'FireflyIII\Shared\Facade\DateKit');
$loader->alias('Navigation', 'FireflyIII\Shared\Facade\Navigation');
$loader->alias('FFForm', 'FireflyIII\Shared\Facade\FFForm');
+ $loader->alias('Steam', 'FireflyIII\Shared\Facade\Steam');
}
);
diff --git a/app/lib/FireflyIII/Shared/Facade/Steam.php b/app/lib/FireflyIII/Shared/Facade/Steam.php
new file mode 100644
index 0000000000..7b21a65619
--- /dev/null
+++ b/app/lib/FireflyIII/Shared/Facade/Steam.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace FireflyIII\Shared\Facade;
+
+use Illuminate\Support\Facades\Facade;
+
+class Steam extends Facade
+{
+
+
+ protected static function getFacadeAccessor()
+ {
+ return 'steam';
+ }
+
+}
\ No newline at end of file
diff --git a/app/lib/FireflyIII/Shared/Toolkit/Steam.php b/app/lib/FireflyIII/Shared/Toolkit/Steam.php
new file mode 100644
index 0000000000..50b06fce46
--- /dev/null
+++ b/app/lib/FireflyIII/Shared/Toolkit/Steam.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace FireflyIII\Shared\Toolkit;
+use Carbon\Carbon;
+
+/**
+ *
+ * Steam is a special class used for those small often occurring things you need your application to do.
+ *
+ * Class Steam
+ *
+ * @package FireflyIII\Shared\Toolkit
+ */
+class Steam
+{
+
+ /**
+ * @param \Account $account
+ * @param Carbon $date
+ *
+ * @return float
+ */
+ public function balance(\Account $account, Carbon $date = null)
+ {
+ $date = is_null($date) ? Carbon::now() : $date;
+
+ return floatval(
+ $account->transactions()->leftJoin(
+ 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
+ )->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
+ );
+ }
+
+}
\ No newline at end of file
diff --git a/app/models/Account.php b/app/models/Account.php
index e20e8f58ce..ad6f210b67 100644
--- a/app/models/Account.php
+++ b/app/models/Account.php
@@ -57,26 +57,6 @@ class Account extends Ardent
return $this->belongsTo('AccountType');
}
- /**
- * Get an accounts current balance.
- *
- * TODO remove this method in favour of something in the FireflyIII libraries.
- *
- * @param \Carbon\Carbon $date
- *
- * @return float
- */
- public function balance(\Carbon\Carbon $date = null)
- {
- $date = is_null($date) ? new \Carbon\Carbon : $date;
-
- return floatval(
- $this->transactions()->leftJoin(
- 'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
- )->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
- );
- }
-
/**
* Transactions.
*
diff --git a/app/views/list/accounts.blade.php b/app/views/list/accounts.blade.php
index 263a88aa32..a803498da6 100644
--- a/app/views/list/accounts.blade.php
+++ b/app/views/list/accounts.blade.php
@@ -15,7 +15,7 @@
</div>
</td>
<td><a href="{{route('accounts.show',$account->id)}}">{{{$account->name}}}</a></td>
- <td>{{mf($account->balance())}}</td>
+ <td>{{mf(Steam::balance($account))}}</td>
<td>
@if($account->active)
<i class="fa fa-fw fa-check"></i>