SDAMA203 ;BPIOFO/ACS-Scheduling API for IMO ;15 April 2003
 ;;5.3;Scheduling;**285,406**;13 Aug 1993
 ;
 ;Scheduling API to return encounter or appointment date/time for
 ;a patient that can receive inpatient medication from an
 ;authorized clinic
 ;
 ;**********************************************************************
 ;                        CHANGE LOG
 ;
 ;  DATE      PATCH       DESCRIPTION                 DEVELOPER
 ;--------  ----------    ----------------------------------------------
 ;04/15/03  SD*5.3*285    ROUTINE WRITTEN             A SAUNDERS
 ;10/12/06  SD*5.3*406    FIXED ERROR CODE -3         A SAUNDERS
 ;
 ;**********************************************************************
 ;
 ;          **** TO BE CALLED WITH AN EXTRINISIC CALL ****
 ;Example:  I $$SDIMO^SDAMA203(CLIEN,DFN) S APPTDT=SDIMO(1) K SDIMO(1)
 ;
 ;INPUT
 ;  SDCLIEN      Clinic IEN (required)
 ;  SDPATDFN     Patient DFN (required)
 ;  
 ;OUTPUT
 ;  The extrinsic call will return one of the following values:
 ;     1   Patient has at least one scheduled appointment or checked-in
 ;         visit in an authorized clinic
 ;     0   Patient has no scheduled appointments or checked-in visits
 ;         in an authorized clinic
 ;    -1   Clinic is not an authorized clinic, clinic is inactive,
 ;         or SDCLIEN is null
 ;    -2   SDPATDFN is null
 ;    -3   Scheduling database is unavailable
 ;  
 ;  If a 1 is returned, then SDIMO(1) = Encounter or appointment 
 ;  date/time in FileMan format
 ;   
 ;**********************************************************************
 ;   Special Logic:
 ; - In line tag SDVISIT, the ACRP Toolkit API EXOE^SDOE is called 
 ;   multiple times as needed.  This API returns the NEXT encounter, 
 ;   given a start and end date/time.  We want to check ALL encounters
 ;   for a match on clinic IEN
 ; - In line tag SDDATE, if the current time is between midnight and 6am,
 ;   the API will start to look for encounters and/or appointments on the 
 ;   previous day
 ;
 ;  Internal variables:
 ;  SDBACK   Contains the value to be returned from this call.  See 
 ;           above for OUTPUT values and corresponding definitions
 ;  SDCONT   Flag to indicate if processing should continue.  If
 ;           the patient has an encounter in an authorized clinic
 ;           today, then we can skip the last step and not look for
 ;           a scheduled appointment
 ;  SDFROM   The date to start searching for an encounter or appointment
 ;  SDAPPTDT Encounter or appointment date/time returned in SDIMO(1)
 ;
 ;**********************************************************************
SDIMO(SDCLIEN,SDPATDFN) ;
 ;
 ;--INITIALIZATION--
 K SDIMO(1)
 N SDBACK,SDCONT,SDFROM,SDAPPTDT
 S SDBACK=1,SDCONT=1,SDAPPTDT=0,SDFROM=0
 ;
 ;--MAIN--
 ; Valid variables passed in?
 D SDVALVAR($G(SDPATDFN),$G(SDCLIEN),.SDBACK)
 ; If no error, is clinic active and authorized?
 I SDBACK=1 D SDAUTHCL(SDCLIEN,.SDBACK)
 ; If no error, set up search "start" date
 I SDBACK=1 D SDDATE(.SDFROM)
 ; If no error, does patient have an encounter in that clinic?
 I SDBACK=1 D SDVISIT(SDPATDFN,SDCLIEN,.SDAPPTDT,.SDCONT,.SDBACK,SDFROM)
 ; If no error and no encounter, does patient have an appointment in that
 ; clinic?
 I SDBACK=1,SDCONT=1 D SDAPPT(SDPATDFN,SDCLIEN,.SDAPPTDT,.SDBACK,SDFROM)
 ;
 ;--FINALIZATION--
 ; If no error
 I SDBACK=1 D
 . ; Set up output array with the encounter or appointment date/time
 . ; Make sure the appointment date/time exists in SDAPPTDT
 . I $G(SDAPPTDT)]"" D
 .. S SDIMO(1)=SDAPPTDT
 . I $G(SDAPPTDT)']"" D
 .. S SDBACK=0
 ; Return value
 Q SDBACK
 ;
 ;----------------------------------------------------------------------
 ;-Validate input variables
