SCAPMC28 ;ALB/REW - Patients with an Appointment ; 1/10/05 2:49pm
 ;;5.3;Scheduling;**41,140,346,564**;AUG 13, 1993;Build 8
 ;;1.0
PTAP(SCCL,SCDATES,SCMAXCNT,SCLIST,SCERR,MORE) ; -- list of patients with an appointment in a given clinic
 ; 
 ; input:
 ;  SCCL = Pointer to File #44
 ;  SCDATES("BEGIN") = begin date to search (inclusive)
 ;                      [default: TODAY]
 ;        ("END")   = end date to search (inclusive)
 ;                      [default: TODAY]
 ;        ("INCL")  = 1: only use patients who were assigned to
 ;                       team for entire date range
 ;                    0: anytime in date range
 ;                      [default: 1] 
 ;  SCMAXCNT        - Maximum # of patients to return, default=99
 ;  SCLIST -array name to store list
 ;          [ex. ^TMP("SCPT",$J)]
 ;        
 ;  SCERR = array NAME to store error messages.
 ;          [ex. ^TMP("ORXX",$J)]
 ;  MORE - This is a flag that says that this list exists and has been
 ;         aborted because it reached the maxcount.  If this =1 it means
 ;         'kill the old list & start where you finished'
 ;  Note: Don't Return DFNs where $D(^TMP("SCMC",$J,"EXCLUDE PT","SCPTA",+DFN)) is true
 ; Output:
 ;  SCLIST() = array of patients
 ;             Format:
 ;               Subscript: Sequential # from 1 to n
 ;               Piece     Description
 ;                 1       IEN of PATIENT file entry
 ;                 2       Name of patient
 ;                 3       ien to 40.7 - Not Stop Code!! stp=$$intstp
 ;                 4       AMIS reporting stop code
 ;                 5       Patient's Long ID (SSN)
 ;
 ; SCEFFDT - negative of effective date
 ; SCN     - current subscript (counter) 1->n
 ; SCPTA0   is 0 node of Patient Team Assignment file 1st piece is DFN
 ;  SCERR() = Array of DIALOG file messages(errors) .
 ;  @SCERR@(0)=number of errors, undefined if none
 ;             Foramt:
 ;               Subscript: Sequential # from 1 to n
 ;               Piece     Description
 ;                 1       IEN of DIALOG file
 ;
 ;   Returned: 1 if ok, 0 if error^More?
 ;
 ;
ST N SCEND,SCVSDT,SCSTART
 N SCLSEQ,SCN,SCESEQ,SCPARM,SCP,SCBEGIN,SCEND,SCINCL,SCDTS
 G:'$$OKDATA APQ ;check/setup variables
 ; -- loop through visit file
LP S SCDT=SCBEGIN
 S:'$P(SCEND,".",2) SCEND=$$FMADD^XLFDT(SCEND,1) ;ending is end of day
 IF $G(MORE) D
 .S SCSTART=$P($G(@SCLIST@(0)),U,2)
 .S SCBEGIN=$P($G(@SCLIST@(0)),U,3)
 .K @SCLIST
APQ Q $$PTAPX(.SCCL,.SCBEGIN,.SCEND,.SCMAXCNT,.SCLIST,.SCERR,.SCSTART)
 ;
