Query & loop optimizations.

This commit is contained in:
James Cole 2015-04-11 12:36:17 +02:00
parent f6999f355b
commit 3e01daa172
4 changed files with 17 additions and 44 deletions

View File

@ -73,10 +73,8 @@ class ReportHelper implements ReportHelperInterface
// find transactions to shared asset accounts, which are without a budget by default:
// which is only relevant when shared asset accounts are hidden.
if ($showSharedReports === false) {
$transfers = $query->sharedExpenses($start, $end);
foreach ($transfers as $transfer) {
$budgets[0]['spent'] += floatval($transfer->amount) * -1;
}
$transfers = $query->sharedExpenses($start, $end)->sum('queryAmount');
$budgets[0]['spent'] += floatval($transfers) * -1;
}
return $budgets;

View File

@ -111,17 +111,11 @@ class ReportQuery implements ReportQueryInterface
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
* @return float
*/
public function balancedTransactionsSum(Account $account, Carbon $start, Carbon $end)
{
$set = $this->balancedTransactionsList($account, $start, $end);
$sum = 0;
foreach ($set as $entry) {
$sum += floatval($entry->amount);
}
return $sum;
return floatval($this->balancedTransactionsList($account, $start, $end)->sum('queryAmount'));
}
/**

View File

@ -43,7 +43,7 @@ interface ReportQueryInterface
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
* @return float
*/
public function balancedTransactionsSum(Account $account, Carbon $start, Carbon $end);

View File

@ -80,8 +80,7 @@ class GoogleChartController extends Controller
$index = 1;
/** @var Account $account */
foreach ($accounts as $account) {
$accountName = $account->name;
$chart->addColumn('Balance for ' . $accountName, 'number');
$chart->addColumn('Balance for ' . $account->name, 'number');
$chart->addCertainty($index);
$index++;
}
@ -251,6 +250,11 @@ class GoogleChartController extends Controller
$bills = $repository->getActiveBills();
$paid = new Collection; // journals.
$unpaid = new Collection; // bills
// loop paid and create single entry:
$paidDescriptions = [];
$paidAmount = 0;
$unpaidDescriptions = [];
$unpaidAmount = 0;
/** @var Bill $bill */
foreach ($bills as $bill) {
@ -287,11 +291,7 @@ class GoogleChartController extends Controller
$paid = $paid->merge($journals);
}
}
// loop paid and create single entry:
$paidDescriptions = [];
$paidAmount = 0;
$unpaidDescriptions = [];
$unpaidAmount = 0;
/** @var TransactionJournal $entry */
foreach ($paid as $entry) {
@ -498,19 +498,9 @@ class GoogleChartController extends Controller
while ($start < $end) {
$currentEnd = clone $start;
$currentEnd->endOfMonth();
// total income:
$income = $query->incomeByPeriod($start, $currentEnd, $showSharedReports);
$incomeSum = 0;
foreach ($income as $entry) {
$incomeSum += floatval($entry->amount);
}
// total expenses:
$expense = $query->journalsByExpenseAccount($start, $currentEnd, $showSharedReports);
$expenseSum = 0;
foreach ($expense as $entry) {
$expenseSum += floatval($entry->amount);
}
// total income && total expenses:
$incomeSum = floatval($query->incomeByPeriod($start, $currentEnd, $showSharedReports)->sum('queryAmount'));
$expenseSum = floatval($query->journalsByExpenseAccount($start, $currentEnd, $showSharedReports)->sum('queryAmount'));
$chart->addRow(clone $start, $incomeSum, $expenseSum);
$start->addMonth();
@ -549,18 +539,9 @@ class GoogleChartController extends Controller
$currentEnd = clone $start;
$currentEnd->endOfMonth();
// total income:
$incomeResult = $query->incomeByPeriod($start, $currentEnd, $showSharedReports);
$incomeSum = 0;
foreach ($incomeResult as $entry) {
$incomeSum += floatval($entry->amount);
}
$incomeSum = floatval($query->incomeByPeriod($start, $currentEnd, $showSharedReports)->sum('queryAmount'));
// total expenses:
$expenseResult = $query->journalsByExpenseAccount($start, $currentEnd, $showSharedReports);
$expenseSum = 0;
foreach ($expenseResult as $entry) {
$expenseSum += floatval($entry->amount);
}
$expenseSum = floatval($query->journalsByExpenseAccount($start, $currentEnd, $showSharedReports)->sum('queryAmount'));
$income += $incomeSum;
$expense += $expenseSum;