mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Code cleanup.
This commit is contained in:
@@ -255,18 +255,24 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
{
|
{
|
||||||
$object = new BudgetCollection;
|
$object = new BudgetCollection;
|
||||||
/** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
|
/** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
|
||||||
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
$repository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface');
|
||||||
$set = $repository->getBudgets();
|
$set = $repository->getBudgets();
|
||||||
|
$allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
|
||||||
|
$allTotalSpent = $repository->spentAllPerDayForAccounts($accounts, $start, $end);
|
||||||
bcscale(2);
|
bcscale(2);
|
||||||
|
|
||||||
foreach ($set as $budget) {
|
foreach ($set as $budget) {
|
||||||
|
|
||||||
$repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
|
$repetitions = $allRepetitions->filter(
|
||||||
|
function (LimitRepetition $rep) use ($budget) {
|
||||||
|
return $rep->budget_id == $budget->id;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$totalSpent = isset($allTotalSpent[$budget->id]) ? $allTotalSpent[$budget->id] : [];
|
||||||
|
|
||||||
// no repetition(s) for this budget:
|
// no repetition(s) for this budget:
|
||||||
if ($repetitions->count() == 0) {
|
if ($repetitions->count() == 0) {
|
||||||
$spent = $repository->balanceInPeriod($budget, $start, $end, $accounts);
|
$spent = array_sum($totalSpent);
|
||||||
$budgetLine = new BudgetLine;
|
$budgetLine = new BudgetLine;
|
||||||
$budgetLine->setBudget($budget);
|
$budgetLine->setBudget($budget);
|
||||||
$budgetLine->setOverspent($spent);
|
$budgetLine->setOverspent($spent);
|
||||||
@@ -281,7 +287,7 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
$budgetLine = new BudgetLine;
|
$budgetLine = new BudgetLine;
|
||||||
$budgetLine->setBudget($budget);
|
$budgetLine->setBudget($budget);
|
||||||
$budgetLine->setRepetition($repetition);
|
$budgetLine->setRepetition($repetition);
|
||||||
$expenses = $repository->balanceInPeriod($budget, $start, $end, $accounts);
|
$expenses = $this->getSumOfRange($start, $end, $totalSpent);
|
||||||
|
|
||||||
// 200 en -100 is 100, vergeleken met 0 === 1
|
// 200 en -100 is 100, vergeleken met 0 === 1
|
||||||
// 200 en -200 is 0, vergeleken met 0 === 0
|
// 200 en -200 is 0, vergeleken met 0 === 0
|
||||||
@@ -454,4 +460,32 @@ class ReportHelper implements ReportHelperInterface
|
|||||||
|
|
||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Take the array as returned by SingleCategoryRepositoryInterface::spentPerDay and SingleCategoryRepositoryInterface::earnedByDay
|
||||||
|
* and sum up everything in the array in the given range.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param array $array
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getSumOfRange(Carbon $start, Carbon $end, array $array)
|
||||||
|
{
|
||||||
|
bcscale(2);
|
||||||
|
$sum = '0';
|
||||||
|
$currentStart = clone $start; // to not mess with the original one
|
||||||
|
$currentEnd = clone $end; // to not mess with the original one
|
||||||
|
|
||||||
|
while ($currentStart <= $currentEnd) {
|
||||||
|
$date = $currentStart->format('Y-m-d');
|
||||||
|
if (isset($array[$date])) {
|
||||||
|
$sum = bcadd($sum, $array[$date]);
|
||||||
|
}
|
||||||
|
$currentStart->addDay();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sum;
|
||||||
|
}
|
||||||
}
|
}
|
@@ -200,18 +200,24 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a collection of all the limit repetitions belonging to this $budget.
|
* @param Carbon $start
|
||||||
*
|
* @param Carbon $end
|
||||||
* @param Budget $budget
|
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection
|
||||||
*/
|
*/
|
||||||
public function getBudgetReps(Budget $budget)
|
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
$set = $budget->limitrepetitions()->count();
|
/** @var Collection $repetitions */
|
||||||
var_dump($set);
|
return LimitRepetition::
|
||||||
|
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||||
|
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||||
|
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
|
||||||
|
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||||
|
->where('budgets.user_id', Auth::user()->id)
|
||||||
|
->get(['limit_repetitions.*', 'budget_limits.budget_id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
@@ -264,7 +270,43 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
->where('transactions.amount', '<', 0)
|
->where('transactions.amount', '<', 0)
|
||||||
->before($end)
|
->before($end)
|
||||||
->after($start)
|
->after($start)
|
||||||
->groupBy('date')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||||
|
|
||||||
|
$return = [];
|
||||||
|
foreach ($query->toArray() as $entry) {
|
||||||
|
$return[$entry['dateFormatted']] = $entry['sum'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array with the following key:value pairs:
|
||||||
|
*
|
||||||
|
* yyyy-mm-dd:<amount>
|
||||||
|
*
|
||||||
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $budget
|
||||||
|
* from all the users accounts.
|
||||||
|
*
|
||||||
|
* @param Budget $budget
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function spentPerDayForAccounts(Budget $budget, Collection $accounts, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
|
/** @var Collection $query */
|
||||||
|
$query = $budget->transactionJournals()
|
||||||
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||||
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->whereIn('transactions.account_id', $ids)
|
||||||
|
->where('transactions.amount', '<', 0)
|
||||||
|
->before($end)
|
||||||
|
->after($start)
|
||||||
|
->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||||
|
|
||||||
$return = [];
|
$return = [];
|
||||||
foreach ($query->toArray() as $entry) {
|
foreach ($query->toArray() as $entry) {
|
||||||
@@ -726,4 +768,53 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
|||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array with the following key:value pairs:
|
||||||
|
*
|
||||||
|
* yyyy-mm-dd:<array>
|
||||||
|
*
|
||||||
|
* That array contains:
|
||||||
|
*
|
||||||
|
* budgetid:<amount>
|
||||||
|
*
|
||||||
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget
|
||||||
|
* from the given users accounts..
|
||||||
|
*
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end)
|
||||||
|
{
|
||||||
|
$ids = $accounts->pluck('id')->toArray();
|
||||||
|
/** @var Collection $query */
|
||||||
|
$query = Auth::user()->transactionJournals()
|
||||||
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||||
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
|
->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||||
|
->whereIn('transactions.account_id', $ids)
|
||||||
|
->where('transactions.amount', '<', 0)
|
||||||
|
->before($end)
|
||||||
|
->after($start)
|
||||||
|
->groupBy('budget_id')
|
||||||
|
->groupBy('dateFormatted')
|
||||||
|
->get(
|
||||||
|
['transaction_journals.date as dateFormatted', 'budget_transaction_journal.budget_id',
|
||||||
|
DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]
|
||||||
|
);
|
||||||
|
|
||||||
|
$return = [];
|
||||||
|
foreach ($query->toArray() as $entry) {
|
||||||
|
$budgetId = $entry['budget_id'];
|
||||||
|
if (!isset($return[$budgetId])) {
|
||||||
|
$return[$budgetId] = [];
|
||||||
|
}
|
||||||
|
$return[$budgetId][$entry['dateFormatted']] = $entry['sum'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -39,15 +39,6 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function firstActivity(Budget $budget);
|
public function firstActivity(Budget $budget);
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a collection of all the limit repetitions belonging to this $budget.
|
|
||||||
*
|
|
||||||
* @param Budget $budget
|
|
||||||
*
|
|
||||||
* @return Collection
|
|
||||||
*/
|
|
||||||
public function getBudgetReps(Budget $budget);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the expenses for this budget grouped per month, with the date
|
* Returns the expenses for this budget grouped per month, with the date
|
||||||
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".
|
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".
|
||||||
@@ -76,6 +67,43 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end);
|
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array with the following key:value pairs:
|
||||||
|
*
|
||||||
|
* yyyy-mm-dd:<amount>
|
||||||
|
*
|
||||||
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget
|
||||||
|
* from the given users accounts..
|
||||||
|
*
|
||||||
|
* @param Budget $budget
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function spentPerDayForAccounts(Budget $budget, Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array with the following key:value pairs:
|
||||||
|
*
|
||||||
|
* yyyy-mm-dd:<array>
|
||||||
|
*
|
||||||
|
* That array contains:
|
||||||
|
*
|
||||||
|
* budgetid:<amount>
|
||||||
|
*
|
||||||
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget
|
||||||
|
* from the given users accounts..
|
||||||
|
*
|
||||||
|
* @param Collection $accounts
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
*
|
*
|
||||||
@@ -143,6 +171,14 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getBudgetLimitRepetitions(Budget $budget, Carbon $start, Carbon $end);
|
public function getBudgetLimitRepetitions(Budget $budget, Carbon $start, Carbon $end);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Budget $budget
|
* @param Budget $budget
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user