mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Some code cleanup.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace FireflyIII\Support;
|
||||
|
||||
use Amount as Amt;
|
||||
use Eloquent;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Input;
|
||||
@@ -13,6 +14,8 @@ use View;
|
||||
* Class ExpandedForm
|
||||
*
|
||||
* @package FireflyIII\Support
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.TooManyMethods)
|
||||
*/
|
||||
class ExpandedForm
|
||||
{
|
||||
@@ -46,7 +49,7 @@ class ExpandedForm
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function label($name, $options)
|
||||
protected function label($name, $options)
|
||||
{
|
||||
if (isset($options['label'])) {
|
||||
return $options['label'];
|
||||
@@ -63,7 +66,7 @@ class ExpandedForm
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function expandOptionArray($name, $label, array $options)
|
||||
protected function expandOptionArray($name, $label, array $options)
|
||||
{
|
||||
$options['class'] = 'form-control';
|
||||
$options['id'] = 'ffInput_' . $name;
|
||||
@@ -78,7 +81,7 @@ class ExpandedForm
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getHolderClasses($name)
|
||||
protected function getHolderClasses($name)
|
||||
{
|
||||
/*
|
||||
* Get errors from session:
|
||||
@@ -100,7 +103,7 @@ class ExpandedForm
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function fillFieldValue($name, $value)
|
||||
protected function fillFieldValue($name, $value)
|
||||
{
|
||||
if (Session::has('preFilled')) {
|
||||
$preFilled = Session::get('preFilled');
|
||||
|
@@ -140,117 +140,6 @@ class Navigation
|
||||
return $currentEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $range
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Carbon
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function jumpToNext($range, Carbon $date)
|
||||
{
|
||||
switch ($range) {
|
||||
case '1D':
|
||||
$date->endOfDay()->addDay();
|
||||
break;
|
||||
case '1W':
|
||||
$date->endOfWeek()->addDay()->startOfWeek();
|
||||
break;
|
||||
case '1M':
|
||||
$date->endOfMonth()->addDay()->startOfMonth();
|
||||
break;
|
||||
case '3M':
|
||||
$date->lastOfQuarter()->addDay();
|
||||
break;
|
||||
case '6M':
|
||||
if ($date->month >= 7) {
|
||||
$date->startOfYear()->addYear();
|
||||
} else {
|
||||
$date->startOfYear()->addMonths(6);
|
||||
}
|
||||
break;
|
||||
case '1Y':
|
||||
$date->startOfYear()->addYear();
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException('Cannot do _next() on ' . $range);
|
||||
break;
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $range
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Carbon
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function jumpToPrevious($range, Carbon $date)
|
||||
{
|
||||
$functionMap = [
|
||||
'1D' => 'Day',
|
||||
'1W' => 'Week',
|
||||
'1M' => 'Month',
|
||||
'1Y' => 'Year'
|
||||
];
|
||||
|
||||
if (isset($functionMap[$range])) {
|
||||
$startFunction = 'startOf' . $functionMap[$range];
|
||||
$subFunction = 'sub' . $functionMap[$range];
|
||||
$date->$startFunction()->$subFunction();
|
||||
|
||||
return $date;
|
||||
}
|
||||
if ($range == '3M') {
|
||||
$date->firstOfQuarter()->subMonths(3)->firstOfQuarter();
|
||||
|
||||
return $date;
|
||||
}
|
||||
if ($range == '6M') {
|
||||
$date->startOfYear();
|
||||
if ($date->month <= 6) {
|
||||
$date->subMonths(6);
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
throw new FireflyException('Cannot do _previous() on ' . $range);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $range
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return string
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function periodName($range, Carbon $date)
|
||||
{
|
||||
$formatMap = [
|
||||
'1D' => 'jS F Y',
|
||||
'1W' => '\w\e\ek W, Y',
|
||||
'1M' => 'F Y',
|
||||
'1Y' => 'Y',
|
||||
];
|
||||
if (isset($formatMap[$range])) {
|
||||
return $date->format($formatMap[$range]);
|
||||
}
|
||||
if ($range == '3M') {
|
||||
|
||||
|
||||
return 'Q' . ceil(($date->month / 12) * 4) . ' ' . $date->year;
|
||||
}
|
||||
if ($range == '6M') {
|
||||
$half = ceil(($date->month / 12) * 2);
|
||||
$halfName = $half == 1 ? 'first' : 'second';
|
||||
|
||||
return $halfName . ' half of ' . $date->year;
|
||||
}
|
||||
throw new FireflyException('No _periodName() for range "' . $range . '"');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $date
|
||||
* @param $repeatFrequency
|
||||
|
@@ -4,7 +4,6 @@ namespace FireflyIII\Support;
|
||||
|
||||
use Auth;
|
||||
use FireflyIII\Models\Preference;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class Preferences
|
||||
|
@@ -6,7 +6,6 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\PiggyBank;
|
||||
use FireflyIII\Models\PiggyBankRepetition;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Class Steam
|
||||
@@ -50,155 +49,4 @@ class Steam
|
||||
return round($balance, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = [
|
||||
'name' => 'Others',
|
||||
'queryAmount' => 0
|
||||
];
|
||||
$return = [];
|
||||
$count = 0;
|
||||
foreach ($array as $id => $entry) {
|
||||
if ($count < ($limit - 1)) {
|
||||
$return[$id] = $entry;
|
||||
} else {
|
||||
$others['queryAmount'] += $entry['queryAmount'];
|
||||
}
|
||||
|
||||
$count++;
|
||||
}
|
||||
$return[0] = $others;
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns a collection into an array. Needs the field 'id' for the key,
|
||||
* and saves only 'name' and 'amount' as a sub array.
|
||||
*
|
||||
* @param \Illuminate\Support\Collection $collection
|
||||
*
|
||||
* @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);
|
||||
$array[$id]['queryAmount'] += floatval($entry->queryAmount);
|
||||
$array[$id]['spent'] += floatval($entry->spent);
|
||||
$array[$id]['encrypted'] = intval($entry->encrypted);
|
||||
} else {
|
||||
$array[$id] = [
|
||||
'amount' => floatval($entry->amount),
|
||||
'queryAmount' => floatval($entry->queryAmount),
|
||||
'spent' => floatval($entry->spent),
|
||||
'encrypted' => intval($entry->encrypted),
|
||||
'name' => $entry->name
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
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])) {
|
||||
$one[$id]['queryAmount'] += $value['queryAmount'];
|
||||
$one[$id]['spent'] += $value['spent'];
|
||||
} else {
|
||||
$one[$id] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $one;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if ($left['queryAmount'] == $right['queryAmount']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($left['queryAmount'] < $right['queryAmount']) ? 1 : -1;
|
||||
}
|
||||
);
|
||||
|
||||
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) {
|
||||
if ($left['queryAmount'] == $right['queryAmount']) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($left['queryAmount'] < $right['queryAmount']) ? -1 : 1;
|
||||
}
|
||||
);
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user