//Given a start date, returns a task's end date based on the duration.
function getEndDate(date, duration, numHolidays){
let endDay = date.getDate();
let endMonth = date.getMonth();
let endYear = date.getFullYear();
if(!numHolidays){
let numHolidays = 0;
}
endDay = endDay + duration + numHolidays;
let endDate = new Date(endYear, endMonth, endDay);
let weekday = endDate.getDay();
endDay = bumpForwardIfWeekend(weekday, endDay);
endDate = new Date(endYear, endMonth, endDay);
return endDate;
}
//Compares two dates and returns the later one.
function returnLaterDate(date1, date2){
if (date1 > date2) {
return date1;
} else if (date2 > date1) {
return date2;
}
}
//This function finds the child tasks for a given parent task and completes them with the appropriate start/end dates.
async function filterAndFill(parentName, parentEndDate){
let query = await table.selectRecordsAsync({sorts: [{field: antecedentField.id}]});
let children = query.records.filter(record => record.getCellValueAsString(antecedentField.name) === parentName);
if (children.length > 0){
for(let i = 0; i < children.length; i++){
let child = children[i];
let duration = child.getCellValue(durationField.name);
let childtask = child.getCellValueAsString(taskField.name);
let newStart = bumpForward(parentEndDate);
let dateArray = getDateStrArray(newStart, duration);
let numHolidays = countHolidaysinRange(holidays, dateArray);
let newEndDate = getEndDate(newStart, duration, numHolidays);
await table.updateRecordAsync(child, {[startField.id]: newStart, [endField.id]: newEndDate});
await filterAndFill(childtask, newEndDate);
}
}
let childrenWithMore = query.records.filter(record => (record.getCellValue(antecedentField)?.length ?? 0) > 1);
if (childrenWithMore.length > 0){
await filterAndFillMulti(childrenWithMore, parentName, parentEndDate);
}
}
//If a given task has multiple antecedents, this function will add the appropriate start/end dates.
async function filterAndFillMulti(childrenWithMore, parentName, parentEndDate){
for(let i = 0; i < childrenWithMore.length; i++){
let child = childrenWithMore[i];
let duration = child.getCellValue(durationField.name);
let childtask = child.getCellValueAsString(taskField.name);
let antecedent = child.getCellValueAsString(antecedentField.name);
if (antecedent.includes(parentName)) {
let iteration = completeMulti.filter(element => element.childtask === childtask);
if (iteration.length === 0) {
let newStart = bumpForward(parentEndDate);
let dateArray = getDateStrArray(newStart, duration);
let numHolidays = countHolidaysinRange(holidays, dateArray);
let newEndDate = getEndDate(newStart, duration, numHolidays);
await table.updateRecordAsync(child, {[startField.id]: newStart, [endField.id]: newEndDate});
let obj = {childtask, newStart};
completeMulti.push(obj);
await filterAndFill(childtask, newEndDate);
} else {
let currentStart = iteration[0].newStart;
let newStart = returnLaterDate(currentStart, parentEndDate);
if(newStart === currentStart){
}else{
let newStart = bumpForward(parentEndDate);
let dateArray = getDateStrArray(newStart, duration);
let numHolidays = countHolidaysinRange(holidays, dateArray);
let newEndDate = getEndDate(newStart, duration, numHolidays);
await table.updateRecordAsync(child, {[startField.id]: newStart, [endField.id]: newEndDate});
await filterAndFill(childtask, newEndDate);
let index = completeMulti.findIndex(element => element.childtask === childtask);
let obj = {childtask, newStart};
completeMulti.splice(index,1,obj);
}
}
}
}
}