Multi year report move to AJAX.

Signed-off-by: James Cole <thegrumpydictator@gmail.com>
This commit is contained in:
James Cole
2016-11-02 20:08:11 +01:00
parent 2ddd4314f1
commit e24f5ec9f3
15 changed files with 267 additions and 272 deletions

View File

@@ -113,66 +113,6 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function getBudgetMultiYear(Carbon $start, Carbon $end, Collection $accounts): array
{
$accountIds = $accounts->pluck('id')->toArray();
$query = TransactionJournal
::leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin(
'transactions', function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereNull('transaction_journals.deleted_at')
->whereNull('transactions.deleted_at')
->where('transaction_types.type', 'Withdrawal')
->where('transaction_journals.user_id', auth()->user()->id);
if (count($accountIds) > 0) {
$query->whereIn('transactions.account_id', $accountIds);
}
$query->groupBy(['budget_transaction_journal.budget_id', 'the_year']);
$queryResult = $query->get(
[
'budget_transaction_journal.budget_id',
DB::raw('DATE_FORMAT(transaction_journals.date,"%Y") AS the_year'),
DB::raw('SUM(transactions.amount) as sum_of_period'),
]
);
$data = [];
$budgets = $this->budgetRepository->getBudgets();
$years = $this->listOfYears($start, $end);
// do budget "zero"
$emptyBudget = new Budget;
$emptyBudget->id = 0;
$emptyBudget->name = strval(trans('firefly.no_budget'));
$budgets->push($emptyBudget);
// get all budgets and years.
foreach ($budgets as $budget) {
$data[$budget->id] = [
'name' => $budget->name,
'entries' => $this->filterAmounts($queryResult, $budget->id, $years),
'sum' => '0',
];
}
// filter out empty ones and fill sum:
$data = $this->getBudgetMultiYearMeta($data);
return $data;
}
/**
* @param Carbon $start
* @param Carbon $end
@@ -293,24 +233,6 @@ class ReportHelper implements ReportHelperInterface
return $months;
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
public function listOfYears(Carbon $start, Carbon $end): array
{
$begin = clone $start;
$years = [];
while ($begin < $end) {
$years[] = $begin->year;
$begin->addYear();
}
return $years;
}
/**
* Returns an array of tags and their comparitive size with amounts bla bla.
*
@@ -385,85 +307,4 @@ class ReportHelper implements ReportHelperInterface
return $collection;
}
/**
* @param array $data
*
* @return array
*/
protected function getBudgetMultiYearMeta(array $data): array
{
/**
* @var int $budgetId
* @var array $set
*/
foreach ($data as $budgetId => $set) {
$sum = '0';
foreach ($set['entries'] as $amount) {
$sum = bcadd($amount, $sum);
}
$data[$budgetId]['sum'] = $sum;
if (bccomp('0', $sum) === 0) {
unset($data[$budgetId]);
}
}
return $data;
}
/**
* Take the array as returned by CategoryRepositoryInterface::spentPerDay and CategoryRepositoryInterface::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)
{
$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;
}
/**
* @param Collection $set
* @param int $budgetId
* @param array $years
*
* @return array
*/
private function filterAmounts(Collection $set, int $budgetId, array $years):array
{
$arr = [];
foreach ($years as $year) {
/** @var stdClass $object */
$result = $set->filter(
function (TransactionJournal $object) use ($budgetId, $year) {
return intval($object->the_year) === $year && $budgetId === intval($object->budget_id);
}
);
$amount = '0';
if (!is_null($result->first())) {
$amount = $result->first()->sum_of_period;
}
$arr[$year] = $amount;
}
return $arr;
}
}