Skip to content

Commit

Permalink
Refractor(bhdateInterval) set correct dates
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremielodi committed Nov 25, 2019
1 parent ab11f5b commit 84ffd2b
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions client/src/js/components/bhDateInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ angular.module('bhima.components')
});

// dependencies injection
bhDateInterval.$inject = ['moment', 'bhConstants', 'FiscalService', 'SessionService'];
bhDateInterval.$inject = [
'bhConstants', 'FiscalService',
'SessionService', 'PeriodService',
];

// controller definition
function bhDateInterval(moment, bhConstants, Fiscal, Session) {
function bhDateInterval(bhConstants, Fiscal, Session, PeriodService) {
const $ctrl = this;

// tzOffset gives the direction we need to go to get to UTC
const tzOffset = (-1 * new Date().getTimezoneOffset());

PeriodService.dateFormat = 'YYYY-MM-DD';
// expose to the view
$ctrl.search = search;
$ctrl.clear = clear;
Expand Down Expand Up @@ -94,31 +95,23 @@ function bhDateInterval(moment, bhConstants, Fiscal, Session) {
}

function day() {
$ctrl.dateFrom = new Date();
$ctrl.dateTo = new Date();
}

function momentStartHelper(interval) {
return moment().startOf(interval).add(tzOffset, 'minutes').toDate();
setDateInterval('today');
}

function momentEndHelper(interval) {
return moment().endOf(interval).add(tzOffset, 'minutes').toDate();
function setDateInterval(key) {
$ctrl.dateFrom = new Date(PeriodService.index[key].limit.start());
$ctrl.dateTo = new Date(PeriodService.index[key].limit.end());
}

function week() {
$ctrl.dateFrom = momentStartHelper('week');
$ctrl.dateTo = new Date();
setDateInterval('week');
}

function month() {
$ctrl.dateFrom = momentStartHelper('month');
$ctrl.dateTo = momentEndHelper('month');
setDateInterval('month');
}

function year() {
$ctrl.dateFrom = momentStartHelper('year');
$ctrl.dateTo = momentEndHelper('year');
setDateInterval('year');
}

function custom() {
Expand Down

0 comments on commit 84ffd2b

Please sign in to comment.