PTAPX(SCCL,SCBEGIN,SCEND,MAXCNT,SCLIST,SCERR,SCSTART) ;return appointments in dt range
 ; Input: (As above plus:)
 ;    SCSTART - Continue with list at this point
 ; output: SCN - COUNT OF PTS
 ; returns:      dfn^ptname^clinic^apptdt^long id
 ; 
 ;initialize variables
 N SCDT,SCARRAY,DFN,SDAPTCNT,SDARRAY,SDERR,SDX,SDY
 K ^TMP($J,"SDAMA301")
 ;setup call to SDAPI
 ;filter for OUTPATIENT ENCOUNTER (OE) pointer for only "KEPT" appointment- sd/564
 ;include field 12 - pointer to OE file
 S SDARRAY(1)=$G(SCBEGIN)_";"_$G(SCEND),SDARRAY(2)=$G(SCCL),SDARRAY("FLDS")="4;12"
 S SDARRAY("SORT")="P"
 ;call SDAPI to retrieve appointments
 S SDAPTCNT=$$SDAPI^SDAMA301(.SDARRAY)
 ;handle errors if any returned from SDAPI and QUIT
 I SDAPTCNT<0 D  Q ($G(@SCERR@(0))<1)_U_(SCN'<SCMAXCNT)
 .;call existing error handler
 .D ERR^SCAPMCU1(.SCESEQ,,,"",.SCERR)
 .K ^TMP($J,"SDAMA301")
 ;if appointments returned 
 I SDAPTCNT>0 D
 .;retrieve patient ID to start at if continuing list (was appt ifn)
 .; * no code could be found to utilize continuation of a list
 .; * if this changes this code should be revisited to ensure only 1
 .;   call to SDAPI is made.
 .S DFN=+$G(SCSTART)
 .S SCSTART=0
 .S SCDT=0
 .;resort appts to ensure same data is returned to user
 .;only 1st appt date/time is needed for each patient
 .;as patient can only be added to the list once.
 .K ^TMP($J,"RE-SORT","SDAMA301")
 .;
 .;identify appointment entries without pointers to OE to exclude them - SD/564
 .S (SDY,SDX)=0
 .F  S SDX=$O(^TMP($J,"SDAMA301",SDX)) Q:'SDX  D
 ..S SDY="" F  S SDY=$O(^TMP($J,"SDAMA301",SDX,SDY)) Q:SDY=""  D
 ...;eliminate not kept appointments; no entry in OUTPATIENT ENCOUNTER
 ...I $P(^TMP($J,"SDAMA301",SDX,SDY),U,12)="" K ^TMP($J,"SDAMA301",SDX,SDY)
 .;
 .S (SDY,SDX)=0
 .F  S SDX=$O(^TMP($J,"SDAMA301",SDX)) Q:'SDX  D
 ..S SDY=$O(^TMP($J,"SDAMA301",SDX,""))
 ..S ^TMP($J,"RE-SORT","SDAMA301",SDY,SDX)=""
 .K ^TMP($J,"SDAMA301")
 .;loop through re-sorted appts returned from SDAPI until
 .; 1. no more patients with appointments exist
 .; 2. number of patients found that match criteria is not less than max
 .F  S SCDT=$O(^TMP($J,"RE-SORT","SDAMA301",SCDT)) Q:'SCDT!(SCN'<SCMAXCNT)  D
 ..;get patient for the kept appointment in the re-sorted list
 ..F  S DFN=$O(^TMP($J,"RE-SORT","SDAMA301",SCDT,DFN)) Q:'DFN!(SCN'<SCMAXCNT)  D
 ...;quit if patient is found in either of the following lists
 ...;this list may be used elsewhere, left in for compatibility
 ...Q:$D(@SCLIST@("SCPTAP",+DFN))
 ...Q:$D(^TMP("SCMC",$J,"EXCLUDE PT","SCPTA",+DFN))
 ...;increment the patient counter and store in SCLIST
 ...S SCN=$G(@SCLIST@(0))+1
 ...S @SCLIST@(0)=SCN
 ...;get the patient's long ID (SSN) and Name
 ...D GETS^DIQ(2,+DFN,".01;.363","","SCARRAY")
 ...;add the following appt info to SCLIST at the current Patient Counter
 ...;1. Patient DFN 2. Patient Name 3. Clinic IEN 4. Appt DTM 5. Patients Long ID
 ...S @SCLIST@(SCN)=DFN_U_$G(SCARRAY(2,+DFN_",",.01))_U_SCCL_U_SCDT_U_$G(SCARRAY(2,+DFN_",",.363))
 ...;add the patient's DFN to the exclusion list
 ...S @SCLIST@("SCPTAP",+DFN,+SCN)=""
 ;kill the re-sorted appt global reference generated
 K ^TMP($J,"RE-SORT","SDAMA301")
 ;if # of patients found that match criteria is NOT LESS than the requested Max then
 ;set SCLIST at the 0 Node to:
 ;1.Current Patient Count 2. Current Patient Processing 3. Appt DTM 4. Clinic IEN
 S:(SCN'<SCMAXCNT) @SCLIST@(0)=SCN_U_+$G(DFN)_U_+$G(SCDT)_U_+$G(SCCL)
 Q ($G(@SCERR@(0))<1)_U_(SCN'<SCMAXCNT)
 ;
OKDATA() ;check/setup variables
 N SCOK
 S SCOK=1
 S SCMAXCNT=$G(SCMAXCNT,99)
 D INIT^SCAPMCU1(.SCOK) ; set default dates & error array (if undefined)
 IF '$D(^SC(+$G(SCCL),0)) D  S SCOK=0
 . S SCPARM("CLINIC")=$G(SCCL,"Undefined")
 . D ERR^SCAPMCU1(.SCESEQ,4045101,.SCPARM,"",.SCERR)
 ; -- is it a valid TEAM ien passed (Error # 4045101 in DIALOG file)
 Q SCOK
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HSCAPMC28   6501     printed  Sep 23, 2025@20:14:23                                                                                                                                                                                                    Page 2
SCAPMC28  ;ALB/REW - Patients with an Appointment ; 1/10/05 2:49pm
 +1       ;;5.3;Scheduling;**41,140,346,564**;AUG 13, 1993;Build 8
 +2       ;;1.0
PTAP(SCCL,SCDATES,SCMAXCNT,SCLIST,SCERR,MORE) ; -- list of patients with an appointment in a given clinic
 +1       ; 
 +2       ; input:
 +3       ;  SCCL = Pointer to File #44
 +4       ;  SCDATES("BEGIN") = begin date to search (inclusive)
 +5       ;                      [default: TODAY]
 +6       ;        ("END")   = end date to search (inclusive)
 +7       ;                      [default: TODAY]
 +8       ;        ("INCL")  = 1: only use patients who were assigned to
 +9       ;                       team for entire date range
 +10      ;                    0: anytime in date range
 +11      ;                      [default: 1] 
 +12      ;  SCMAXCNT        - Maximum # of patients to return, default=99
 +13      ;  SCLIST -array name to store list
 +14      ;          [ex. ^TMP("SCPT",$J)]
 +15      ;        
 +16      ;  SCERR = array NAME to store error messages.
 +17      ;          [ex. ^TMP("ORXX",$J)]
 +18      ;  MORE - This is a flag that says that this list exists and has been
 +19      ;         aborted because it reached the maxcount.  If this =1 it means
 +20      ;         'kill the old list & start where you finished'
 +21      ;  Note: Don't Return DFNs where $D(^TMP("SCMC",$J,"EXCLUDE PT","SCPTA",+DFN)) is true
 +22      ; Output:
 +23      ;  SCLIST() = array of patients
 +24      ;             Format:
 +25      ;               Subscript: Sequential # from 1 to n
 +26      ;               Piece     Description
 +27      ;                 1       IEN of PATIENT file entry
 +28      ;                 2       Name of patient
 +29      ;                 3       ien to 40.7 - Not Stop Code!! stp=$$intstp
 +30      ;                 4       AMIS reporting stop code
 +31      ;                 5       Patient's Long ID (SSN)
 +32      ;
 +33      ; SCEFFDT - negative of effective date
 +34      ; SCN     - current subscript (counter) 1->n
 +35      ; SCPTA0   is 0 node of Patient Team Assignment file 1st piece is DFN
 +36      ;  SCERR() = Array of DIALOG file messages(errors) .
 +37      ;  @SCERR@(0)=number of errors, undefined if none
 +38      ;             Foramt:
 +39      ;               Subscript: Sequential # from 1 to n
 +40      ;               Piece     Description
 +41      ;                 1       IEN of DIALOG file
 +42      ;
 +43      ;   Returned: 1 if ok, 0 if error^More?
 +44      ;
 +45      ;
ST         NEW SCEND,SCVSDT,SCSTART
 +1        NEW SCLSEQ,SCN,SCESEQ,SCPARM,SCP,SCBEGIN,SCEND,SCINCL,SCDTS
 +2       ;check/setup variables
           if '$$OKDATA
               GOTO APQ
 +3       ; -- loop through visit file
LP         SET SCDT=SCBEGIN
 +1       ;ending is end of day
           if '$PIECE(SCEND,".",2)
               SET SCEND=$$FMADD^XLFDT(SCEND,1)
 +2        IF $GET(MORE)
               Begin DoDot:1
 +3                SET SCSTART=$PIECE($GET(@SCLIST@(0)),U,2)
 +4                SET SCBEGIN=$PIECE($GET(@SCLIST@(0)),U,3)
 +5                KILL @SCLIST
               End DoDot:1
APQ        QUIT $$PTAPX(.SCCL,.SCBEGIN,.SCEND,.SCMAXCNT,.SCLIST,.SCERR,.SCSTART)
 +1       ;
PTAPX(SCCL,SCBEGIN,SCEND,MAXCNT,SCLIST,SCERR,SCSTART) ;return appointments in dt range
 +1       ; Input: (As above plus:)
 +2       ;    SCSTART - Continue with list at this point
 +3       ; output: SCN - COUNT OF PTS
 +4       ; returns:      dfn^ptname^clinic^apptdt^long id
 +5       ; 
 +6       ;initialize variables
 +7        NEW SCDT,SCARRAY,DFN,SDAPTCNT,SDARRAY,SDERR,SDX,SDY
 +8        KILL ^TMP($JOB,"SDAMA301")
 +9       ;setup call to SDAPI
 +10      ;filter for OUTPATIENT ENCOUNTER (OE) pointer for only "KEPT" appointment- sd/564
 +11      ;include field 12 - pointer to OE file
 +12       SET SDARRAY(1)=$GET(SCBEGIN)_";"_$GET(SCEND)
           SET SDARRAY(2)=$GET(SCCL)
           SET SDARRAY("FLDS")="4;12"
 +13       SET SDARRAY("SORT")="P"
 +14      ;call SDAPI to retrieve appointments
 +15       SET SDAPTCNT=$$SDAPI^SDAMA301(.SDARRAY)
 +16      ;handle errors if any returned from SDAPI and QUIT
 +17       IF SDAPTCNT<0
               Begin DoDot:1
 +18      ;call existing error handler
 +19               DO ERR^SCAPMCU1(.SCESEQ,,,"",.SCERR)
 +20               KILL ^TMP($JOB,"SDAMA301")
               End DoDot:1
               QUIT ($GET(@SCERR@(0))<1)_U_(SCN'<SCMAXCNT)
 +21      ;if appointments returned 
 +22       IF SDAPTCNT>0
               Begin DoDot:1
 +23      ;retrieve patient ID to start at if continuing list (was appt ifn)
 +24      ; * no code could be found to utilize continuation of a list
 +25      ; * if this changes this code should be revisited to ensure only 1
 +26      ;   call to SDAPI is made.
 +27               SET DFN=+$GET(SCSTART)
 +28               SET SCSTART=0
 +29               SET SCDT=0
 +30      ;resort appts to ensure same data is returned to user
 +31      ;only 1st appt date/time is needed for each patient
 +32      ;as patient can only be added to the list once.
 +33               KILL ^TMP($JOB,"RE-SORT","SDAMA301")
 +34      ;
 +35      ;identify appointment entries without pointers to OE to exclude them - SD/564
 +36               SET (SDY,SDX)=0
 +37               FOR 
                       SET SDX=$ORDER(^TMP($JOB,"SDAMA301",SDX))
                       if 'SDX
                           QUIT 
                       Begin DoDot:2
 +38                       SET SDY=""
                           FOR 
                               SET SDY=$ORDER(^TMP($JOB,"SDAMA301",SDX,SDY))
                               if SDY=""
                                   QUIT 
                               Begin DoDot:3
 +39      ;eliminate not kept appointments; no entry in OUTPATIENT ENCOUNTER
 +40                               IF $PIECE(^TMP($JOB,"SDAMA301",SDX,SDY),U,12)=""
                                       KILL ^TMP($JOB,"SDAMA301",SDX,SDY)
                               End DoDot:3
                       End DoDot:2
 +41      ;
 +42               SET (SDY,SDX)=0
 +43               FOR 
                       SET SDX=$ORDER(^TMP($JOB,"SDAMA301",SDX))
                       if 'SDX
                           QUIT 
                       Begin DoDot:2
 +44                       SET SDY=$ORDER(^TMP($JOB,"SDAMA301",SDX,""))
 +45                       SET ^TMP($JOB,"RE-SORT","SDAMA301",SDY,SDX)=""
                       End DoDot:2
 +46               KILL ^TMP($JOB,"SDAMA301")
 +47      ;loop through re-sorted appts returned from SDAPI until
 +48      ; 1. no more patients with appointments exist
 +49      ; 2. number of patients found that match criteria is not less than max
 +50               FOR 
                       SET SCDT=$ORDER(^TMP($JOB,"RE-SORT","SDAMA301",SCDT))
                       if 'SCDT!(SCN'<SCMAXCNT)
                           QUIT 
                       Begin DoDot:2
 +51      ;get patient for the kept appointment in the re-sorted list
 +52                       FOR 
                               SET DFN=$ORDER(^TMP($JOB,"RE-SORT","SDAMA301",SCDT,DFN))
                               if 'DFN!(SCN'<SCMAXCNT)
                                   QUIT 
                               Begin DoDot:3
 +53      ;quit if patient is found in either of the following lists
 +54      ;this list may be used elsewhere, left in for compatibility
 +55                               if $DATA(@SCLIST@("SCPTAP",+DFN))
                                       QUIT 
 +56                               if $DATA(^TMP("SCMC",$JOB,"EXCLUDE PT","SCPTA",+DFN))
                                       QUIT 
 +57      ;increment the patient counter and store in SCLIST
 +58                               SET SCN=$GET(@SCLIST@(0))+1
 +59                               SET @SCLIST@(0)=SCN
 +60      ;get the patient's long ID (SSN) and Name
 +61                               DO GETS^DIQ(2,+DFN,".01;.363","","SCARRAY")
 +62      ;add the following appt info to SCLIST at the current Patient Counter
 +63      ;1. Patient DFN 2. Patient Name 3. Clinic IEN 4. Appt DTM 5. Patients Long ID
 +64                               SET @SCLIST@(SCN)=DFN_U_$GET(SCARRAY(2,+DFN_",",.01))_U_SCCL_U_SCDT_U_$GET(SCARRAY(2,+DFN_",",.363))
 +65      ;add the patient's DFN to the exclusion list
 +66                               SET @SCLIST@("SCPTAP",+DFN,+SCN)=""
                               End DoDot:3
                       End DoDot:2
               End DoDot:1
 +67      ;kill the re-sorted appt global reference generated
 +68       KILL ^TMP($JOB,"RE-SORT","SDAMA301")
 +69      ;if # of patients found that match criteria is NOT LESS than the requested Max then
 +70      ;set SCLIST at the 0 Node to:
 +71      ;1.Current Patient Count 2. Current Patient Processing 3. Appt DTM 4. Clinic IEN
 +72       if (SCN'<SCMAXCNT)
               SET @SCLIST@(0)=SCN_U_+$GET(DFN)_U_+$GET(SCDT)_U_+$GET(SCCL)
 +73       QUIT ($GET(@SCERR@(0))<1)_U_(SCN'<SCMAXCNT)
 +74      ;
OKDATA()  ;check/setup variables
 +1        NEW SCOK
 +2        SET SCOK=1
 +3        SET SCMAXCNT=$GET(SCMAXCNT,99)
 +4       ; set default dates & error array (if undefined)
           DO INIT^SCAPMCU1(.SCOK)
 +5        IF '$DATA(^SC(+$GET(SCCL),0))
               Begin DoDot:1
 +6                SET SCPARM("CLINIC")=$GET(SCCL,"Undefined")
 +7                DO ERR^SCAPMCU1(.SCESEQ,4045101,.SCPARM,"",.SCERR)
               End DoDot:1
               SET SCOK=0
 +8       ; -- is it a valid TEAM ien passed (Error # 4045101 in DIALOG file)
 +9        QUIT SCOK