Add two new “spentInPeriod” methods that use the collector and not big queries.

This commit is contained in:
James Cole
2017-01-05 09:07:04 +01:00
parent 3ef569d280
commit 4241ae035e
6 changed files with 94 additions and 14 deletions

View File

@@ -291,7 +291,7 @@ class BudgetRepository implements BudgetRepositoryInterface
}
);
}
)->orderBy('budget_limits.start_date','DESC')->get(['budget_limits.*']);
)->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
return $set;
}
@@ -521,6 +521,33 @@ class BudgetRepository implements BudgetRepositoryInterface
return bcadd($first, $second);
}
/**
* @param Collection $budgets
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriodCollector(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class, [$this->user]);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setBudgets($budgets);
if($accounts->count() > 0) {
$collector->setAccounts($accounts);
}
if($accounts->count() === 0) {
$collector->setAllAssetAccounts();
}
$set = $collector->getJournals();
$sum = strval($set->sum('transaction_amount'));
return $sum;
}
/**
* @param Collection $accounts
* @param Carbon $start
@@ -663,4 +690,40 @@ class BudgetRepository implements BudgetRepositoryInterface
return $limit;
}
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function spentInPeriodWithoutBudgetCollector(Collection $accounts, Carbon $start, Carbon $end): string
{
/** @var JournalCollectorInterface $collector */
$collector = app(JournalCollectorInterface::class, [$this->user]);
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
if ($accounts->count() > 0) {
$collector->setAccounts($accounts);
}
if ($accounts->count() === 0) {
$collector->setAllAssetAccounts();
}
$set = $collector->getJournals();
$set = $set->filter(
function (Transaction $transaction) {
if (bccomp($transaction->transaction_amount, '0') === -1) {
return $transaction;
}
return null;
}
);
$sum = strval($set->sum('transaction_amount'));
return $sum;
}
}