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

PXCOMPACT.m

Go to the documentation of this file.
PXCOMPACT ;ALB/BPA,CMC - Routine for COMPACT Act APIs;05/06/2024@12:01
 ;;1.0;PCE PATIENT CARE ENCOUNTER;**240**;Aug 12, 1996;Build 55
 ; *240* APIs for Episode of Care APIs (^PXCOMP(818))
 ; Reference to $$ELIG^DGCOMPACTELIG in ICR #7462
 ; Reference to SETPTFFLG^DGCOMPACT and SETPTFMVMT^DGCOMPACT in ICR #7463
 ; Reference to ^DGPM("ATID3") in ICR #17
 ; 
 Q
GETSTDT(DFN) ;
 ; Retrieve the start date from the zero level of the most current episode sequence
 ; DFN is the Patient ID
 ;
 N PXEOCNUM,PXEOCSEQ,PXSTDT
 S (PXEOCNUM,PXEOCSEQ,PXERRMSG,PXSTDT)=""
 ;D VALDFN(DFN,.PXERRMSG) I PXERRMSG'="" Q PXERRMSG
 I $D(^PXCOMP(818,"B",DFN)) D
 . S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN),PXSTDT=$P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",1)
 Q PXSTDT
 ;
GETIPDT(DFN) ;
 ;Retrieve the IP benefit end date
 ;
 N PXEOCNUM,PXEOCSEQ,IPENDDT
 S (PXEOCNUM,PXEOCSEQ,IPENDDT)=""
 I $D(^PXCOMP(818,"B",DFN)) D
 . S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN),IPENDDT=$P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",4)
 Q IPENDDT
 ;
GETEOC(DFN) ;
 ; Get the Episode of Care number assigned to the patient
 N PXEOCNUM
 I DFN="" S PXEOCNUM=""
 E  S PXEOCNUM=$O(^PXCOMP(818,"B",DFN,""))
 Q PXEOCNUM
 ;
GETEOCSEQ(DFN) ;
 ; Get the current/last Episode of Care sequence
 N PXEOCNUM,PXEOCSEQ
 S PXEOCSEQ="",PXEOCNUM=$$GETEOC(DFN)
 I $G(PXEOCNUM)'="",$D(^PXCOMP(818,PXEOCNUM,10,0)) S PXEOCSEQ=$O(^PXCOMP(818,PXEOCNUM,10,"B"),-1)
 Q PXEOCSEQ
 ;
GETPOINTRSEQ(DFN,PXENC,PXTY) ;
 ; Get the pointer sequence from the episode of care
 ; DFN - Internal Patient ID *required
 ; PXENC - Internal Encounter ID (VISIT or PTF) *required
 ; PXTY - Inpatient or Outpatient
 N PXEOCNUM,PXEOCSEQ,PXPONTRNUM
 S PXPONTRNUM=""
 I $G(PXTY)="" S PXTY=$$GETBENTYP(DFN)
 S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN)
 I PXEOCNUM'="",PXTY'="" D
 . I PXTY="O" S PXPONTRNUM=$O(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,41,"B",PXENC,""))
 . E  S PXPONTRNUM=$O(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,40,"B",PXENC,""))
 Q PXPONTRNUM
 ;
GETBENTYP(DFN) ;
 N PXBENTYP,PXEOCNUM,PXEOCSEQ
 S PXEOCNUM=$$GETEOC(DFN)
 I PXEOCNUM'="" S PXBENTYP=$P(^PXCOMP(818,PXEOCNUM,0),"^",3)
 ; If there is no value in the benefit type field for a closed episode of care, 
 ; look at the benefit end date history
 I PXBENTYP="" D
 . ; Default setting to outpatient
 . S PXEOCSEQ=$$GETEOCSEQ(DFN),PXBENTYP="O"
 . ; Look for the inpatient benefit end date and outpatient benefit end date to 
 . ; determine a closed episode of care benefit type
 . I $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",4)'="",$P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",5)="" S PXBENTYP="I"
 Q PXBENTYP
 ;
