Skip to content

Commit

Permalink
Merge #4018
Browse files Browse the repository at this point in the history
4018: Refractor(bhdateInterval) set correct dates r=mbayopanda a=jeremielodi

closes #4017

Co-authored-by: jeremielodi <jeremielodi@gmail.com>
  • Loading branch information
bors[bot] and jeremielodi authored Nov 27, 2019
2 parents 2470f18 + d5c2fe7 commit 8415ed8
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions client/src/js/components/bhDateInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ 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;

$ctrl.$onInit = function $onInit() {
$ctrl.$onInit = () => {
// specify if clear button can be displayed
if (!angular.isDefined($ctrl.canClear)) {
$ctrl.canClear = true;
Expand Down Expand Up @@ -93,32 +94,25 @@ function bhDateInterval(moment, bhConstants, Fiscal, Session) {
$ctrl.onChangeDate();
}

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

function momentStartHelper(interval) {
return moment().startOf(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 momentEndHelper(interval) {
return moment().endOf(interval).add(tzOffset, 'minutes').toDate();
function day() {
setDateInterval('today');
}

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 Expand Up @@ -146,7 +140,6 @@ function bhDateInterval(moment, bhConstants, Fiscal, Session) {
} else {
custom();
}

// set clean mode
if ($ctrl.mode === 'clean') {
clear();
Expand Down

0 comments on commit 8415ed8

Please sign in to comment.