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

SDES2BLOCKANDMOV.m

Go to the documentation of this file.
SDES2BLOCKANDMOV ;ALB/BLB,TJB,BLB - BLOCK AND MOVE ;MAY 23, 2024
 ;;5.3;Scheduling;**875,880,889**;Aug 13, 1993;Build 9
 ;;Per VHA Directive 6402, this routine should not be modified
 ;
 Q
 ;
BLOCKANDMOVE(JSON,SDCONTEXT,BLOCKANDMOVE) ;
 N RETURN,ERRORS,RESOURCEIEN,APPOINTMENT,JSONCANCEL,JSONCLINAVAIL,ORIGINALCLINIC,JSONAPPT,NEWAPPT,CANCELAPPT,CANCELAVAIL,OVERBOOK,VARIABLESLOTS
 ;
 ; populate vars
 D POPULATE(.BLOCKANDMOVE,.APPTIEN,.TARGETCLINIC,.TARGETDATE,.ORIGINALCLINIC,.CANCELAPPT,.CANCELAVAIL,.VARIABLESLOTS)
 ;
 ; validate Block and Move
 D VALIDATE(.ERRORS,.SDCONTEXT,$G(APPTIEN),$G(TARGETCLINIC),.TARGETDATE,.RESOURCEIEN,.OVERBOOK,.APPOINTMENT,.CANCELAVAIL,.CANCELAPPT,ORIGINALCLINIC,VARIABLESLOTS)
 I $D(ERRORS) M RETURN=ERRORS D BUILDJSON(.JSON,.RETURN) Q
 ;
 ; cancel original appointment
 D CANCELAPPT^SDES2CANCELAPPT(.JSONCANCEL,.SDCONTEXT,.CANCELAPPT)
 ;
 ; block availability in original clinic slot
 D CANCEL^SDES2CANCLNAVAIL(.JSONCLINAVAIL,.SDCONTEXT,.CANCELAVAIL)
 ;
 ; create new appointment in new slot
 D BUILDAPPTARRAY(.APPOINTMENT,APPTIEN,TARGETDATE,TARGETCLINIC,OVERBOOK,ORIGINALCLINIC)
 D CREATE^SDES2CREATEAPPT(.JSONAPPT,.SDCONTEXT,.APPOINTMENT)
 D DECODE^XLFJSON("JSONAPPT","NEWAPPT")
 ;
 S RETURN("BlockAndMoveAppointment","NewAppointmentIEN")=$G(NEWAPPT("Appointment","IEN"))
 D BUILDJSON(.JSON,.RETURN)
 Q
 ;
POPULATE(BLOCKANDMOVE,APPTIEN,TARGETCLINIC,TARGETDATE,ORIGINALCLINIC,CANCELAPPT,CANCELAVAIL,VARIABLESLOTS) ;
 ;
 ; block and move variables
 S APPTIEN=$G(BLOCKANDMOVE("APPOINTMENT IEN"))
 S TARGETCLINIC=$G(BLOCKANDMOVE("TARGET CLINIC"))
 S TARGETDATE=$G(BLOCKANDMOVE("TARGET DATE TIME"))
 S ORIGINALCLINIC=$$GET1^DIQ(409.831,$$GET1^DIQ(409.84,$G(APPTIEN),.07,"I"),.04,"I")
 I $$GET1^DIQ(409.84,APPTIEN,.18,"I"),$$GET1^DIQ(44,ORIGINALCLINIC,1912,"I") D
 .S VARIABLESLOTS=$$GET1^DIQ(409.84,APPTIEN,.18,"I")/$$GET1^DIQ(44,ORIGINALCLINIC,1912,"I")-1
 S VARIABLESLOTS=$S($L($G(VARIABLESLOTS)):VARIABLESLOTS,1:0)
 ;
 ; cancel appointment array
 S CANCELAPPT("APPT IEN")=APPTIEN
 S CANCELAPPT("CLINIC IEN")=ORIGINALCLINIC
 S CANCELAPPT("DFN")=$$GET1^DIQ(409.84,APPTIEN,.05,"I")
 S CANCELAPPT("CANCELLED BY")="C"
 S CANCELAPPT("CANCEL REASON")="BLOCK AND MOVE"
 ;
 ; cancel clinic availability array
 S CANCELAVAIL("CLINIC IEN")=ORIGINALCLINIC
 S CANCELAVAIL("FULL PARTIAL FLAG")="P"
 S CANCELAVAIL("START DATE TIME")=$$FMTISO^SDAMUTDT($$GET1^DIQ(409.84,APPTIEN,.01,"I"),ORIGINALCLINIC)
 S CANCELAVAIL("END DATE TIME")=$$FMTISO^SDAMUTDT($$GET1^DIQ(409.84,APPTIEN,.02,"I"),ORIGINALCLINIC)
 S CANCELAVAIL("REMARKS")="BLOCK AND MOVE"
 Q
 ;
