mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
make sure reports work as expected.
This commit is contained in:
@@ -44,8 +44,6 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
trait AugumentData
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Searches for the opposing account.
|
||||
*
|
||||
@@ -78,8 +76,8 @@ trait AugumentData
|
||||
*
|
||||
* @param Collection $assets
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
@@ -141,8 +139,8 @@ trait AugumentData
|
||||
*
|
||||
* @param Collection $assets
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -219,9 +217,9 @@ trait AugumentData
|
||||
* Returns the budget limits belonging to the given budget and valid on the given day.
|
||||
*
|
||||
* @param Collection $budgetLimits
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -352,9 +350,9 @@ trait AugumentData
|
||||
* Get the expenses for a budget in a date range.
|
||||
*
|
||||
* @param Collection $limits
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
@@ -388,6 +386,63 @@ trait AugumentData
|
||||
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
|
||||
{
|
||||
/** @var BudgetRepositoryInterface $repository */
|
||||
$repository = app(BudgetRepositoryInterface::class);
|
||||
|
||||
$return = [];
|
||||
$format = (string)trans('config.month_and_day');
|
||||
$name = $budget->name;
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
foreach ($limits as $budgetLimit) {
|
||||
$expenses = $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all budget limits for a budget.
|
||||
*
|
||||
@@ -425,25 +480,22 @@ trait AugumentData
|
||||
return $set;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper function that groups expenses.
|
||||
*
|
||||
* @param Collection $set
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function groupByBudget(Collection $set): array // filter + group data
|
||||
protected function groupByBudget(array $array): array // filter + group data
|
||||
{
|
||||
// group by category ID:
|
||||
$grouped = [];
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($set as $transaction) {
|
||||
$jrnlBudId = (int)$transaction->transaction_journal_budget_id;
|
||||
$transBudId = (int)$transaction->transaction_budget_id;
|
||||
$budgetId = max($jrnlBudId, $transBudId);
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
$budgetId = (int)$journal['budget_id'];
|
||||
$grouped[$budgetId] = $grouped[$budgetId] ?? '0';
|
||||
$grouped[$budgetId] = bcadd($transaction->transaction_amount, $grouped[$budgetId]);
|
||||
$grouped[$budgetId] = bcadd($journal['amount'], $grouped[$budgetId]);
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
@@ -452,21 +504,19 @@ trait AugumentData
|
||||
/**
|
||||
* Group transactions by category.
|
||||
*
|
||||
* @param Collection $set
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function groupByCategory(Collection $set): array // filter + group data
|
||||
protected function groupByCategory(array $array): array // filter + group data
|
||||
{
|
||||
// group by category ID:
|
||||
$grouped = [];
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($set as $transaction) {
|
||||
$jrnlCatId = (int)$transaction->transaction_journal_category_id;
|
||||
$transCatId = (int)$transaction->transaction_category_id;
|
||||
$categoryId = max($jrnlCatId, $transCatId);
|
||||
/** @var array $journal */
|
||||
foreach($array as $journal) {
|
||||
$categoryId = (int)$journal['category_id'];
|
||||
$grouped[$categoryId] = $grouped[$categoryId] ?? '0';
|
||||
$grouped[$categoryId] = bcadd($transaction->transaction_amount, $grouped[$categoryId]);
|
||||
$grouped[$categoryId] = bcadd($journal['amount'], $grouped[$categoryId]);
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
@@ -493,6 +543,10 @@ trait AugumentData
|
||||
return $grouped;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
|
||||
/**
|
||||
* Group transactions by tag.
|
||||
*
|
||||
@@ -519,8 +573,6 @@ trait AugumentData
|
||||
return $grouped;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
|
||||
/**
|
||||
@@ -528,8 +580,8 @@ trait AugumentData
|
||||
*
|
||||
* @param Collection $assets
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
@@ -592,8 +644,8 @@ trait AugumentData
|
||||
*
|
||||
* @param Collection $assets
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
@@ -657,8 +709,8 @@ trait AugumentData
|
||||
*
|
||||
* @param Collection $assets
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -698,65 +750,6 @@ trait AugumentData
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
|
||||
/**
|
||||
*
|
||||
* 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
|
||||
{
|
||||
/** @var BudgetRepositoryInterface $repository */
|
||||
$repository = app(BudgetRepositoryInterface::class);
|
||||
|
||||
$return = [];
|
||||
$format = (string)trans('config.month_and_day');
|
||||
$name = $budget->name;
|
||||
/** @var BudgetLimit $budgetLimit */
|
||||
foreach ($limits as $budgetLimit) {
|
||||
$expenses = $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;
|
||||
}
|
||||
|
||||
/** @noinspection MoreThanThreeArgumentsInspection */
|
||||
|
||||
/**
|
||||
* Returns an array with the following values:
|
||||
* 'name' => "no budget" in local language
|
||||
|
@@ -24,11 +24,7 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Support\Http\Controllers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
|
||||
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
|
||||
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
|
||||
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
|
||||
use FireflyIII\Helpers\Filter\TransferFilter;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -43,18 +39,21 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*/
|
||||
protected function getExpensesForOpposing(Collection $accounts, Collection $opposing, Carbon $start, Carbon $end): Collection // get data + augument
|
||||
protected function getExpensesForOpposing(Collection $accounts, Collection $opposing, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setOpposingAccounts($opposing);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)
|
||||
->setRange($start, $end)
|
||||
->setTypes([TransactionType::WITHDRAWAL])
|
||||
->setAccounts($opposing);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,24 +61,21 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*
|
||||
*/
|
||||
protected function getExpensesForTags(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): Collection // get data + augument
|
||||
protected function getExpensesForTags(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setTags($tags)->withOpposingAccount();
|
||||
$collector->removeFilter(TransferFilter::class);
|
||||
->setTags($tags)->withAccountInformation();
|
||||
|
||||
$collector->addFilter(OpposingAccountFilter::class);
|
||||
$collector->addFilter(PositiveAmountFilter::class);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,23 +83,19 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $budgets
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*/
|
||||
protected function getExpensesInBudgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end): Collection // get data + augment with info
|
||||
protected function getExpensesInBudgets(Collection $accounts, Collection $budgets, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setBudgets($budgets)->withOpposingAccount();
|
||||
$collector->removeFilter(TransferFilter::class);
|
||||
->setBudgets($budgets)->withAccountInformation();
|
||||
|
||||
$collector->addFilter(OpposingAccountFilter::class);
|
||||
$collector->addFilter(PositiveAmountFilter::class);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,25 +103,25 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*
|
||||
*
|
||||
*/
|
||||
protected function getExpensesInCategories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Collection // get data + augument
|
||||
protected function getExpensesInCategories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setCategories($categories)->withOpposingAccount();
|
||||
$collector->removeFilter(TransferFilter::class);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector
|
||||
->setAccounts($accounts)
|
||||
->setRange($start, $end)
|
||||
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setCategories($categories)
|
||||
->withAccountInformation();
|
||||
|
||||
$collector->addFilter(OpposingAccountFilter::class);
|
||||
$collector->addFilter(PositiveAmountFilter::class);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,22 +129,19 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $categories
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*/
|
||||
protected function getIncomeForCategories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): Collection // get data + augument
|
||||
protected function getIncomeForCategories(Collection $accounts, Collection $categories, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
||||
->setCategories($categories)->withOpposingAccount();
|
||||
->setCategories($categories)->withAccountInformation();
|
||||
|
||||
$collector->addFilter(OpposingAccountFilter::class);
|
||||
$collector->addFilter(NegativeAmountFilter::class);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,18 +149,19 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $opposing
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
* @return array
|
||||
*/
|
||||
protected function getIncomeForOpposing(Collection $accounts, Collection $opposing, Carbon $start, Carbon $end): Collection // get data + augument
|
||||
protected function getIncomeForOpposing(Collection $accounts, Collection $opposing, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])->setOpposingAccounts($opposing);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT])
|
||||
->setAccounts($opposing);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,24 +169,21 @@ trait TransactionCalculation
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Collection $tags
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
||||
*/
|
||||
protected function getIncomeForTags(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): Collection // get data + augument
|
||||
protected function getIncomeForTags(Collection $accounts, Collection $tags, Carbon $start, Carbon $end): array
|
||||
{
|
||||
/** @var TransactionCollectorInterface $collector */
|
||||
$collector = app(TransactionCollectorInterface::class);
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($accounts)->setRange($start, $end)->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
||||
->setTags($tags)->withOpposingAccount();
|
||||
->setTags($tags)->withAccountInformation();
|
||||
|
||||
$collector->addFilter(OpposingAccountFilter::class);
|
||||
$collector->addFilter(NegativeAmountFilter::class);
|
||||
|
||||
return $collector->getTransactions();
|
||||
return $collector->getExtractedJournals();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user