BSDAPI4 ; IHS/ITSC/LJF & HMW - PCC API FOR RPMS [ 03/24/2005 1:44 PM ]
;;5.3;PIMS;**1002,1003,1004,1005,1009,1010**;MAY 28, 2004
;IHS/ITSC/LJF PATCH 1002 routine created
; PATCH 1003 check-in logic improved
;IHS/OIT/LJF 09/15/2005 PATCH 1004 added APCDOPT to help text
; 10/05/2005 PATCH 1004 kill APCDALVR variable after visit created
; 10/06/2005 PATCH 1004 made changes for ancillary visits
; 12/21/2005 PATCH 1005 added APCDOLOC to optional variables
; 03/03/2006 PATCH 1005 fixed hole in triage logic
;cmi/anch/maw05/14/2008 PATCH 1009 change logic for triage so it creates 2 visits
;cmi/anch/maw06/01/2008 PATCH 1009 requirement 58 added logic for matching on ordering location and returning that visit
;cmi/anch/maw06/10/2008 PATCH 1009 requirement 58 added logic to display visits for user to select if more than one
;cmi/anch/maw09/02/2009 PATCH 1010 added optional set of Option Used to Create
;
GETVISIT(BSDIN,BSDOUT) ;Private Entry Point with PCC - API for PCC visit Creation by RPMS applications
; >> All date/time variables must be in FileMan internal format
; Special Incoming Variables:
; BSDIN("FORCE ADD") = 1 ; no matter what, create new visit (Optional)
; BSDIN ("NEVER ADD") = 1 ; never add visit, just try to find one or more (Optional)
; BSDIN("ANCILLARY") = 1 ; for ancillary packages to create noon visit if no match found (Optional)
; BSDIN("SHOW VISITS") = 1 ; this will display visits if more than one match
; Incoming Variables used in Matching: REQUIRED
; BSDIN("PAT") = patient IEN (file 2 or 9000001)
; BSDIN("VISIT DATE") = visit date & time (same as check-in date & time)
; BSDIN ("SITE") = location of encounter IEN (file 4 or 9999999.06)
; BSDIN("VISIT TYPE") = internal value for field .03 in Visit file
; BSDIN("SRV CAT") = internal value for service category
; BSDIN("TIME RANGE") = # ; range in minutes for matching on visit time; REQUIRED unless FORCE ADD set
; ; zero=exact matches only; -1=don't match on time
; These are used to match if sent (Optional)
; BSDIN("PROVIDER") = IEN for provider to match from file 200
; BSDIN("CLINIC CODE") = IEN of clinic stop code (file 40.7)
; BSDIN("HOS LOC") = IEN of hospital location (file 44, field .22 in VISIT file)
; BSDIN("DEF CC") = IEN of default clinic code for package making call PATCH 1009
; BSDIN("DEF HL") = IEN of default hospital location for package making call PATCH 1009
; Incoming Variables used in creating appt and visit
; BSDIN("APPT DATE") = appt date & time (Required for scheduled appts and walk-ins; check-in will be performed)
; BSDIN("USR") = user IEN in file 200; REQUIRED
; BSDIN("OPT") = name for Option Used To Create field, for check-in only (Optional)
; BSDIN("OI") = reason for appointment; for walk-ins (Optional)
; Incoming PCC variables for adding additional info to visit (Optional)
; BSDIN("APCDTPB") = Third Party Billed (#.04)
; BSDIN("APCDPVL") = Parent Visit Link (#.12)
; BSDIN("APCDAPPT") = WalkIn/Appt (#.16)
; BSDIN("APCDEVM") = Evaluation and Management Code (#.17)
; BSDIN("APCDCODT") = Check Out Date & Time (#.18)
; BSDIN("APCDLS") = Level of Service -PCC Form (#.19).
; BSDIN("APCDVELG") = Eligibility (#.21)
; BSDIN("APCDPROT") = Protocol (#.25).
; BSDIN("APCDOPT") = Option Used To Create (#.24) ;IHS/OIT/LJF 09/15/2005 PATCH 1004
; BSDIN("APCDOLOC") = Outside Location (#2101) ;IHS/OIT/LJF 12/21/2005 PATCH 1005
; Outgoing Array:
; BSDOUT(0) always set; if = 0 none found and may have error message in 2nd piece
; if = 1 and BSDOUT(visit ien)="ADD" new visit just created
; if = 1 and BSDOUT(visit ien)=#; # is time difference in minutes
; if >1, multiple BSDOUT(visit ien) entries exist
NEW BSDTMP K BSDOUT
M BSDTMP=BSDIN ;don't mess with incoming array
IF '$$HAVEREQ(.BSDTMP,.BSDOUT) Q ;check required fields
;IHS/ITSC/LJF 4/28/2005 PATCH 1003 if FORCE ADD set, bypass check-in & create visit
; if forced add, skip visit match attempt
;IF $G(BSDTMP("FORCE ADD")) D APPTDT Q
;cmi/flag/maw 9/2/2009 PATCH 1010
I '$G(BSDTMP("APCDOPT")) D
.I $G(BSDTMP("OPT"))]"",BSDTMP("OPT")?.N,$D(^DIC(19,BSDTMP("OPT"))) S BSDTMP("APCDOPT")=BSDTMP("OPT") Q
.I $G(BSDTMP("OPT"))]"",$E(BSDTMP("OPT"),1,1)="`" S BSDTMP("APCDOPT")=$TR(BSDTMP("OPT"),"`") Q
.I $G(BSDTMP("OPT"))]"",BSDTMP("OPT")'?.N S BSDTMP("APCDOPT")=$O(^DIC(19,"B",BSDTMP("OPT"),0)) Q
.I $G(BSDTMP("APCDOPT"))]"",$E(BSDTMP("APCDOPT"),1,1)="`" S BSDTMP("APCDOPT")=$TR(BSDTMP("APCDOPT"),"`") Q
.I $G(BSDTMP("APCDOPT"))]"",BSDTMP("APCDOPT")'?.N S BSDTMP("APCDOPT")=$O(^DIC(19,"B",BSDTMP("APCDOPT"),0)) Q
IF $G(BSDTMP("FORCE ADD")) D ADDVIST(.BSDTMP,.BSDOUT) Q
; attempt to find matching visits; return BSDOUT array
D MATCH(.BSDTMP,.BSDOUT)
;IHS/ITSC/LJF 4/22/2005 PATCH 1003 if appt date set, go to check-in
;IF $G(BSDTMP("APPT DATE")),'$G(BSDTMP("NEVER ADD")) D APPTDT Q
;if only 1 visit found, return ien and quit
IF BSDOUT(0)=1 Q
;if >1 visits found, return full array and quit, unless they pass it the variable to show visits then we will display
;(calling app decides next step)
IF BSDOUT(0)>1 Q
;IHS/OIT/LJF 10/06/2005 PATCH 1004 added 2nd match, move never add checks & not kill variables
;if called by ancillary package, just create noon visit and quit
IF $G(BSDTMP("ANCILLARY")) D Q
. K BSDTMP("ANCILLARY"),BSDTMP("PROVIDER") ; set up to find other ancillaries
. D MATCH(.BSDTMP,.BSDOUT) I BSDOUT(0)=1 Q ; try to match on hos loc or clinic
. I $G(BSDTMP("NEVER ADD"))=1 Q ; if in never add mode, quit after 2nd match
. S BSDTMP("VISIT DATE")=BSDTMP("VISIT DATE")\1 ; take off time; PCC will add noon
. D ADDVIST(.BSDTMP,.BSDOUT) ; create noon visit; no PIMS link
;if no visits found but in never add mode, just quit
IF $G(BSDTMP("NEVER ADD"))=1 Q
;IHS/OIT/LJF 10/06/2005 PATCH 1004 end of changes for this section
;otherwise, logic falls through to create visit choices
APPTDT ;
I $G(BSDTMP("CALLER"))]"",$G(BSDTMP("CALLER"))="BSD CHECKIN" Q ;cmi/maw 6/10/2008 PATCH 1009 for interactive visit creation
;if no appointment date/time sent, just create visit and quit
IF '$G(BSDTMP("APPT DATE")) D ADDVIST(.BSDTMP,.BSDOUT) Q
;IHS/ITSC/LJF 4/22/2005 PATCH 1003 if one matching visit found, check-in but don't create visit
I BSDOUT(0)=1 S BSDTMP("VIEN")=$O(BSDOUT(0))
;if patient already has appt at this time, call Check-in (which creates visit) then quit
NEW IEN,ERR,V
S IEN=$$SCIEN^BSDU2(BSDTMP("PAT"),BSDTMP("HOS LOC"),BSDTMP("APPT DATE")) ;find appt
I IEN D Q
. ; set variables used by checkin call
. S BSDTMP("CDT")=BSDTMP("VISIT DATE")
. S BSDTMP("CC")=$G(BSDTMP("CLINIC CODE"))
. S BSDTMP("PRV")=$G(BSDTMP("PROVIDER"))
. ;IHS/ITSC/LJF 5/4/2005 PATCH 1003 set more variables to use in BSDAPI
. S BSDTMP("CLN")=$G(BSDTMP("HOS LOC"))
. S BSDTMP("ADT")=$G(BSDTMP("APPT DATE"))
. S ERR=$$CHECKIN^BSDAPI(.BSDTMP) ;check in and create visit
. ;IHS/ITSC/LJF 5/5/2005 PATCH 1003 reset BSDOUT only if truly added one.
. ;I 'ERR S V=$$GETVST^BSDU2(BSDTMP("PAT"),BSDTMP("APPT DATE")) I V S BSDOUT(0)=1,BSDOUT(V)="ADD" Q
. I 'ERR S V=$$GETVST^BSDU2(BSDTMP("PAT"),BSDTMP("APPT DATE")) I V,'$G(BSDTMP("VIEN")) S:BSDOUT(0)=0 BSDOUT(0)=1 S BSDOUT(V)="ADD" Q
. I ERR S BSDOUT(0)=0_U_$P(ERR,U,2)
; else call walk-in (which calls make appt, checkin and create visit)
D WALKIN(.BSDTMP,.BSDOUT)
Q
MATCH(IN,OUT) ; find matching visits based on IN array
S OUT(0)=0
NEW END,DATE,VIEN,STOP,DIFF,MATCH
S MATCH=0
D TIME(IN("TIME RANGE"),IN("VISIT DATE"),.DATE,.END)
F S DATE=$O(^AUPNVSIT("AA",IN("PAT"),DATE)) Q:'DATE Q:(DATE>END) D
. S VIEN=0
. F S VIEN=$O(^AUPNVSIT("AA",IN("PAT"),DATE,VIEN)) Q:'VIEN D
. . I $$GET1^DIQ(9000010,VIEN,.11)="DELETED" Q ;check for delete flag just in case xref not killed
. . I IN("SITE")'=$$GET1^DIQ(9000010,VIEN,.06,"I") Q ;no match on loc of enc
. . I IN("VISIT TYPE")'=$$GET1^DIQ(9000010,VIEN,.03,"I") Q ;no match on vist type
. . ;cmi/maw 06/01/2008 PATCH 1009 get observation and day surgery visits
. . ;I IN("SRV CAT")'=$$GET1^DIQ(9000010,VIEN,.07,"I") Q ;no match on service category
. . I IN("SRV CAT")["CENRT" Q ;don't look at HIM excluded visits
. . I $$GET1^DIQ(90000010,VIEN,.07,"I")["CENRT" Q ;don't look at HIM excluded visits
. . I IN("SRV CAT")=$$GET1^DIQ(9000010,VIEN,.07,"I") S MATCH=1 ;no match on service category
. . I IN("SRV CAT")="A",$G(IN("ANCILLARY")),$$GET1^DIQ(9000010,VIEN,.07,"I")="O" S MATCH=1 ;match if observation
. . I IN("SRV CAT")="A",$G(IN("ANCILLARY")),$$GET1^DIQ(9000010,VIEN,.07,"I")="D" S MATCH=1
. . I '$G(MATCH) Q
. . ;cmi/maw 06/01/2008 PATCH 1009 end of mods
. . I IN("TIME RANGE")>-1 S STOP=0 D Q:STOP ;check time range
. . . S DIFF=$$TIMEDIF(IN("VISIT DATE"),VIEN) ;find difference in minutes
. . . I $$ABS^XLFMTH(DIFF)>IN("TIME RANGE") S STOP=1
. . I '$$PRVMTCH Q ; if provider sent and didn't match, skip
. . ; if called by ancillary, falls through and sets visit into array
. . ; otherwise, check if app wants to match on clinic code or hosp location
. . I '$G(IN("ANCILLARY")) S STOP=0 D Q:STOP
. . . I $G(IN("HOS LOC")),'$G(IN("CLINIC CODE")) S IN("CLINIC CODE")=$$GET1^DIQ(44,IN("HOS LOC"),8,"I")
. . . I $G(IN("CLINIC CODE")),IN("CLINIC CODE")'=$$GET1^DIQ(9000010,VIEN,.08,"I") S STOP=1 Q ;no match on clinic code
. . . ;IHS/ITSC/LJF 4/22/2005 PATCH 1003 if both have appt date and visit was triage clinic, is a match
. . . ;cmi/anch/maw 5/14/2008 PATCH 1009 requirement 59 create visit on same day no matter what
. . . ;I $G(IN("APPT DATE")),$$GET1^DIQ(9000010,VIEN,.26,"I"),$$TRIAGE(VIEN) Q ;cmi/maw 5/14/2008 comment out PATCH 1009 requirement 59
. . . I $G(IN("HOS LOC")),(IN("HOS LOC")'=$$GET1^DIQ(9000010,VIEN,.22,"I")) S STOP=1 Q ;no match on hospital location
. . . ;IHS/OIT/LJF 02/03/2006 PATCH 1005 if same clinic & same provider but not triage, make new visit
. . . I $G(IN("APPT DATE")),$$GET1^DIQ(9000010,VIEN,.26,"I"),'$$TRIAGE(VIEN) S STOP=1 Q
. . ; must be good match, increment counter and set array node
. . S OUT(0)=OUT(0)+1
. . S OUT(VIEN)=$$TIMEDIF(IN("VISIT DATE"),VIEN)
Q
;
PRVMTCH() ; do visits match on provider?
NEW PRVS,IEN
I '$G(IN("PROVIDER")) Q 1 ; if no provider sent, assume okay
;IHS/ITSC/LJF 5/4/2005 PATCH 1003
;if visit is triage clinic & new encounter is not ancillary, skip provider match
I $$TRIAGE(VIEN),'$G(IN("ANCILLARY")) Q 1
; find all v provider entries for visit
S IEN=0 F S IEN=$O(^AUPNVPRV("AD",VIEN,IEN)) Q:'IEN D
. S PRVS(+$G(^AUPNVPRV(IEN,0)))=""
;if incoming provider in list, this is match
I $D(PRVS(IN("PROVIDER"))) Q 1
;otherwise, no match
Q 0
;
TIMEDIF(VDTTM,VIEN) ; return time diff between incoming time and current visit
Q $$FMDIFF^XLFDT(VDTTM,+$G(^AUPNVSIT(VIEN,0)),2)\60
;
ADDVIST(BSDTMP,BSDOUT) ;
K APCDALVR NEW %DT,SUB
S SUB="APCD" F S SUB=$O(BSDTMP(SUB)) Q:SUB]"APCDZZZZ" S APCDALVR(SUB)=BSDTMP(SUB)
S APCDALVR("AUPNTALK")="",APCDALVR("APCDANE")="" ;keep it silent
S APCDALVR("APCDLOC")=BSDTMP("SITE") ;facility
S APCDALVR("APCDPAT")=BSDTMP("PAT") ;patient
S APCDALVR("APCDTYPE")=BSDTMP("VISIT TYPE") ;visit type
S APCDALVR("APCDCAT")=BSDTMP("SRV CAT") ;srv cat
S APCDALVR("APCDDATE")=BSDTMP("VISIT DATE") ;chkin dt
I $G(BSDTMP("CLINIC CODE")) S APCDALVR("APCDCLN")="`"_BSDTMP("CLINIC CODE") ;clinic code ien w/`
S APCDALVR("APCDHL")=$G(BSDTMP("HOS LOC")) ;clinic name
S APCDALVR("APCDAPDT")=$G(BSDTMP("APPT DATE")) ;appt date
;I '$G(APCDALVR("APCDOPT")) S APCDALVR("APCDOPT")=$G(BSDTMP("OPT")) ;option name PATCH 1010
S APCDALVR("APCDADD")=1 ;force add
;create visit
D ^APCDALV
;if no visit created,error quit
I '$G(APCDALVR("APCDVSIT")) D Q
. S BSDOUT(0)="0^Error Creating Visit"
; set new visit info in out array
S BSDOUT(APCDALVR("APCDVSIT"))="ADD",BSDOUT(0)=1
K APCDALVR ;IHS/OIT/LJF 10/05/2005 PATCH 1004
Q
;
WALKIN(BSDATA,OUT) ;EP; create walkin appt which is checked in and visit created
; also called by BSDAPI3 to create ancillary walkin appt
NEW ERR,V
S OUT(0)=0 ;initialize outgoing count
S BSDATA("CLN")=$G(BSDATA("HOS LOC"))
S BSDATA("TYP")=4 ;4=walkin
S BSDATA("ADT")=$G(BSDATA("APPT DATE"))
I '$D(BSDATA("LEN")) S BSDATA("LEN")=$$GET1^DIQ(44,BSDATA("CLN"),1912)
; make walkin appt
S ERR=$$MAKE^BSDAPI(.BSDATA) I ERR S $P(OUT(0),U,2)=$P(ERR,U,2) Q
; set variables used by checkin call
S BSDATA("CDT")=BSDATA("VISIT DATE")
S BSDATA("CC")=$G(BSDATA("CLINIC CODE"))
S BSDATA("PRV")=$G(BSDATA("PROVIDER"))
; check in appt and create visit
S ERR=$$CHECKIN^BSDAPI(.BSDATA)
; update out array based on result
;IHS/ITSC/LJF 5/5/2005 PATCH 1003 reset BSDOUT(0) only if added new visit
;I 'ERR S V=$$GETVST^BSDU2(BSDATA("PAT"),BSDATA("APPT DATE")) I V S OUT(0)=1,OUT(V)="ADD" ;visit added
I 'ERR S V=$$GETVST^BSDU2(BSDATA("PAT"),BSDATA("APPT DATE")) I V,'$G(BSDTMP("VIEN")) S:OUT(0)=0 OUT(0)=1 S OUT(V)="ADD" ;visit added
I ERR S $P(OUT(0),U,2)=$P(ERR,U,2) ;error
Q
;
HAVEREQ(IN,OUT) ; check required fields
I ('$G(IN("FORCE ADD"))),('$D(IN("TIME RANGE"))) S OUT(0)="0^Missing Time Range" Q 0
I '$D(IN("PAT")) S OUT(0)="0^Missing Patient IEN" Q 0
I '$D(IN("VISIT DATE")) S OUT(0)="0^Missing Visit Date" Q 0
I '$D(IN("SITE")) S OUT(0)="0^Missing Facility/Site" Q 0
I '$D(IN("VISIT TYPE")) S OUT(0)="0^Missing Visit Type" Q 0
I '$D(IN("SRV CAT")) S OUT(0)="0^Missing Service Category" Q 0
I '$D(IN("USR")) S OUT(0)="0^Missing User IEN" Q 0
;IHS/ITSC/LJF 5/5/2005 PATCH 1003 moved line to set clinic code earlier
I $G(IN("HOS LOC")),'$G(IN("CLINIC CODE")) S IN("CLINIC CODE")=$$GET1^DIQ(44,IN("HOS LOC"),8,"I")
;IHS/ITSC/LJF 5/5/2005 PATCH 1003 convert service category
I $G(IN("APPT DATE")),$G(IN("HOS LOC")) S IN("SRV CAT")=$$SERCAT^BSDV(IN("HOS LOC"),IN("PAT"))
Q 1
;
TIME(RANGE,VISIT,DATE,END) ; set DATE and END based on TIME RANGE setting in minutes
NEW TMDIF,SW
S TMDIF=$S(RANGE<1:0,1:RANGE)
S DATE=$$FMADD^XLFDT(VISIT,,,-TMDIF)
S END=$$FMADD^XLFDT(VISIT,,,TMDIF)
I (DATE\1)<(END\1) S SW=(END\1),END=(DATE\1)_".9999",DATE=SW
S DATE=(9999999-(DATE\1)_"."_$P(DATE,".",2))-.0001
S END=9999999-(END\1)_"."_$P(END,".",2)
I RANGE=-1 S END=(END\1)_".9999",DATE=(DATE\1) ;no time range used; go from begin one day to end
Q
;
TRIAGE(VST) ; returns 1 if visit's hosp loc is triage type ;IHS/ITSC/LJF 4/22/2005 PATCH 1003
NEW HL
S HL=$$GET1^DIQ(9000010,VST,.22,"I") I 'HL Q 0
Q +$$GET1^DIQ(9009017.2,HL,.16,"I")
;
BSDAPI4 ; IHS/ITSC/LJF & HMW - PCC API FOR RPMS [ 03/24/2005 1:44 PM ]
+1 ;;5.3;PIMS;**1002,1003,1004,1005,1009,1010**;MAY 28, 2004
+2 ;IHS/ITSC/LJF PATCH 1002 routine created
+3 ; PATCH 1003 check-in logic improved
+4 ;IHS/OIT/LJF 09/15/2005 PATCH 1004 added APCDOPT to help text
+5 ; 10/05/2005 PATCH 1004 kill APCDALVR variable after visit created
+6 ; 10/06/2005 PATCH 1004 made changes for ancillary visits
+7 ; 12/21/2005 PATCH 1005 added APCDOLOC to optional variables
+8 ; 03/03/2006 PATCH 1005 fixed hole in triage logic
+9 ;cmi/anch/maw05/14/2008 PATCH 1009 change logic for triage so it creates 2 visits
+10 ;cmi/anch/maw06/01/2008 PATCH 1009 requirement 58 added logic for matching on ordering location and returning that visit
+11 ;cmi/anch/maw06/10/2008 PATCH 1009 requirement 58 added logic to display visits for user to select if more than one
+12 ;cmi/anch/maw09/02/2009 PATCH 1010 added optional set of Option Used to Create
+13 ;
GETVISIT(BSDIN,BSDOUT) ;Private Entry Point with PCC - API for PCC visit Creation by RPMS applications
+1 ; >> All date/time variables must be in FileMan internal format
+2 ; Special Incoming Variables:
+3 ; BSDIN("FORCE ADD") = 1 ; no matter what, create new visit (Optional)
+4 ; BSDIN ("NEVER ADD") = 1 ; never add visit, just try to find one or more (Optional)
+5 ; BSDIN("ANCILLARY") = 1 ; for ancillary packages to create noon visit if no match found (Optional)
+6 ; BSDIN("SHOW VISITS") = 1 ; this will display visits if more than one match
+7 ; Incoming Variables used in Matching: REQUIRED
+8 ; BSDIN("PAT") = patient IEN (file 2 or 9000001)
+9 ; BSDIN("VISIT DATE") = visit date & time (same as check-in date & time)
+10 ; BSDIN ("SITE") = location of encounter IEN (file 4 or 9999999.06)
+11 ; BSDIN("VISIT TYPE") = internal value for field .03 in Visit file
+12 ; BSDIN("SRV CAT") = internal value for service category
+13 ; BSDIN("TIME RANGE") = # ; range in minutes for matching on visit time; REQUIRED unless FORCE ADD set
+14 ; ; zero=exact matches only; -1=don't match on time
+15 ; These are used to match if sent (Optional)
+16 ; BSDIN("PROVIDER") = IEN for provider to match from file 200
+17 ; BSDIN("CLINIC CODE") = IEN of clinic stop code (file 40.7)
+18 ; BSDIN("HOS LOC") = IEN of hospital location (file 44, field .22 in VISIT file)
+19 ; BSDIN("DEF CC") = IEN of default clinic code for package making call PATCH 1009
+20 ; BSDIN("DEF HL") = IEN of default hospital location for package making call PATCH 1009
+21 ; Incoming Variables used in creating appt and visit
+22 ; BSDIN("APPT DATE") = appt date & time (Required for scheduled appts and walk-ins; check-in will be performed)
+23 ; BSDIN("USR") = user IEN in file 200; REQUIRED
+24 ; BSDIN("OPT") = name for Option Used To Create field, for check-in only (Optional)
+25 ; BSDIN("OI") = reason for appointment; for walk-ins (Optional)
+26 ; Incoming PCC variables for adding additional info to visit (Optional)
+27 ; BSDIN("APCDTPB") = Third Party Billed (#.04)
+28 ; BSDIN("APCDPVL") = Parent Visit Link (#.12)
+29 ; BSDIN("APCDAPPT") = WalkIn/Appt (#.16)
+30 ; BSDIN("APCDEVM") = Evaluation and Management Code (#.17)
+31 ; BSDIN("APCDCODT") = Check Out Date & Time (#.18)
+32 ; BSDIN("APCDLS") = Level of Service -PCC Form (#.19).
+33 ; BSDIN("APCDVELG") = Eligibility (#.21)
+34 ; BSDIN("APCDPROT") = Protocol (#.25).
+35 ; BSDIN("APCDOPT") = Option Used To Create (#.24) ;IHS/OIT/LJF 09/15/2005 PATCH 1004
+36 ; BSDIN("APCDOLOC") = Outside Location (#2101) ;IHS/OIT/LJF 12/21/2005 PATCH 1005
+37 ; Outgoing Array:
+38 ; BSDOUT(0) always set; if = 0 none found and may have error message in 2nd piece
+39 ; if = 1 and BSDOUT(visit ien)="ADD" new visit just created
+40 ; if = 1 and BSDOUT(visit ien)=#; # is time difference in minutes
+41 ; if >1, multiple BSDOUT(visit ien) entries exist
+42 NEW BSDTMP
KILL BSDOUT
+43 ;don't mess with incoming array
MERGE BSDTMP=BSDIN
+44 ;check required fields
IF '$$HAVEREQ(.BSDTMP,.BSDOUT)
QUIT
+45 ;IHS/ITSC/LJF 4/28/2005 PATCH 1003 if FORCE ADD set, bypass check-in & create visit
+46 ; if forced add, skip visit match attempt
+47 ;IF $G(BSDTMP("FORCE ADD")) D APPTDT Q
+48 ;cmi/flag/maw 9/2/2009 PATCH 1010
+49 IF '$GET(BSDTMP("APCDOPT"))
Begin DoDot:1
+50 IF $GET(BSDTMP("OPT"))]""
IF BSDTMP("OPT")?.N
IF $DATA(^DIC(19,BSDTMP("OPT")))
SET BSDTMP("APCDOPT")=BSDTMP("OPT")
QUIT
+51 IF $GET(BSDTMP("OPT"))]""
IF $EXTRACT(BSDTMP("OPT"),1,1)="`"
SET BSDTMP("APCDOPT")=$TRANSLATE(BSDTMP("OPT"),"`")
QUIT
+52 IF $GET(BSDTMP("OPT"))]""
IF BSDTMP("OPT")'?.N
SET BSDTMP("APCDOPT")=$ORDER(^DIC(19,"B",BSDTMP("OPT"),0))
QUIT
+53 IF $GET(BSDTMP("APCDOPT"))]""
IF $EXTRACT(BSDTMP("APCDOPT"),1,1)="`"
SET BSDTMP("APCDOPT")=$TRANSLATE(BSDTMP("APCDOPT"),"`")
QUIT
+54 IF $GET(BSDTMP("APCDOPT"))]""
IF BSDTMP("APCDOPT")'?.N
SET BSDTMP("APCDOPT")=$ORDER(^DIC(19,"B",BSDTMP("APCDOPT"),0))
QUIT
End DoDot:1
+55 IF $GET(BSDTMP("FORCE ADD"))
DO ADDVIST(.BSDTMP,.BSDOUT)
QUIT
+56 ; attempt to find matching visits; return BSDOUT array
+57 DO MATCH(.BSDTMP,.BSDOUT)
+58 ;IHS/ITSC/LJF 4/22/2005 PATCH 1003 if appt date set, go to check-in
+59 ;IF $G(BSDTMP("APPT DATE")),'$G(BSDTMP("NEVER ADD")) D APPTDT Q
+60 ;if only 1 visit found, return ien and quit
+61 IF BSDOUT(0)=1
QUIT
+62 ;if >1 visits found, return full array and quit, unless they pass it the variable to show visits then we will display
+63 ;(calling app decides next step)
+64 IF BSDOUT(0)>1
QUIT
+65 ;IHS/OIT/LJF 10/06/2005 PATCH 1004 added 2nd match, move never add checks & not kill variables
+66 ;if called by ancillary package, just create noon visit and quit
+67 IF $GET(BSDTMP("ANCILLARY"))
Begin DoDot:1
+68 ; set up to find other ancillaries
KILL BSDTMP("ANCILLARY"),BSDTMP("PROVIDER")
+69 ; try to match on hos loc or clinic
DO MATCH(.BSDTMP,.BSDOUT)
IF BSDOUT(0)=1
QUIT
+70 ; if in never add mode, quit after 2nd match
IF $GET(BSDTMP("NEVER ADD"))=1
QUIT
+71 ; take off time; PCC will add noon
SET BSDTMP("VISIT DATE")=BSDTMP("VISIT DATE")\1
+72 ; create noon visit; no PIMS link
DO ADDVIST(.BSDTMP,.BSDOUT)
End DoDot:1
QUIT
+73 ;if no visits found but in never add mode, just quit
+74 IF $GET(BSDTMP("NEVER ADD"))=1
QUIT
+75 ;IHS/OIT/LJF 10/06/2005 PATCH 1004 end of changes for this section
+76 ;otherwise, logic falls through to create visit choices
APPTDT ;
+1 ;cmi/maw 6/10/2008 PATCH 1009 for interactive visit creation
IF $GET(BSDTMP("CALLER"))]""
IF $GET(BSDTMP("CALLER"))="BSD CHECKIN"
QUIT
+2 ;if no appointment date/time sent, just create visit and quit
+3 IF '$GET(BSDTMP("APPT DATE"))
DO ADDVIST(.BSDTMP,.BSDOUT)
QUIT
+4 ;IHS/ITSC/LJF 4/22/2005 PATCH 1003 if one matching visit found, check-in but don't create visit
+5 IF BSDOUT(0)=1
SET BSDTMP("VIEN")=$ORDER(BSDOUT(0))
+6 ;if patient already has appt at this time, call Check-in (which creates visit) then quit
+7 NEW IEN,ERR,V
+8 ;find appt
SET IEN=$$SCIEN^BSDU2(BSDTMP("PAT"),BSDTMP("HOS LOC"),BSDTMP("APPT DATE"))
+9 IF IEN
Begin DoDot:1
+10 ; set variables used by checkin call
+11 SET BSDTMP("CDT")=BSDTMP("VISIT DATE")
+12 SET BSDTMP("CC")=$GET(BSDTMP("CLINIC CODE"))
+13 SET BSDTMP("PRV")=$GET(BSDTMP("PROVIDER"))
+14 ;IHS/ITSC/LJF 5/4/2005 PATCH 1003 set more variables to use in BSDAPI
+15 SET BSDTMP("CLN")=$GET(BSDTMP("HOS LOC"))
+16 SET BSDTMP("ADT")=$GET(BSDTMP("APPT DATE"))
+17 ;check in and create visit
SET ERR=$$CHECKIN^BSDAPI(.BSDTMP)
+18 ;IHS/ITSC/LJF 5/5/2005 PATCH 1003 reset BSDOUT only if truly added one.
+19 ;I 'ERR S V=$$GETVST^BSDU2(BSDTMP("PAT"),BSDTMP("APPT DATE")) I V S BSDOUT(0)=1,BSDOUT(V)="ADD" Q
+20 IF 'ERR
SET V=$$GETVST^BSDU2(BSDTMP("PAT"),BSDTMP("APPT DATE"))
IF V
IF '$GET(BSDTMP("VIEN"))
IF BSDOUT(0)=0
SET BSDOUT(0)=1
SET BSDOUT(V)="ADD"
QUIT
+21 IF ERR
SET BSDOUT(0)=0_U_$PIECE(ERR,U,2)
End DoDot:1
QUIT
+22 ; else call walk-in (which calls make appt, checkin and create visit)
+23 DO WALKIN(.BSDTMP,.BSDOUT)
+24 QUIT
MATCH(IN,OUT) ; find matching visits based on IN array
+1 SET OUT(0)=0
+2 NEW END,DATE,VIEN,STOP,DIFF,MATCH
+3 SET MATCH=0
+4 DO TIME(IN("TIME RANGE"),IN("VISIT DATE"),.DATE,.END)
+5 FOR
SET DATE=$ORDER(^AUPNVSIT("AA",IN("PAT"),DATE))
IF 'DATE
QUIT
IF (DATE>END)
QUIT
Begin DoDot:1
+6 SET VIEN=0
+7 FOR
SET VIEN=$ORDER(^AUPNVSIT("AA",IN("PAT"),DATE,VIEN))
IF 'VIEN
QUIT
Begin DoDot:2
+8 ;check for delete flag just in case xref not killed
IF $$GET1^DIQ(9000010,VIEN,.11)="DELETED"
QUIT
+9 ;no match on loc of enc
IF IN("SITE")'=$$GET1^DIQ(9000010,VIEN,.06,"I")
QUIT
+10 ;no match on vist type
IF IN("VISIT TYPE")'=$$GET1^DIQ(9000010,VIEN,.03,"I")
QUIT
+11 ;cmi/maw 06/01/2008 PATCH 1009 get observation and day surgery visits
+12 ;I IN("SRV CAT")'=$$GET1^DIQ(9000010,VIEN,.07,"I") Q ;no match on service category
+13 ;don't look at HIM excluded visits
IF IN("SRV CAT")["CENRT"
QUIT
+14 ;don't look at HIM excluded visits
IF $$GET1^DIQ(90000010,VIEN,.07,"I")["CENRT"
QUIT
+15 ;no match on service category
IF IN("SRV CAT")=$$GET1^DIQ(9000010,VIEN,.07,"I")
SET MATCH=1
+16 ;match if observation
IF IN("SRV CAT")="A"
IF $GET(IN("ANCILLARY"))
IF $$GET1^DIQ(9000010,VIEN,.07,"I")="O"
SET MATCH=1
+17 IF IN("SRV CAT")="A"
IF $GET(IN("ANCILLARY"))
IF $$GET1^DIQ(9000010,VIEN,.07,"I")="D"
SET MATCH=1
+18 IF '$GET(MATCH)
QUIT
+19 ;cmi/maw 06/01/2008 PATCH 1009 end of mods
+20 ;check time range
IF IN("TIME RANGE")>-1
SET STOP=0
Begin DoDot:3
+21 ;find difference in minutes
SET DIFF=$$TIMEDIF(IN("VISIT DATE"),VIEN)
+22 IF $$ABS^XLFMTH(DIFF)>IN("TIME RANGE")
SET STOP=1
End DoDot:3
IF STOP
QUIT
+23 ; if provider sent and didn't match, skip
IF '$$PRVMTCH
QUIT
+24 ; if called by ancillary, falls through and sets visit into array
+25 ; otherwise, check if app wants to match on clinic code or hosp location
+26 IF '$GET(IN("ANCILLARY"))
SET STOP=0
Begin DoDot:3
+27 IF $GET(IN("HOS LOC"))
IF '$GET(IN("CLINIC CODE"))
SET IN("CLINIC CODE")=$$GET1^DIQ(44,IN("HOS LOC"),8,"I")
+28 ;no match on clinic code
IF $GET(IN("CLINIC CODE"))
IF IN("CLINIC CODE")'=$$GET1^DIQ(9000010,VIEN,.08,"I")
SET STOP=1
QUIT
+29 ;IHS/ITSC/LJF 4/22/2005 PATCH 1003 if both have appt date and visit was triage clinic, is a match
+30 ;cmi/anch/maw 5/14/2008 PATCH 1009 requirement 59 create visit on same day no matter what
+31 ;I $G(IN("APPT DATE")),$$GET1^DIQ(9000010,VIEN,.26,"I"),$$TRIAGE(VIEN) Q ;cmi/maw 5/14/2008 comment out PATCH 1009 requirement 59
+32 ;no match on hospital location
IF $GET(IN("HOS LOC"))
IF (IN("HOS LOC")'=$$GET1^DIQ(9000010,VIEN,.22,"I"))
SET STOP=1
QUIT
+33 ;IHS/OIT/LJF 02/03/2006 PATCH 1005 if same clinic & same provider but not triage, make new visit
+34 IF $GET(IN("APPT DATE"))
IF $$GET1^DIQ(9000010,VIEN,.26,"I")
IF '$$TRIAGE(VIEN)
SET STOP=1
QUIT
End DoDot:3
IF STOP
QUIT
+35 ; must be good match, increment counter and set array node
+36 SET OUT(0)=OUT(0)+1
+37 SET OUT(VIEN)=$$TIMEDIF(IN("VISIT DATE"),VIEN)
End DoDot:2
End DoDot:1
+38 QUIT
+39 ;
PRVMTCH() ; do visits match on provider?
+1 NEW PRVS,IEN
+2 ; if no provider sent, assume okay
IF '$GET(IN("PROVIDER"))
QUIT 1
+3 ;IHS/ITSC/LJF 5/4/2005 PATCH 1003
+4 ;if visit is triage clinic & new encounter is not ancillary, skip provider match
+5 IF $$TRIAGE(VIEN)
IF '$GET(IN("ANCILLARY"))
QUIT 1
+6 ; find all v provider entries for visit
+7 SET IEN=0
FOR
SET IEN=$ORDER(^AUPNVPRV("AD",VIEN,IEN))
IF 'IEN
QUIT
Begin DoDot:1
+8 SET PRVS(+$GET(^AUPNVPRV(IEN,0)))=""
End DoDot:1
+9 ;if incoming provider in list, this is match
+10 IF $DATA(PRVS(IN("PROVIDER")))
QUIT 1
+11 ;otherwise, no match
+12 QUIT 0
+13 ;
TIMEDIF(VDTTM,VIEN) ; return time diff between incoming time and current visit
+1 QUIT $$FMDIFF^XLFDT(VDTTM,+$GET(^AUPNVSIT(VIEN,0)),2)\60
+2 ;
ADDVIST(BSDTMP,BSDOUT) ;
+1 KILL APCDALVR
NEW %DT,SUB
+2 SET SUB="APCD"
FOR
SET SUB=$ORDER(BSDTMP(SUB))
IF SUB]"APCDZZZZ"
QUIT
SET APCDALVR(SUB)=BSDTMP(SUB)
+3 ;keep it silent
SET APCDALVR("AUPNTALK")=""
SET APCDALVR("APCDANE")=""
+4 ;facility
SET APCDALVR("APCDLOC")=BSDTMP("SITE")
+5 ;patient
SET APCDALVR("APCDPAT")=BSDTMP("PAT")
+6 ;visit type
SET APCDALVR("APCDTYPE")=BSDTMP("VISIT TYPE")
+7 ;srv cat
SET APCDALVR("APCDCAT")=BSDTMP("SRV CAT")
+8 ;chkin dt
SET APCDALVR("APCDDATE")=BSDTMP("VISIT DATE")
+9 ;clinic code ien w/`
IF $GET(BSDTMP("CLINIC CODE"))
SET APCDALVR("APCDCLN")="`"_BSDTMP("CLINIC CODE")
+10 ;clinic name
SET APCDALVR("APCDHL")=$GET(BSDTMP("HOS LOC"))
+11 ;appt date
SET APCDALVR("APCDAPDT")=$GET(BSDTMP("APPT DATE"))
+12 ;I '$G(APCDALVR("APCDOPT")) S APCDALVR("APCDOPT")=$G(BSDTMP("OPT")) ;option name PATCH 1010
+13 ;force add
SET APCDALVR("APCDADD")=1
+14 ;create visit
+15 DO ^APCDALV
+16 ;if no visit created,error quit
+17 IF '$GET(APCDALVR("APCDVSIT"))
Begin DoDot:1
+18 SET BSDOUT(0)="0^Error Creating Visit"
End DoDot:1
QUIT
+19 ; set new visit info in out array
+20 SET BSDOUT(APCDALVR("APCDVSIT"))="ADD"
SET BSDOUT(0)=1
+21 ;IHS/OIT/LJF 10/05/2005 PATCH 1004
KILL APCDALVR
+22 QUIT
+23 ;
WALKIN(BSDATA,OUT) ;EP; create walkin appt which is checked in and visit created
+1 ; also called by BSDAPI3 to create ancillary walkin appt
+2 NEW ERR,V
+3 ;initialize outgoing count
SET OUT(0)=0
+4 SET BSDATA("CLN")=$GET(BSDATA("HOS LOC"))
+5 ;4=walkin
SET BSDATA("TYP")=4
+6 SET BSDATA("ADT")=$GET(BSDATA("APPT DATE"))
+7 IF '$DATA(BSDATA("LEN"))
SET BSDATA("LEN")=$$GET1^DIQ(44,BSDATA("CLN"),1912)
+8 ; make walkin appt
+9 SET ERR=$$MAKE^BSDAPI(.BSDATA)
IF ERR
SET $PIECE(OUT(0),U,2)=$PIECE(ERR,U,2)
QUIT
+10 ; set variables used by checkin call
+11 SET BSDATA("CDT")=BSDATA("VISIT DATE")
+12 SET BSDATA("CC")=$GET(BSDATA("CLINIC CODE"))
+13 SET BSDATA("PRV")=$GET(BSDATA("PROVIDER"))
+14 ; check in appt and create visit
+15 SET ERR=$$CHECKIN^BSDAPI(.BSDATA)
+16 ; update out array based on result
+17 ;IHS/ITSC/LJF 5/5/2005 PATCH 1003 reset BSDOUT(0) only if added new visit
+18 ;I 'ERR S V=$$GETVST^BSDU2(BSDATA("PAT"),BSDATA("APPT DATE")) I V S OUT(0)=1,OUT(V)="ADD" ;visit added
+19 ;visit added
IF 'ERR
SET V=$$GETVST^BSDU2(BSDATA("PAT"),BSDATA("APPT DATE"))
IF V
IF '$GET(BSDTMP("VIEN"))
IF OUT(0)=0
SET OUT(0)=1
SET OUT(V)="ADD"
+20 ;error
IF ERR
SET $PIECE(OUT(0),U,2)=$PIECE(ERR,U,2)
+21 QUIT
+22 ;
HAVEREQ(IN,OUT) ; check required fields
+1 IF ('$GET(IN("FORCE ADD")))
IF ('$DATA(IN("TIME RANGE")))
SET OUT(0)="0^Missing Time Range"
QUIT 0
+2 IF '$DATA(IN("PAT"))
SET OUT(0)="0^Missing Patient IEN"
QUIT 0
+3 IF '$DATA(IN("VISIT DATE"))
SET OUT(0)="0^Missing Visit Date"
QUIT 0
+4 IF '$DATA(IN("SITE"))
SET OUT(0)="0^Missing Facility/Site"
QUIT 0
+5 IF '$DATA(IN("VISIT TYPE"))
SET OUT(0)="0^Missing Visit Type"
QUIT 0
+6 IF '$DATA(IN("SRV CAT"))
SET OUT(0)="0^Missing Service Category"
QUIT 0
+7 IF '$DATA(IN("USR"))
SET OUT(0)="0^Missing User IEN"
QUIT 0
+8 ;IHS/ITSC/LJF 5/5/2005 PATCH 1003 moved line to set clinic code earlier
+9 IF $GET(IN("HOS LOC"))
IF '$GET(IN("CLINIC CODE"))
SET IN("CLINIC CODE")=$$GET1^DIQ(44,IN("HOS LOC"),8,"I")
+10 ;IHS/ITSC/LJF 5/5/2005 PATCH 1003 convert service category
+11 IF $GET(IN("APPT DATE"))
IF $GET(IN("HOS LOC"))
SET IN("SRV CAT")=$$SERCAT^BSDV(IN("HOS LOC"),IN("PAT"))
+12 QUIT 1
+13 ;
TIME(RANGE,VISIT,DATE,END) ; set DATE and END based on TIME RANGE setting in minutes
+1 NEW TMDIF,SW
+2 SET TMDIF=$SELECT(RANGE<1:0,1:RANGE)
+3 SET DATE=$$FMADD^XLFDT(VISIT,,,-TMDIF)
+4 SET END=$$FMADD^XLFDT(VISIT,,,TMDIF)
+5 IF (DATE\1)<(END\1)
SET SW=(END\1)
SET END=(DATE\1)_".9999"
SET DATE=SW
+6 SET DATE=(9999999-(DATE\1)_"."_$PIECE(DATE,".",2))-.0001
+7 SET END=9999999-(END\1)_"."_$PIECE(END,".",2)
+8 ;no time range used; go from begin one day to end
IF RANGE=-1
SET END=(END\1)_".9999"
SET DATE=(DATE\1)
+9 QUIT
+10 ;
TRIAGE(VST) ; returns 1 if visit's hosp loc is triage type ;IHS/ITSC/LJF 4/22/2005 PATCH 1003
+1 NEW HL
+2 SET HL=$$GET1^DIQ(9000010,VST,.22,"I")
IF 'HL
QUIT 0
+3 QUIT +$$GET1^DIQ(9009017.2,HL,.16,"I")
+4 ;