SDVALVAR(SDPATDFN,SDCLIEN,SDBACK) ;
 ; Clinic IEN and patient DFN cannot be null
 I $G(SDCLIEN)="" S SDBACK=-1 Q
 I $G(SDPATDFN)="" S SDBACK=-2 Q
 Q
 ;
 ;-Clinic must be type "C", authorized to administer inpatient meds,
 ;-and active
SDAUTHCL(SDCLIEN,SDBACK) ;
 N SDAUTH,SDTYPE
 S SDAUTH=0,SDTYPE=0
 ; clinic must be type "C"
 S SDTYPE=$P($G(^SC(SDCLIEN,0)),"^",3)
 I $G(SDTYPE)="C" D
 . ; clinic must be authorized to administer inpatient meds
 . I $D(^SC("AE",1,SDCLIEN)) S SDAUTH=1
 I SDAUTH'=1 S SDBACK=-1 Q
 ; clinic must be active
 ; if clinic inactivate date exists, check further
 N SDINACT,SDREACT
 S SDINACT=$P($G(^SC(SDCLIEN,"I")),"^",1)
 I $G(SDINACT)]"" D
 . ; if inactivate date is today or earlier, get reactivate date
 . I SDINACT'>DT D
 .. S SDREACT=$P($G(^SC(SDCLIEN,"I")),"^",2)
 .. ; reactivate date can't be null
 .. I $G(SDREACT)="" S SDBACK=-1
 .. ; if reactivate date exists
 .. E  D
 ... ; reactivate date must be less than or equal to today
 ... ; but greater than or equal to inactivate date
 ... I (SDREACT>DT!(SDREACT<SDINACT)) S SDBACK=-1
 Q
 ;-Set up start date for encounters and appointments
SDDATE(SDFROM) ;
 N %,X
 D NOW^%DTC
 ;if the current time is before 6am, set 'start' date to yesterday
 I ("."_$P(%,".",2))<.060000 S SDFROM=(X-1)
 E  S SDFROM=X
 Q
 ;-Look for encounter that occurred in the authorized clinic
SDVISIT(SDPATDFN,SDCLIEN,SDAPPTDT,SDCONT,SDBACK,SDFROM) ;
 N SDSTART,SDEND,SDENCNUM,SDENCDT,SDENCCL
 ; set up start and end date/time
 S SDSTART=SDFROM_".0000"
 S SDEND=DT_".2359"
 ; get encounters
 F  D  Q:+SDENCNUM=0
 . ; call API to get next encounter
 . S SDENCNUM=+$$EXOE^SDOE(SDPATDFN,SDSTART,SDEND)
 . I $G(SDENCNUM) D
 .. ; encounter found.  call API to get more encounter data
 .. D GETGEN^SDOE(SDENCNUM,"SDDATA")
 .. I $G(SDDATA(0)) D
 ... ; get encounter date/time and clinic IEN
 ... S SDENCDT=$P($G(SDDATA(0)),"^",1),SDENCCL=$P($G(SDDATA(0)),"^",4)
 ... ; if encounter clinic matches authorized clinic, set flags
 ... I $G(SDENCCL)=SDCLIEN S SDENCNUM=0,SDCONT=0,SDAPPTDT=$G(SDENCDT)
 ... ; if no match on clinic, reset start date for next encounter
 ... I $G(SDENCCL)'=SDCLIEN S SDSTART=(SDENCDT+.000001)
 ... K SDDATA
 Q
 ;-Look for scheduled appointment in the authorized clinic
