From 41096a7bce9322b4162b3b04080ffcef9eb6c617 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 2 Feb 2017 14:54:43 +0200 Subject: [PATCH] Fix dashboard totals when 'First month' is set --- app/Models/Account.php | 12 ++++++++++++ app/Ninja/Repositories/DashboardRepository.php | 11 ++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Models/Account.php b/app/Models/Account.php index 56d17004d25e..9e9523a7af36 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -1804,6 +1804,18 @@ class Account extends Eloquent { return $this->company->accounts->count() > 1; } + + public function financialYearStart() + { + $yearStart = Carbon::parse($this->financial_year_start); + $yearStart->year = date('Y'); + + if ($yearStart->isFuture()) { + $yearStart->subYear(); + } + + return $yearStart->format('Y-m-d'); + } } Account::updated(function ($account) { diff --git a/app/Ninja/Repositories/DashboardRepository.php b/app/Ninja/Repositories/DashboardRepository.php index 6cfeb9d79383..951993d56081 100644 --- a/app/Ninja/Repositories/DashboardRepository.php +++ b/app/Ninja/Repositories/DashboardRepository.php @@ -8,7 +8,6 @@ use App\Models\Task; use DateInterval; use DatePeriod; use DB; -use Carbon; use stdClass; class DashboardRepository @@ -211,12 +210,7 @@ class DashboardRepository if ($startDate) { $paidToDate->where('payments.payment_date', '>=', $startDate); } elseif ($account->financial_year_start) { - $yearStart = Carbon::parse($account->financial_year_start); - $yearStart->year = date('Y'); - if ($yearStart->isFuture()) { - $yearStart->subYear(); - } - $paidToDate->where('payments.payment_date', '>=', $yearStart->format('Y-m-d')); + $paidToDate->where('payments.payment_date', '>=', $account->financialYearStart()); } return $paidToDate->groupBy('payments.account_id') @@ -246,8 +240,7 @@ class DashboardRepository } if ($account->financial_year_start) { - $yearStart = str_replace('2000', date('Y'), $account->financial_year_start); - $averageInvoice->where('invoices.invoice_date', '>=', $yearStart); + $averageInvoice->where('invoices.invoice_date', '>=', $account->financialYearStart()); } return $averageInvoice->groupBy('accounts.id')