Filter dashboard expenses by first month of year setting

This commit is contained in:
Hillel Coren 2017-02-07 14:04:13 +02:00
parent ecc6cc0b7a
commit 44eb7d52f9
3 changed files with 18 additions and 10 deletions

View File

@ -38,7 +38,7 @@ class DashboardController extends BaseController
$pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll); $pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll);
$upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll); $upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll);
$payments = $dashboardRepo->payments($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); $tasks = $dashboardRepo->tasks($accountId, $userId, $viewAll);
$showBlueVinePromo = $user->is_admin $showBlueVinePromo = $user->is_admin

View File

@ -1663,6 +1663,10 @@ class Account extends Eloquent
public function financialYearStart() public function financialYearStart()
{ {
if (! $this->financial_year_start) {
return false;
}
$yearStart = Carbon::parse($this->financial_year_start); $yearStart = Carbon::parse($this->financial_year_start);
$yearStart->year = date('Y'); $yearStart->year = date('Y');

View File

@ -211,8 +211,8 @@ class DashboardRepository
if ($startDate) { if ($startDate) {
$paidToDate->where('payments.payment_date', '>=', $startDate); $paidToDate->where('payments.payment_date', '>=', $startDate);
} elseif ($account->financial_year_start) { } elseif ($startDate = $account->financialYearStart()) {
$paidToDate->where('payments.payment_date', '>=', $account->financialYearStart()); $paidToDate->where('payments.payment_date', '>=', $startDate);
} }
return $paidToDate->groupBy('payments.account_id') return $paidToDate->groupBy('payments.account_id')
@ -242,8 +242,8 @@ class DashboardRepository
$averageInvoice->where('invoices.user_id', '=', $userId); $averageInvoice->where('invoices.user_id', '=', $userId);
} }
if ($account->financial_year_start) { if ($startDate = $account->financialYearStart()) {
$averageInvoice->where('invoices.invoice_date', '>=', $account->financialYearStart()); $averageInvoice->where('invoices.invoice_date', '>=', $startDate);
} }
return $averageInvoice->groupBy('accounts.id') return $averageInvoice->groupBy('accounts.id')
@ -365,7 +365,7 @@ class DashboardRepository
->get(); ->get();
} }
public function expenses($accountId, $userId, $viewAll) public function expenses($account, $userId, $viewAll)
{ {
$amountField = DB::getQueryGrammar()->wrap('expenses.amount', true); $amountField = DB::getQueryGrammar()->wrap('expenses.amount', true);
$taxRate1Field = DB::getQueryGrammar()->wrap('expenses.tax_rate1', 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," "SUM({$amountField} + ({$amountField} * {$taxRate1Field} / 100) + ({$amountField} * {$taxRate2Field} / 100)) as value,"
.DB::getQueryGrammar()->wrap('expenses.expense_currency_id', true).' as currency_id' .DB::getQueryGrammar()->wrap('expenses.expense_currency_id', true).' as currency_id'
); );
$paidToDate = DB::table('accounts') $expenses = DB::table('accounts')
->select($select) ->select($select)
->leftJoin('expenses', 'accounts.id', '=', 'expenses.account_id') ->leftJoin('expenses', 'accounts.id', '=', 'expenses.account_id')
->where('accounts.id', '=', $accountId) ->where('accounts.id', '=', $account->id)
->where('expenses.is_deleted', '=', false); ->where('expenses.is_deleted', '=', false);
if (! $viewAll) { 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') ->groupBy('expenses.expense_currency_id')
->get(); ->get();
} }