@@ -455,6 +455,135 @@ EHR.DataEntryUtils = new function(){
455455 panel . onSubmit ( btn ) ;
456456 } ,
457457 disableOn : 'WARN'
458+ } ,
459+ /**
460+ * This button will give the option to close treatment orders associated with a case when closing the case.
461+ */
462+ SUBMITCASE : {
463+ text : 'Submit Final' ,
464+ name : 'submit' ,
465+ requiredQC : 'Completed' ,
466+ targetQC : 'Completed' ,
467+ errorThreshold : 'INFO' ,
468+ successURL : LABKEY . ActionURL . getParameter ( 'returnUrl' ) || LABKEY . ActionURL . buildURL ( 'ehr' , 'enterData.view' ) ,
469+ disabled : true ,
470+ itemId : 'submitBtn' ,
471+ handler : function ( btn ) {
472+ const panel = btn . up ( 'ehr-dataentrypanel' ) ;
473+
474+ const casesStore = panel . storeCollection . getClientStoreByName ( 'cases' ) ;
475+ const rec = casesStore ?. getAt ( 0 ) ;
476+ if ( ! rec ) {
477+ console . error ( 'Case record not found.' ) ;
478+ return ;
479+ }
480+
481+ const showConfirm = ( ) => {
482+ Ext4 . Msg . confirm ( 'Finalize Form' , 'You are about to finalize this form. Do you want to do this?' , function ( v ) {
483+ if ( v == 'yes' )
484+ this . onSubmit ( btn ) ;
485+ } , this ) ;
486+ }
487+
488+ const enddate = rec . get ( 'enddate' ) ;
489+ if ( enddate ) {
490+
491+ // Define this only when needed
492+ const closeTreatmentOrders = ( enddate , objectids ) => {
493+
494+ // Close treatment orders in form
495+ const treatmentOrderStore = panel . storeCollection . getClientStoreByName ( 'Treatment Orders' ) ;
496+ treatmentOrderStore . each ( ( order ) => {
497+ if ( ! order . get ( "enddate" ) ) {
498+ order . set ( "enddate" , enddate )
499+ }
500+ } , this ) ;
501+
502+ // If saved treatment orders associated with the case, then close those
503+ if ( objectids ) {
504+ const commands = [ ] ;
505+ const updateRows = [ ] ;
506+
507+ objectids . forEach ( row => {
508+ updateRows . push ( { objectid : row . objectid , enddate : enddate } ) ;
509+ } )
510+
511+ commands . push ( {
512+ command : 'update' ,
513+ schemaName : 'study' ,
514+ queryName : 'treatment_order' ,
515+ rows : updateRows
516+ } )
517+
518+ LABKEY . Query . saveRows ( {
519+ commands : commands ,
520+ scope : this ,
521+ failure : LDK . Utils . getErrorCallback ( ) ,
522+ success : function ( results ) {
523+ this . onSubmit ( btn ) ;
524+ }
525+ } ) ;
526+ }
527+ else {
528+ this . onSubmit ( btn ) ;
529+ }
530+ }
531+
532+ // First check if open treatment orders in the form
533+ let openFoundInForm = false ;
534+ const treatmentOrderStore = panel . storeCollection . getClientStoreByName ( 'Treatment Orders' ) ;
535+ treatmentOrderStore . each ( ( order ) => {
536+ if ( ! openFoundInForm && ! order . get ( "enddate" ) )
537+ openFoundInForm = true ;
538+ } , this ) ;
539+
540+ const caseId = rec . get ( 'caseid' ) ;
541+ if ( ! caseId ) {
542+ Ext4 . Msg . confirm ( 'Finalize Closed Case' , 'There are treatment orders still active for this case. Do you want to end all treatment orders on the same end date as this case?' , function ( v ) {
543+ if ( v == 'yes' ) {
544+ closeTreatmentOrders ( enddate ) ;
545+ }
546+ else {
547+ this . onSubmit ( btn ) ;
548+ }
549+ } , this ) ;
550+ return ;
551+ }
552+
553+ // Check for open treatment orders associated with the case
554+ LABKEY . Query . selectRows ( {
555+ schemaName : 'study' ,
556+ queryName : 'treatment_order' ,
557+ columns : 'objectid' ,
558+ filterArray : [
559+ LABKEY . Filter . create ( 'caseid' , caseId , LABKEY . Filter . Types . EQUAL ) ,
560+ LABKEY . Filter . create ( 'enddate' , null , LABKEY . Filter . Types . ISBLANK )
561+ ] ,
562+ failure : LDK . Utils . getErrorCallback ( ) ,
563+ scope : this ,
564+ success : function ( results ) {
565+ if ( results ?. rows ?. length || openFoundInForm ) {
566+ Ext4 . Msg . confirm ( 'Finalize Closed Case' , 'There are treatment orders still active for this case. Do you want to end all treatment orders on the same end date as this case?' , function ( v ) {
567+ if ( v == 'yes' ) {
568+ closeTreatmentOrders ( enddate , results ?. rows ) ;
569+ }
570+ else {
571+ this . onSubmit ( btn ) ;
572+ }
573+ } , this ) ;
574+ }
575+ else {
576+ showConfirm ( ) ;
577+ }
578+ }
579+ } ) ;
580+ }
581+
582+ else {
583+ showConfirm ( ) ;
584+ }
585+ } ,
586+ disableOn : 'WARN'
458587 }
459588 } ;
460589
0 commit comments