SETSTDT(DFN,PXNWSTDT) ;
 ; Set a new start date for the current / most recent episode of care
 ; DFN      - Patient ID
 ; PXNWSTDT - New start date (VA format)
 ; 
 N CDATA,CMPMSG,PXBENTYP,PXEOCNUM,PXEOCSEQ,PXERRMSG,PXFNDOBI,PXFNDOBO,PXIENS
 S (CDATA,CMPMSG,PXBENTYP,PXEOCNUM,PXEOCSEQ,PXERRMSG,PXFNDOBI,PXFNDOBO,PXIENS)=""
 ;
 ; Check to see if date is less than seven digits in length
 I $L(PXNWSTDT)<7 S PXERRMSG="The start date value "_PXNWSTDT_" is less than seven digits in length." D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 ; Check to see if date is more than seven digits in length
 I $L(PXNWSTDT)>7 S PXERRMSG="The start date value "_PXNWSTDT_" is greater than seven digits in length." D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 ; Check to see if the start date is prior to January 17th, 2023
 I PXNWSTDT<3230117 S PXERRMSG="The start date "_PXNWSTDT_" is prior to January 17th, 2023." D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 ; Check to see if start date is in the future
 I PXNWSTDT>DT S PXERRMSG="The start date "_PXNWSTDT_" is in the future." D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 ;
 D VALDFN(DFN,.PXERRMSG) I PXERRMSG'="" Q
 S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN),PXBENTYP=$$GETBENTYP(DFN)
 I PXBENTYP="I" S PXFNDOBI=$$FMADD^XLFDT(PXNWSTDT,29)
 I PXBENTYP="O" S PXFNDOBO=$$FMADD^XLFDT(PXNWSTDT,89)
 S PXIENS=PXEOCSEQ_","_PXEOCNUM_","
 ; 
 I $G(PXNWSTDT)'="" S CDATA(818.01,PXIENS,.01)=PXNWSTDT
 I $G(PXFNDOBI)'="" S CDATA(818.01,PXIENS,4)=PXFNDOBI
 I $G(PXFNDOBO)'="" S CDATA(818.01,PXIENS,5)=PXFNDOBO
 D FILE^DIE("","CDATA","CMPMSG")
 I $D(CMPMSG("DIERR")) D FILEMANERR^PXCOMPACT1(DFN,.CDATA,.CMPMSG) Q
 Q
 ;
SETENDDT(DFN,PXENDDT,PXENDSRC,PXAUTH,PXCOMM) ;
 N PXEOCNUM,PXEOCSEQ,PXERRMSG,PXSTDT
 S (PXEOCNUM,PXEOCSEQ,PXERRMSG,PXSTDT)=""
 D VALDFN(DFN,.PXERRMSG) I PXERRMSG'="" Q
 I $G(PXENDDT)="" Q
 I $G(PXAUTH)'="" D VALUSR(PXAUTH,.PXERRMSG) I PXERRMSG'="" Q
 ;
 S PXENDSRC=$S($G(PXENDSRC)="PA":"PA",$G(PXENDSRC)="PR":"PR",$G(PXENDSRC)="TE":"TE",1:"")
 ;
 S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN)
 S PXSTDT=$P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^")
 I PXSTDT>PXENDDT Q
 ;
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",2)=PXENDDT
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",3)=PXENDSRC
 I $P(^PXCOMP(818,PXEOCNUM,0),"^",3)="I" S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",4)=PXENDDT
 I $P(^PXCOMP(818,PXEOCNUM,0),"^",3)="O" S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",5)=PXENDDT
 I $G(PXAUTH)'="" S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,1),"^",1)=PXAUTH
 I $G(PXCOMM)'="" S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,1),"^",2)=PXCOMM
 ; Set the ACUTE SUICIDAL CRISIS FLAG to zero and benefit type flag to NULL
 S $P(^PXCOMP(818,PXEOCNUM,0),"^",2)=0,$P(^PXCOMP(818,PXEOCNUM,0),"^",3)=""
 Q
 ;
