firefly-iii/app/Support/Steam.php

198 lines
5.6 KiB
PHP
Raw Normal View History

<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
namespace FireflyIII\Support;
2015-07-26 12:07:02 -05:00
use Auth;
use Carbon\Carbon;
2015-07-10 00:39:59 -05:00
use DB;
use FireflyIII\Models\Account;
2015-07-10 00:39:59 -05:00
use FireflyIII\Models\Transaction;
/**
* Class Steam
*
* @package FireflyIII\Support
*/
class Steam
{
2015-07-26 12:07:02 -05:00
/**
*
2015-05-05 03:23:01 -05:00
* @param \FireflyIII\Models\Account $account
* @param \Carbon\Carbon $date
* @param bool $ignoreVirtualBalance
*
* @return float
*/
2015-05-17 02:35:49 -05:00
public function balance(Account $account, Carbon $date, $ignoreVirtualBalance = false)
{
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(
'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-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-05-23 10:38:16 -05:00
return round($balance, 2);
}
/**
* Gets the balance for the given account during the whole range, using this format:
*
* [yyyy-mm-dd] => 123,2
2015-12-29 15:48:55 -06:00
*
2016-01-01 14:49:27 -06:00
* @param \FireflyIII\Models\Account $account
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @return array
*/
public function balanceInRange(Account $account, Carbon $start, Carbon $end)
{
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty('balance-in-range');
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
$balances = [];
$start->subDay();
$end->addDay();
$startBalance = $this->balance($account, $start);
$balances[$start->format('Y-m-d')] = $startBalance;
$start->addDay();
// query!
$set = $account->transactions()
->leftJoin('transaction_journals', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
->groupBy('transaction_journals.date')
->get(['transaction_journals.date', DB::Raw('SUM(`transactions`.`amount`) as `modified`')]);
$currentBalance = $startBalance;
foreach ($set as $entry) {
$currentBalance = bcadd($currentBalance, $entry->modified);
$balances[$entry->date] = $currentBalance;
}
$cache->store($balances);
return $balances;
}
2015-07-10 00:39:59 -05:00
/**
*
* @param array $ids
* @param \Carbon\Carbon $date
*
2016-01-02 09:57:31 -06:00
* @return array
2015-07-10 00:39:59 -05:00
*/
public function balancesById(array $ids, Carbon $date)
{
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($ids);
$cache->addProperty('balances');
$cache->addProperty($date);
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
}
bcscale(2);
$balances = Transaction::
leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
->groupBy('transactions.account_id')
->whereIn('transactions.account_id', $ids)
->get(['transactions.account_id', DB::Raw('sum(`transactions`.`amount`) as aggregate')]);
$result = [];
foreach ($balances as $entry) {
$accountId = intval($entry->account_id);
$balance = round($entry->aggregate, 2);
$result[$accountId] = $balance;
}
$cache->store($result);
return $result;
}
2016-01-20 08:23:36 -06:00
/**
* @param array $accounts
*
* @return array
*/
public function getLastActivities(array $accounts)
{
$list = [];
$set = Auth::user()->transactions()
->whereIn('account_id', $accounts)
->groupBy('account_id')
->get(['transactions.account_id', DB::raw('MAX(`transaction_journals`.`date`) as `max_date`')]);
foreach ($set as $entry) {
$list[intval($entry->account_id)] = new Carbon($entry->max_date);
}
return $list;
}
2015-07-19 07:30:20 -05:00
// parse PHP size:
2016-01-20 08:23:36 -06:00
2015-07-19 07:30:20 -05:00
/**
* @param $string
*
* @return int
*/
2015-07-26 08:51:07 -05:00
public function phpBytes($string)
2015-07-19 07:30:20 -05:00
{
$string = strtolower($string);
if (!(strpos($string, 'k') === false)) {
// has a K in it, remove the K and multiply by 1024.
$bytes = bcmul(rtrim($string, 'k'), 1024);
return intval($bytes);
}
if (!(strpos($string, 'm') === false)) {
// has a M in it, remove the M and multiply by 1048576.
$bytes = bcmul(rtrim($string, 'm'), 1048576);
return intval($bytes);
}
return $string;
}
2015-03-29 01:14:32 -05:00
}