YSCL122P ; ALB/RTW - NCC POST INSTALL;05/10/2016
 ;;5.01;MENTAL HEALTH;**122**;Dec 30, 1994;Build 18
 ;THIS ROUTINE IS DESIGNED TO GO THROUGH THE CLOZAPINE PATIENTS IN FILE PS(55 PHARMACY PATIEN FILE
 ;discover active clozapine patients registered longer than 57 days ago that do not have a recent clozapine 
 ;prescription or order, set them to discontinued and send a report to the NCC.
 ;the NCC software will maintain the file in real time from this point on.
INIT ;
 ;INITIALIZE ^XTMP("YSCLDEM" and ^XTMP("YSCLTRN"
 N X,X1,X2 S X1=DT,X2=365 D C^%DTC
 S ^XTMP("YSCLDEM",0)=X_"^",$P(^XTMP("YSCLDEM",0),"^",2)=DT,$P(^XTMP("YSCLDEM",0),"^",3)="CLOZAPINE DAILY ROLLUP DATA",$P(^XTMP("YSCLDEM",0),"^",4)=DT-1_".000001"
 S ^XTMP("YSCLTRN",0)=X_"^",$P(^XTMP("YSCLDEM",0),"^",2)=DT,$P(^XTMP("YSCLTRN",0),"^",3)="CLOZAPINE DAILY ROLLUP DATA",$P(^XTMP("YSCLTRN",0),"^",4)=DT-1_".000001"
 D START
 Q
 ;
START ;
 K ^XTMP("YSCLDIS"),^XTMP("YSCLDATA")
 S YSCLD4="The clozapine patient status has changed to 'Discontinued' during the post "
 S YSCLD44="installation of patch YS*7*122. Their registration date is more than 56 days"
 S YSCLD444="ago and there is no a recent filled the prescription/order present. "
 S DFN=0 F  S DFN=$O(^PS(55,"ASAND",DFN)) 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 YSCLREGD=$P(YSCLDATA,"^",6)
 . S X1=DT,X2=YSCLREGD D ^%DTC S YSCLREGA=X
 . Q:YSCLREGA<57  ;Not checking those registered 56 days or less
 . S ^XTMP("YSCLDATA",DT,DFN)=YSCLDATA
 . D OPT I YSCLCNT=1,YSCLOPT="" S YSCLFLAG=2
 . D:YSCLFLAG'=1 INP
 . I YSCLFLAG=2 S YSCLDIS1=1 D DC,DMG^YSCLTST5
 D:$D(^XTMP("YSCLDIS")) TR
 K DA,DFN,X,X1,X2,YSCLCNT,YSCLDIS1,YSCLFLAG,YSCLFLDA,YSCLFLDT,YSCLREGA,YSCLREGD,YSCLPSDA,YSCLREGDA,YSCLREGN,YSCLSPDA
 K YSCLLN,YSCLDIS2,XMTEXT,XMSUB,XMDUZ,YSCLD4,YSCLD44,YSCLD444
 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 Q:'YSCLOPT  Q:YSCLFLAG>0  D
 . S YSCLRX=$G(^PS(55,DFN,"P",YSCLOPT,0)),YSCLRX0=$G(^PSRX(YSCLRX,0))
 . Q:'+$G(YSCLRX0)
 . S YSCLDRG=$P(YSCLRX0,"^",6)
 . S YSCLPSDT=$P(^PSRX(YSCLRX,2),"^",6)
 . S X1=DT,X2=YSCLPSDT D ^%DTC S YSCLPSDA=X
 . I YSCLPSDA>56,'$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=2,YSCLDIS2=4 Q
 Q
INP ;
 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
 . 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>56,'$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=2,YSCLDIS2=4
 . I YSCLSPDT'="",YSCLSPDA>56,$D(^PSDRUG("ACLOZ",YSCLDRG)) S YSCLFLAG=1 Q
 Q
DC ;
 N DIE,DR
 S DIE="^PS(55,",DA=DFN,DR="54///"_"D"_";56///1" D ^DIE
 Q
TR ;
 S XMSUB=$P($$SITE^VASITE,"^",2)_" Discontinued Status by YS*5.01*122 post"
 S XMDUZ="CLOZAPINE MONITOR"
 S ^TMP("YSCL",$J,1,0)="Clozapine Discontinued Patient(s) Data was transmitted, at the time of patch YS*5.01*122 installation "_YSCLLN_" records were sent.",XMTEXT="^TMP(""YSCL"",$J,"
 D TR^YSCLDIS
 Q
CHANGE ; FOR TESTING I SET ALL PATIENTS TO ACTIVE AND NOT TRANSMITTED
 K ^XTMP("YSCLDISPOST"),^XTMP("YSCLDATA")
 S DFN=0 F  S DFN=$O(^PS(55,"ASAND",DFN)) Q:'DFN  D
 . S DIE="^PS(55,",DA=DFN,DR="54///"_"A"_";56///0" D ^DIE
 Q