NEWEOC(DFN,PXENC,PXTY,PXSTDT,PXSRC) ;
 ; API to input a new Episode of Care
 ;
 N CDATA,CMPMSG,EPDATA,PXELIG,PXEOCNUM,PXEOCSEQ,PXERRMSG,PXFNDOBI,PXFNDOBO,PXIEN,PXIENEP,PXTYCHK
 S (CDATA,CMPMSG,EPDATA,PXELIG,PXEOCNUM,PXEOCSEQ,PXERRMSG,PXFNDOBI,PXFNDOBO,PXIEN,PXIENEP,PXTYCHK)=""
 I $G(DFN)="" S PXERRMSG="Patient ID (DFN) cannot be null" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 I '$D(^DPT(DFN)) S PXERRMSG="Patient is invalid, not in the patient file #2" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 S PXTYCHK=$S($G(PXTY)="I":1,$G(PXTY)="O":1,1:0)
 I PXTYCHK=0 S PXERRMSG="Inpatient (I) or Outpatient (O) must be the designated type" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 I PXENC="" S PXERRMSG="The encounter value can not be null" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 I $D(^PXCOMP(818,"B",DFN)) D
 . S PXEOCNUM=$$GETEOC(DFN)
 . I $P(^PXCOMP(818,PXEOCNUM,0),"^",2)=1 S PXERRMSG="Patient has an open episode" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN)
 I PXERRMSG'="" Q
 ; If the start date is not passed in, use the start date of the encounter
 I $D(^DGPT(PXENC)),$G(PXSTDT)="",$G(PXTY)="I" S PXSTDT=$P($P(^DGPT(PXENC,0),"^",2),".")
 I $D(^AUPNVSIT(PXENC)),$G(PXSTDT)="",$G(PXTY)="O" S PXSTDT=$P($P(^AUPNVSIT(PXENC,0),"^"),".")
 I $G(PXSTDT)="" S PXSTDT=DT
 S PXELIG=$$ELIG^DGCOMPACTELIG(DFN,"PXCOMPACT")
 S PXELIG=$S(PXELIG="ELIGIBLE":"E",PXELIG="NOT ELIGIBLE":"N",1:"U")
 I $G(PXSRC)="" S PXSRC=""
 I PXTY="I" S PXFNDOBI=$$FMADD^XLFDT(PXSTDT,29)
 I PXTY="O" S PXFNDOBO=$$FMADD^XLFDT(PXSTDT,90)
 ;Set top level of ^PXCOMP
 I $G(PXEOCNUM)="" D
 . N CDATA,CMPMSG,PXIEN
 . S PXIEN="?+1,"
 . S CDATA(818,PXIEN,.01)=DFN
 . S CDATA(818,PXIEN,2)=1
 . S CDATA(818,PXIEN,3)=PXTY
 . D UPDATE^DIE("","CDATA","","CMPMSG")
 . I $D(CMPMSG("DIERR")) D FILEMANERR^PXCOMPACT1(DFN,.CDATA,.CMPMSG) Q
 E  D
 . S $P(^PXCOMP(818,PXEOCNUM,0),"^",2)=1
 . S $P(^PXCOMP(818,PXEOCNUM,0),"^",3)=PXTY
 ;
 I $D(CMPMSG("DIERR")) Q
 I PXEOCNUM="" S PXEOCNUM=$$GETEOC(DFN)
 ;
 ;Set episode level (10)
 S PXIENEP="?+1,"_PXEOCNUM_","
 I $G(PXSTDT)'="" S EPDATA(818.01,PXIENEP,.01)=PXSTDT
 I $G(PXFNDOBI)'="" S EPDATA(818.01,PXIENEP,4)=PXFNDOBI
 I $G(PXFNDOBO)'="" S EPDATA(818.01,PXIENEP,5)=PXFNDOBO
 I $G(PXSRC)'="" S EPDATA(818.01,PXIENEP,7)=PXSRC
 I $G(PXELIG)'="" S EPDATA(818.01,PXIENEP,8)=PXELIG
 D UPDATE^DIE("","EPDATA","","CMPMSG")
 I $D(CMPMSG("DIERR")) D FILEMANERR^PXCOMPACT1(DFN,.EPDATA,.CMPMSG)
 ;
 S (PXEOCNUM,PXEOCSEQ)=""
 S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN)
 I $G(PXELIG)'="" S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",8)=PXELIG
 ; CALL 40/41 API HERE
 D VISIT(PXENC,PXTY,PXEOCNUM,DFN)
 Q
 ;