SDAPPT(SDPATDFN,SDCLIEN,SDAPPTDT,SDBACK,SDFROM) ;
 N SDRESULT,SDAPPTCL,SDMATCH
 S SDMATCH=0
 ; call API to get appointments for this patient
 D GETAPPT^SDAMA201(SDPATDFN,"1;2","R;NT",SDFROM,,.SDRESULT)
 ; SDRESULT contains a count of the returned appointments
 I SDRESULT>0 D
 . N SDI
 . ; spin through returned appointments and look for match on clinic IEN
 . F SDI=1:1:SDRESULT D  Q:SDMATCH=1
 .. S SDAPPTCL=$G(^TMP($J,"SDAMA201","GETAPPT",SDI,2))
 .. I +$G(SDAPPTCL)=SDCLIEN D
 ... S SDAPPTDT=$G(^TMP($J,"SDAMA201","GETAPPT",SDI,1))
 ... S SDMATCH=1
 . ; delete appointment array returned from Scheduling API
 . K ^TMP($J,"SDAMA201","GETAPPT")
 I ((SDRESULT=0)!(SDMATCH=0)) S SDBACK=0
 I SDRESULT=-1 D
 . S SDBACK=0
 . ; if database unavailable, set database-specific flag
 . I $D(^TMP($J,"SDAMA201","GETAPPT","ERROR",101)) S SDBACK=-3
 . ; delete error array returned from Scheduling API
 . K ^TMP($J,"SDAMA201","GETAPPT")
 Q
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HSDAMA203   7058     printed  Sep 23, 2025@20:23:36                                                                                                                                                                                                    Page 2
SDAMA203  ;BPIOFO/ACS-Scheduling API for IMO ;15 April 2003
 +1       ;;5.3;Scheduling;**285,406**;13 Aug 1993
 +2       ;
 +3       ;Scheduling API to return encounter or appointment date/time for
 +4       ;a patient that can receive inpatient medication from an
 +5       ;authorized clinic
 +6       ;
 +7       ;**********************************************************************
 +8       ;                        CHANGE LOG
 +9       ;
 +10      ;  DATE      PATCH       DESCRIPTION                 DEVELOPER
 +11      ;--------  ----------    ----------------------------------------------
 +12      ;04/15/03  SD*5.3*285    ROUTINE WRITTEN             A SAUNDERS
 +13      ;10/12/06  SD*5.3*406    FIXED ERROR CODE -3         A SAUNDERS
 +14      ;
 +15      ;**********************************************************************
 +16      ;
 +17      ;          **** TO BE CALLED WITH AN EXTRINISIC CALL ****
 +18      ;Example:  I $$SDIMO^SDAMA203(CLIEN,DFN) S APPTDT=SDIMO(1) K SDIMO(1)
 +19      ;
 +20      ;INPUT
 +21      ;  SDCLIEN      Clinic IEN (required)
 +22      ;  SDPATDFN     Patient DFN (required)
 +23      ;  
 +24      ;OUTPUT
 +25      ;  The extrinsic call will return one of the following values:
 +26      ;     1   Patient has at least one scheduled appointment or checked-in
 +27      ;         visit in an authorized clinic
 +28      ;     0   Patient has no scheduled appointments or checked-in visits
 +29      ;         in an authorized clinic
 +30      ;    -1   Clinic is not an authorized clinic, clinic is inactive,
 +31      ;         or SDCLIEN is null
 +32      ;    -2   SDPATDFN is null
 +33      ;    -3   Scheduling database is unavailable
 +34      ;  
 +35      ;  If a 1 is returned, then SDIMO(1) = Encounter or appointment 
 +36      ;  date/time in FileMan format
 +37      ;   
 +38      ;**********************************************************************
 +39      ;   Special Logic:
 +40      ; - In line tag SDVISIT, the ACRP Toolkit API EXOE^SDOE is called 
 +41      ;   multiple times as needed.  This API returns the NEXT encounter, 
 +42      ;   given a start and end date/time.  We want to check ALL encounters
 +43      ;   for a match on clinic IEN
 +44      ; - In line tag SDDATE, if the current time is between midnight and 6am,
 +45      ;   the API will start to look for encounters and/or appointments on the 
 +46      ;   previous day
 +47      ;
 +48      ;  Internal variables:
 +49      ;  SDBACK   Contains the value to be returned from this call.  See 
 +50      ;           above for OUTPUT values and corresponding definitions
 +51      ;  SDCONT   Flag to indicate if processing should continue.  If
 +52      ;           the patient has an encounter in an authorized clinic
 +53      ;           today, then we can skip the last step and not look for
 +54      ;           a scheduled appointment
 +55      ;  SDFROM   The date to start searching for an encounter or appointment
 +56      ;  SDAPPTDT Encounter or appointment date/time returned in SDIMO(1)
 +57      ;
 +58      ;**********************************************************************
