diff --git a/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js b/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js index d382b863b..30a298a30 100644 --- a/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js +++ b/onprc_ehr/resources/scripts/onprc_ehr/onprc_triggers.js @@ -1256,85 +1256,118 @@ exports.init = function(EHR){ } } }); + + //Added by Kollil, April 2026 + function toDateOnly(val) { + if (!val) + return null; + + // row.date object + if (typeof val === 'object' && val.time) { + var d = new Date(val.time); + return new Date(d.getFullYear(), d.getMonth(), d.getDate()); + } + + // date strings like "2026-04-20 00:00:00.000" + var s = String(val).split(' ')[0]; // keep only YYYY-MM-DD + var parts = s.split('-'); + if (parts.length !== 3) + return null; + + return new Date(Number(parts[0]), Number(parts[1]) - 1, Number(parts[2])); + } + // Added 10-17-2025 R. Blasa EHR.Server.TriggerManager.unregisterAllHandlersForQueryNameAndEvent('study', 'assignment', EHR.Server.TriggerManager.Events.BEFORE_UPSERT); - EHR.Server.TriggerManager.registerHandlerForQuery(EHR.Server.TriggerManager.Events.BEFORE_UPSERT, 'study', 'assignment', function(helper, scriptErrors, row, oldRow){ - if (!helper.isETL()){ + EHR.Server.TriggerManager.registerHandlerForQuery(EHR.Server.TriggerManager.Events.BEFORE_UPSERT, 'study', 'assignment', function(helper, scriptErrors, row, oldRow) { + if (!helper.isETL()) { //note: the the date field is handled above by removeTimeFromDate EHR.Server.Utils.removeTimeFromDate(row, scriptErrors, 'enddate'); EHR.Server.Utils.removeTimeFromDate(row, scriptErrors, 'projectedRelease'); } - //check number of allowed animals at assign/approve time if (!helper.isETL() && !helper.isQuickValidation() && helper.doStandardProtocolCountValidation() && //this is designed to always perform the check on imports, but also updates where the Id was changed - !(oldRow && oldRow.Id && oldRow.Id==row.Id) && + !(oldRow && oldRow.Id && oldRow.Id == row.Id) && row.Id && row.project && row.date - ){ + ) { var assignmentsInTransaction = helper.getProperty('assignmentsInTransaction'); assignmentsInTransaction = assignmentsInTransaction || []; var msgs = helper.getJavaHelper().verifyProtocolCounts(row.Id, row.project, assignmentsInTransaction); - if (msgs){ + if (msgs) { msgs = msgs.split("<>"); - for (var i=0;i row.enddatefinalized.getTime()){ - row.enddatefinalized = row.enddate; - } + // we want to record the date a record was marked endded, in addition to the actual end itself + // NOTE: we only do this when both enddate and releaseType are entered + if (!row.enddatefinalized && row.enddate && row.releaseCondition && EHR.Server.Security.getQCStateByLabel(row.QCStateLabel).PublicData) { + //note: if ended in the future, defer to that date + row.enddatefinalized = new Date(); + if (row.enddate.getTime() > row.enddatefinalized.getTime()) { + row.enddatefinalized = row.enddate; } + } - //check for condition downgrade for assign condition - if (!helper.isETL() && row.Id && row.assignCondition){ - var msg = triggerHelper.checkForConditionDowngrade(row.Id, row.date, row.assignCondition); - if (msg){ - EHR.Server.Utils.addError(scriptErrors, 'assignCondition', msg, 'INFO'); - } + //check for condition downgrade for assign condition + if (!helper.isETL() && row.Id && row.assignCondition) { + var msg = triggerHelper.checkForConditionDowngrade(row.Id, row.date, row.assignCondition); + if (msg) { + EHR.Server.Utils.addError(scriptErrors, 'assignCondition', msg, 'INFO'); } + } - //check for condition downgrade for assign condition - if (!helper.isETL() && row.Id && row.date && row.assignCondition){ - var msg = triggerHelper.checkForConditionDowngrade(row.Id, row.date, row.assignCondition); - if (msg){ - EHR.Server.Utils.addError(scriptErrors, 'assignCondition', msg, 'INFO'); - } + //check for condition downgrade for assign condition + if (!helper.isETL() && row.Id && row.date && row.assignCondition) { + var msg = triggerHelper.checkForConditionDowngrade(row.Id, row.date, row.assignCondition); + if (msg) { + EHR.Server.Utils.addError(scriptErrors, 'assignCondition', msg, 'INFO'); } + } + + //Added by Kollil, April 2026. Refer to ticket # 13807 + if (row.project && row.date) { + var projectEndDate = triggerHelper.getProjectEndDate(row.project); //helper.getJavaHelper().getProjectEndDate(row.project); + var assignmentDate = toDateOnly(row.date); + var endDate = toDateOnly(projectEndDate); + + if (assignmentDate && endDate && assignmentDate.getTime() > endDate.getTime()) { + // console.log('4.5 DEBUG assignmentDate=' + assignmentDate); + EHR.Server.Utils.addError(scriptErrors, 'project', 'The assignment start date occurs after the end date of the selected center project. Please review the assignment date and confirm!', 'WARN'); + + } + } + }); //Added 10-5-2022 R.Blasa diff --git a/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java b/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java index af711cba9..de49e9cc6 100644 --- a/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java +++ b/onprc_ehr/src/org/labkey/onprc_ehr/query/ONPRC_EHRTriggerHelper.java @@ -2021,6 +2021,33 @@ public boolean requiresAssistingStaff(Integer procedureId) return "Surgery".equals(category); } + //Added by Kollil + //Date: Apr 2026 + public Date getProjectEndDate(Object projectId) + { + if (projectId == null) + return null; + + int pid; + if (projectId instanceof Number) + pid = ((Number) projectId).intValue(); + else + pid = Integer.parseInt(projectId.toString()); + + UserSchema ehrSchema = QueryService.get().getUserSchema(_user, _container, "ehr"); + if (ehrSchema == null) + return null; + + TableInfo ti = ehrSchema.getTable("project"); + if (ti == null) + return null; + + SimpleFilter filter = new SimpleFilter(FieldKey.fromString("project"), pid); + TableSelector ts = new TableSelector(ti, Collections.singleton("enddate"), filter, null); + + return ts.getObject(Date.class); + } + public String getSpeciesForDam(String dam) { return new TableSelector(getTableInfo("study", "demographics"), PageFlowUtil.set("species"), new SimpleFilter(FieldKey.fromString("Id"), dam), null).getObject(String.class);