2014-11-21 19:33:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace FireflyIII\Shared\Toolkit;
|
2014-11-24 23:02:08 +01:00
|
|
|
|
2014-11-21 19:33:09 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2014-12-30 18:44:58 +01:00
|
|
|
\Log::debug('Now in Steam::balance() for account #' . $account->id . ' (' . $account->name . ')');
|
2014-11-25 22:04:50 +01:00
|
|
|
if (is_null($date)) {
|
2014-12-07 15:37:53 +01:00
|
|
|
$key = 'account.' . $account->id . '.latestBalance';
|
|
|
|
|
} else {
|
|
|
|
|
$key = 'account.' . $account->id . '.balanceOn' . $date->format('dmy');
|
|
|
|
|
}
|
|
|
|
|
if (\Cache::has($key)) {
|
2014-12-15 18:03:16 +01:00
|
|
|
// TODO find a way to reliably remove cache entries for accounts.
|
|
|
|
|
#return \Cache::get($key);
|
2014-11-25 22:04:50 +01:00
|
|
|
}
|
2014-12-30 18:44:58 +01:00
|
|
|
$date = is_null($date) ? Carbon::now() : $date;
|
2014-12-23 21:55:08 +01:00
|
|
|
\Log::debug('Now reached the moment we fire the query.');
|
2014-11-25 22:04:50 +01:00
|
|
|
$balance = floatval(
|
2014-11-21 19:33:09 +01:00
|
|
|
$account->transactions()->leftJoin(
|
|
|
|
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
|
|
|
|
)->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
|
|
|
|
|
);
|
2014-12-07 15:37:53 +01:00
|
|
|
\Cache::put($key, $balance, 20160);
|
2014-11-25 22:04:50 +01:00
|
|
|
|
|
|
|
|
return $balance;
|
2014-11-21 19:33:09 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-24 23:02:08 +01:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param \PiggyBank $piggyBank
|
|
|
|
|
* @param \PiggyBankRepetition $repetition
|
2014-11-24 23:02:08 +01:00
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function percentage(\PiggyBank $piggyBank, \PiggyBankRepetition $repetition)
|
2014-11-24 23:02:08 +01:00
|
|
|
{
|
|
|
|
|
$pct = $repetition->currentamount / $piggyBank->targetamount * 100;
|
|
|
|
|
if ($pct > 100) {
|
|
|
|
|
return 100;
|
|
|
|
|
} else {
|
|
|
|
|
return floor($pct);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-14 20:40:02 +01:00
|
|
|
public function removeEmptyBudgetLimits()
|
|
|
|
|
{
|
|
|
|
|
$user = \Auth::user();
|
|
|
|
|
if ($user) {
|
2014-12-20 07:33:59 +01:00
|
|
|
\BudgetLimit::where('amount', 0)->delete();
|
2014-12-14 20:40:02 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-21 19:33:09 +01:00
|
|
|
}
|