VALIDATE(ERRORS,SDCONTEXT,APPTIEN,TARGETCLINIC,TARGETDATE,RESOURCEIEN,OVERBOOK,APPOINTMENT,CANCELAVAIL,CANCELAPPT,ORIGINALCLINIC,VARIABLESLOTS) ;
 N ORIGINALSLOTS,TARGETSLOTS,SINC,STARTOFDAY,APPTARRAY,ARY84,ARY44,ARY2,VAL,FDA,ORIGINATINGSLOTS
 ;
 D VALCONTEXT^SDES2VALCONTEXT(.ERRORS,.SDCONTEXT)
 I $D(ERRORS) Q
 ;
 S RESOURCEIEN=$$GET1^DIQ(409.84,$G(APPTIEN),.07,"I")
 D VALIDATEDATE(.ERRORS,.TARGETDATE,$G(TARGETCLINIC))
 D VALIDATEAPPTIEN(.ERRORS,$G(APPTIEN),$G(TARGETDATE))
 D VALIDATECLINIC(.ERRORS,$G(TARGETCLINIC))
 I $D(ERRORS) Q
 ;
 D PRIVILEGEDUSER(.ERRORS,$G(TARGETCLINIC),$S($G(SDCONTEXT("USER DUZ")):SDCONTEXT("USER DUZ"),1:DUZ))
 D VALIDATECLINOPEN(.ERRORS,$G(TARGETCLINIC),$G(TARGETDATE))
 D VALIDATELENGTHS(.ERRORS,RESOURCEIEN,$G(TARGETCLINIC),$G(APPTIEN))
 D VALIDATETARGSLOT(.ERRORS,.TARGETSLOTS,TARGETCLINIC,TARGETDATE,$G(APPTIEN),.OVERBOOK)
 D VALIDATEORIGSLOT(.ERRORS,.ORIGINATINGSLOTS,$$GET1^DIQ(409.831,$$GET1^DIQ(409.84,$G(APPTIEN),.07,"I"),.04,"I"),$P($$FMTISO^SDAMUTDT($$GET1^DIQ(409.84,$G(APPTIEN),.01,"I")),"T"),$P($$GET1^DIQ(409.84,$G(APPTIEN),.01,"I"),".",2),APPTIEN)
 I $D(ERRORS) Q
 ;
 ; validate cancel appt
 D VALPARAMS^SDES2CANCELAPPT(.CANCELAPPT,.ERRORS)
 I $D(ERRORS) Q
 ;
 ; validate cancel clinic availability
 D VALIDATE^SDES2CANCLNAVAIL(.ERRORS,.SDCONTEXT,.CANCELAVAIL,$G(CANCELAVAIL("CLINIC IEN")),$G(CANCELAVAIL("FULL PARTIAL FLAG")),$G(CANCELAVAIL("REMARKS")))
 I $D(ERRORS) Q
 ;
 ; validate create appointment - status on request is opened prior to validating appointment to avoid hitting a false error for the request status in SDES2CREATEAPPT
 S FDA(409.85,$P($$GET1^DIQ(409.84,APPTIEN,.22,"I"),";")_",",23)="O" D FILE^DIE(,"FDA") K FDA
 D BUILDAPPTARRAY(.APPOINTMENT,APPTIEN,TARGETDATE,TARGETCLINIC,OVERBOOK,ORIGINALCLINIC)
 D VALIDATE^SDES2CREATEAPPT(.ERRORS,.SDCONTEXT,.APPOINTMENT,.VAL,1)
 S FDA(409.85,$P($$GET1^DIQ(409.84,APPTIEN,.22,"I"),";")_",",23)="C" D FILE^DIE(,"FDA") K FDA
 ;
 ; reset ISO times for cancel clin avail
 S CANCELAVAIL("START DATE TIME")=$$FMTISO^SDAMUTDT($G(CANCELAVAIL("START DATE TIME")),ORIGINALCLINIC)
 S CANCELAVAIL("END DATE TIME")=$$FMTISO^SDAMUTDT($G(CANCELAVAIL("END DATE TIME")),ORIGINALCLINIC)
 ;
 Q
 ;
