2015-02-07 01:23:44 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Steam
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
|
|
|
class Steam
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
2015-05-05 03:23:01 -05:00
|
|
|
* @param \FireflyIII\Models\Account $account
|
2015-05-05 05:57:27 -05:00
|
|
|
* @param \Carbon\Carbon $date
|
|
|
|
* @param bool $ignoreVirtualBalance
|
2015-02-07 01:23:44 -06:00
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2015-05-17 02:35:49 -05:00
|
|
|
public function balance(Account $account, Carbon $date, $ignoreVirtualBalance = false)
|
2015-02-07 01:23:44 -06:00
|
|
|
{
|
2015-06-03 11:22:47 -05:00
|
|
|
|
|
|
|
// abuse chart properties:
|
2015-06-03 14:25:11 -05:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($account->id);
|
|
|
|
$cache->addProperty('balance');
|
|
|
|
$cache->addProperty($date);
|
|
|
|
$cache->addProperty($ignoreVirtualBalance);
|
|
|
|
if ($cache->has()) {
|
2015-06-04 14:35:36 -05:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2015-06-03 11:22:47 -05:00
|
|
|
}
|
|
|
|
|
2015-05-23 10:38:16 -05:00
|
|
|
bcscale(2);
|
2015-06-14 01:22:02 -05:00
|
|
|
|
2015-06-27 01:06:24 -05:00
|
|
|
$balance = $account->transactions()->leftJoin(
|
2015-05-23 08:42:19 -05:00
|
|
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
2015-06-14 01:22:02 -05:00
|
|
|
)->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount');
|
2015-05-23 08:42:19 -05:00
|
|
|
|
2015-04-01 12:45:13 -05:00
|
|
|
if (!$ignoreVirtualBalance) {
|
2015-05-23 10:38:16 -05:00
|
|
|
$balance = bcadd($balance, $account->virtual_balance);
|
2015-04-01 12:45:13 -05:00
|
|
|
}
|
2015-06-03 14:25:11 -05:00
|
|
|
$cache->store(round($balance, 2));
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2015-05-23 10:38:16 -05:00
|
|
|
return round($balance, 2);
|
2015-02-07 01:23:44 -06:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|