mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-16 22:58:09 +00:00
Refactor code to traits.
This commit is contained in:
@@ -476,154 +476,4 @@ class BudgetController extends Controller
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the amount of money budgeted in a period.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getBudgetedInPeriod(Budget $budget, Carbon $start, Carbon $end): array // get data + augment with info
|
||||
{
|
||||
$key = app('navigation')->preferredCarbonFormat($start, $end);
|
||||
$range = app('navigation')->preferredRangeFormat($start, $end);
|
||||
$current = clone $start;
|
||||
$budgeted = [];
|
||||
while ($current < $end) {
|
||||
/** @var Carbon $currentStart */
|
||||
$currentStart = app('navigation')->startOfPeriod($current, $range);
|
||||
/** @var Carbon $currentEnd */
|
||||
$currentEnd = app('navigation')->endOfPeriod($current, $range);
|
||||
$budgetLimits = $this->repository->getBudgetLimits($budget, $currentStart, $currentEnd);
|
||||
$index = $currentStart->format($key);
|
||||
$budgeted[$index] = $budgetLimits->sum('amount');
|
||||
$currentEnd->addDay();
|
||||
$current = clone $currentEnd;
|
||||
}
|
||||
|
||||
return $budgeted;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the expenses for a budget in a date range.
|
||||
*
|
||||
* @param Collection $limits
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
protected function getExpensesForBudget(Collection $limits, Budget $budget, Carbon $start, Carbon $end): array // get data + augment with info
|
||||
{
|
||||
$return = [];
|
||||
if (0 === $limits->count()) {
|
||||
$spent = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end);
|
||||
if (0 !== bccomp($spent, '0')) {
|
||||
$return[$budget->name]['spent'] = bcmul($spent, '-1');
|
||||
$return[$budget->name]['left'] = 0;
|
||||
$return[$budget->name]['overspent'] = 0;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
$rows = $this->spentInPeriodMulti($budget, $limits);
|
||||
foreach ($rows as $name => $row) {
|
||||
if (0 !== bccomp($row['spent'], '0') || 0 !== bccomp($row['left'], '0')) {
|
||||
$return[$name] = $row;
|
||||
}
|
||||
}
|
||||
unset($rows);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Returns an array with the following values:
|
||||
* 0 =>
|
||||
* 'name' => name of budget + repetition
|
||||
* 'left' => left in budget repetition (always zero)
|
||||
* 'overspent' => spent more than budget repetition? (always zero)
|
||||
* 'spent' => actually spent in period for budget
|
||||
* 1 => (etc)
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Collection $limits
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*
|
||||
*/
|
||||
protected function spentInPeriodMulti(Budget $budget, Collection $limits): array // get data + augment with info
|
||||
{
|
||||
$return = [];
|
||||
$format = (string)trans('config.month_and_day');
|
||||
$name = $budget->name;
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
foreach ($limits as $budgetLimit) {
|
||||
$expenses = $this->repository->spentInPeriod(new Collection([$budget]), new Collection, $budgetLimit->start_date, $budgetLimit->end_date);
|
||||
$expenses = app('steam')->positive($expenses);
|
||||
|
||||
if ($limits->count() > 1) {
|
||||
$name = $budget->name . ' ' . trans(
|
||||
'firefly.between_dates',
|
||||
[
|
||||
'start' => $budgetLimit->start_date->formatLocalized($format),
|
||||
'end' => $budgetLimit->end_date->formatLocalized($format),
|
||||
]
|
||||
);
|
||||
}
|
||||
$amount = $budgetLimit->amount;
|
||||
$leftInLimit = bcsub($amount, $expenses);
|
||||
$hasOverspent = bccomp($leftInLimit, '0') === -1;
|
||||
$left = $hasOverspent ? '0' : bcsub($amount, $expenses);
|
||||
$spent = $hasOverspent ? $amount : $expenses;
|
||||
$overspent = $hasOverspent ? app('steam')->positive($leftInLimit) : '0';
|
||||
|
||||
$return[$name] = [
|
||||
'left' => $left,
|
||||
'overspent' => $overspent,
|
||||
'spent' => $spent,
|
||||
];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the following values:
|
||||
* 'name' => "no budget" in local language
|
||||
* 'repetition_left' => left in budget repetition (always zero)
|
||||
* 'repetition_overspent' => spent more than budget repetition? (always zero)
|
||||
* 'spent' => actually spent in period for budget.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function spentInPeriodWithout(Carbon $start, Carbon $end): string // get data + augment with info
|
||||
{
|
||||
// collector
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
$types = [TransactionType::WITHDRAWAL];
|
||||
$collector->setAllAssetAccounts()->setTypes($types)->setRange($start, $end)->withoutBudget();
|
||||
$transactions = $collector->getTransactions();
|
||||
$sum = '0';
|
||||
/** @var Transaction $entry */
|
||||
foreach ($transactions as $entry) {
|
||||
$sum = bcadd($entry->transaction_amount, $sum);
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use FireflyIII\Support\Http\Controllers\AugumentData;
|
||||
use FireflyIII\Support\Http\Controllers\ChartGeneration;
|
||||
use FireflyIII\Support\Http\Controllers\DateCalculation;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -43,7 +44,7 @@ use Log;
|
||||
*/
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
use DateCalculation, AugumentData;
|
||||
use DateCalculation, AugumentData, ChartGeneration;
|
||||
/** @var GeneratorInterface Chart generation methods. */
|
||||
protected $generator;
|
||||
|
||||
@@ -393,91 +394,4 @@ class CategoryController extends Controller
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Chart for a specific period (start and end).
|
||||
*
|
||||
*
|
||||
* @param Category $category
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
protected function makePeriodChart(Category $category, Carbon $start, Carbon $end): array // chart helper method.
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($end);
|
||||
$cache->addProperty($category->id);
|
||||
$cache->addProperty('chart.category.period-chart');
|
||||
|
||||
|
||||
if ($cache->has()) {
|
||||
//return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
||||
$repository = app(CategoryRepositoryInterface::class);
|
||||
|
||||
// chart data
|
||||
$chartData = [
|
||||
[
|
||||
'label' => (string)trans('firefly.spent'),
|
||||
'entries' => [],
|
||||
'type' => 'bar',
|
||||
'backgroundColor' => 'rgba(219, 68, 55, 0.5)', // red
|
||||
],
|
||||
[
|
||||
'label' => (string)trans('firefly.earned'),
|
||||
'entries' => [],
|
||||
'type' => 'bar',
|
||||
'backgroundColor' => 'rgba(0, 141, 76, 0.5)', // green
|
||||
],
|
||||
[
|
||||
'label' => (string)trans('firefly.sum'),
|
||||
'entries' => [],
|
||||
'type' => 'line',
|
||||
'fill' => false,
|
||||
],
|
||||
];
|
||||
|
||||
$step = $this->calculateStep($start, $end);
|
||||
|
||||
|
||||
while ($start <= $end) {
|
||||
$spent = $repository->spentInPeriod(new Collection([$category]), $accounts, $start, $start);
|
||||
$earned = $repository->earnedInPeriod(new Collection([$category]), $accounts, $start, $start);
|
||||
$sum = bcadd($spent, $earned);
|
||||
$label = trim(app('navigation')->periodShow($start, $step));
|
||||
$chartData[0]['entries'][$label] = round(bcmul($spent, '-1'), 12);
|
||||
$chartData[1]['entries'][$label] = round($earned, 12);
|
||||
$chartData[2]['entries'][$label] = round($sum, 12);
|
||||
|
||||
switch ($step) {
|
||||
default:
|
||||
case '1D':
|
||||
$start->addDay();
|
||||
break;
|
||||
case '1W':
|
||||
$start->addDays(7);
|
||||
break;
|
||||
case '1M':
|
||||
$start->addMonth();
|
||||
break;
|
||||
case '1Y':
|
||||
$start->addYear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$data = $this->generator->multiSet($chartData);
|
||||
$cache->store($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ use FireflyIII\Helpers\Report\NetWorthInterface;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Repositories\Account\AccountTaskerInterface;
|
||||
use FireflyIII\Support\CacheProperties;
|
||||
use FireflyIII\Support\Http\Controllers\BasicDataSupport;
|
||||
use FireflyIII\Support\Http\Controllers\ChartGeneration;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
@@ -40,7 +40,7 @@ use Log;
|
||||
*/
|
||||
class ReportController extends Controller
|
||||
{
|
||||
use BasicDataSupport;
|
||||
use BasicDataSupport, ChartGeneration;
|
||||
/** @var GeneratorInterface Chart generation methods. */
|
||||
protected $generator;
|
||||
|
||||
@@ -84,7 +84,7 @@ class ReportController extends Controller
|
||||
// filter accounts on having the preference for being included.
|
||||
/** @var AccountRepositoryInterface $accountRepository */
|
||||
$accountRepository = app(AccountRepositoryInterface::class);
|
||||
$filtered = $accounts->filter(
|
||||
$filtered = $accounts->filter(
|
||||
function (Account $account) use ($accountRepository) {
|
||||
$includeNetWorth = $accountRepository->getMetaValue($account, 'include_net_worth');
|
||||
$result = null === $includeNetWorth ? true : '1' === $includeNetWorth;
|
||||
@@ -97,7 +97,6 @@ class ReportController extends Controller
|
||||
);
|
||||
|
||||
|
||||
|
||||
while ($current < $end) {
|
||||
// get balances by date, grouped by currency.
|
||||
$result = $helper->getNetWorthByCurrency($filtered, $current);
|
||||
@@ -263,67 +262,4 @@ class ReportController extends Controller
|
||||
|
||||
return response()->json($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the incomes and expenses for the given periods, grouped per month. Will cache its results.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
protected function getChartData(Collection $accounts, Carbon $start, Carbon $end): array // chart helper function
|
||||
{
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty('chart.report.get-chart-data');
|
||||
$cache->addProperty($start);
|
||||
$cache->addProperty($accounts);
|
||||
$cache->addProperty($end);
|
||||
if ($cache->has()) {
|
||||
return $cache->get(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$currentStart = clone $start;
|
||||
$spentArray = [];
|
||||
$earnedArray = [];
|
||||
|
||||
/** @var AccountTaskerInterface $tasker */
|
||||
$tasker = app(AccountTaskerInterface::class);
|
||||
|
||||
while ($currentStart <= $end) {
|
||||
$currentEnd = app('navigation')->endOfPeriod($currentStart, '1M');
|
||||
$earned = (string)array_sum(
|
||||
array_map(
|
||||
function ($item) {
|
||||
return $item['sum'];
|
||||
},
|
||||
$tasker->getIncomeReport($currentStart, $currentEnd, $accounts)
|
||||
)
|
||||
);
|
||||
|
||||
$spent = (string)array_sum(
|
||||
array_map(
|
||||
function ($item) {
|
||||
return $item['sum'];
|
||||
},
|
||||
$tasker->getExpenseReport($currentStart, $currentEnd, $accounts)
|
||||
)
|
||||
);
|
||||
|
||||
$label = $currentStart->format('Y-m') . '-01';
|
||||
$spentArray[$label] = bcmul($spent, '-1');
|
||||
$earnedArray[$label] = $earned;
|
||||
$currentStart = app('navigation')->addPeriod($currentStart, '1M', 0);
|
||||
}
|
||||
$result = [
|
||||
'spent' => $spentArray,
|
||||
'earned' => $earnedArray,
|
||||
];
|
||||
$cache->store($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user