CHGTYPSTAT(DFN,PXTY,PXCHNGDT) ;
 N PXEOCNUM,PXEOCSEQ,PXFNDOBI,PXFNDOBO
 S (PXFNDOBI,PXFNDOBO)=""
 S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN)
 I PXEOCNUM="" Q
 ;
 I $$GETBENTYP(DFN)=PXTY Q
 I $G(PXCHNGDT)="" S PXCHNGDT=DT
 S $P(^PXCOMP(818,PXEOCNUM,0),"^",3)=PXTY
 ; Set the updated benefit end dates based on the type of change
 I PXTY="O" S PXFNDOBI=PXCHNGDT,PXFNDOBO=$$FMADD^XLFDT(PXCHNGDT,90)
 I PXTY="I" S PXFNDOBO=PXCHNGDT,PXFNDOBI=$$FMADD^XLFDT(PXCHNGDT,29)
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",4)=PXFNDOBI
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",5)=PXFNDOBO
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",7)="A"
 Q 
 ;
VISIT(ENC,PXTY,PXEOCNUM,DFN) ;
 ;
 N CDATA,CMPMSG,PXIENS,PXEOCSEQ
 S (PXIENS)=""
 S PXEOCSEQ=$$GETEOCSEQ(DFN)
 I PXTY="I" D
 . S (CMPMSG,CDATA(818.04))=""
 . ;Set top level 40 node for PTF pointer
 . S PXIENS="?+1,"_PXEOCSEQ_","_PXEOCNUM_","
 . I $G(ENC)'="" D
 . . S CDATA(818.04,PXIENS,.01)=ENC
 . . D UPDATE^DIE("","CDATA","","CMPMSG")
 . . I $D(CMPMSG("DIERR")) D FILEMANERR^PXCOMPACT1(DFN,.CDATA,.CMPMSG)
 . . I $D(^DGPT(ENC,0)) D SETPTFFLG^DGCOMPACT(ENC,1)
 I PXTY="O" D
 . S (CMPMSG,CDATA(818.141))=""
 . ;Set top level 41 node for VISIT pointer 
 . S PXIENS="?+1,"_PXEOCSEQ_","_PXEOCNUM_","
 . I $G(ENC)'="" D
 . . S CDATA(818.141,PXIENS,.01)=ENC
 . . D UPDATE^DIE("","CDATA","","CMPMSG")
 . . I $D(CMPMSG("DIERR")) D FILEMANERR^PXCOMPACT1(DFN,.CDATA,.CMPMSG)
 . . D SETVSTFLG(DFN,ENC,1)
 Q
 ;
SETVSTFLG(DFN,PXENC,PXVAL) ;
 N PXEOCNUM,PXEOCSEQ,PXPOINTRSEQ
 S PXEOCNUM=$$GETEOC(DFN),PXEOCSEQ=$$GETEOCSEQ(DFN),PXPOINTRSEQ=$$GETPOINTRSEQ(DFN,PXENC,"O")
 I PXPOINTRSEQ'="" S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,41,PXPOINTRSEQ,0),"^",2)=PXVAL
 Q
 ;
VALDFN(DFN,PXERRMSG) ;
 N PXERRMSG
 S PXERRMSG=""
 I DFN="" S PXERRMSG="Patient required" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 I '$D(^DPT(DFN)) S PXERRMSG="Patient is invalid, not in the patient file #2" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 I '$D(^PXCOMP(818,"B",DFN)) S PXERRMSG="Patient is not in the COMPACT Act Episode of Care file #818" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 Q
 ;
VALUSR(PXAUTH,PXERRMSG) ;
 N PXERRMSG
 S PXERRMSG=""
 I PXAUTH="" S PXERRMSG="User value can not be null" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 I '$D(^VA(200,PXAUTH,0)) S PXERRMSG="Invalid user "_PXAUTH_", not in the new person file #200" D COMPACTERR^PXCOMPACT1(PXERRMSG,DFN) Q
 Q
 ;