SDIMO(SDCLIEN,SDPATDFN) ;
 +1       ;
 +2       ;--INITIALIZATION--
 +3        KILL SDIMO(1)
 +4        NEW SDBACK,SDCONT,SDFROM,SDAPPTDT
 +5        SET SDBACK=1
           SET SDCONT=1
           SET SDAPPTDT=0
           SET SDFROM=0
 +6       ;
 +7       ;--MAIN--
 +8       ; Valid variables passed in?
 +9        DO SDVALVAR($GET(SDPATDFN),$GET(SDCLIEN),.SDBACK)
 +10      ; If no error, is clinic active and authorized?
 +11       IF SDBACK=1
               DO SDAUTHCL(SDCLIEN,.SDBACK)
 +12      ; If no error, set up search "start" date
 +13       IF SDBACK=1
               DO SDDATE(.SDFROM)
 +14      ; If no error, does patient have an encounter in that clinic?
 +15       IF SDBACK=1
               DO SDVISIT(SDPATDFN,SDCLIEN,.SDAPPTDT,.SDCONT,.SDBACK,SDFROM)
 +16      ; If no error and no encounter, does patient have an appointment in that
 +17      ; clinic?
 +18       IF SDBACK=1
               IF SDCONT=1
                   DO SDAPPT(SDPATDFN,SDCLIEN,.SDAPPTDT,.SDBACK,SDFROM)
 +19      ;
 +20      ;--FINALIZATION--
 +21      ; If no error
 +22       IF SDBACK=1
               Begin DoDot:1
 +23      ; Set up output array with the encounter or appointment date/time
 +24      ; Make sure the appointment date/time exists in SDAPPTDT
 +25               IF $GET(SDAPPTDT)]""
                       Begin DoDot:2
 +26                       SET SDIMO(1)=SDAPPTDT
                       End DoDot:2
 +27               IF $GET(SDAPPTDT)']""
                       Begin DoDot:2
 +28                       SET SDBACK=0
                       End DoDot:2
               End DoDot:1
 +29      ; Return value
 +30       QUIT SDBACK
 +31      ;
 +32      ;----------------------------------------------------------------------
 +33      ;-Validate input variables
SDVALVAR(SDPATDFN,SDCLIEN,SDBACK) ;
 +1       ; Clinic IEN and patient DFN cannot be null
 +2        IF $GET(SDCLIEN)=""
               SET SDBACK=-1
               QUIT 
 +3        IF $GET(SDPATDFN)=""
               SET SDBACK=-2
               QUIT 
 +4        QUIT 
 +5       ;
 +6       ;-Clinic must be type "C", authorized to administer inpatient meds,
 +7       ;-and active
SDAUTHCL(SDCLIEN,SDBACK) ;
 +1        NEW SDAUTH,SDTYPE
 +2        SET SDAUTH=0
           SET SDTYPE=0
 +3       ; clinic must be type "C"
 +4        SET SDTYPE=$PIECE($GET(^SC(SDCLIEN,0)),"^",3)
 +5        IF $GET(SDTYPE)="C"
               Begin DoDot:1
 +6       ; clinic must be authorized to administer inpatient meds
 +7                IF $DATA(^SC("AE",1,SDCLIEN))
                       SET SDAUTH=1
               End DoDot:1
 +8        IF SDAUTH'=1
               SET SDBACK=-1
               QUIT 
 +9       ; clinic must be active
 +10      ; if clinic inactivate date exists, check further
 +11       NEW SDINACT,SDREACT
 +12       SET SDINACT=$PIECE($GET(^SC(SDCLIEN,"I")),"^",1)
 +13       IF $GET(SDINACT)]""
               Begin DoDot:1
 +14      ; if inactivate date is today or earlier, get reactivate date
 +15               IF SDINACT'>DT
                       Begin DoDot:2
 +16                       SET SDREACT=$PIECE($GET(^SC(SDCLIEN,"I")),"^",2)
 +17      ; reactivate date can't be null
 +18                       IF $GET(SDREACT)=""
                               SET SDBACK=-1
 +19      ; if reactivate date exists
 +20                      IF '$TEST
                               Begin DoDot:3
 +21      ; reactivate date must be less than or equal to today
 +22      ; but greater than or equal to inactivate date
 +23                               IF (SDREACT>DT!(SDREACT<SDINACT))
                                       SET SDBACK=-1
                               End DoDot:3
                       End DoDot:2
               End DoDot:1
 +24       QUIT 
 +25      ;-Set up start date for encounters and appointments
