Refactor code to traits.

This commit is contained in:
James Cole
2018-12-31 08:11:57 +01:00
parent e7bcc01fe8
commit f80de95bb0
14 changed files with 972 additions and 922 deletions

View File

@@ -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;
}
}