diff --git a/app/Http/Controllers/Common/Dashboards.php b/app/Http/Controllers/Common/Dashboards.php index f0d912bcf..f255d5e36 100644 --- a/app/Http/Controllers/Common/Dashboards.php +++ b/app/Http/Controllers/Common/Dashboards.php @@ -72,9 +72,14 @@ class Dashboards extends Controller return Widgets::canShow($widget->class); }); - $financial_start = $this->getFinancialStart()->format('Y-m-d'); + $date_picker_shortcuts = $this->getDatePickerShortcuts(); - return view('common.dashboards.show', compact('dashboard', 'widgets', 'financial_start')); + if (!request()->has('start_date')) { + request()->merge(['start_date' => $date_picker_shortcuts[trans('reports.this_year')]['start']]); + request()->merge(['end_date' => $date_picker_shortcuts[trans('reports.this_year')]['end']]); + } + + return view('common.dashboards.show', compact('dashboard', 'widgets', 'date_picker_shortcuts')); } /** diff --git a/app/Traits/DateTime.php b/app/Traits/DateTime.php index ef3f102e4..61f987ff9 100644 --- a/app/Traits/DateTime.php +++ b/app/Traits/DateTime.php @@ -136,6 +136,50 @@ trait DateTime return $quarters; } + public function getDatePickerShortcuts() + { + $today = new Date(); + $financial_year = $this->getFinancialYear(); + $financial_quarters = $this->getFinancialQuarters(); + + foreach ($financial_quarters as $quarter) { + if ($today->lessThan($quarter->getStartDate()) || $today->greaterThan($quarter->getEndDate())) { + continue; + } + + $this_quarter = $quarter; + } + + if (!isset($this_quarter)) { + $this_quarter = $financial_quarters[0]; + } + + $date_picker_shortcuts = [ + trans('reports.this_year') => [ + 'start' => $financial_year->getStartDate()->format('Y-m-d'), + 'end' => $financial_year->getEndDate()->format('Y-m-d'), + ], + trans('reports.previous_year') => [ + 'start' => $financial_year->getStartDate()->copy()->subYear()->format('Y-m-d'), + 'end' => $financial_year->getEndDate()->copy()->subYear()->format('Y-m-d'), + ], + trans('reports.this_quarter') => [ + 'start' => $this_quarter->getStartDate()->format('Y-m-d'), + 'end' => $this_quarter->getEndDate()->format('Y-m-d'), + ], + trans('reports.previous_quarter') => [ + 'start' => $this_quarter->getStartDate()->copy()->subQuarter()->format('Y-m-d'), + 'end' => $this_quarter->getEndDate()->copy()->subQuarter()->format('Y-m-d'), + ], + trans('reports.last_12_months') => [ + 'start' => $today->copy()->subYear()->startOfDay()->format('Y-m-d'), + 'end' => $today->copy()->subDay()->endOfDay()->format('Y-m-d'), + ], + ]; + + return $date_picker_shortcuts; + } + public function getMonthlyDateFormat($year = null) { $format = 'M'; diff --git a/resources/views/common/dashboards/show.blade.php b/resources/views/common/dashboards/show.blade.php index 4301e4f9f..ce3d3a6fc 100644 --- a/resources/views/common/dashboards/show.blade.php +++ b/resources/views/common/dashboards/show.blade.php @@ -81,18 +81,15 @@ value-format="yyyy-MM-dd" @change="onChangeFilterDate" range-separator=">>" - start-placeholder="{{ trans('general.start_date')}}" - end-placeholder="{{ trans('general.end_date')}}" + start-placeholder="{{ $date_picker_shortcuts[trans("reports.this_year")]["start"] }}" + end-placeholder="{{ $date_picker_shortcuts[trans("reports.this_year")]["end"] }}" :picker-options="{ shortcuts: [ { text: '{{ trans("reports.this_year") }}', onClick(picker) { - const end = new Date('{{ $financial_start }}'); - const start = new Date('{{ $financial_start }}'); - - end.setFullYear(start.getFullYear() + 1); - end.setTime(end.getTime() - 3600 * 1000 * 24 * 1); + const start = new Date('{{ $date_picker_shortcuts[trans("reports.this_year")]["start"] }}'); + const end = new Date('{{ $date_picker_shortcuts[trans("reports.this_year")]["end"] }}'); picker.$emit('pick', [start, end]); } @@ -100,13 +97,8 @@ { text: '{{ trans("reports.previous_year") }}', onClick(picker) { - const end = new Date('{{ $financial_start }}'); - const start = new Date('{{ $financial_start }}'); - - start.setTime(start.getTime() - 3600 * 1000 * 24 * 365); - - end.setFullYear(start.getFullYear() + 1); - end.setTime(end.getTime() - 3600 * 1000 * 24 * 1); + const start = new Date('{{ $date_picker_shortcuts[trans("reports.previous_year")]["start"] }}'); + const end = new Date('{{ $date_picker_shortcuts[trans("reports.previous_year")]["end"] }}'); picker.$emit('pick', [start, end]); } @@ -114,10 +106,8 @@ { text: '{{ trans("reports.this_quarter") }}', onClick(picker) { - const now = new Date(); - const quarter = Math.floor((now.getMonth() / 3)); - const start = new Date(now.getFullYear(), quarter * 3, 1); - const end = new Date(start.getFullYear(), start.getMonth() + 3, 0); + const start = new Date('{{ $date_picker_shortcuts[trans("reports.this_quarter")]["start"] }}'); + const end = new Date('{{ $date_picker_shortcuts[trans("reports.this_quarter")]["end"] }}'); picker.$emit('pick', [start, end]); } @@ -125,13 +115,8 @@ { text: '{{ trans("reports.previous_quarter") }}', onClick(picker) { - const now = new Date(); - const quarter = Math.floor((now.getMonth() / 3)); - const start = new Date(now.getFullYear(), quarter * 3, 1); - const end = new Date(start.getFullYear(), start.getMonth() + 3, 0); - - start.setMonth(start.getMonth() - 3); - end.setMonth(end.getMonth() - 3); + const start = new Date('{{ $date_picker_shortcuts[trans("reports.previous_quarter")]["start"] }}'); + const end = new Date('{{ $date_picker_shortcuts[trans("reports.previous_quarter")]["end"] }}'); picker.$emit('pick', [start, end]); } @@ -139,10 +124,8 @@ { text: '{{ trans("reports.last_12_months") }}', onClick(picker) { - const end = new Date(); - const start = new Date(); - - start.setTime(start.getTime() - 3600 * 1000 * 24 * 365); + const start = new Date('{{ $date_picker_shortcuts[trans("reports.last_12_months")]["start"] }}'); + const end = new Date('{{ $date_picker_shortcuts[trans("reports.last_12_months")]["end"] }}'); picker.$emit('pick', [start, end]); }