From db11b0cf835a5ba0896418305c8edba8bd838181 Mon Sep 17 00:00:00 2001 From: Sevan Nerse Date: Fri, 5 Feb 2021 20:34:35 +0300 Subject: [PATCH] quarterly based reports should consider financial year start --- app/Abstracts/Report.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/Abstracts/Report.php b/app/Abstracts/Report.php index 7a060173d..e2fedbbae 100644 --- a/app/Abstracts/Report.php +++ b/app/Abstracts/Report.php @@ -16,6 +16,7 @@ use App\Traits\Charts; use App\Traits\DateTime; use App\Traits\SearchString; use App\Utilities\Chartjs; +use Carbon\CarbonPeriod; use Date; use Illuminate\Support\Str; @@ -388,15 +389,26 @@ abstract class Report switch ($this->getSetting('period')) { case 'yearly': $i = $date->copy()->format($this->getYearlyDateFormat()); + break; case 'quarterly': - $start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat($this->year)); - $end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat($this->year)); + $quarters = $this->getFiscalBaseQuarters($this->year); + + foreach ($quarters as $quarter) { + if ($date->lessThan($quarter->getStartDate()) || $date->greaterThan($quarter->getEndDate())) { + continue; + } + + $start = $quarter->getStartDate()->format($this->getQuarterlyDateFormat($this->year)); + $end = $quarter->getEndDate()->format($this->getQuarterlyDateFormat($this->year)); + } $i = $start . '-' . $end; + break; default: $i = $date->copy()->format($this->getMonthlyDateFormat($this->year)); + break; } @@ -502,4 +514,16 @@ abstract class Report ], ]; } + + private function getFiscalBaseQuarters($year) + { + $periods = []; + $fiscal_start = $this->getFinancialStart($year); + + for ($i = 0; $i < 4; $i++) { + $periods[] = CarbonPeriod::create($fiscal_start->copy()->addQuarters($i), $fiscal_start->copy()->addQuarters($i + 1)->subDay()); + } + + return $periods; + } }