firefly-iii/app/Support/Steam.php

365 lines
11 KiB
PHP
Raw Normal View History

<?php
/**
* Steam.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types=1);
namespace FireflyIII\Support;
use Carbon\Carbon;
2017-02-19 00:34:39 -06:00
use Crypt;
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;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Collection;
/**
* Class Steam
*
* @package FireflyIII\Support
*/
class Steam
{
/**
*
2015-05-05 03:23:01 -05:00
* @param \FireflyIII\Models\Account $account
* @param \Carbon\Carbon $date
*
* @return string
*/
2016-04-27 12:23:24 -05:00
public function balance(Account $account, Carbon $date): string
{
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);
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2015-06-03 11:22:47 -05:00
}
$currencyId = intval($account->getMeta('currency_id'));
2017-06-24 06:04:41 -05:00
// use system default currency:
if ($currencyId === 0) {
2017-10-05 04:49:06 -05:00
$currency = app('amount')->getDefaultCurrency();
$currencyId = $currency->id;
}
// first part: get all balances in own currency:
$nativeBalance = strval(
$account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
->where('transactions.transaction_currency_id', $currencyId)
->sum('transactions.amount')
);
2015-06-03 11:22:47 -05:00
// get all balances in foreign currency:
$foreignBalance = strval(
$account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
->where('transactions.foreign_currency_id', $currencyId)
->sum('transactions.foreign_amount')
);
$balance = bcadd($nativeBalance, $foreignBalance);
$virtual = is_null($account->virtual_balance) ? '0' : strval($account->virtual_balance);
$balance = bcadd($balance, $virtual);
2016-04-27 12:21:47 -05:00
$cache->store($balance);
return $balance;
}
/**
*
* @param \FireflyIII\Models\Account $account
* @param \Carbon\Carbon $date
*
* @return string
*/
2016-04-27 12:23:24 -05:00
public function balanceIgnoreVirtual(Account $account, Carbon $date): string
2016-04-27 12:21:47 -05:00
{
2016-04-27 12:21:47 -05:00
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty('balance-no-virtual');
$cache->addProperty($date);
if ($cache->has()) {
2017-03-04 04:19:44 -06:00
return $cache->get(); // @codeCoverageIgnore
2015-04-01 12:45:13 -05:00
}
$currencyId = intval($account->getMeta('currency_id'));
$nativeBalance = strval(
$account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
->where('transactions.transaction_currency_id', $currencyId)
->sum('transactions.amount')
);
2016-04-27 12:21:47 -05:00
// get all balances in foreign currency:
$foreignBalance = strval(
$account->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
->where('transactions.foreign_currency_id', $currencyId)
->sum('transactions.foreign_amount')
2016-04-27 12:21:47 -05:00
);
$balance = bcadd($nativeBalance, $foreignBalance);
2016-04-27 12:21:47 -05:00
$cache->store($balance);
return $balance;
}
/**
* 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
*/
2016-04-05 15:00:03 -05:00
public function balanceInRange(Account $account, Carbon $start, Carbon $end): array
{
// abuse chart properties:
$cache = new CacheProperties;
$cache->addProperty($account->id);
$cache->addProperty('balance-in-range');
$cache->addProperty($start);
$cache->addProperty($end);
if ($cache->has()) {
2017-06-07 00:38:58 -05:00
return $cache->get(); // @codeCoverageIgnore
}
$start->subDay();
$end->addDay();
$balances = [];
$formatted = $start->format('Y-m-d');
$startBalance = $this->balance($account, $start);
$balances[$formatted] = $startBalance;
$currencyId = intval($account->getMeta('currency_id'));
$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')
->groupBy('transactions.transaction_currency_id')
->groupBy('transactions.foreign_currency_id')
->orderBy('transaction_journals.date', 'ASC')
->whereNull('transaction_journals.deleted_at')
->get(
[
'transaction_journals.date',
'transactions.transaction_currency_id',
DB::raw('SUM(transactions.amount) AS modified'),
'transactions.foreign_currency_id',
DB::raw('SUM(transactions.foreign_amount) AS modified_foreign'),
]
);
$currentBalance = $startBalance;
/** @var Transaction $entry */
foreach ($set as $entry) {
// normal amount and foreign amount
$modified = is_null($entry->modified) ? '0' : strval($entry->modified);
$foreignModified = is_null($entry->modified_foreign) ? '0' : strval($entry->modified_foreign);
$amount = '0';
2017-08-10 13:48:29 -05:00
if ($currencyId === $entry->transaction_currency_id || $currencyId === 0) {
// use normal amount:
$amount = $modified;
}
if ($currencyId === $entry->foreign_currency_id) {
2017-08-10 13:48:29 -05:00
// use foreign amount:
$amount = $foreignModified;
}
$currentBalance = bcadd($currentBalance, $amount);
$carbon = new Carbon($entry->date);
$date = $carbon->format('Y-m-d');
$balances[$date] = $currentBalance;
}
$cache->store($balances);
return $balances;
}
2015-07-10 00:39:59 -05:00
/**
2016-02-18 03:04:53 -06:00
* This method always ignores the virtual balance.
2015-07-10 00:39:59 -05:00
*
* @param \Illuminate\Support\Collection $accounts
* @param \Carbon\Carbon $date
2015-07-10 00:39:59 -05:00
*
2016-01-02 09:57:31 -06:00
* @return array
2015-07-10 00:39:59 -05:00
*/
public function balancesByAccounts(Collection $accounts, Carbon $date): array
2015-07-10 00:39:59 -05:00
{
$ids = $accounts->pluck('id')->toArray();
// cache this property.
2015-07-10 00:39:59 -05:00
$cache = new CacheProperties;
$cache->addProperty($ids);
$cache->addProperty('balances');
$cache->addProperty($date);
if ($cache->has()) {
2017-06-07 00:38:58 -05:00
return $cache->get(); // @codeCoverageIgnore
2015-07-10 00:39:59 -05:00
}
// need to do this per account.
2015-07-10 00:39:59 -05:00
$result = [];
/** @var Account $account */
foreach ($accounts as $account) {
$result[$account->id] = $this->balance($account, $date);
2015-07-10 00:39:59 -05:00
}
$cache->store($result);
return $result;
}
2017-02-19 00:34:39 -06:00
/**
* @param int $isEncrypted
* @param $value
*
* @return string
*/
public function decrypt(int $isEncrypted, string $value)
{
if ($isEncrypted === 1) {
return Crypt::decrypt($value);
}
return $value;
}
2016-01-20 08:23:36 -06:00
/**
* @param array $accounts
*
* @return array
*/
2016-04-05 15:00:03 -05:00
public function getLastActivities(array $accounts): array
2016-01-20 08:23:36 -06:00
{
$list = [];
2016-09-16 05:15:58 -05:00
$set = auth()->user()->transactions()
->whereIn('transactions.account_id', $accounts)
->groupBy(['transactions.account_id', 'transaction_journals.user_id'])
2016-10-05 22:26:38 -05:00
->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) AS max_date')]);
2016-01-20 08:23:36 -06:00
foreach ($set as $entry) {
$list[intval($entry->account_id)] = new Carbon($entry->max_date);
}
return $list;
}
/**
* @param string $amount
*
* @return string
*/
public function negative(string $amount): string
{
if (bccomp($amount, '0') === 1) {
$amount = bcmul($amount, '-1');
}
return $amount;
}
2017-06-20 14:04:25 -05:00
/**
* @param string $amount
*
* @return string
*/
public function opposite(string $amount): string
{
$amount = bcmul($amount, '-1');
return $amount;
}
2015-07-19 07:30:20 -05:00
/**
* @param $string
*
* @return int
*/
2016-04-05 15:00:03 -05:00
public function phpBytes($string): int
2015-07-19 07:30:20 -05:00
{
$string = strtolower($string);
if (!(stripos($string, 'k') === false)) {
2015-07-19 07:30:20 -05:00
// has a K in it, remove the K and multiply by 1024.
$bytes = bcmul(rtrim($string, 'kK'), '1024');
2015-07-19 07:30:20 -05:00
return intval($bytes);
}
if (!(stripos($string, 'm') === false)) {
2015-07-19 07:30:20 -05:00
// has a M in it, remove the M and multiply by 1048576.
$bytes = bcmul(rtrim($string, 'mM'), '1048576');
2015-07-19 07:30:20 -05:00
return intval($bytes);
}
if (!(stripos($string, 'g') === false)) {
// has a G in it, remove the G and multiply by (1024)^3.
$bytes = bcmul(rtrim($string, 'gG'), '1073741824');
return intval($bytes);
}
return intval($string);
2015-07-19 07:30:20 -05:00
}
2017-02-11 02:34:04 -06:00
/**
* @param string $amount
*
* @return string
*/
public function positive(string $amount): string
{
2017-02-11 08:52:55 -06:00
if (bccomp($amount, '0') === -1) {
2017-02-11 02:34:04 -06:00
$amount = bcmul($amount, '-1');
}
return $amount;
}
2017-07-23 01:32:51 -05:00
/**
* @param $value
*
* @return mixed
*/
public function tryDecrypt($value)
{
try {
$value = Crypt::decrypt($value);
} catch (DecryptException $e) {
// do not care.
}
return $value;
}
2015-03-29 01:14:32 -05:00
}