Index: Closing Status by Period

Principle:

Calculates the period corresponding to the current date and returns CLOSED or UNCLOSED next if the document date is included in the current period.

Script:

var CLOSING_DAY = 20;
var NUMBER_OF_MONTH = 3;
var START_MONTH = 0; //January is 0!
var CLOSED = "Cloturé";
var UNCLOSED = "Non cloturé";

var getPartOfYear = function(pDate, closingDay, numberOfMonth, startMonth){
    var nbDiffMonth = Math.abs(pDate.getMonth() - startMonth); 
	var period = Math.ceil(nbDiffMonth / numberOfMonth);
	return nbDiffMonth % numberOfMonth == 0 && pDate.getDate() > closingDay ? period + 1 : period;
}

var getClotureFromDateState = function(dateInvoice, today, closingDay, numberOfMonth, startMonth){
	if(today.getFullYear() != dateInvoice.getFullYear()){
	 	var diffMonth = today.getMonth() + 12 - dateInvoice.getMonth();
	 	if(diffMonth > numberOfMonth)
	 		return CLOSED;
	 	
	 	if(today.getDate() > closingDay)
	 		diffMonth++;
	 	if(dateInvoice.getDate() < closingDay)
	 		diffMonth++;
	 	
	 	return diffMonth > numberOfMonth ? CLOSED : UNCLOSED;
	}
	return getPartOfYear(today, closingDay, numberOfMonth, startMonth) == getPartOfYear(dateInvoice, closingDay, numberOfMonth, startMonth) ? UNCLOSED : CLOSED;
};

var getClotureState = function(pParameters){
	var dateGeneralIndex = getTextFromIndexInfo(getFirstIndexFromID(pParameters,"DATE_INDEX"));
	var dateParts = dateGeneralIndex.split("/");
	var dateInvoice = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
	return getClotureFromDateState(dateInvoice, new Date(), CLOSING_DAY, NUMBER_OF_MONTH, START_MONTH);
};

return getClotureState(pParameters);

Tests:

var testPeriodDate = function(pNumber, dateInvoice, today, closingDay, numberOfMonth, startMonth, pResult){
	
	var result = getClotureFromDateState(dateInvoice, today, closingDay, numberOfMonth, startMonth) == pResult;
	print(pNumber+ " result : "+result + " t factDate:"+dateInvoice.getDate()+"/"+dateInvoice.getMonth()+"/"+dateInvoice.getFullYear()+" actualDate : "+today.getDate()+"/"+today.getMonth()+"/"+today.getFullYear()+ " closingDay : "+closingDay+ " periodLength : "+numberOfMonth+ " start : "+startMonth);
};

testPeriodDate(1, new Date(), new Date(), 20, 3, 0, UNCLOSED);
testPeriodDate(2, new Date(2018, 3, 19), new Date(2018, 3, 21), 20, 3, 0, CLOSED);
testPeriodDate(3, new Date(2017, 3, 19), new Date(2018, 3, 21), 20, 3, 0, CLOSED);
testPeriodDate(4, new Date(2017,11,25), new Date(2018, 0, 1), 20, 3, 0, UNCLOSED);
testPeriodDate(5, new Date(2017,11,1), new Date(2018, 0, 1), 20, 3, 0, UNCLOSED);
testPeriodDate(6, new Date(2018,1,19), new Date(2018, 11, 21), 20, 3, 0, CLOSED);
testPeriodDate(7, new Date(2017,11,19), new Date(2018, 1, 21), 20, 3, 0, CLOSED);
testPeriodDate(8, new Date(2018,11,17), new Date(2018, 1, 1), 20, 3, 1, CLOSED);
testPeriodDate(9, new Date(2017,10,25), new Date(2018, 1, 17), 20, 3, 1, UNCLOSED);
testPeriodDate(10, new Date(2017,11,1), new Date(2018, 1, 17), 20, 3, 1, UNCLOSED);
testPeriodDate(11, new Date(2017,9,1), new Date(2018, 9, 17), 31, 12, 0, CLOSED);
testPeriodDate(12, new Date(2018,1,1), new Date(2018, 11, 26), 31, 12, 0, UNCLOSED);
testPeriodDate(13, new Date(2018,0,1), new Date(2018, 0, 31), 31, 1, 0, UNCLOSED);
testPeriodDate(14, new Date(2018,0,31), new Date(2018, 1, 1), 31, 1, 0, CLOSED);