Skip to content

Commit

Permalink
date filter on dashboard will work fiscal year based akaunting/softwa…
Browse files Browse the repository at this point in the history
…re#78
  • Loading branch information
sevannerse committed Mar 23, 2021
1 parent 7449071 commit 468b843
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
11 changes: 8 additions & 3 deletions app/Http/Controllers/Common/Dashboards.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public function index()
*/
public function show($dashboard_id = null)
{
$date_picker_shortcuts = $this->getDatePickerShortcuts();

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']]);
}

$dashboard_id = $dashboard_id ?? session('dashboard_id');

try {
Expand All @@ -72,9 +79,7 @@ public function show($dashboard_id = null)
return Widgets::canShow($widget->class);
});

$financial_start = $this->getFinancialStart()->format('Y-m-d');

return view('common.dashboards.show', compact('dashboard', 'widgets', 'financial_start'));
return view('common.dashboards.show', compact('dashboard', 'widgets', 'date_picker_shortcuts'));
}

/**
Expand Down
40 changes: 40 additions & 0 deletions app/Traits/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,46 @@ public function getFinancialQuarters($year = null)
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;
}

$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';
Expand Down
41 changes: 12 additions & 29 deletions resources/views/common/dashboards/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,68 +81,51 @@
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]);
}
},
{
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]);
}
},
{
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]);
}
},
{
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]);
}
},
{
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]);
}
Expand Down

0 comments on commit 468b843

Please sign in to comment.