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

SDCED1.m

Go to the documentation of this file.
SDCED1 ;ALB/JCH - VSE ENCOUNTER XREF ; 19 Oct 14  04:11PM
 ;;5.3;Scheduling;**628**;Aug 13, 1993;Build 371
 ;;
 Q
 ;
EN(SDRPTAR,SDBEGRNG,SDENDRNG,SDNODE) ; 
 ;; This utility will build a cross reference for the ENCOUNTER (#409.68) file
 ;; that will contain all encounters for a provider, by date ( PROVIDER,DATE,ENCOUNTER )
 ;; and a cross reference that will contain all encounters for a facility, by date
 ;; ( LOCATION, DATE, ENCOUNTER) 
 ;
 N SDFAC,DATE,DT,SDWEEKS,SDATE,SDLCNT,SDRPT,SDBEGDT,SDENDDT,SDSTART
 S DT=$$NOW^XLFDT,SDATE=DT
 S SDATE=$O(^XTMP("SDVSE","DT","")) Q:'SDATE
 S SDSTART=$$FMADD^XLFDT(SDATE,-365)
 S SDFAC=+$$SITE^VASITE()
 S SDBEGRNG=$G(SDBEGRNG),SDENDRNG=$G(SDENDRNG)
 I ($G(SDBEGRNG)&$G(SDENDRNG)) S SDBEGDT=$P(SDBEGRNG,".")
 S SDENDATE=$S($G(SDBEGDT):$P(SDENDRNG,".")_".24",1:"")
 I $G(SDENDATE),$G(SDBEGDT) S DTRANGE=$P(SDBEGDT,".")_"-"_$P(SDENDATE,".")
 ;
 S SDNODE=$S($G(DTRANGE):$J_",SDCEX",1:"SDCEX")
 I $G(^XTMP(SDNODE,0))=$G(^XTMP("SDVSE",0)) Q
 K ^XTMP(SDNODE) S ^XTMP(SDNODE,0)=^XTMP("SDVSE",0)
 D WEEKS(SDSTART,.SDWEEKS)
 D PROVIDER(SDBEGRNG,SDENDRNG,$G(DTRANGE),SDNODE)
 D AGGDT(SDBEGRNG,SDENDRNG,$G(DTRANGE),SDNODE)
 D CLEAN(SDNODE)
 Q
 ;
PROVIDER(SDBEGRNG,SDENDRNG,DTRANGE,SDNODE) ; aggregate daily encounters by provider 
 N SDENC,SDEST,SDNEW,TELCATD,DIVEX,CLINEX,PROVEX,SDEFLT
 N SDIV,SDEC,SDCL,SDPR,SDACTDT,SDIV,SDGRP,AGGDATA,SDVSEBEG
 ;
 S SDEST=0,SDNEW=0,SDDT=0
 S SDEFLT=$$FMADD^XLFDT($$NOW^XLFDT,-366)
 ;
 S SDACTDT=$O(^XTMP("SDVSE","DT","")) Q:'SDACTDT
 S SDRPTYP="" F  S SDRPTYP=$O(SDRPTAR(SDRPTYP)) Q:SDRPTYP=""  D
 .S SDCL=0 F  S SDCL=$O(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL)) Q:'SDCL  D
 ..S SDIV=$$GETDIV(SDCL)
 ..S SDDT=$S($G(DTRANGE):+DTRANGE,1:SDEFLT),SDDT=$$FMADD^XLFDT(SDDT,,,-1)
 ..F  S SDDT=$O(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL,SDDT)) Q:'SDDT!($G(DTRANGE)&($P(SDDT,".")>$P(DTRANGE,"-",2)))  D
 ...S SDPR="" F  S SDPR=$O(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL,SDDT,SDPR)) Q:SDPR=""  D
 ....D ENCOUNTR(SDACTDT,SDRPTYP,SDFAC,SDIV,SDCL,SDDT,SDPR,$G(DTRANGE),SDNODE)
 ....D APPT(SDACTDT,SDRPTYP,SDFAC,SDIV,SDCL,SDDT,SDPR,$G(DTRANGE),SDNODE)
 ...N SUPPLY S SUPPLY=$G(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL,SDDT,0,"APP","SUP"))
 ...Q:'SUPPLY
 ...D SUPPLY(SDNODE,"Date",SDRPTYP,SDDT,SDFAC,SDIV,SDCL,SUPPLY)
 Q
 ;
