2015-02-07 01:23:44 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\Account;
|
2015-02-27 04:02:08 -06:00
|
|
|
use FireflyIII\Models\PiggyBank;
|
|
|
|
use FireflyIII\Models\PiggyBankRepetition;
|
2015-02-23 13:25:48 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-07 01:23:44 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-01 12:45:13 -05:00
|
|
|
public function balance(Account $account, Carbon $date = null, $ignoreVirtualBalance = false)
|
2015-02-07 01:23:44 -06:00
|
|
|
{
|
2015-03-27 13:42:35 -05:00
|
|
|
$date = is_null($date) ? Carbon::now() : $date;
|
|
|
|
|
|
|
|
// find the first known transaction on this account:
|
|
|
|
//
|
|
|
|
$firstDateObject = $account
|
|
|
|
->transactions()
|
|
|
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
->orderBy('transaction_journals.date', 'ASC')->first(['transaction_journals.date']);
|
|
|
|
|
|
|
|
$firstDate = is_null($firstDateObject) ? clone $date : new Carbon($firstDateObject->date);
|
|
|
|
$date = $date < $firstDate ? $firstDate : $date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*select * from transactions
|
|
|
|
* left join transaction_journals ON transaction_journals.id = transactions.transaction_journal_id
|
|
|
|
* order by date ASC
|
|
|
|
*/
|
|
|
|
|
2015-02-07 01:23:44 -06:00
|
|
|
$balance = 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')
|
|
|
|
);
|
2015-04-01 12:45:13 -05:00
|
|
|
if (!$ignoreVirtualBalance) {
|
|
|
|
$balance += floatval($account->virtual_balance);
|
|
|
|
}
|
2015-02-07 01:23:44 -06:00
|
|
|
|
|
|
|
return $balance;
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:42:35 -05:00
|
|
|
/**
|
|
|
|
* Only return the top X entries, group the rest by amount
|
|
|
|
* and described as 'Others'. id = 0 as well
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @param int $limit
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function limitArray(array $array, $limit = 10)
|
|
|
|
{
|
|
|
|
$others = [
|
2015-04-09 14:18:18 -05:00
|
|
|
'name' => 'Others',
|
|
|
|
'queryAmount' => 0
|
2015-03-27 13:42:35 -05:00
|
|
|
];
|
|
|
|
$return = [];
|
|
|
|
$count = 0;
|
|
|
|
foreach ($array as $id => $entry) {
|
|
|
|
if ($count < ($limit - 1)) {
|
|
|
|
$return[$id] = $entry;
|
|
|
|
} else {
|
2015-04-09 14:18:18 -05:00
|
|
|
$others['queryAmount'] += $entry['queryAmount'];
|
2015-03-27 13:42:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
$return[0] = $others;
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:25:48 -06:00
|
|
|
/**
|
|
|
|
* Turns a collection into an array. Needs the field 'id' for the key,
|
|
|
|
* and saves only 'name' and 'amount' as a sub array.
|
|
|
|
*
|
2015-05-05 03:23:01 -05:00
|
|
|
* @param \Illuminate\Support\Collection $collection
|
2015-02-23 13:25:48 -06:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function makeArray(Collection $collection)
|
|
|
|
{
|
|
|
|
$array = [];
|
|
|
|
foreach ($collection as $entry) {
|
|
|
|
$entry->spent = isset($entry->spent) ? floatval($entry->spent) : 0.0;
|
|
|
|
$id = intval($entry->id);
|
|
|
|
if (isset($array[$id])) {
|
|
|
|
$array[$id]['amount'] += floatval($entry->amount);
|
2015-04-09 14:14:15 -05:00
|
|
|
$array[$id]['queryAmount'] += floatval($entry->queryAmount);
|
2015-02-23 13:25:48 -06:00
|
|
|
$array[$id]['spent'] += floatval($entry->spent);
|
2015-03-31 14:18:22 -05:00
|
|
|
$array[$id]['encrypted'] = intval($entry->encrypted);
|
2015-02-23 13:25:48 -06:00
|
|
|
} else {
|
|
|
|
$array[$id] = [
|
2015-04-09 14:14:15 -05:00
|
|
|
'amount' => floatval($entry->amount),
|
|
|
|
'queryAmount' => floatval($entry->queryAmount),
|
|
|
|
'spent' => floatval($entry->spent),
|
|
|
|
'encrypted' => intval($entry->encrypted),
|
|
|
|
'name' => $entry->name
|
2015-02-23 13:25:48 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges two of the arrays as defined above. Can't handle more (yet)
|
|
|
|
*
|
|
|
|
* @param array $one
|
|
|
|
* @param array $two
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function mergeArrays(array $one, array $two)
|
|
|
|
{
|
|
|
|
foreach ($two as $id => $value) {
|
|
|
|
// $otherId also exists in $one:
|
|
|
|
if (isset($one[$id])) {
|
2015-04-09 14:14:15 -05:00
|
|
|
$one[$id]['queryAmount'] += $value['queryAmount'];
|
2015-02-23 13:25:48 -06:00
|
|
|
$one[$id]['spent'] += $value['spent'];
|
|
|
|
} else {
|
|
|
|
$one[$id] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $one;
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:42:35 -05:00
|
|
|
/**
|
|
|
|
* @param PiggyBank $piggyBank
|
|
|
|
* @param PiggyBankRepetition $repetition
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function percentage(PiggyBank $piggyBank, PiggyBankRepetition $repetition)
|
|
|
|
{
|
|
|
|
$pct = $repetition->currentamount / $piggyBank->targetamount * 100;
|
|
|
|
if ($pct > 100) {
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
return 100;
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
} else {
|
|
|
|
return floor($pct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:25:48 -06:00
|
|
|
/**
|
|
|
|
* Sort an array where all 'amount' keys are positive floats.
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function sortArray(array $array)
|
|
|
|
{
|
|
|
|
uasort(
|
|
|
|
$array, function ($left, $right) {
|
2015-04-09 14:14:15 -05:00
|
|
|
if ($left['queryAmount'] == $right['queryAmount']) {
|
2015-02-23 13:25:48 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-09 14:14:15 -05:00
|
|
|
return ($left['queryAmount'] < $right['queryAmount']) ? 1 : -1;
|
2015-02-23 13:25:48 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort an array where all 'amount' keys are negative floats.
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function sortNegativeArray(array $array)
|
|
|
|
{
|
|
|
|
uasort(
|
|
|
|
$array, function ($left, $right) {
|
2015-04-09 14:14:15 -05:00
|
|
|
if ($left['queryAmount'] == $right['queryAmount']) {
|
2015-02-23 13:25:48 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-09 14:14:15 -05:00
|
|
|
return ($left['queryAmount'] < $right['queryAmount']) ? -1 : 1;
|
2015-02-23 13:25:48 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|