VALIDATETARGSLOT(ERRORS,TARGETSLOTS,TARGETCLINIC,TARGETSTARTDATE,APPTIEN,OVERBOOK) ;
 N SLOTS,SLOTNUM,DONE
 D GETCLAVAILABLTY^SDESCLINICAVAIL(.SLOTS,TARGETCLINIC,$$FMTISO^SDAMUTDT(TARGETSTARTDATE),$$FMTISO^SDAMUTDT($P(TARGETSTARTDATE,".")))
 D DECODE^XLFJSON("SLOTS","TARGETSLOTS")
 ;
 S SLOTNUM=0,OVERBOOK=""
 F  S SLOTNUM=$O(TARGETSLOTS("ClinAvail",SLOTNUM)) Q:'SLOTNUM!(OVERBOOK=1)  D
 .I $G(TARGETSLOTS("ClinAvail",SLOTNUM,"BeginTime"))=$$FMTISO^SDAMUTDT(TARGETSTARTDATE) D
 ..I '$G(TARGETSLOTS("ClinAvail",SLOTNUM,"SlotsAvail")),"abcdefghijklmnopqrstuvwxyz"'[$G(TARGETSLOTS("ClinAvail",SLOTNUM,"SlotsAvail")) S OVERBOOK="O"
 Q
 ;
VALIDATEORIGSLOT(ERRORS,ORIGINATINGSLOTS,ORIGINALCLINIC,APPTDATE,APPTSTARTTIME,APPTIEN) ;
 N SLOTS,COUNT,SLOTNUM,DONE,NUMBEROFAPPTS
 ;
 S SLOTNUM=0,DONE=0,COUNT=1
 D GETCLAVAILABLTY^SDESCLINICAVAIL(.SLOTS,ORIGINALCLINIC,$$FMTISO^SDAMUTDT($$GET1^DIQ(409.84,APPTIEN,.01,"I"),ORIGINALCLINIC),$$FMTISO^SDAMUTDT($$GET1^DIQ(409.84,APPTIEN,.02,"I"),ORIGINALCLINIC))
 D DECODE^XLFJSON("SLOTS","ORIGINATINGSLOTS")
 ;
 F  S SLOTNUM=$O(ORIGINATINGSLOTS("ClinAvail",SLOTNUM)) Q:'SLOTNUM!($D(ERRORS))!(DONE=1)  D
 .I $G(ORIGINATINGSLOTS("ClinAvail",SLOTNUM,"BeginTime"))=$$FMTISO^SDAMUTDT($$GET1^DIQ(409.84,APPTIEN,.02,"I"),ORIGINALCLINIC) S DONE=1 Q
 .I $G(ORIGINATINGSLOTS("ClinAvail",SLOTNUM,"SlotsAvail"))'=0 D ERRLOG^SDES2JSON(.ERRORS,468) Q
 .S DATETIME=$S(SLOTNUM=1:$$GET1^DIQ(409.84,APPTIEN,.01,"I"),1:$$FMADD^XLFDT($$GET1^DIQ(409.84,APPTIEN,.01,"I"),,,$$GET1^DIQ(44,ORIGINALCLINIC,1912,"I")*(SLOTNUM-1)))
 .S NUMBEROFAPPTS=$$NUMBEROFAPPTS(ORIGINALCLINIC,DATETIME)
 .I SLOTNUM=1,NUMBEROFAPPTS'=1 D ERRLOG^SDES2JSON(.ERRORS,468)
 .I SLOTNUM>1,NUMBEROFAPPTS'=0 D ERRLOG^SDES2JSON(.ERRORS,468)
 ;
 I $$GET1^DIQ(44.003,$$GET44RECORDIENS^SDESCANAPPT2(ORIGINALCLINIC,$$GET1^DIQ(409.84,APPTIEN,.01,"I"),$$GET1^DIQ(409.84,APPTIEN,.05,"I")),9,"E")="OVERBOOK" D ERRLOG^SDES2JSON(.ERRORS,469)
 Q
 ;
NUMBEROFAPPTS(CLINICIEN,DATETIME) ;
 N COUNT
 S COUNT=0,SUBIEN=0
 F  S SUBIEN=$O(^SC(CLINICIEN,"S",DATETIME,1,SUBIEN)) Q:'SUBIEN  D
 .I $$GET1^DIQ(44.003,SUBIEN_","_DATETIME_","_CLINICIEN_",",310)="CANCELLED" Q
 .S COUNT=COUNT+1
 Q COUNT
 ;
VALIDATEAPPTIEN(ERRORS,APPTIEN,TARGETDATE) ;
 I APPTIEN="" D ERRLOG^SDES2JSON(.ERRORS,14) Q
 I APPTIEN'="",'$D(^SDEC(409.84,APPTIEN,0)) D ERRLOG^SDES2JSON(.ERRORS,15) Q
 I $P($$GET1^DIQ(409.84,APPTIEN,.01,"I"),".")'=$P($$GET1^DIQ(409.84,APPTIEN,.02,"I"),".") D ERRLOG^SDES2JSON(.ERRORS,462) Q
 I $$GET1^DIQ(409.84,APPTIEN,.12,"I")!($$GET1^DIQ(409.84,APPTIEN,.03,"I"))!($$GET1^DIQ(409.84,APPTIEN,.14,"I"))!($$GET1^DIQ(409.84,APPTIEN,.101,"I")) D ERRLOG^SDES2JSON(.ERRORS,463)
 Q
 ;
VALIDATECLINIC(ERRORS,TARGETCLINIC) ;
 I TARGETCLINIC="" D ERRLOG^SDES2JSON(.ERRORS,18) Q
 I TARGETCLINIC'="",'$D(^SC(TARGETCLINIC,0)) D ERRLOG^SDES2JSON(.ERRORS,19) Q
 Q
 ;
VALIDATECLINOPEN(ERRORS,TARGETCLINIC,TARGETDATE) ;
 N TIMECLINICOPENS
 S TIMECLINICOPENS=$$GET1^DIQ(44,TARGETCLINIC,1914,"I")
 I TIMECLINICOPENS="" S TIMECLINICOPENS=8
 ;
 I +$E($P(TARGETDATE,".",2)_"0000",1,4)<TIMECLINICOPENS D ERRLOG^SDES2JSON(.ERRORS,344)
 I '$G(^SC(TARGETCLINIC,"ST",$P(TARGETDATE,"."),0)) D ERRLOG^SDES2JSON(.ERRORS,466)
 Q
 ;
VALIDATELENGTHS(ERRORS,RESOURCEIEN,TARGETCLINIC,APPTIEN) ;
 I $$GET1^DIQ(409.84,APPTIEN,.18,"I")'=$$GET1^DIQ(44,TARGETCLINIC,1912) D ERRLOG^SDES2JSON(.ERRORS,507)
 Q
 ;
PRIVILEGEDUSER(ERRORS,TARGETCLINIC,USERID) ;
 I $$GET1^DIQ(44,TARGETCLINIC,2500,"I")="Y" D
 .I '$D(^SC(TARGETCLINIC,"SDPRIV",USERID,0)) D ERRLOG^SDES2JSON(.ERRORS,343)
 Q
 ;
VALIDATEDATE(ERRORS,DATE,CLINICIEN) ;
 I DATE="" D ERRLOG^SDES2JSON(.ERRORS,9) Q
 S DATE=$$ISOTFM^SDAMUTDT(DATE)
 I DATE=-1 D ERRLOG^SDES2JSON(.ERRORS,11) Q
 I '$P(DATE,".",2) D ERRLOG^SDES2JSON(.ERRORS,511) Q
 ; cant schedule on holiday if clinic does not allow
 I $$GET1^DIQ(44,CLINICIEN,1918.5,"I")'="Y",$D(^HOLIDAY($P(DATE,"."),0)) D ERRLOG^SDES2JSON(.ERRORS,465)
 Q
 ;
BUILDJSON(JSON,CANRETURN) ;
 N JSONERROR
 D ENCODE^XLFJSON("CANRETURN","JSON","JSONERR")
 Q
 ;
BUILDAPPTARRAY(APPOINTMENT,APPTIEN,TARGETDATE,TARGETCLINIC,OVERBOOK,ORIGINALCLINIC) ;
 N ORIGINALAPPT,FMDATETIME,PATIENS,RESIEN,CLINIEN,SDDFN,ARRAY298,ARRAY44003,SDIEN,NOTEIDX
 ;
 S FMDATETIME=$$ISOTFM^SDAMUTDT(TARGETDATE)
 D GETS^DIQ(409.84,APPTIEN,"*","ZI","ORIGINALAPPT")
 S SDDFN=$G(ORIGINALAPPT(409.84,APPTIEN_",",.05,"I"))
 S PATIENS=$G(ORIGINALAPPT(409.84,APPTIEN_",",.01,"I"))_","_SDDFN_","
 D GETS^DIQ(2.98,PATIENS,"**","IE","ARRAY298","ERR")
 S RESIEN=$G(ORIGINALAPPT(409.84,APPTIEN_",",.07,"I"))
 S CLINIEN=$$GET1^DIQ(409.831,RESIEN_",",.04,"I")
 S SDIEN=$$GET44RECORDIENS(CLINIEN,$G(ORIGINALAPPT(409.84,APPTIEN_",",.01,"I")),SDDFN)
 D GETS^DIQ(44.003,SDIEN,"**","IE","ARRAY44003","SDMSG")
 ;
 S APPOINTMENT("START DATE TIME")=$$FMTISO^SDAMUTDT(TARGETDATE)
 S APPOINTMENT("END DATE TIME")=$$FMTISO^SDAMUTDT($$FMADD^XLFDT(TARGETDATE,,,$$GET1^DIQ(409.84,APPTIEN,.18,"I")))
 S APPOINTMENT("DFN")=$G(ORIGINALAPPT(409.84,APPTIEN_",",.05,"I"))
 S APPOINTMENT("RESOURCE IEN")=$$GETRES^SDESINPUTVALUTL(TARGETCLINIC,1)
 S APPOINTMENT("WALKIN")="N"
 S APPOINTMENT("PATIENT INDICATED DATE")=$$FMTISO^SDAMUTDT($G(ORIGINALAPPT(409.84,APPTIEN_",",.2,"I")))
 S APPOINTMENT("EXTERNAL ID")=$G(ORIGINALAPPT(409.84,APPTIEN_",",.21,"I"))
 S APPOINTMENT("REQUEST TYPE")=$E($$GET1^DIQ(409.84,APPTIEN,.22,"E"),1,1)_"|"_$P($G(ORIGINALAPPT(409.84,APPTIEN_",",.22,"I")),";",1)
 S APPOINTMENT("PROVIDER IEN")=$S(TARGETCLINIC=ORIGINALCLINIC:$G(ORIGINALAPPT(409.84,APPTIEN_",",.16,"I")),1:"")
 S APPOINTMENT("CLINIC IEN")=TARGETCLINIC
 S NOTEIDX=0
 S APPOINTMENT("NOTE")=""
 F  S NOTEIDX=$O(ORIGINALAPPT(409.84,APPTIEN_",",1,NOTEIDX)) Q:+NOTEIDX=0  D
 . S APPOINTMENT("NOTE")=APPOINTMENT("NOTE")_ORIGINALAPPT(409.84,APPTIEN_",",1,NOTEIDX,0)
 S APPOINTMENT("APPOINTMENT TYPE")=""
 S APPOINTMENT("APPOINTMENT TYPE NAME")=$$GET1^DIQ(409.1,$G(ORIGINALAPPT(409.84,APPTIEN_",",.06,"I")),.01,"E")
 S APPOINTMENT("PATIENT STATUS")=$G(ORIGINALAPPT(409.84,APPTIEN_",",.23,"I"))
 S APPOINTMENT("APPOINTMENT LENGTH")=$G(ORIGINALAPPT(409.84,APPTIEN_",",.18,"I"))
 S APPOINTMENT("SERVICE CONNECTED")="" ;looking do not see why this is being sent, doesn't update in appointment
 S APPOINTMENT("SERVICE CONNECTED PERCENTAGE")="" ; doesn't update in appointment
 I $$GET1^DIQ(409.85,$P($G(ORIGINALAPPT(409.84,APPTIEN_",",.22,"I")),";"),43.8) D
  . S APPOINTMENT("MRTC")="TRUE"
  . S APPOINTMENT("MRTC PARENT")=$$GET1^DIQ(409.85,$P($G(ORIGINALAPPT(409.84,APPTIEN_",",.22,"I")),";"),43.8)
 S APPOINTMENT("APPOINTMENT REASON")=$G(ARRAY44003(44.003,SDIEN,3,"I"))
 S APPOINTMENT("PATIENT ELIGIBILITY")=$G(ARRAY44003(44.003,SDIEN,30,"I"))
 S APPOINTMENT("OVERBOOK")=OVERBOOK
 S APPOINTMENT("LAB DATE TIME")=$$FMTISO^SDAMUTDT($G(ARRAY298(2.98,PATIENS,5,"I")),CLINIEN)
 S APPOINTMENT("XRAY DATE TIME")=$$FMTISO^SDAMUTDT($G(ARRAY298(2.98,PATIENS,6,"I")),CLINIEN)
 S APPOINTMENT("EKG DATE TIME")=$$FMTISO^SDAMUTDT($G(ARRAY298(2.98,PATIENS,7,"I")),CLINIEN)
 S APPOINTMENT("PURPOSE")=$G(ARRAY298(2.98,PATIENS,9,"E"))
 S APPOINTMENT("COLLATERAL")=$G(ARRAY298(2.98,PATIENS,13,"E"))
 S APPOINTMENT("SCHEDULE REQUEST TYPE")=$G(ARRAY298(2.98,PATIENS,25,"I"))
 S APPOINTMENT("NEXT AVAILABLE APPOINTMENT")=$G(ARRAY298(2.98,PATIENS,26,"I"))
 S APPOINTMENT("FOLLOWUP")=$G(ARRAY298(2.98,PATIENS,28,"E"))
 Q
 ;
GET44RECORDIENS(CLINICIEN,APPTSTARTTIME,SDDFN,APPTOBJ) ;
 ;want to process through until we get to the correct appointment for time and clinic
 N FOUND,IENS44003,SUBIEN,APPTCNT,MATCH
 S FOUND=0,APPTCNT=0
 S SUBIEN="A" F  S SUBIEN=$O(^SC(CLINICIEN,"S",APPTSTARTTIME,1,SUBIEN),-1) Q:'SUBIEN!($G(FOUND)=1)  D
 .I $$GET1^DIQ(44.003,SUBIEN_","_APPTSTARTTIME_","_CLINICIEN_",",.01,"I")=SDDFN D
 ..S IENS44003=SUBIEN_","_APPTSTARTTIME_","_CLINICIEN_",",FOUND=1 Q
 Q $G(IENS44003)
 ;