Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: SDAMA203

SDAMA203.m

Go to the documentation of this file.
  1. SDAMA203 ;BPIOFO/ACS-Scheduling API for IMO ;15 April 2003
  1. ;;5.3;Scheduling;**285,406**;13 Aug 1993
  1. ;
  1. ;Scheduling API to return encounter or appointment date/time for
  1. ;a patient that can receive inpatient medication from an
  1. ;authorized clinic
  1. ;
  1. ;**********************************************************************
  1. ; CHANGE LOG
  1. ;
  1. ; DATE PATCH DESCRIPTION DEVELOPER
  1. ;-------- ---------- ----------------------------------------------
  1. ;04/15/03 SD*5.3*285 ROUTINE WRITTEN A SAUNDERS
  1. ;10/12/06 SD*5.3*406 FIXED ERROR CODE -3 A SAUNDERS
  1. ;
  1. ;**********************************************************************
  1. ;
  1. ; **** TO BE CALLED WITH AN EXTRINISIC CALL ****
  1. ;Example: I $$SDIMO^SDAMA203(CLIEN,DFN) S APPTDT=SDIMO(1) K SDIMO(1)
  1. ;
  1. ;INPUT
  1. ; SDCLIEN Clinic IEN (required)
  1. ; SDPATDFN Patient DFN (required)
  1. ;
  1. ;OUTPUT
  1. ; The extrinsic call will return one of the following values:
  1. ; 1 Patient has at least one scheduled appointment or checked-in
  1. ; visit in an authorized clinic
  1. ; 0 Patient has no scheduled appointments or checked-in visits
  1. ; in an authorized clinic
  1. ; -1 Clinic is not an authorized clinic, clinic is inactive,
  1. ; or SDCLIEN is null
  1. ; -2 SDPATDFN is null
  1. ; -3 Scheduling database is unavailable
  1. ;
  1. ; If a 1 is returned, then SDIMO(1) = Encounter or appointment
  1. ; date/time in FileMan format
  1. ;
  1. ;**********************************************************************
  1. ; Special Logic:
  1. ; - In line tag SDVISIT, the ACRP Toolkit API EXOE^SDOE is called
  1. ; multiple times as needed. This API returns the NEXT encounter,
  1. ; given a start and end date/time. We want to check ALL encounters
  1. ; for a match on clinic IEN
  1. ; - In line tag SDDATE, if the current time is between midnight and 6am,
  1. ; the API will start to look for encounters and/or appointments on the
  1. ; previous day
  1. ;
  1. ; Internal variables:
  1. ; SDBACK Contains the value to be returned from this call. See
  1. ; above for OUTPUT values and corresponding definitions
  1. ; SDCONT Flag to indicate if processing should continue. If
  1. ; the patient has an encounter in an authorized clinic
  1. ; today, then we can skip the last step and not look for
  1. ; a scheduled appointment
  1. ; SDFROM The date to start searching for an encounter or appointment
  1. ; SDAPPTDT Encounter or appointment date/time returned in SDIMO(1)
  1. ;
  1. ;**********************************************************************
  1. SDIMO(SDCLIEN,SDPATDFN) ;
  1. ;
  1. ;--INITIALIZATION--
  1. K SDIMO(1)
  1. N SDBACK,SDCONT,SDFROM,SDAPPTDT
  1. S SDBACK=1,SDCONT=1,SDAPPTDT=0,SDFROM=0
  1. ;
  1. ;--MAIN--
  1. ; Valid variables passed in?
  1. D SDVALVAR($G(SDPATDFN),$G(SDCLIEN),.SDBACK)
  1. ; If no error, is clinic active and authorized?
  1. I SDBACK=1 D SDAUTHCL(SDCLIEN,.SDBACK)
  1. ; If no error, set up search "start" date
  1. I SDBACK=1 D SDDATE(.SDFROM)
  1. ; If no error, does patient have an encounter in that clinic?
  1. I SDBACK=1 D SDVISIT(SDPATDFN,SDCLIEN,.SDAPPTDT,.SDCONT,.SDBACK,SDFROM)
  1. ; If no error and no encounter, does patient have an appointment in that
  1. ; clinic?
  1. I SDBACK=1,SDCONT=1 D SDAPPT(SDPATDFN,SDCLIEN,.SDAPPTDT,.SDBACK,SDFROM)
  1. ;
  1. ;--FINALIZATION--
  1. ; If no error
  1. I SDBACK=1 D
  1. . ; Set up output array with the encounter or appointment date/time
  1. . ; Make sure the appointment date/time exists in SDAPPTDT
  1. . I $G(SDAPPTDT)]"" D
  1. .. S SDIMO(1)=SDAPPTDT
  1. . I $G(SDAPPTDT)']"" D
  1. .. S SDBACK=0
  1. ; Return value
  1. Q SDBACK
  1. ;
  1. ;----------------------------------------------------------------------
  1. ;-Validate input variables
  1. SDVALVAR(SDPATDFN,SDCLIEN,SDBACK) ;
  1. ; Clinic IEN and patient DFN cannot be null
  1. I $G(SDCLIEN)="" S SDBACK=-1 Q
  1. I $G(SDPATDFN)="" S SDBACK=-2 Q
  1. Q
  1. ;
  1. ;-Clinic must be type "C", authorized to administer inpatient meds,
  1. ;-and active
  1. SDAUTHCL(SDCLIEN,SDBACK) ;
  1. N SDAUTH,SDTYPE
  1. S SDAUTH=0,SDTYPE=0
  1. ; clinic must be type "C"
  1. S SDTYPE=$P($G(^SC(SDCLIEN,0)),"^",3)
  1. I $G(SDTYPE)="C" D
  1. . ; clinic must be authorized to administer inpatient meds
  1. . I $D(^SC("AE",1,SDCLIEN)) S SDAUTH=1
  1. I SDAUTH'=1 S SDBACK=-1 Q
  1. ; clinic must be active
  1. ; if clinic inactivate date exists, check further
  1. N SDINACT,SDREACT
  1. S SDINACT=$P($G(^SC(SDCLIEN,"I")),"^",1)
  1. I $G(SDINACT)]"" D
  1. . ; if inactivate date is today or earlier, get reactivate date
  1. . I SDINACT'>DT D
  1. .. S SDREACT=$P($G(^SC(SDCLIEN,"I")),"^",2)
  1. .. ; reactivate date can't be null
  1. .. I $G(SDREACT)="" S SDBACK=-1
  1. .. ; if reactivate date exists
  1. .. E D
  1. ... ; reactivate date must be less than or equal to today
  1. ... ; but greater than or equal to inactivate date
  1. ... I (SDREACT>DT!(SDREACT<SDINACT)) S SDBACK=-1
  1. Q
  1. ;-Set up start date for encounters and appointments
  1. SDDATE(SDFROM) ;
  1. N %,X
  1. D NOW^%DTC
  1. ;if the current time is before 6am, set 'start' date to yesterday
  1. I ("."_$P(%,".",2))<.060000 S SDFROM=(X-1)
  1. E S SDFROM=X
  1. Q
  1. ;-Look for encounter that occurred in the authorized clinic
  1. SDVISIT(SDPATDFN,SDCLIEN,SDAPPTDT,SDCONT,SDBACK,SDFROM) ;
  1. N SDSTART,SDEND,SDENCNUM,SDENCDT,SDENCCL
  1. ; set up start and end date/time
  1. S SDSTART=SDFROM_".0000"
  1. S SDEND=DT_".2359"
  1. ; get encounters
  1. F D Q:+SDENCNUM=0
  1. . ; call API to get next encounter
  1. . S SDENCNUM=+$$EXOE^SDOE(SDPATDFN,SDSTART,SDEND)
  1. . I $G(SDENCNUM) D
  1. .. ; encounter found. call API to get more encounter data
  1. .. D GETGEN^SDOE(SDENCNUM,"SDDATA")
  1. .. I $G(SDDATA(0)) D
  1. ... ; get encounter date/time and clinic IEN
  1. ... S SDENCDT=$P($G(SDDATA(0)),"^",1),SDENCCL=$P($G(SDDATA(0)),"^",4)
  1. ... ; if encounter clinic matches authorized clinic, set flags
  1. ... I $G(SDENCCL)=SDCLIEN S SDENCNUM=0,SDCONT=0,SDAPPTDT=$G(SDENCDT)
  1. ... ; if no match on clinic, reset start date for next encounter
  1. ... I $G(SDENCCL)'=SDCLIEN S SDSTART=(SDENCDT+.000001)
  1. ... K SDDATA
  1. Q
  1. ;-Look for scheduled appointment in the authorized clinic
  1. SDAPPT(SDPATDFN,SDCLIEN,SDAPPTDT,SDBACK,SDFROM) ;
  1. N SDRESULT,SDAPPTCL,SDMATCH
  1. S SDMATCH=0
  1. ; call API to get appointments for this patient
  1. D GETAPPT^SDAMA201(SDPATDFN,"1;2","R;NT",SDFROM,,.SDRESULT)
  1. ; SDRESULT contains a count of the returned appointments
  1. I SDRESULT>0 D
  1. . N SDI
  1. . ; spin through returned appointments and look for match on clinic IEN
  1. . F SDI=1:1:SDRESULT D Q:SDMATCH=1
  1. .. S SDAPPTCL=$G(^TMP($J,"SDAMA201","GETAPPT",SDI,2))
  1. .. I +$G(SDAPPTCL)=SDCLIEN D
  1. ... S SDAPPTDT=$G(^TMP($J,"SDAMA201","GETAPPT",SDI,1))
  1. ... S SDMATCH=1
  1. . ; delete appointment array returned from Scheduling API
  1. . K ^TMP($J,"SDAMA201","GETAPPT")
  1. I ((SDRESULT=0)!(SDMATCH=0)) S SDBACK=0
  1. I SDRESULT=-1 D
  1. . S SDBACK=0
  1. . ; if database unavailable, set database-specific flag
  1. . I $D(^TMP($J,"SDAMA201","GETAPPT","ERROR",101)) S SDBACK=-3
  1. . ; delete error array returned from Scheduling API
  1. . K ^TMP($J,"SDAMA201","GETAPPT")
  1. Q