ENCOUNTR(SDACTDT,SDRPTYP,SDFAC,SDIV,SDCL,SDDT,SDPR,DTRANGE,SDNODE) ; Get encounters from ^XTMP("SDVSE","DT"
 Q:'SDPR  ; Encounters must have a provider
 N SDUTLND,SVCAT,SDGRP,PRVNAM,CLNAM,STOPIEN,STOPGRP,SDEDDT,SDTEL
 S SDEDDT=SDDT
 S SDUTLND=$G(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL,SDEDDT,SDPR,"ENC"))
 Q:SDUTLND=""
 S SDENC=$P(SDUTLND,"^"),SDNEW=$P(SDUTLND,"^",2),SDEST=$P(SDUTLND,"^",3),SDTEL=$P(SDUTLND,"^",4)
 ;S SDGRP=$P($G(^SC(+SDCL,0)),"^",7)
 D SDGRP^SDECXUTL(+SDCL,.SDGRP)
 ; Aggregate daily provider total
 I $G(DTRANGE) S SDEDDT=DTRANGE
 S PRVNAM=$P($G(^VA(200,+SDPR,0)),"^")
 S CLNAM=$P($G(^SC(+SDCL,0)),"^")
 N SDTMPND S SDTMPND=$G(^XTMP(SDNODE,"Date",SDRPTYP,$P(SDEDDT,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR))
 S AGGDATA="" N I F I=1:1:$L(SDUTLND,"^") D
 .S $P(AGGDATA,"^",I)=$P(SDUTLND,"^",I)+$P(SDTMPND,"^",I)
 S ^XTMP(SDNODE,"Date",SDRPTYP,$P(SDEDDT,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR)=AGGDATA
 ;
 N SDTMPND S SDTMPND=$G(^XTMP(SDNODE,"Date",SDRPTYP,$P(SDEDDT,"."),SDFAC,SDIV,0,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR))
 S AGGDATA="" N I F I=1:1:$L(SDUTLND,"^") D
 .S $P(AGGDATA,"^",I)=$P(SDUTLND,"^",I)+$P(SDTMPND,"^",I)
 S PRVNAM=$P($G(^VA(200,+SDPR,0)),"^")
 S ^XTMP(SDNODE,"Date",SDRPTYP,$P(SDEDDT,"."),SDFAC,SDIV,0,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR)=AGGDATA
 Q
 ;
APPT(SDACTDT,SDRPTYP,SDFAC,SDIV,SDCL,SDDT,SDPAT,DTRANGE,SDNODE) ; Get appointments from ^XTMP("SDVSE","DT"
 N SDUTLND,SDPR,SDTMPND,SDGRP,AGGDATA,LEN,STATUS,STOPIEN,STOPGRP
 S SDPR="" F  S SDPR=$O(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL,SDDT,SDPAT,"APPT",SDPR)) Q:SDPR=""  D
 .S SDUTLND=$G(^XTMP("SDVSE","DT",SDACTDT,SDRPTYP,SDCL,SDDT,SDPAT,"APPT",SDPR))
 .Q:SDUTLND=""
 .; Aggregate the total of all groups, and each group
 .D APPTG
 Q
 ;
APPTG ; Add data to groups "All" and SDGRP
 N PRVNAM,SDDATE,CLNAM,TMPGRP,SDGRP
 S PRVNAM=$S($G(SDPR):$P($G(^VA(200,+SDPR,0)),"^"),1:" None")
 S SDDATE=$S($G(DTRANGE):DTRANGE,1:SDDT)
 S CLNAM=$P($G(^SC(+$G(SDCL),0)),"^")
 D SDGRP^SDECXUTL(+SDCL,.TMPGRP)
 S SDGRP=TMPGRP
 ;
 ; Aggregate Overbooks^New^Established
 S LEN=$P(SDUTLND,"^",4),STATUS=$P(SDUTLND,"^",5)
 I 'LEN S LEN=$$APPLEN^SDCED2($G(SDCL))
 Q:'$G(LEN)
 ;
 ; Appointment Lengths specific ResourceGroup
 S AGGDATA=""
 S SDTMPND=$G(^XTMP(SDNODE,"Date",SDRPTYP,$P(SDDATE,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR,"APP","LEN",LEN,TMPGRP))
 S AGGDATA=$P(SDTMPND,"^")+LEN
 ; Continued
 N I F I=1:1:3 D
 .S $P(AGGDATA,"^",I+1)=$P(SDUTLND,"^",I)+$P(SDTMPND,"^",I+1)
 S ^XTMP(SDNODE,"Date",SDRPTYP,$P(SDDATE,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR,"APP","LEN",LEN,TMPGRP)=AGGDATA
 ;
 ; Appointment Status, Specific ResourceGroup
 S SDTMPND=$G(^XTMP(SDNODE,"Date",SDRPTYP,$P(SDDATE,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR,"APP","LEN",LEN,"STAT",STATUS))
 S SDTMPND=SDTMPND+1
 S ^XTMP(SDNODE,"Date",SDRPTYP,$P(SDDATE,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,PRVNAM_"^"_SDPR,"APP","LEN",LEN,"STAT",STATUS)=SDTMPND
 ;
 Q
 ;
CLEAN(SDNODE) ; Clean up
 Q
 ;
CHKEST(DFN,BDATE,ENC,CLSTOP) ; Was Patient DFN's encounter on date DATE considered Established or New?
 N OLOC,SDLOC,OENCND,SDCHIT,PRVDATE,PRVENC,OCLSTOP S PRVDATE=BDATE,PRVENC=ENC
 Q:'$G(DFN) ""  Q:'$G(BDATE) ""  Q:'$G(ENC) ""  Q:'$D(^SCE(ENC,0)) ""
 S SDLOC=$P($G(^SCE(ENC,0)),"^",4),CLSTOP=""
 D CLSTOP(SDLOC,.CLSTOP) I 'CLSTOP S CLSTOP=$P($G(^SCE(ENC,0)),"^",3) Q:'CLSTOP ""
 S SDCHIT=0
 F  Q:$G(SDCHIT)  S PRVDATE=$O(^SCE("ADFN",DFN,PRVDATE),-1) Q:'PRVDATE!($$FMDIFF^XLFDT(BDATE,PRVDATE)>730)  S PRVENC=0 F  S PRVENC=$O(^SCE("ADFN",DFN,PRVDATE,PRVENC)) Q:'PRVENC!$G(SDCHIT)  D
 .S OENCND=$G(^SCE(PRVENC,0)) S OCLSTOP="",OLOC=$P(OENCND,"^",4)
 .D CLSTOP(OLOC,.OCLSTOP) S:'$G(OCLSTOP) OCLSTOP=$P(OENCND,"^",3)
 .I OCLSTOP=CLSTOP S SDCHIT=$P(^SCE(ENC,0),"^",4)
 Q $S($G(SDCHIT):$G(SDCHIT),1:0)
 ;
GETSCAT(SDLOC,SDSGNM,SDSGAR)  ; Get Stop Code Group Name
 N CSTOP S CSTOP="",SDSGNM="UNKNOWN"
 D CLSTOP(SDLOC,.CSTOP)
 I $G(CSTOP) D STOPCAT(CSTOP,.SDSGNM,.SDSGAR)
 Q
 ;
CLSTOP(SDLOC,CSTOP) ; Get Clinic Stop
 N SDCS K CSTOP S CSTOP=""
 S CSTOP=$P(^SC(SDLOC,0),U,7)
 Q:'CSTOP
 ; Get AMIS REPORTING STOP CODE
 S CSTOP=$P(^DIC(40.7,CSTOP,0),U,2)
 Q
STOPCAT(CLSTOP,CAT,STOPAR) ; Get stop code category (Mental Health, Specialty, Primary)
 N CLINEX,STPCNT,OK,TMPCAT,FOUND S FOUND=0
 Q:'$G(CLSTOP)  K CAT S TMPCAT="UNKNOWN"
 S STPCNT="" F  S STPCNT=$O(STOPAR(STPCNT)) Q:(STPCNT="")!$G(FOUND)  D
 .S CLINEX="" F  S CLINEX=$O(STOPAR(STPCNT,CLINEX)) Q:CLINEX=""!$G(FOUND)  D
 ..I +$P($G(STOPAR(STPCNT,CLINEX)),"^",2)=+CLSTOP S FOUND=CLSTOP
 ..S TMPCAT=STPCNT
 S CAT=$S(TMPCAT="S":"SPECIALTY CARE",TMPCAT="M":"MENTAL HEALTH",TMPCAT="P":"PRIMARY CARE",1:"UNKNOWN")
 Q
 ;
DIV(DIVINT) ; convert internal division to external
 N DIC,DA,DR,DIVAR,DIVXAR
 S DIC=40.8,DA=DIVINT,DR=".01"
 D GETS^DIQ(DIC,DA,DR,"E","DIVXAR")
 S DIVEX=$G(DIVXAR(40.8,DIVINT_",",.01,"E"))
 I DIVEX=""!($L(DIVEX)<3) S DIVEX=$P($$SITE^VASITE,"^",2)
 Q DIVEX
 ;
WEEKS(SDDT,WEEKS) ; Get ending dates for 52 weeks
 N TODAY,NEXTD,STARTD,NEXTW K WEEKS S WEEKS="",STARTD=""
 S TODAY=$$DOW^XLFDT(SDDT) I TODAY="Sunday" S STARTD=SDDT
 I TODAY'="Sunday" F I=1:1:6 D  Q:$G(STARTD)
 .S NEXTD=$$FMADD^XLFDT(SDDT,I) I $$DOW^XLFDT(NEXTD)="Sunday" S STARTD=NEXTD
 F I=1:1:53 S NEXTW=$P($$FMADD^XLFDT(STARTD,I*7),".") S WEEKS(NEXTW)=NEXTW
 Q
 ;
GETDIV(CLINIC) ; Return CLINIC's division from file 40.8
 N DIC,DA,DR,CLINAR
 Q:'$G(CLINIC) ""
 S DIQ(0)="I",DIC=44,DA=CLINIC,DR="3.5"
 D GETS^DIQ(DIC,+DA,DR,"I","CLINAR")
 S SDIV=$G(CLINAR(44,CLINIC_",",3.5,"I"))
 I '(SDIV>0) S SDIV=$P($$SITE^VASITE,"^",2)
 Q SDIV
 ;
AGGDT(SDBEGRNG,SDENDRNG,DTRANGE,SDNODE) ; Aggregate Facility, Divisions, Clinics, Providers by Date
 N SDFAC,SDIV,SDRPTYP,SDCL,SDPRV,SDDT,SDDTAG,SDENDATE,SDPRVND
 N TMPWK,TMPMON,TMPQRT,TMPYR,AGGDATA
 S SDRPTYP="" F  S SDRPTYP=$O(^XTMP(SDNODE,"Date",SDRPTYP)) Q:SDRPTYP=""  D
 .S SDDT=0 F  S SDDT=$O(^XTMP(SDNODE,"Date",SDRPTYP,SDDT)) Q:'SDDT  D
 ..S TMPMON=$E(SDDT,1,5)_"00"
 ..I '$G(TMPYR) S TMPYR=$E(SDDT,1,3)_"0000"
 ..S TMPWK=$$GETWEEK(SDDT,.SDWEEKS)
 ..S TMPQRT=$$GETQUART(SDDT)
 ..S SDFAC="" F  S SDFAC=$O(^XTMP(SDNODE,"Date",SDRPTYP,SDDT,SDFAC)) Q:SDFAC=""  D
 ...S SDIV="" F  S SDIV=$O(^XTMP(SDNODE,"Date",SDRPTYP,SDDT,SDFAC,SDIV)) Q:'SDIV  D
 ....;Q:SDIV="APP"
 ....S SDGRP="" F  S SDGRP=$O(^XTMP(SDNODE,"Date",SDRPTYP,SDDT,SDFAC,SDIV,SDGRP)) Q:SDGRP=""  D
 .....Q:SDGRP="APP"  Q:(SDGRP=0)
 .....S SDCL="" F  S SDCL=$O(^XTMP(SDNODE,"Date",SDRPTYP,SDDT,SDFAC,SDIV,SDGRP,SDCL)) Q:SDCL=""  D
 ......Q:SDCL="APP"  D SUP^SDCED2
 ......S SDPRV="" F  S SDPRV=$O(^XTMP(SDNODE,"Date",SDRPTYP,SDDT,SDFAC,SDIV,SDGRP,SDCL,SDPRV)) Q:SDPRV=""  D
 .......Q:(SDPRV'["^")
 .......;
 .......; Appointments
 .......D APPT^SDCED2(SDNODE,SDRPTYP,SDDT,SDFAC,SDIV,SDGRP,SDCL,SDPRV,DTRANGE,TMPMON,TMPQRT,TMPYR,.SDWEEKS)
 .......;
 .......; Get smallest encounter aggregation
 .......S SDPRVND=$G(^XTMP(SDNODE,"Date",SDRPTYP,SDDT,SDFAC,SDIV,SDGRP,SDCL,SDPRV))
 .......Q:SDPRVND=""
 .......;
 .......; Add daily provider aggregation to Group
 .......D AGGSET("^XTMP(SDNODE,""Date"",SDRPTYP,SDDT,SDFAC,SDIV,SDGRP)","Date",SDPRVND)
 .......;
 .......; Add daily provider aggregation to appropriate Clinic
 .......D AGGSET("^XTMP(SDNODE,""Date"",SDRPTYP,SDDT,SDFAC,SDIV,SDGRP,SDCL)","Date",SDPRVND)
 .......;
 .......N DTNAM,TOT1,TOT2,TOT3,TOT4,SDGLROOT,SDGLFULL
 .......I '($G(SDBEGRNG)&$G(SDENDRNG)) S SDGLROOT="^XTMP(SDNODE,DTNAM,SDRPTYP,TMPDT,SDFAC" D
 ........;  Aggregate Annual, Quarterly, Monthly, Weekly Group, Division, Clinic, Provider
 ........F DTNAM="Year","Month","Week","Quarter" D
 .........F SDGLFULL=SDGLROOT_")",SDGLROOT_",SDIV)",SDGLROOT_",SDIV,SDGRP)",SDGLROOT_",SDIV,SDGRP,SDCL)",SDGLROOT_",SDIV,SDGRP,SDCL,SDPRV)" D
 ..........; Don't add each group to facility and division totals - already done with group 0 (all)
 ..........;I SDGRP Q:(SDGLFULL[",SDIV)")!(SDGLFULL[",SDFAC)")
 ..........I SDGRP Q:'(SDGLFULL["SDGRP")
 ..........D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 .......;
 .......; Resource Group Specific aggregations (Annual, Quarterly, Monthly, Weekly, Daily)
 .......S SDGLROOT="^XTMP(SDNODE,""GROUP"",DTNAM,SDRPTYP,TMPDT" D
 ........F DTNAM="Year","Quarter","Month","Week","Date" D
 .........I $G(SDBEGRNG)&$G(SDENDRNG) Q:'(DTNAM="Date")
 .........S SDGLFULL=SDGLROOT_",SDFAC,SDGRP)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 .........S SDGLFULL=SDGLROOT_",SDFAC,SDGRP,""CLIN"",SDCL)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 .......;
 .......; Provider Specific aggregations (Annual, Quarterly, Monthly, Weekly, Daily)
 .......S SDGLROOT="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT"
 .......F DTNAM="Year","Quarter","Month","Week","Date" D
 ........I $G(SDBEGRNG)&$G(SDENDRNG) Q:'(DTNAM="Date")
 ........S SDGLFULL="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT,""FAC"",SDFAC,SDPRV)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 ........S SDGLFULL="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT,""FAC"",SDFAC,""CLIN"",SDCL,""PROV"",SDPRV)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 ........S SDGLFULL="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT,""DIV"",SDIV,SDPRV)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 ........S SDGLFULL="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT,""DIV"",SDIV,""CLIN"",SDCL,SDPRV)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 ........S SDGLFULL="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT,""GRP"",SDGRP,SDPRV)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 ........S SDGLFULL="^XTMP(SDNODE,""PROVIDER"",DTNAM,SDRPTYP,TMPDT,"_SDIV_",""GRP"",SDGRP,SDPRV)" D AGGSET(SDGLFULL,DTNAM,SDPRVND)
 ........;
 ........; Don't add each group to facility and division totals - already done with group 0 (all)
 ........; Add daily provider aggregation to division
 ........Q:($G(SDGRP)=0)
 ........D AGGSET("^XTMP(SDNODE,DTNAM,SDRPTYP,TMPDT,SDFAC,SDIV)",DTNAM,SDPRVND)
 ........;
 ........; Add daily provider aggregation to facility
 ........D AGGSET("^XTMP(SDNODE,DTNAM,SDRPTYP,TMPDT,SDFAC)",DTNAM,SDPRVND)
 Q
 ;
AGGSET(SDGLOB,DTNAM,PRVND) ; Perform generic aggregation of Pn values into SDGLOB
 ; Encounter data stored in @SDGLOB
 N I,TMPDT,TOT,AGGDATA
 S TMPDT=$S(DTNAM="Year":TMPYR,DTNAM="Quarter":TMPQRT,DTNAM="Month":TMPMON,DTNAM="Week":TMPWK,1:SDDT)
 S TOT=$G(@(SDGLOB))
 F I=1:1:$L(PRVND,"^") S $P(AGGDATA,"^",I)=($P(TOT,"^",I)+$P(PRVND,"^",I))
 S @(SDGLOB)=AGGDATA
 Q
 ;
GRP(LOC,GROUP) ; Get clinic groups
 ; Look in Clinic Group for now, until Medsphere code is available
 K GROUP S GROUP=""
 N LOCNUM,DIC,X,Y S DIC=44,DIC(0)="MZ",X=LOC D ^DIC Q:(Y<1)  S LOCNUM=+Y
 N CGRP S CGRP=0 F  S CGRP=$O(^PS(57.8,"AC",LOCNUM,CGRP)) Q:'CGRP  D
 .S GROUP(LOC,CGRP)=$P($G(^PS(57.8,+CGRP,0)),"^")
 Q
 ;
GETWEEK(SDDT,SDWEEKS) ; Return the week-ending date for date SDDT
 K TMPWK
 S TMPWK=$G(SDWEEKS($P(SDDT,"."))) I 'TMPWK S TMPWK=$P($O(SDWEEKS(SDDT)),".") I TMPWK["."
 S TMPWK=$$FMADD^XLFDT(TMPWK,-1)
 Q TMPWK
 ;
GETQUART(SDDT)  ; Return quarter
 K SDQUART N MONTH,YEAR
 S YEAR=$$FMTE^XLFDT($E(SDDT,1,3)_"0000")
 S MONTH=$E(SDDT,4,5)
 S SDQUART=$S(MONTH<4:"Q1",MONTH<7:"Q2",MONTH<10:"Q3",MONTH<13:"Q4",1:"Q0")
 ;S SDQUART=SDQUART_"-"_YEAR
 S SDQUART=YEAR_"-"_SDQUART
 Q SDQUART
 ;
SUPPLY(SDNODE,DTINC,SDRPTYP,SDDT,SDFAC,SDIV,SDCL,SUPPLY) ; Get Supply
 Q:'SUPPLY
 N SDDATE,SDGRP,TMPSUP,CLNAM
 D SDGRP^SDECXUTL(+SDCL,.SDGRP)
 S SDDATE=$S($G(DTRANGE):DTRANGE,1:SDDT)
 S CLNAM=$S($G(SDCL):$P($G(^SC(+SDCL,0)),"^"),1:$P(SDCL,"^"))
 ;
 S TMPSUP=$G(^XTMP(SDNODE,DTINC,SDRPTYP,$P(SDDATE,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,"APP","SUP"))
 S TMPSUP=TMPSUP+SUPPLY
 S ^XTMP(SDNODE,DTINC,SDRPTYP,$P(SDDATE,"."),SDFAC,SDIV,SDGRP,CLNAM_"^"_SDCL,"APP","SUP")=TMPSUP
 Q