YSCLDIS ;HINOI/RTW-DISCONTINUE CLOZAPINE PATIENT STATUS ;07 April 16
 ;;5.01;MENTAL HEALTH;**122**;Dec 30, 1994;Build 18
 ; Reference to ^DPT supported by IA #10035
 ; Reference to ^PS(55 supported by IA #787
 ;
 ;This routine will loop through ^PS(55,DFN,"ASAND" and check the last prescription
 ; enddate and/or the the Inpatient Order stop date. If the patient has not had an
 ; active prescription or Inpatent Clozapine Order in the last 56 days, the CLOZAPINE
 ; STATUS will be marked as discontinued.
 ; DFN is the patient DFN found in ^DPT( file #2 the patient field
 ; YSCLDATA hold the clozapine patient data from the "SAND"xref
 ; YSCLFLAG changes from 0 to 1 if criteria to avoid discontinueing the patient is met
 ; also if the YSCLFLAG is already changed to 1 after OPT there is not need to check inpatient
 ; YSCLFLAG changes from 0 to 2 if discontinuation is required
 ; YSCLPSDT is the PRESCRIPTION STOP or END DaTe
 ; YSCLRX is the prescription IEN in the ^PSRX precription file
 ; YSCLDRG is the drug number used in this to determine if the drug number is part of the clozapine xref
 ;If a new patient is registered with the NCCC and 28 days have passed since the registration and
 ; there has been NO Clozapine preciptions or Inpatient orders, the CLOZAPINE STATUS will be
 ; marked as discontinued.
 ;YSCLREGD is the registration date recorded in PS(55,DFN,"SAND" peice 6
 ;YSCLDIS1 determines subject of message these from the POST 122 install
 ;YSCLDIS2 determines the message used in the xmtext for this patient. is it 28 day 56 day or temp cloz discontinue
 Q
START ;
 K ^XTMP("YSCLDIS"),^XTMP("YSCLDATA")
 S DFN=0 F  S DFN=$O(^PS(55,"ASAND",DFN)),YSCLFLAG=0 Q:'DFN  D
 . N YSCLDATA,YSCLOPT
 . S YSCLDATA=$G(^PS(55,DFN,"SAND"))
 . Q:$G(YSCLDATA)=""
 . Q:$P(YSCLDATA,"^",2)="D"  ;Not checking those already discontinued
 . S YSCLREGN=$P(YSCLDATA,"^",1)
 . S YSCLREGD=$P(YSCLDATA,"^",6)
 . S X1=DT,X2=YSCLREGD D ^%DTC S YSCLREGA=X
 . I YSCLREGN["Z",YSCLREGA>4 S YSCLFLAG=2,YSCLDIS2=3 D SET ;temps greater than four days since reg
 . ;Q:YSCLREGA<(28)  ;Not checking those registered 27 days or less
 . I YSCLREGN'["Z",YSCLREGA<(28) Q
 . S ^XTMP("YSCLDATA",DT,DFN)=YSCLDATA
 . I '$G(YSCLFLAG) D OPT I YSCLCNT=1,YSCLOPT=0 S YSCLFLAG=2
 . D:YSCLFLAG'=1 INP
 . I YSCLFLAG=2 D YSCLFLAG,DC,DMG^YSCLTST5
 D:$D(^XTMP("YSCLDIS")) TR
 K DA,DFN,X,X1,X2,YSCLCNT,YSCLDIS1,YSCLFLAG,YSCLFLDA,YSCLFLDT,YSCLREGA,YSCLREGD,YSCLPSDA,YSCLREGDA,YSCLSPDA
 K PSOLAST4,YSCLD1,YSCLD11,YSCLD111,YSCLD2,YSCLD22,YSCLD222,YSCLD3,YSCLD33,YSCLD333,YSCLDATE,YSCLDIS2,YSCLLN,YSCLREGN
 K XMDUZ,XMSUB,XMTEXT,Y,YSCLD4,YSCLD44,YSCLD444,YSCLDFN,YSCLMESG
 Q
OPT ; first loop YSCLOPT="A"),-1) causes the loop in reverse order from where "P","A" is in the global upward from the last prescription
 N YSCLRX,YSCLRX0,YSCLDRG,YSCLPSDT
 S YSCLOPT="A",(YSCLFLAG,YSCLCNT,YSCLPSDT)=0 F  S YSCLOPT=$O(^PS(55,DFN,"P",YSCLOPT),-1) S YSCLCNT=YSCLCNT+1 D:'YSCLOPT YSCLFLAG Q:'YSCLOPT  Q:YSCLFLAG>0  D
 . S YSCLRX=$G(^PS(55,DFN,"P",YSCLOPT,0)),YSCLRX0=$G(^PSRX(YSCLRX,0))
 . Q:'+$G(YSCLRX0)
 . S YSCLPSDT=$P(^PSRX(YSCLRX,2),"^",6)
 . S X1=DT,X2=YSCLPSDT D ^%DTC S YSCLPSDA=X
 . Q:YSCLPSDA>56
 . S YSCLFLDT=$P(^PSRX(YSCLRX,2),"^",2)
 . S X1=DT,X2=YSCLFLDT D ^%DTC S YSCLFLDA=X
 . S YSCLDRG=$P(YSCLRX0,"^",6)
 . ;Below is for new registrations or reregistrations must have a fill within 28 days or they are discontinued
 . I YSCLREGA<57,YSCLREGA>27,YSCLFLDT>(YSCLREGD) D
 . . I $D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=1 Q
 . . I '$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLDIS2=1,YSCLFLAG=2
 . . ;Below is for  exiting registrations must have a fill less than 56 days apart or they are discontinued
 . D:YSCLPSDA<57  ;stop date 56 days or less in the past check for clozapine
 . . I $D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=1 Q
 . . I '$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLDIS2=2,YSCLFLAG=2 Q
 Q
INP ;
 ;YSCLIPT incremented inpatient orders, example PS(55,DFN,5,1,1,0)="84409"
 ;YSCLUD
 ;YSCLRXO
 ;YSCLDRG is the drug number
 ;YSCLSPDT is the date the last clozapine order stopped
 N YSCLIPT,YSCLUD,YSCLRX0,YSCLDRG,YSCLSPDT
 S:YSCLFLAG'=2 YSCLFLAG=0
 S YSCLIPT="A" F  S YSCLIPT=$O(^PS(55,DFN,5,YSCLIPT),-1) Q:'YSCLIPT!(YSCLFLAG>0)  D
 . Q:'$D(^PS(55,DFN,5,YSCLIPT,1,1,0))
 . S YSCLDRG=$P(^PS(55,DFN,5,YSCLIPT,1,1,0),"^",1)
 . Q:'$G(YSCLDRG)
 . Q:'$D(^PSDRUG("ACLOZ",YSCLDRG))
 . S YSCLSPDT=$P($P(^PS(55,DFN,5,YSCLIPT,2),"^",4),".",1)
 . S X1=DT,X2=YSCLSPDT D ^%DTC S YSCLSPDA=X
 . I YSCLSPDT'="",YSCLSPDA<57,'$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLDIS2=2,YSCLFLAG=2
 . I YSCLREGA>27,YSCLREGA<57,'$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLDIS2=1,YSCLFLAG=2
 . I YSCLREGA>27,YSCLREGA<57,$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=1,YSCLDIS2=1 Q
 . I YSCLSPDT'="",YSCLSPDA<57,$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=1,YSCLDIS2=2 Q
 Q
DC ;
 N DIE,DR
 S DIE="^PS(55,",DA=DFN,DR="54///"_"D"_";56///1" D ^DIE
 Q
TR ;
 K ^TMP("YSCL",$J)
 X ^%ZOSF("UCI") I Y=^%ZOSF("PROD") D
 . D YSCLDRSN
 . K XMY S XMY=("G.CLOZAPINE ROLL-UP@FORUM.DNS   ")
 . D YSXMTEXT
 . S XMDUZ="CLOZAPINE MONITOR",^TMP("YSCL",$J,1,0)="Clozapine Discontinued Patient(s) Data was transmitted, "_YSCLLN_" records were sent."
 . I YSCLDIS2'>3 S XMSUB=$P($$SITE^VASITE,"^",2)_" Discontinued Status",^TMP("YSCL",$J,2,0)=" "
 . S XMTEXT="^TMP(""YSCL"",$J," D ^XMD S $P(^YSCL(603.03,1,0),"^",6)=$$NOW^XLFDT
 I Y'=^%ZOSF("PROD") D
 . D YSCLDRSN
 . K XMY S XMY("G.CLOZAPINE ROLL-UP")=""
 . D YSXMTEXT
 . I YSCLDIS2'>3 S XMSUB=$P($$SITE^VASITE,"^",2)_" Discontinued Status",^TMP("YSCL",$J,2,0)=" "
 . S XMDUZ="CLOZAPINE MONITOR",^TMP("YSCL",$J,1,0)="Clozapine Discontinued Patient(s) Data was transmitted, "_YSCLLN_" records were sent."
 . S XMTEXT="^TMP(""YSCL"",$J," D ^XMD S $P(^YSCL(603.03,1,0),"^",6)=$$NOW^XLFDT
 Q
SET ;XTMP BUILD USED FOR TESTING
 S ^XTMP("YSCLDIS",DT,DFN,0)=YSCLDIS2
 Q
YSCLFLAG ;used when there are no prescriptions or orders
 I YSCLREGA>28,YSCLREGA<57 S (YSCLFLAG,YSCLDIS1,YSCLDIS2)=2 D SET Q
 I YSCLREGA=28 S (YSCLFLAG,YSCLDIS1)=2,YSCLDIS2=1 D SET Q
 Q
YSXMTEXT ;CALLED BY YSCLTST3 /RTW Start: Added to build message of discontinued clozapine patients data for NCC
 S (YSCLLN,YSCLDATE)=0,YSCLCNT=2 F  S YSCLDATE=$O(^XTMP("YSCLDIS",YSCLDATE)) Q:'YSCLDATE  D
 . S YSCLDFN=0 F  S YSCLDFN=$O(^XTMP("YSCLDIS",YSCLDATE,YSCLDFN)) Q:'YSCLDFN  D
 . . S YSCLDIS2=$P(^XTMP("YSCLDIS",YSCLDATE,YSCLDFN,0),"^",1)
 . . S PSOLAST4=$E($P($G(^DPT(YSCLDFN,0)),"^",9),6,9)
 . . S YSCLMESG=$$GET1^DIQ(2,YSCLDFN,.01)_" ("_PSOLAST4_")"
 . . I YSCLDIS2=1 D  ;28 day patients
 . . . S YSCLCNT=YSCLCNT+1,YSCLLN=YSCLLN+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLMESG ;message from YSCLDRSN
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD1
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD11
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD111
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)="" ;blank line
 . . I YSCLDIS2=2 D  ;56 day patients
 . . . S YSCLCNT=YSCLCNT+1,YSCLLN=YSCLLN+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLMESG ;message from YSCLDRSN
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD2
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD22
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD222
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)="" ;blank line
 . . I YSCLDIS2=3 D  ; temporary clozapine patients
 . . . S YSCLCNT=YSCLCNT+1,YSCLLN=YSCLLN+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLMESG ;message from YSCLDRSN
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD3
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD33
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD333
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)="" ;blank line
 . . I YSCLDIS2=4 D  ; temporary clozapine patients
 . . . S YSCLCNT=YSCLCNT+1,YSCLLN=YSCLLN+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLMESG ;message from YSCLDRSN
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD4
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD44
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)=YSCLD444
 . . . S YSCLCNT=YSCLCNT+1
 . . . S ^TMP("YSCL",$J,YSCLCNT,0)="" ;blank line
 Q
YSCLDRSN ;CALLED BY YSCLTST3  discontinued reasons
 S YSCLD1="The patient status has changed to 'Discontinued' because the new clozapine"
 S YSCLD11="patient has not filled the prescription/order within 28 days of being marked "
 S YSCLD111="'Active'. "
 S YSCLD2="The patient status has changed to 'Discontinued' because the active clozapine"
 S YSCLD22="patient has not filled the prescription/order within 56 days of being "
 S YSCLD222="prescribed/ordered."
 S YSCLD3="The patient status has changed to 'Discontinued' because the temporary local "
 S YSCLD33="authorization number assigned has expired and NCCC has not issued a new "
 S YSCLD333="authorization number. "
 Q 
 ;/RTW End: Added to build message of discontinued clozapine patients data for NCC