From 44eb7d52f9fb1a0e1125bf2cd5b3cdf1603a66eb Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 7 Feb 2017 14:04:13 +0200 Subject: [PATCH] Filter dashboard expenses by first month of year setting --- app/Http/Controllers/DashboardController.php | 2 +- app/Models/Account.php | 4 ++++ .../Repositories/DashboardRepository.php | 22 +++++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 212e30a3b741..5b915635d494 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -38,7 +38,7 @@ class DashboardController extends BaseController $pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll); $upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll); $payments = $dashboardRepo->payments($accountId, $userId, $viewAll); - $expenses = $dashboardRepo->expenses($accountId, $userId, $viewAll); + $expenses = $dashboardRepo->expenses($account, $userId, $viewAll); $tasks = $dashboardRepo->tasks($accountId, $userId, $viewAll); $showBlueVinePromo = $user->is_admin diff --git a/app/Models/Account.php b/app/Models/Account.php index b6c229958ce6..62924f917f34 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1663,6 +1663,10 @@ class Account extends Eloquent public function financialYearStart() { + if (! $this->financial_year_start) { + return false; + } + $yearStart = Carbon::parse($this->financial_year_start); $yearStart->year = date('Y'); diff --git a/app/Ninja/Repositories/DashboardRepository.php b/app/Ninja/Repositories/DashboardRepository.php index 926043c73def..4061a1521386 100644 --- a/app/Ninja/Repositories/DashboardRepository.php +++ b/app/Ninja/Repositories/DashboardRepository.php @@ -211,8 +211,8 @@ class DashboardRepository if ($startDate) { $paidToDate->where('payments.payment_date', '>=', $startDate); - } elseif ($account->financial_year_start) { - $paidToDate->where('payments.payment_date', '>=', $account->financialYearStart()); + } elseif ($startDate = $account->financialYearStart()) { + $paidToDate->where('payments.payment_date', '>=', $startDate); } return $paidToDate->groupBy('payments.account_id') @@ -242,8 +242,8 @@ class DashboardRepository $averageInvoice->where('invoices.user_id', '=', $userId); } - if ($account->financial_year_start) { - $averageInvoice->where('invoices.invoice_date', '>=', $account->financialYearStart()); + if ($startDate = $account->financialYearStart()) { + $averageInvoice->where('invoices.invoice_date', '>=', $startDate); } return $averageInvoice->groupBy('accounts.id') @@ -365,7 +365,7 @@ class DashboardRepository ->get(); } - public function expenses($accountId, $userId, $viewAll) + public function expenses($account, $userId, $viewAll) { $amountField = DB::getQueryGrammar()->wrap('expenses.amount', true); $taxRate1Field = DB::getQueryGrammar()->wrap('expenses.tax_rate1', true); @@ -375,17 +375,21 @@ class DashboardRepository "SUM({$amountField} + ({$amountField} * {$taxRate1Field} / 100) + ({$amountField} * {$taxRate2Field} / 100)) as value," .DB::getQueryGrammar()->wrap('expenses.expense_currency_id', true).' as currency_id' ); - $paidToDate = DB::table('accounts') + $expenses = DB::table('accounts') ->select($select) ->leftJoin('expenses', 'accounts.id', '=', 'expenses.account_id') - ->where('accounts.id', '=', $accountId) + ->where('accounts.id', '=', $account->id) ->where('expenses.is_deleted', '=', false); if (! $viewAll) { - $paidToDate = $paidToDate->where('expenses.user_id', '=', $userId); + $expenses = $expenses->where('expenses.user_id', '=', $userId); } - return $paidToDate->groupBy('accounts.id') + if ($startDate = $account->financialYearStart()) { + $expenses->where('expenses.expense_date', '>=', $startDate); + } + + return $expenses->groupBy('accounts.id') ->groupBy('expenses.expense_currency_id') ->get(); }