ASC(DFN) ;
 ; Determine if patient is currently in an acute suicidal crisis
 N PXEOCNUM,ASC
 S PXEOCNUM="",PXEOCNUM=$$GETEOC(DFN),ASC="N"
 I PXEOCNUM,$P(^PXCOMP(818,PXEOCNUM,0),"^",2)>0 S ASC="Y"
 Q ASC
 ;
DISPLAY(DFN) ;
 ;before calling this tag, verify eligibility by calling ELIG^DGCOMPACTELIG(DFN)
 ;if ELIGIBLE or UNDETERMINED, call this tag
 N DISPLAY,ELIGSEQ,ENDDT,EOCTYP,IPBENEND,OPBENEND,OPEXT,PXIEN,PXLASTSEQ,PXNUMOPX,PXSEQ,STARTDT
 S (ENDDT,EOCTYP,IPBENEND,OPBENEND,OPEXT,PXIEN,PXLASTSEQ,PXNUMOPX,PXSEQ,STARTDT,ELIGSEQ,DISPLAY)=""
 ;get Compact Act sequence
 I $D(^PXCOMP(818,"B",DFN)) S PXSEQ=$$GETEOC(DFN)
 I PXSEQ'="" D
 . ;find the latest sequence in the episode
 . S PXLASTSEQ=$$GETEOCSEQ(DFN)
 . ;get the start date and format it
 . S STARTDT=$P(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,0),"^",1),STARTDT=$$FMTE^XLFDT(STARTDT)
 . S EOCTYP=$P(^PXCOMP(818,PXSEQ,0),"^",3)
 . ;check if the episode has ended. If so, only display Episode Start and End Date
 . S ENDDT=$P($G(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,0)),"^",2)
 . S IPBENEND=$P($G(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,0)),"^",4) I IPBENEND'="" S IPBENEND=$$FMTE^XLFDT(IPBENEND)
 . S OPBENEND=$P($G(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,0)),"^",5) I OPBENEND'="" S OPBENEND=$$FMTE^XLFDT(OPBENEND)
 . I ENDDT'="" D
 . . S ENDDT=$$FMTE^XLFDT(ENDDT)
 . . ;check for extensions
 . . S OPEXT=$D(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,30))
 . . I OPEXT=0 D
 . . . S DISPLAY="COMPACT Act Start Date^"_STARTDT_"^End Date^"_ENDDT_"^IP Benefit End Date^"_IPBENEND_"^OP Benefit end date^"_OPBENEND
 . . . ; if there's an extension, display extension start and episode end date
 . . I OPEXT D
 . . . S STARTDT=$P($G(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,30,1,0)),"^",1),STARTDT=$$FMTE^XLFDT(STARTDT)
 . . . S DISPLAY="Extension Start Date^"_STARTDT_"^Episode End Date^"_ENDDT
 . ;if not ended, display Episode Start Date and Remaining Days (if no extensions)
 . E  D
 . . ;check for extensions
 . . S OPEXT=$D(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,30))
 . . I OPEXT=0 D 
 . . . S PXIEN=PXLASTSEQ_","_PXSEQ
 . . . I EOCTYP="I",$P(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,0),"^",4)'="" D
 . . . . S DISPLAY="COMPACT Act Start Date^"_STARTDT_"^Remaining Days^"_$$GET1^DIQ(818.01,PXIEN,42)_"^Inpatient Benefit End Date^"_IPBENEND
 . . . E  S DISPLAY="COMPACT Act Start Date^"_STARTDT_"^Remaining Days^"_$S(EOCTYP="O":$$GET1^DIQ(818.01,PXIEN,43),1:$$GET1^DIQ(818.01,PXIEN,42))
 . . I OPEXT D
 . . . S STARTDT=$P($G(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,30,1,0)),"^",1),STARTDT=$$FMTE^XLFDT(STARTDT)
 . . . S PXNUMOPX=$P(^PXCOMP(818,PXSEQ,10,PXLASTSEQ,30,0),"^",3),PXIEN=PXNUMOPX_","_PXLASTSEQ_","_PXSEQ
 . . . S DISPLAY="Extension Start Date^"_STARTDT_"^Remaining Days^"_$S(EOCTYP="O":$$GET1^DIQ(818.01,PXIEN,43),1:$$GET1^DIQ(818.01,PXIEN,42))
 Q DISPLAY
 ;
