SCDXPRGD ;ALB/JRP - DATE UTILITIES FOR ACRP PURGING;04-SEP-97
;;5.3;Scheduling;**128,1015**;AUG 13, 1993;Build 21
;
FY4DT(DATE) ;Return fiscal year given date falls within
;
;Input : DATE - Date (FileMan) (Defaults to TODAY)
;Output : YYYY - Fiscal year date falls within (ex: 1997)
;
;Check input
S DATE=+$P($G(DATE),".",1)
S:(DATE'?7N) DATE=$$DT^XLFDT()
;Declare variables
N YEAR,MONTH
;Pull year from given date
S YEAR=$E(DATE,1,3)
;Pull month from given date
S MONTH=+$E(DATE,4,5)
;Fiscal year begins in October (add one to year for Oct, Nov, and Dec)
S:(MONTH>9) YEAR=YEAR+1
;Convert year to external format
S YEAR=YEAR+1700
;Done
Q YEAR
;
PREVFY(DATE) ;Return previous fiscal year from given date
;
;Input : DATE - Date (FileMan) (Defaults to TODAY)
;Output : YYYY - Previous fiscal year from given date (ex: 1996)
;
;Check input
S DATE=+$P($G(DATE),".",1)
S:(DATE'?7N) DATE=$$DT^XLFDT()
;Declare variables
N YEAR
;Convert date to same date of previous year
S YEAR=+$E(DATE,1,3)
S YEAR=YEAR-1
S DATE=YEAR_$E(DATE,4,7)
;Return fiscal year for date in last year (done)
Q $$FY4DT(DATE)
;
DR4FY(FISCAL) ;Return date range for a given fiscal year
;
;Input : FISCAL - Year (external) (Default to current year)
;Ouput : Begin ^ End - Beginning and ending dates (FileMan)
;
;Check input
S FISCAL=+$G(FISCAL)
S:(FISCAL'?4N) FISCAL=1700+$E($$DT^XLFDT(),1,3)
;Declare variables
N BEGIN,END
;Fiscal year begins in October of previous year
S BEGIN=((FISCAL-1)-1700)_"1001"
;Fiscal year ends in September of given year
S END=(FISCAL-1700)_"0930"
;Done
Q (BEGIN_"^"_END)
;
LASTDBCO(DATE) ;Return last NPCD database close-out from given date
;
;Input : DATE - Date (FileMan) (Defaults to TODAY)
;Output : Date - Date that NPCD was last closed for database credit
;Notes : If the database close-out date for the input date can not
; be determined, the first day of the fiscal year will be
; returned. The same holds true if the database close-out
; date for a previous month can not be determined.
;
;Check input
S DATE=+$P($G(DATE),".",1)
S:(DATE'?7N) DATE=$$DT^XLFDT()
;Declare variables
N MONTH,YEAR,CLOSEOUT,DBCLOSE,TMP
;Get current database close-out date
S DBCLOSE=+$$CLOSEOUT^SCDXFU04(DATE)
;Error - return first day of fiscal year
I (DBCLOSE<0) S YEAR=$$FY4DT(DATE) Q +$$DR4FY(YEAR)
S CLOSEOUT=DBCLOSE
;Break year & month off of given date
S YEAR=$E(DATE,1,3)
S MONTH=$E(DATE,4,5)
;Go backwards, one month at a time, from given month
; Stop when database close-out date is prior to current close-out date
F S MONTH=MONTH-1 D Q:(CLOSEOUT<DBCLOSE)
.;Account for jump from Jan to Dec
.I ('MONTH) S MONTH=12,YEAR=YEAR-1
.;Create FileMan date out of year/month
.S MONTH="00"_MONTH
.S TMP=$L(MONTH)
.S MONTH=$E(MONTH,(TMP-1),TMP)
.S TMP=YEAR_MONTH_"01"
.;Get database close-out date
.S CLOSEOUT=+$$CLOSEOUT^SCDXFU04(TMP)
;Error - return first day of fiscal year
I (CLOSEOUT<0) S YEAR=$$FY4DT(DATE) Q +$$DR4FY(YEAR)
;Done
Q CLOSEOUT
;
PREVMNTH(DATE) ;Return first day of previous month
;
;Input : DATE - Month/year to return previous month from (FileMan)
; Defaults to TODAY
;Output : Date - First day of previous month (FileMan)
;
;Check input
S DATE=+$P($G(DATE),".",1)
S:(DATE'?7N) DATE=$$DT^XLFDT()
;Declare variables
N MONTH,YEAR,TMP
;Break year & month off of given date
S YEAR=$E(DATE,1,3)
S MONTH=$E(DATE,4,5)
;Decrement month by 1
S MONTH=MONTH-1
;Account for jump from Jan to Dec
I ('MONTH) S MONTH=12,YEAR=YEAR-1
;Re-build FileMan date
S MONTH="00"_MONTH
S TMP=$L(MONTH)
S MONTH=$E(MONTH,(TMP-1),TMP)
S TMP=YEAR_MONTH_"01"
; Done - Return first day of previous month
Q TMP
;
LASTDAY(DATE) ;Return last day of specified month
;
;Input : DATE - Month/year to return last day of (FileMan)
; Defaults to TODAY
;Output : Date - Last day of month (FileMan)
;Notes : This call does not return the number of days in the month
;
;Check input
S DATE=+$P($G(DATE),".",1)
S:(DATE'?7N) DATE=$$DT^XLFDT()
;Declare variables
N MONTH,YEAR,TMP
;Break year & month off of given date
S YEAR=$E(DATE,1,3)
S MONTH=$E(DATE,4,5)
;Last day of month is day before first day of next month
; Increment month by 1
S MONTH=MONTH+1
; Account for jump from Dec to Jan
I (MONTH=13) S MONTH=1,YEAR=YEAR+1
; Build FileMan date denoting first day of next month
S MONTH="00"_MONTH
S TMP=$L(MONTH)
S MONTH=$E(MONTH,(TMP-1),TMP)
S TMP=YEAR_MONTH_"01"
; Done - Return day prior to first day of next month
Q $$FMADD^XLFDT(TMP,-1,0,0,0)
SCDXPRGD ;ALB/JRP - DATE UTILITIES FOR ACRP PURGING;04-SEP-97
+1 ;;5.3;Scheduling;**128,1015**;AUG 13, 1993;Build 21
+2 ;
FY4DT(DATE) ;Return fiscal year given date falls within
+1 ;
+2 ;Input : DATE - Date (FileMan) (Defaults to TODAY)
+3 ;Output : YYYY - Fiscal year date falls within (ex: 1997)
+4 ;
+5 ;Check input
+6 SET DATE=+$PIECE($GET(DATE),".",1)
+7 IF (DATE'?7N)
SET DATE=$$DT^XLFDT()
+8 ;Declare variables
+9 NEW YEAR,MONTH
+10 ;Pull year from given date
+11 SET YEAR=$EXTRACT(DATE,1,3)
+12 ;Pull month from given date
+13 SET MONTH=+$EXTRACT(DATE,4,5)
+14 ;Fiscal year begins in October (add one to year for Oct, Nov, and Dec)
+15 IF (MONTH>9)
SET YEAR=YEAR+1
+16 ;Convert year to external format
+17 SET YEAR=YEAR+1700
+18 ;Done
+19 QUIT YEAR
+20 ;
PREVFY(DATE) ;Return previous fiscal year from given date
+1 ;
+2 ;Input : DATE - Date (FileMan) (Defaults to TODAY)
+3 ;Output : YYYY - Previous fiscal year from given date (ex: 1996)
+4 ;
+5 ;Check input
+6 SET DATE=+$PIECE($GET(DATE),".",1)
+7 IF (DATE'?7N)
SET DATE=$$DT^XLFDT()
+8 ;Declare variables
+9 NEW YEAR
+10 ;Convert date to same date of previous year
+11 SET YEAR=+$EXTRACT(DATE,1,3)
+12 SET YEAR=YEAR-1
+13 SET DATE=YEAR_$EXTRACT(DATE,4,7)
+14 ;Return fiscal year for date in last year (done)
+15 QUIT $$FY4DT(DATE)
+16 ;
DR4FY(FISCAL) ;Return date range for a given fiscal year
+1 ;
+2 ;Input : FISCAL - Year (external) (Default to current year)
+3 ;Ouput : Begin ^ End - Beginning and ending dates (FileMan)
+4 ;
+5 ;Check input
+6 SET FISCAL=+$GET(FISCAL)
+7 IF (FISCAL'?4N)
SET FISCAL=1700+$EXTRACT($$DT^XLFDT(),1,3)
+8 ;Declare variables
+9 NEW BEGIN,END
+10 ;Fiscal year begins in October of previous year
+11 SET BEGIN=((FISCAL-1)-1700)_"1001"
+12 ;Fiscal year ends in September of given year
+13 SET END=(FISCAL-1700)_"0930"
+14 ;Done
+15 QUIT (BEGIN_"^"_END)
+16 ;
LASTDBCO(DATE) ;Return last NPCD database close-out from given date
+1 ;
+2 ;Input : DATE - Date (FileMan) (Defaults to TODAY)
+3 ;Output : Date - Date that NPCD was last closed for database credit
+4 ;Notes : If the database close-out date for the input date can not
+5 ; be determined, the first day of the fiscal year will be
+6 ; returned. The same holds true if the database close-out
+7 ; date for a previous month can not be determined.
+8 ;
+9 ;Check input
+10 SET DATE=+$PIECE($GET(DATE),".",1)
+11 IF (DATE'?7N)
SET DATE=$$DT^XLFDT()
+12 ;Declare variables
+13 NEW MONTH,YEAR,CLOSEOUT,DBCLOSE,TMP
+14 ;Get current database close-out date
+15 SET DBCLOSE=+$$CLOSEOUT^SCDXFU04(DATE)
+16 ;Error - return first day of fiscal year
+17 IF (DBCLOSE<0)
SET YEAR=$$FY4DT(DATE)
QUIT +$$DR4FY(YEAR)
+18 SET CLOSEOUT=DBCLOSE
+19 ;Break year & month off of given date
+20 SET YEAR=$EXTRACT(DATE,1,3)
+21 SET MONTH=$EXTRACT(DATE,4,5)
+22 ;Go backwards, one month at a time, from given month
+23 ; Stop when database close-out date is prior to current close-out date
+24 FOR
SET MONTH=MONTH-1
Begin DoDot:1
+25 ;Account for jump from Jan to Dec
+26 IF ('MONTH)
SET MONTH=12
SET YEAR=YEAR-1
+27 ;Create FileMan date out of year/month
+28 SET MONTH="00"_MONTH
+29 SET TMP=$LENGTH(MONTH)
+30 SET MONTH=$EXTRACT(MONTH,(TMP-1),TMP)
+31 SET TMP=YEAR_MONTH_"01"
+32 ;Get database close-out date
+33 SET CLOSEOUT=+$$CLOSEOUT^SCDXFU04(TMP)
End DoDot:1
IF (CLOSEOUT<DBCLOSE)
QUIT
+34 ;Error - return first day of fiscal year
+35 IF (CLOSEOUT<0)
SET YEAR=$$FY4DT(DATE)
QUIT +$$DR4FY(YEAR)
+36 ;Done
+37 QUIT CLOSEOUT
+38 ;
PREVMNTH(DATE) ;Return first day of previous month
+1 ;
+2 ;Input : DATE - Month/year to return previous month from (FileMan)
+3 ; Defaults to TODAY
+4 ;Output : Date - First day of previous month (FileMan)
+5 ;
+6 ;Check input
+7 SET DATE=+$PIECE($GET(DATE),".",1)
+8 IF (DATE'?7N)
SET DATE=$$DT^XLFDT()
+9 ;Declare variables
+10 NEW MONTH,YEAR,TMP
+11 ;Break year & month off of given date
+12 SET YEAR=$EXTRACT(DATE,1,3)
+13 SET MONTH=$EXTRACT(DATE,4,5)
+14 ;Decrement month by 1
+15 SET MONTH=MONTH-1
+16 ;Account for jump from Jan to Dec
+17 IF ('MONTH)
SET MONTH=12
SET YEAR=YEAR-1
+18 ;Re-build FileMan date
+19 SET MONTH="00"_MONTH
+20 SET TMP=$LENGTH(MONTH)
+21 SET MONTH=$EXTRACT(MONTH,(TMP-1),TMP)
+22 SET TMP=YEAR_MONTH_"01"
+23 ; Done - Return first day of previous month
+24 QUIT TMP
+25 ;
LASTDAY(DATE) ;Return last day of specified month
+1 ;
+2 ;Input : DATE - Month/year to return last day of (FileMan)
+3 ; Defaults to TODAY
+4 ;Output : Date - Last day of month (FileMan)
+5 ;Notes : This call does not return the number of days in the month
+6 ;
+7 ;Check input
+8 SET DATE=+$PIECE($GET(DATE),".",1)
+9 IF (DATE'?7N)
SET DATE=$$DT^XLFDT()
+10 ;Declare variables
+11 NEW MONTH,YEAR,TMP
+12 ;Break year & month off of given date
+13 SET YEAR=$EXTRACT(DATE,1,3)
+14 SET MONTH=$EXTRACT(DATE,4,5)
+15 ;Last day of month is day before first day of next month
+16 ; Increment month by 1
+17 SET MONTH=MONTH+1
+18 ; Account for jump from Dec to Jan
+19 IF (MONTH=13)
SET MONTH=1
SET YEAR=YEAR+1
+20 ; Build FileMan date denoting first day of next month
+21 SET MONTH="00"_MONTH
+22 SET TMP=$LENGTH(MONTH)
+23 SET MONTH=$EXTRACT(MONTH,(TMP-1),TMP)
+24 SET TMP=YEAR_MONTH_"01"
+25 ; Done - Return day prior to first day of next month
+26 QUIT $$FMADD^XLFDT(TMP,-1,0,0,0)