I am trying to write logic in ES5 (in BIRT) to assess if an employee has worked 10 days in a row. I have extracted the start dates but can’t seem to get the logic to work as intended. If an employee clocks in on the 14th, for example, and they clock in every day between that start date and the 24th, then that should be recorded in a column to signify that 10 consecutive clock ins have been found.
function getDayFromDateAndTime(datetimeString) {
logger.warning("Received date-time string: " + datetimeString);
if (!datetimeString || typeof datetimeString == "undefined") {
logger.warning("Invalid day format: " + datetimeString);
return null;
}
return (
BirtDateTime.day(datetimeString) + "/" + BirtDateTime.month(datetimeString)
);
}
function getConsecutiveShifts(ConsecutiveShifts) {
if (ConsecutiveShifts.length === 0) {
logger.warning("ConsecutiveShifts array is empty. Exiting function.");
return false;
}
ConsecutiveShifts.sort(function (a, b) {
var dayA = getDayFromDateAndTime(a.EmployeeStartDate);
var dayB = getDayFromDateAndTime(b.EmployeeStartDate);
logger.warning("Sorting shift days: " + dayA + ", " + dayB);
return dayA - dayB;
});
var consecutiveCount = 0;
var previousDay = -1;
for (var i = 0; i < ConsecutiveShifts.length; i++) {
var shiftDay = getDayFromDateAndTime(
ConsecutiveShifts[i].EmployeeStartDate
);
if (shiftDay === null) {
logger.warning("Null shift day encountered. Skipping shift.");
continue;
}
if (shiftDay === previousDay + 1) {
consecutiveCount++;
} else {
consecutiveCount = 1;
}
logger.warning(
"Shift Day: " + shiftDay + ", Consecutive Count: " + consecutiveCount
);
if (consecutiveCount >= 10) {
logger.warning("Found 10 consecutive shifts!");
return true;
}
previousDay = shiftDay;
}
logger.warning("Did not find 10 consecutive shifts.");
return false;
}
var items = {
EmployeeId: row["PEOPLE_PERSON_NUMBER_1"],
EmployeeFullName: row["EMP_COMMON_FULL_NAME_1"],
EmployeeHireDate: row["PEOPLE_HIRE_DATE_1"],
EmployeeGender: row["PEOPLE_CUSTOM_3"],
EmployeePermanentTemporary: row["PEOPLE_CUSTOM_2"],
EmployeeReportsTo: row["PEOPLE_MANAGER_NAME_1"],
EmployeeJobTitle: row["PEOPLE_CUSTOM_1"],
EmployeeCerts: row["SCH_PEOPLE_CERTIFICATION_NAMES_FULL_DETAILS_1"],
EmployeeStartDate: row["TKSHIFT_ACTUAL_START_DATETIME_ROUNDED_1"],
EmployeeEndDate: row["TKSHIFT_ACTUAL_END_DATETIME_ROUNDED_1"],
EmployeeConsecutiveShifts: getConsecutiveShifts(ConsecutiveShifts),
};
ConsecutiveShifts.push(items);
also, this is written in the fetch tab –
var counter = Number(reportContext.getPersistentGlobalVariable("Counter"));
var length = ConsecutiveShifts.length;
if (counter < length) {
var items = ConsecutiveShifts[counter];
row["Employee Id"] = items.EmployeeId;
row["Employee Full Name"] = items.EmployeeFullName;
row["Employee Hire Date"] = items.EmployeeHireDate;
row["Employee Gender"] = items.EmployeeGender;
row["Employee Permanent/Temporary"] = items.EmployeePermanentTemporary;
row["Employee Hierarchical Manager"] = items.EmployeeReportsTo;
row["Employee Job Title"] = items.EmployeeJobTitle;
row["Employee Position"] = items.EmployeeCerts;
row["Employee Start Date & Time"] = items.EmployeeStartDate;
row["Employee End Date & Time"] = items.EmployeeConsecutiveShifts;
row["Mail Triggered On"] = items.EmployeeConsecutiveShifts;
counter++;
reportContext.setPersistentGlobalVariable("Counter", counter.toString());
return true;
}
return false;
Jacquelyn Bloomfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.