ADMIT(DFN,STARTDT,ADMIT,PTF) ;
 ;called from DGPM ADMIT input template
 ;first, close the episode of care IF it's outpatient
 N EOCNUM,EOCSEQ,LTD,REOPNFLG
 S (EOCNUM,EOCSEQ)="",REOPNFLG=0
 S EOCNUM=$$GETEOC(DFN)
 I EOCNUM="" D NEWEOC(DFN,PTF,"I",STARTDT,"V") I ADMIT="F" D SETPTFMVMT^DGCOMPACT(PTF,"Y")
 ;Processing complete, new episode of care created
 I EOCNUM="" Q
 S EOCSEQ=$$GETEOCSEQ(DFN) I EOCSEQ="" Q
 ;
 ;checking for scenario where patient is discharged but admitted same day
 I $P(^PXCOMP(818,EOCNUM,0),"^",3)="O",$P(^PXCOMP(818,EOCNUM,0),"^",2)=1 D
 . ; - get the last discharge date
 . S LTD=+$O(^DGPM("ATID3",DFN,"")) S:LTD LTD=9999999.9999999-LTD\1
 . ;Processing for patient that has been discharged/admitted the same day
 . I DT=LTD D REOPNEOC(EOCNUM,EOCSEQ,"") S REOPNFLG=1
 . I REOPNFLG=0 D SETENDDT(DFN,DT,"PR",DUZ,"admitting")
 ;if a current inpatient EOC exists do the following:
 ;link both PTF records to the episode
 I $P(^PXCOMP(818,EOCNUM,0),"^",3)="I",$P(^PXCOMP(818,EOCNUM,0),"^",2)=1 D
 . D VISIT(PTF,"I",EOCNUM,DFN)
 . D SETPTFMVMT^DGCOMPACT(PTF,"Y")
 ;if no current EOC inpatient record
 I $P(^PXCOMP(818,EOCNUM,0),"^",2)=0 D 
 . D NEWEOC(DFN,PTF,"I",STARTDT,"V")
 . ;set movement for Full admit
 . I ADMIT="F" D SETPTFMVMT^DGCOMPACT(PTF,"Y")
 Q
 ;
REOPNEOC(PXEOCNUM,PXEOCSEQ,STARTDT) ;
 N DFN,ELIG S (DFN,ELIG)=""
 ; Reopen an inpatient Episode of Care
 S $P(^PXCOMP(818,PXEOCNUM,0),"^",2)=1 ;Reset the episode of care open/close flag
 S $P(^PXCOMP(818,PXEOCNUM,0),"^",3)="I" ;Reset the Benefit Type
 ; Start date processing needs benefit type
 S DFN=$P(^PXCOMP(818,PXEOCNUM,0),"^",1)
 I $G(STARTDT)="" S STARTDT=$P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",1)
 E  D SETSTDT(DFN,STARTDT)
 ;
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",2)="" ;Remove the end date
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",3)="" ;Remove the end source
 I $D(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,1)) K ^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,1)
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",4)=$$FMADD^XLFDT(STARTDT,29) ;Reset the inpatient benefit end date
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",5)="" ;Remove the outpatient benefit end date
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",6)="" ;Remove the final status
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",7)="" ;Remove the source
 S ELIG=$$ELIG^DGCOMPACTELIG(DFN,"PXCOMPACT")
 S $P(^PXCOMP(818,PXEOCNUM,10,PXEOCSEQ,0),"^",8)=$S(ELIG="ELIGIBLE":"E",ELIG="NOT ELIGIBLE":"N",1:"U") ;Reset the patient eligibility
 Q
 ;