SDDATE(SDFROM) ;
 +1        NEW %,X
 +2        DO NOW^%DTC
 +3       ;if the current time is before 6am, set 'start' date to yesterday
 +4        IF ("."_$PIECE(%,".",2))<.060000
               SET SDFROM=(X-1)
 +5       IF '$TEST
               SET SDFROM=X
 +6        QUIT 
 +7       ;-Look for encounter that occurred in the authorized clinic
SDVISIT(SDPATDFN,SDCLIEN,SDAPPTDT,SDCONT,SDBACK,SDFROM) ;
 +1        NEW SDSTART,SDEND,SDENCNUM,SDENCDT,SDENCCL
 +2       ; set up start and end date/time
 +3        SET SDSTART=SDFROM_".0000"
 +4        SET SDEND=DT_".2359"
 +5       ; get encounters
 +6        FOR 
               Begin DoDot:1
 +7       ; call API to get next encounter
 +8                SET SDENCNUM=+$$EXOE^SDOE(SDPATDFN,SDSTART,SDEND)
 +9                IF $GET(SDENCNUM)
                       Begin DoDot:2
 +10      ; encounter found.  call API to get more encounter data
 +11                       DO GETGEN^SDOE(SDENCNUM,"SDDATA")
 +12                       IF $GET(SDDATA(0))
                               Begin DoDot:3
 +13      ; get encounter date/time and clinic IEN
 +14                               SET SDENCDT=$PIECE($GET(SDDATA(0)),"^",1)
                                   SET SDENCCL=$PIECE($GET(SDDATA(0)),"^",4)
 +15      ; if encounter clinic matches authorized clinic, set flags
 +16                               IF $GET(SDENCCL)=SDCLIEN
                                       SET SDENCNUM=0
                                       SET SDCONT=0
                                       SET SDAPPTDT=$GET(SDENCDT)
 +17      ; if no match on clinic, reset start date for next encounter
 +18                               IF $GET(SDENCCL)'=SDCLIEN
                                       SET SDSTART=(SDENCDT+.000001)
 +19                               KILL SDDATA
                               End DoDot:3
                       End DoDot:2
               End DoDot:1
               if +SDENCNUM=0
                   QUIT 
 +20       QUIT 
 +21      ;-Look for scheduled appointment in the authorized clinic
SDAPPT(SDPATDFN,SDCLIEN,SDAPPTDT,SDBACK,SDFROM) ;
 +1        NEW SDRESULT,SDAPPTCL,SDMATCH
 +2        SET SDMATCH=0
 +3       ; call API to get appointments for this patient
 +4        DO GETAPPT^SDAMA201(SDPATDFN,"1;2","R;NT",SDFROM,,.SDRESULT)
 +5       ; SDRESULT contains a count of the returned appointments
 +6        IF SDRESULT>0
               Begin DoDot:1
 +7                NEW SDI
 +8       ; spin through returned appointments and look for match on clinic IEN
 +9                FOR SDI=1:1:SDRESULT
                       Begin DoDot:2
 +10                       SET SDAPPTCL=$GET(^TMP($JOB,"SDAMA201","GETAPPT",SDI,2))
 +11                       IF +$GET(SDAPPTCL)=SDCLIEN
                               Begin DoDot:3
 +12                               SET SDAPPTDT=$GET(^TMP($JOB,"SDAMA201","GETAPPT",SDI,1))
 +13                               SET SDMATCH=1
                               End DoDot:3
                       End DoDot:2
                       if SDMATCH=1
                           QUIT 
 +14      ; delete appointment array returned from Scheduling API
 +15               KILL ^TMP($JOB,"SDAMA201","GETAPPT")
               End DoDot:1
 +16       IF ((SDRESULT=0)!(SDMATCH=0))
               SET SDBACK=0
 +17       IF SDRESULT=-1
               Begin DoDot:1
 +18               SET SDBACK=0
 +19      ; if database unavailable, set database-specific flag
 +20               IF $DATA(^TMP($JOB,"SDAMA201","GETAPPT","ERROR",101))
                       SET SDBACK=-3
 +21      ; delete error array returned from Scheduling API
 +22               KILL ^TMP($JOB,"SDAMA201","GETAPPT")
               End DoDot:1
 +23       QUIT