FBAAIAR ;ALB/ESG - FEE IPAC Vendor DoD Invoice (Summary) Report ;1/16/2014
 ;;3.5;FEE BASIS;**123**;JAN 30, 1995;Build 51
 ;;Per VA Directive 6402, this routine should not be modified.
 ;
 Q
 ;
EN ; main report entry point
 ;
 N FBIAVEN,FBIABEG,FBIAEND,FBIATYPE,FBIAEXCEL
P1 I '$$VENDSEL(.FBIAVEN) G EX
P2 I '$$DATES(.FBIABEG,.FBIAEND) G EX:$$STOP,P1
P3 I '$$TYPESEL(.FBIATYPE) G EX:$$STOP,P2
P4 I '$$FORMAT(.FBIAEXCEL) G EX:$$STOP,P3
P5 I '$$DEVICE() G EX:$$STOP,P4
 ;
EX ; main report exit point
 Q
 ;
STOP() ; Determine if user wants to exit out of the option entirely
 ; 1=yes, get out entirely
 ; 0=no, just go back to the previous question
 ;
 N DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT
 ;
 S DIR(0)="Y"
 S DIR("A")="Do you want to exit out of this option entirely"
 S DIR("B")="YES"
 S DIR("?",1)="  Enter YES to immediately exit out of this option."
 S DIR("?")="  Enter NO to return to the previous question."
 W ! D ^DIR K DIR
 I $D(DIRUT) S Y=1
 Q Y
 ;
VENDSEL(FBIAVEN) ; user selection function for IPAC vendors
 ; FBIAVEN is an output array, pass by reference
 ; FBIAVEN(vendor ien) = vendor name selected
 ; Function value is 1 if at least 1 vendor was selected, 0 otherwise
 ;
 N DIC,RET,VAUTSTR,VAUTNI,VAUTVB,V,X,Y
 K FBIAVEN
 S RET=1    ; default to 1 indicating all OK
 ;
 W @IOF,!,"IPAC Vendor DoD Invoice Report"
 W !!,"This report will display summary information on all of the DoD invoices"
 W !,"for the selected IPAC vendors, within the selected date range, and for"
 W !,"the selected payment types."
 W !
 ;
 S DIC="^FBAAV("
 S DIC("S")="I +$O(^FBAA(161.95,""V"",Y,0))"
 S VAUTSTR="IPAC Vendor",VAUTNI=2,VAUTVB="FBIAVEN"
 D FIRST^VAUTOMA     ; DBIA# 4398
 I FBIAVEN S V=0 F  S V=$O(^FBAA(161.95,"V",V)) Q:'V  S FBIAVEN(V)=$P($G(^FBAAV(V,0)),U,1)   ; all IPAC vendors selected
 I '$O(FBIAVEN(0)) S RET=0 W $C(7)        ; no vendors found/selected
 Q RET
 ;
DATES(FBIABEG,FBIAEND) ; capture the start date and end date from the user
 ; both are output parameters, pass by reference
 ; function value is 0/1 indicating if valid dates were selected
 ;
 N RET,DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT
 S RET=1
 S (FBIABEG,FBIAEND)=""
 ;
 S DIR(0)="D^:DT:EX"
 S DIR("A")="Enter the Start Date"
 S DIR("B")=$$FMTE^XLFDT($$FMADD^XLFDT(DT,-30),"5DZ")   ; default date is T-30
 S DIR("?",1)="The start and end dates for this report refer to the date that the"
 S DIR("?",2)="associated batch and payment line items are finalized (certified)"
 S DIR("?")="in VistA Fee through the ""Finalize a Batch"" menu option."
 W ! D ^DIR K DIR
 I $D(DIRUT)!'Y S RET=0 W $C(7) G DATEX
 S FBIABEG=Y
 ;
 S DIR(0)="D^"_FBIABEG_":DT:EX"
 S DIR("A")="Enter the End Date"
 S DIR("B")=$$FMTE^XLFDT(DT,"5DZ")   ; default date is Today
 S DIR("?",1)="The start and end dates for this report refer to the date that the"
 S DIR("?",2)="associated batch and payment line items are finalized (certified)"
 S DIR("?")="in VistA Fee through the ""Finalize a Batch"" menu option."
 W ! D ^DIR K DIR
 I $D(DIRUT)!'Y S RET=0 W $C(7) G DATEX
 S FBIAEND=Y
DATEX ;
 Q RET
 ;
TYPESEL(FBIATYPE) ; function for user selection of the types of invoices to search
 ; FBIATYPE is an output array, pass by reference
 ; FBIATYPE(type)="" where type can be OUT,RX,INP,ANC
 ; Function value is 1 if at least 1 invoice type was selected, 0 otherwise
 ;
 N RET,DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT,FD,G
 K FBIATYPE
 S RET=1    ; default to 1 indicating all OK
 ;
 F  D  Q:Y="ALL"!$D(DIRUT)!(Y="")
 . S DIR(0)="SO"
 . S FD="OUT:"_$$LJ^XLFSTR("Outpatient",27)_$S($D(FBIATYPE("OUT")):"SELECTED",1:"")
 . S FD=FD_";RX:"_$$LJ^XLFSTR("Pharmacy",27)_$S($D(FBIATYPE("RX")):"SELECTED",1:"")
 . S FD=FD_";INP:"_$$LJ^XLFSTR("Civil Hospital",27)_$S($D(FBIATYPE("INP")):"SELECTED",1:"")
 . S FD=FD_";ANC:"_$$LJ^XLFSTR("Civil Hospital Ancillary",27)_$S($D(FBIATYPE("ANC")):"SELECTED",1:"")
 . S FD=FD_";ALL:All"
 . S $P(DIR(0),U,2)=FD
 . ;
 . I '$D(FBIATYPE) S DIR("A")="Select an Invoice Type",DIR("B")="ALL"
 . E  S DIR("A")="Select Another Invoice Type" K DIR("B")
 . W ! D ^DIR K DIR
 . ;
 . I Y="ALL" D  Q     ; user selected all types, so set them and get out
 .. F G="OUT","RX","INP","ANC" S FBIATYPE(G)=""
 . ;
 . I $D(DIRUT)!(Y="") Q
 . I $D(FBIATYPE(Y)) K FBIATYPE(Y) Q     ; if already selected, toggle the selection off then quit
 . S FBIATYPE(Y)=""                      ; toggle selection on
 . Q
 ;
 I $D(DUOUT)!$D(DTOUT) S RET=0           ; exit via up-arrow or time-out should get out
 I '$D(FBIATYPE) S RET=0 W $C(7)
 Q RET
 ;
FORMAT(FBIAEXCEL) ; capture the report format from the user (normal or CSV output)
 ; FBIAEXCEL=0 for normal output
 ; FBIAEXCEL=1 for CSV (comma separated values) for Excel output
 ; pass parameter by reference
 ;
 N RET,DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT
 S FBIAEXCEL=0,RET=1
 S DIR(0)="Y"
 S DIR("A")="Do you want to capture the output in a CSV format"
 S DIR("B")="NO"
 S DIR("?",1)="If you want to capture the output from this report in a comma-separated"
 S DIR("?",2)="values (CSV) format, then answer YES here.  A CSV format is something that"
 S DIR("?",3)="could be easily imported into a spreadsheet program like Excel."
 S DIR("?",4)=" "
 S DIR("?")="If you just want a normal report output, then answer NO here."
 W ! D ^DIR K DIR
 I $D(DIRUT) S RET=0 W $C(7)
 S FBIAEXCEL=Y
 Q RET
 ;
DEVICE() ; Device Selection
 N ZTRTN,ZTDESC,ZTSAVE,POP,RET,ZTSK,DIR,X,Y
 S RET=1
 I 'FBIAEXCEL W !!,"This report is 132 characters wide.  Please choose an appropriate device.",!
 I FBIAEXCEL D
 . W !!,"For CSV output, turn logging or capture on now."
 . W !,"To avoid undesired wrapping of the data saved to the file,"
 . W !,"please enter ""0;256;99999"" at the ""DEVICE:"" prompt.",!
 ;
 S ZTRTN="COMPILE^FBAAIAR"
 S ZTDESC="Fee Basis IPAC Vendor DoD Invoice Report"
 S ZTSAVE("FBIAVEN(")=""
 S ZTSAVE("FBIABEG")=""
 S ZTSAVE("FBIAEND")=""
 S ZTSAVE("FBIATYPE(")=""
 S ZTSAVE("FBIAEXCEL")=""
 D EN^XUTMDEVQ(ZTRTN,ZTDESC,.ZTSAVE,"QM",1)
 I POP S RET=0
 I $G(ZTSK) W !!,"Report compilation has started with task# ",ZTSK,".",! S DIR(0)="E" D ^DIR
 Q RET
 ;
COMPILE ; entry point for the compile to build the scratch global
 ; may be background task if job queued
 ;
 K ^TMP("FBAAIAR",$J)
 I '$D(ZTQUEUED) W !!,"Compiling IPAC Vendor DoD Invoice Report.  Please wait ... "
 I $D(FBIATYPE("OUT"))!$D(FBIATYPE("ANC")) D COMPOUT
 I $D(FBIATYPE("INP")) D COMPIN
 I $D(FBIATYPE("RX")) D COMPRX^FBAAIARA
 ;
 D PRINT^FBAAIARA                 ; print report
 D ^%ZISC                         ; close the device
 K ^TMP("FBAAIAR",$J)             ; kill scratch
 I $D(ZTQUEUED) S ZTREQ="@"       ; purge the task
COMPILX ;
 Q
 ;
COMPOUT ; compile Outpatient and Ancillary data
 ;
 N DATA,FBDODINV,FBDT,FBJ,FBK,FBL,FBM,FBVENAME,FBVENID,FBY0,FBY2,FBY3,FEEPROG,FBYREJ
 S FBDT=$O(^FBAAC("AK",FBIABEG),-1)
 F  S FBDT=$O(^FBAAC("AK",FBDT)) Q:'FBDT!(FBDT>FBIAEND)  D
 . S FBJ=0 F  S FBJ=$O(^FBAAC("AK",FBDT,FBJ)) Q:'FBJ  D
 .. S FBK=0 F  S FBK=$O(^FBAAC("AK",FBDT,FBJ,FBK)) Q:'FBK  D       ; FBK=vendor ien
 ... I '$D(FBIAVEN(FBK)) Q                  ; make sure vendor is among the selected vendors for the report
 ... S FBVENAME=$P($G(^FBAAV(FBK,0)),U,1)   ; vendor name
 ... S FBVENID=$P($G(^FBAAV(FBK,0)),U,2)    ; vendor external ID
 ... S FBL=0 F  S FBL=$O(^FBAAC("AK",FBDT,FBJ,FBK,FBL)) Q:'FBL  D
 .... S FBM=0 F  S FBM=$O(^FBAAC("AK",FBDT,FBJ,FBK,FBL,FBM)) Q:'FBM  D
 ..... S FBY0=$G(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,0))
 ..... S FEEPROG=+$P(FBY0,U,9)   ; Fee Program ptr
 ..... I FEEPROG=2,'$D(FBIATYPE("OUT")) Q          ; Outpatient not a chosen type for report
 ..... I FEEPROG'=2,'$D(FBIATYPE("ANC")) Q         ; Civil Hosp Ancillary not a chosen type for report
 ..... S FBY2=$G(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,2))
 ..... S FBY3=$G(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,3))
 ..... S FBYREJ=$G(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,"FBREJ"))
 ..... S FBDODINV=$P(FBY3,U,7) I FBDODINV="" Q        ; DoD invoice# must be present
 ..... I $D(^TMP("FBAAIAR",$J,FBVENAME,FBDODINV)) Q   ; DoD invoice# data already exists
 ..... I $P(FBY2,U,4) Q                               ; cancellation date exists
 ..... I $P(FBY0,U,21)'="" Q                          ; line has been voided
 ..... I $P(FBYREJ,U,1)'="" Q                         ; line has been rejected
 ..... ;
 ..... S DATA=FBK_U_FBVENID_U_FBDT
 ..... S $P(DATA,U,7)=$P(FBY0,U,16)                               ; fee invoice number
 ..... S $P(DATA,U,9)=$P($G(^FBAA(161.7,+$P(FBY0,U,8),0)),U,1)    ; external batch#
 ..... S $P(DATA,U,11)=$P($G(^FBAA(161.7,+$P(FBY0,U,8),0)),U,2)   ; obligation# from the batch file
 ..... S $P(DATA,U,13)=$P(FBY0,U,14)                              ; Date Paid
 ..... S $P(DATA,U,16)=$P(FBY2,U,3)                               ; check number
 ..... S ^TMP("FBAAIAR",$J,FBVENAME,FBDODINV)=DATA                ; store new data for this DoD invoice#
 ..... D GET(FBVENAME,FBDODINV)                                   ; gather totals for DoD invoice#
 ..... Q
 .... Q
 ... Q
 .. Q
 . Q
COMPOUTX ;
 Q
 ;
COMPIN ; compile Inpatient data
 ;
 N DATA,FBDODINV,FBDT,FBJ,FBV,FBVENAME,FBVENID,FBY0,FBY2,FBY5,FBYREJ
 S FBDT=$O(^FBAAI("AD",FBIABEG),-1)
 F  S FBDT=$O(^FBAAI("AD",FBDT)) Q:'FBDT!(FBDT>FBIAEND)  S FBJ=0 F  S FBJ=$O(^FBAAI("AD",FBDT,FBJ)) Q:'FBJ  D
 . S FBY0=$G(^FBAAI(FBJ,0))
 . S FBY2=$G(^FBAAI(FBJ,2))
 . S FBY5=$G(^FBAAI(FBJ,5))
 . S FBYREJ=$G(^FBAAI(FBJ,"FBREJ"))
 . S FBDODINV=$P(FBY5,U,7) I FBDODINV="" Q        ; DoD invoice# must be present
 . S FBV=+$P(FBY0,U,3)      ; vendor ien
 . I '$D(FBIAVEN(FBV)) Q    ; vendor is not among the selected vendors for the report
 . S FBVENAME=$P($G(^FBAAV(FBV,0)),U,1)           ; vendor name
 . S FBVENID=$P($G(^FBAAV(FBV,0)),U,2)            ; vendor external ID
 . I $D(^TMP("FBAAIAR",$J,FBVENAME,FBDODINV)) Q   ; DoD invoice# data already exists
 . I $P(FBY2,U,5) Q                               ; cancelled
 . I $P(FBY0,U,14)'="" Q                          ; voided
 . I $P(FBYREJ,U,1)'="" Q                         ; rejected
 . ;
 . S DATA=FBV_U_FBVENID_U_FBDT
 . S $P(DATA,U,7)=$P(FBY0,U,1)                                 ; fee invoice number
 . S $P(DATA,U,9)=$P($G(^FBAA(161.7,+$P(FBY0,U,17),0)),U,1)    ; external batch#
 . S $P(DATA,U,11)=$P($G(^FBAA(161.7,+$P(FBY0,U,17),0)),U,2)   ; obligation# from the batch file
 . S $P(DATA,U,13)=$P(FBY2,U,1)                                ; Date Paid
 . S $P(DATA,U,16)=$P(FBY2,U,4)                                ; check number
 . S ^TMP("FBAAIAR",$J,FBVENAME,FBDODINV)=DATA                 ; store new data for this DoD invoice#
 . D GET(FBVENAME,FBDODINV)                                    ; gather totals for DoD invoice#
 . Q
COMPINX ;
 Q
 ;
GET(FBVENAME,FBDODINV) ; gather totals and other data for all Fee line items for the given vendor and DoD invoice#
 ; update the established scratch global with information
 ;
 N ADJTOT,CLAIMED,DISBURSED,FBDISGD,FBG0,FBG2,FBGREJ,K,L,M,N,P,PAID,FBTT
 ;
 I $G(FBVENAME)="" G GETX
 I $G(FBDODINV)="" G GETX
 ;
 S (CLAIMED,PAID,ADJTOT,DISBURSED)=0                   ; initialize dollar totals to 0
 S FBDISGD=$G(^TMP("FBAAIAR",$J,FBVENAME,FBDODINV))    ; current contents of scratch global data
 S FBTT=$G(^TMP("FBAAIAR",$J,FBVENAME))                ; current vendor totals
 ;
 ; gather outpatient/ancillary totals for this DoD invoice#
 S K=0 F  S K=$O(^FBAAC("DODI",FBDODINV,K)) Q:'K  S L=0 F  S L=$O(^FBAAC("DODI",FBDODINV,K,L)) Q:'L  S M=0 F  S M=$O(^FBAAC("DODI",FBDODINV,K,L,M)) Q:'M  S N=0 F  S N=$O(^FBAAC("DODI",FBDODINV,K,L,M,N)) Q:'N  D
 . S FBG0=$G(^FBAAC(K,1,L,1,M,1,N,0))
 . S FBG2=$G(^FBAAC(K,1,L,1,M,1,N,2))
 . S FBGREJ=$G(^FBAAC(K,1,L,1,M,1,N,"FBREJ"))
 . I $P(FBG2,U,4) Q           ; cancelled
 . I $P(FBG0,U,21)'="" Q      ; voided
 . I $P(FBGREJ,U,1)'="" Q     ; rejected
 . S CLAIMED=CLAIMED+$P(FBG0,U,2)
 . S PAID=PAID+$P(FBG0,U,3)
 . S DISBURSED=DISBURSED+$P(FBG2,U,8)
 . S P=0 F  S P=$O(^FBAAC(K,1,L,1,M,1,N,7,P)) Q:'P  S ADJTOT=ADJTOT+$P($G(^FBAAC(K,1,L,1,M,1,N,7,P,0)),U,3)
 . ;
 . ; check for certain fields that may have multiple values or missing values for the same DoD invoice number
 . ; across all VistA line items for all payment types.
 . D CKMLT($P(FBG0,U,16),7,8)                               ; fee invoice number
 . D CKMLT($P($G(^FBAA(161.7,+$P(FBG0,U,8),0)),U,1),9,10)   ; external batch#
 . D CKMLT($P($G(^FBAA(161.7,+$P(FBG0,U,8),0)),U,2),11,12)  ; obligation# (taken from the batch)
 . D CKMLT($P(FBG0,U,14),13,14,15)                          ; date paid (also check for missing values)
 . D CKMLT($P(FBG2,U,3),16,17,18)                           ; check number (also check for missing values)
 . Q
 ;
 ; gather inpatient totals for this DoD invoice#
 S K=0 F  S K=$O(^FBAAI("DODI",FBDODINV,K)) Q:'K  D
 . S FBG0=$G(^FBAAI(K,0))
 . S FBG2=$G(^FBAAI(K,2))
 . S FBGREJ=$G(^FBAAI(K,"FBREJ"))
 . I $P(FBG2,U,5) Q            ; cancelled
 . I $P(FBG0,U,14)'="" Q       ; voided
 . I $P(FBGREJ,U,1)'="" Q      ; rejected
 . S CLAIMED=CLAIMED+$P(FBG0,U,8)
 . S PAID=PAID+$P(FBG0,U,9)
 . S DISBURSED=DISBURSED+$P(FBG2,U,8)
 . S P=0 F  S P=$O(^FBAAI(K,8,P)) Q:'P  S ADJTOT=ADJTOT+$P($G(^FBAAI(K,8,P,0)),U,3)
 . ;
 . ; check for certain fields that may have multiple values or missing values for the same DoD invoice number
 . ; across all VistA line items for all payment types.
 . D CKMLT($P(FBG0,U,1),7,8)                                ; fee invoice number
 . D CKMLT($P($G(^FBAA(161.7,+$P(FBG0,U,17),0)),U,1),9,10)  ; external batch#
 . D CKMLT($P($G(^FBAA(161.7,+$P(FBG0,U,17),0)),U,2),11,12) ; obligation# (taken from the batch)
 . D CKMLT($P(FBG2,U,1),13,14,15)                           ; date paid (also check for missing values)
 . D CKMLT($P(FBG2,U,4),16,17,18)                           ; check number (also check for missing values)
 . Q
 ;
 ; gather pharmacy totals for this DoD invoice#
 S K=0 F  S K=$O(^FBAA(162.1,"DODI",FBDODINV,K)) Q:'K  S L=0 F  S L=$O(^FBAA(162.1,"DODI",FBDODINV,K,L)) Q:'L  D
 . S FBG0=$G(^FBAA(162.1,K,"RX",L,0))
 . S FBG2=$G(^FBAA(162.1,K,"RX",L,2))
 . S FBGREJ=$G(^FBAA(162.1,K,"RX",L,"FBREJ"))
 . I $P(FBG2,U,11) Q          ; cancelled
 . I $P(FBG2,U,3)'="" Q       ; voided
 . I $P(FBGREJ,U,1)'="" Q     ; rejected
 . S CLAIMED=CLAIMED+$P(FBG0,U,4)
 . S PAID=PAID+$P(FBG0,U,16)
 . S DISBURSED=DISBURSED+$P(FBG2,U,14)
 . S P=0 F  S P=$O(^FBAA(162.1,K,"RX",L,4,P)) Q:'P  S ADJTOT=ADJTOT+$P($G(^FBAA(162.1,K,"RX",L,4,P,0)),U,3)
 . ;
 . ; check for certain fields that may have multiple values or missing values for the same DoD invoice number
 . ; across all VistA line items for all payment types.
 . D CKMLT(K,7,8)                                           ; fee invoice number (K is DINUM'd with the .01 field)
 . D CKMLT($P($G(^FBAA(161.7,+$P(FBG0,U,17),0)),U,1),9,10)  ; external batch#
 . D CKMLT($P($G(^FBAA(161.7,+$P(FBG0,U,17),0)),U,2),11,12) ; obligation# (taken from the batch)
 . D CKMLT($P(FBG2,U,8),13,14,15)                           ; date paid (also check for missing values)
 . D CKMLT($P(FBG2,U,10),16,17,18)                          ; check number (also check for missing values)
 . Q
 ;
 ; update scratch global
 S $P(FBDISGD,U,4)=CLAIMED      ; total amount claimed
 S $P(FBDISGD,U,5)=PAID         ; total amount paid
 S $P(FBDISGD,U,6)=ADJTOT       ; total adjustment amount
 S $P(FBDISGD,U,19)=DISBURSED   ; total disbursed amount
 S ^TMP("FBAAIAR",$J,FBVENAME,FBDODINV)=FBDISGD
 ;
 ; also update vendor totals
 S $P(FBTT,U,1)=$P(FBTT,U,1)+1          ; count
 S $P(FBTT,U,4)=$P(FBTT,U,4)+CLAIMED
 S $P(FBTT,U,5)=$P(FBTT,U,5)+PAID
 S $P(FBTT,U,6)=$P(FBTT,U,6)+ADJTOT
 S $P(FBTT,U,19)=$P(FBTT,U,19)+DISBURSED
 S ^TMP("FBAAIAR",$J,FBVENAME)=FBTT
 ;
GETX ;
 Q
 ;
CKMLT(VAL,VALPCE,MLTPCE,NOVPCE) ; check for multiple/missing data values
 ;    Variable FBDISGD is assumed to contain the contents of the scratch global
 ;    VAL - value from the payment file to check
 ; VALPCE - piece# from the scratch global to check for multiple values
 ; MLTPCE - piece# of the scratch global to set if multiple values found
 ; NOVPCE - piece# of the scratch global to set if no data exists in VAL (optional)
 ;
 I VAL'="",$P(FBDISGD,U,VALPCE)'="",VAL'=$P(FBDISGD,U,VALPCE) S $P(FBDISGD,U,MLTPCE)=1
 I $G(NOVPCE),VAL="" S $P(FBDISGD,U,NOVPCE)=1
 Q
 ;
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HFBAAIAR   16461     printed  Sep 23, 2025@19:31:39                                                                                                                                                                                                    Page 2
FBAAIAR   ;ALB/ESG - FEE IPAC Vendor DoD Invoice (Summary) Report ;1/16/2014
 +1       ;;3.5;FEE BASIS;**123**;JAN 30, 1995;Build 51
 +2       ;;Per VA Directive 6402, this routine should not be modified.
 +3       ;
 +4        QUIT 
 +5       ;
EN        ; main report entry point
 +1       ;
 +2        NEW FBIAVEN,FBIABEG,FBIAEND,FBIATYPE,FBIAEXCEL
P1         IF '$$VENDSEL(.FBIAVEN)
               GOTO EX
P2         IF '$$DATES(.FBIABEG,.FBIAEND)
               if $$STOP
                   GOTO EX
               GOTO P1
P3         IF '$$TYPESEL(.FBIATYPE)
               if $$STOP
                   GOTO EX
               GOTO P2
P4         IF '$$FORMAT(.FBIAEXCEL)
               if $$STOP
                   GOTO EX
               GOTO P3
P5         IF '$$DEVICE()
               if $$STOP
                   GOTO EX
               GOTO P4
 +1       ;
EX        ; main report exit point
 +1        QUIT 
 +2       ;
STOP()    ; Determine if user wants to exit out of the option entirely
 +1       ; 1=yes, get out entirely
 +2       ; 0=no, just go back to the previous question
 +3       ;
 +4        NEW DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT
 +5       ;
 +6        SET DIR(0)="Y"
 +7        SET DIR("A")="Do you want to exit out of this option entirely"
 +8        SET DIR("B")="YES"
 +9        SET DIR("?",1)="  Enter YES to immediately exit out of this option."
 +10       SET DIR("?")="  Enter NO to return to the previous question."
 +11       WRITE !
           DO ^DIR
           KILL DIR
 +12       IF $DATA(DIRUT)
               SET Y=1
 +13       QUIT Y
 +14      ;
VENDSEL(FBIAVEN) ; user selection function for IPAC vendors
 +1       ; FBIAVEN is an output array, pass by reference
 +2       ; FBIAVEN(vendor ien) = vendor name selected
 +3       ; Function value is 1 if at least 1 vendor was selected, 0 otherwise
 +4       ;
 +5        NEW DIC,RET,VAUTSTR,VAUTNI,VAUTVB,V,X,Y
 +6        KILL FBIAVEN
 +7       ; default to 1 indicating all OK
           SET RET=1
 +8       ;
 +9        WRITE @IOF,!,"IPAC Vendor DoD Invoice Report"
 +10       WRITE !!,"This report will display summary information on all of the DoD invoices"
 +11       WRITE !,"for the selected IPAC vendors, within the selected date range, and for"
 +12       WRITE !,"the selected payment types."
 +13       WRITE !
 +14      ;
 +15       SET DIC="^FBAAV("
 +16       SET DIC("S")="I +$O(^FBAA(161.95,""V"",Y,0))"
 +17       SET VAUTSTR="IPAC Vendor"
           SET VAUTNI=2
           SET VAUTVB="FBIAVEN"
 +18      ; DBIA# 4398
           DO FIRST^VAUTOMA
 +19      ; all IPAC vendors selected
           IF FBIAVEN
               SET V=0
               FOR 
                   SET V=$ORDER(^FBAA(161.95,"V",V))
                   if 'V
                       QUIT 
                   SET FBIAVEN(V)=$PIECE($GET(^FBAAV(V,0)),U,1)
 +20      ; no vendors found/selected
           IF '$ORDER(FBIAVEN(0))
               SET RET=0
               WRITE $CHAR(7)
 +21       QUIT RET
 +22      ;
DATES(FBIABEG,FBIAEND) ; capture the start date and end date from the user
 +1       ; both are output parameters, pass by reference
 +2       ; function value is 0/1 indicating if valid dates were selected
 +3       ;
 +4        NEW RET,DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT
 +5        SET RET=1
 +6        SET (FBIABEG,FBIAEND)=""
 +7       ;
 +8        SET DIR(0)="D^:DT:EX"
 +9        SET DIR("A")="Enter the Start Date"
 +10      ; default date is T-30
           SET DIR("B")=$$FMTE^XLFDT($$FMADD^XLFDT(DT,-30),"5DZ")
 +11       SET DIR("?",1)="The start and end dates for this report refer to the date that the"
 +12       SET DIR("?",2)="associated batch and payment line items are finalized (certified)"
 +13       SET DIR("?")="in VistA Fee through the ""Finalize a Batch"" menu option."
 +14       WRITE !
           DO ^DIR
           KILL DIR
 +15       IF $DATA(DIRUT)!'Y
               SET RET=0
               WRITE $CHAR(7)
               GOTO DATEX
 +16       SET FBIABEG=Y
 +17      ;
 +18       SET DIR(0)="D^"_FBIABEG_":DT:EX"
 +19       SET DIR("A")="Enter the End Date"
 +20      ; default date is Today
           SET DIR("B")=$$FMTE^XLFDT(DT,"5DZ")
 +21       SET DIR("?",1)="The start and end dates for this report refer to the date that the"
 +22       SET DIR("?",2)="associated batch and payment line items are finalized (certified)"
 +23       SET DIR("?")="in VistA Fee through the ""Finalize a Batch"" menu option."
 +24       WRITE !
           DO ^DIR
           KILL DIR
 +25       IF $DATA(DIRUT)!'Y
               SET RET=0
               WRITE $CHAR(7)
               GOTO DATEX
 +26       SET FBIAEND=Y
DATEX     ;
 +1        QUIT RET
 +2       ;
TYPESEL(FBIATYPE) ; function for user selection of the types of invoices to search
 +1       ; FBIATYPE is an output array, pass by reference
 +2       ; FBIATYPE(type)="" where type can be OUT,RX,INP,ANC
 +3       ; Function value is 1 if at least 1 invoice type was selected, 0 otherwise
 +4       ;
 +5        NEW RET,DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT,FD,G
 +6        KILL FBIATYPE
 +7       ; default to 1 indicating all OK
           SET RET=1
 +8       ;
 +9        FOR 
               Begin DoDot:1
 +10               SET DIR(0)="SO"
 +11               SET FD="OUT:"_$$LJ^XLFSTR("Outpatient",27)_$SELECT($DATA(FBIATYPE("OUT")):"SELECTED",1:"")
 +12               SET FD=FD_";RX:"_$$LJ^XLFSTR("Pharmacy",27)_$SELECT($DATA(FBIATYPE("RX")):"SELECTED",1:"")
 +13               SET FD=FD_";INP:"_$$LJ^XLFSTR("Civil Hospital",27)_$SELECT($DATA(FBIATYPE("INP")):"SELECTED",1:"")
 +14               SET FD=FD_";ANC:"_$$LJ^XLFSTR("Civil Hospital Ancillary",27)_$SELECT($DATA(FBIATYPE("ANC")):"SELECTED",1:"")
 +15               SET FD=FD_";ALL:All"
 +16               SET $PIECE(DIR(0),U,2)=FD
 +17      ;
 +18               IF '$DATA(FBIATYPE)
                       SET DIR("A")="Select an Invoice Type"
                       SET DIR("B")="ALL"
 +19              IF '$TEST
                       SET DIR("A")="Select Another Invoice Type"
                       KILL DIR("B")
 +20               WRITE !
                   DO ^DIR
                   KILL DIR
 +21      ;
 +22      ; user selected all types, so set them and get out
                   IF Y="ALL"
                       Begin DoDot:2
 +23                       FOR G="OUT","RX","INP","ANC"
                               SET FBIATYPE(G)=""
                       End DoDot:2
                       QUIT 
 +24      ;
 +25               IF $DATA(DIRUT)!(Y="")
                       QUIT 
 +26      ; if already selected, toggle the selection off then quit
                   IF $DATA(FBIATYPE(Y))
                       KILL FBIATYPE(Y)
                       QUIT 
 +27      ; toggle selection on
                   SET FBIATYPE(Y)=""
 +28               QUIT 
               End DoDot:1
               if Y="ALL"!$DATA(DIRUT)!(Y="")
                   QUIT 
 +29      ;
 +30      ; exit via up-arrow or time-out should get out
           IF $DATA(DUOUT)!$DATA(DTOUT)
               SET RET=0
 +31       IF '$DATA(FBIATYPE)
               SET RET=0
               WRITE $CHAR(7)
 +32       QUIT RET
 +33      ;
FORMAT(FBIAEXCEL) ; capture the report format from the user (normal or CSV output)
 +1       ; FBIAEXCEL=0 for normal output
 +2       ; FBIAEXCEL=1 for CSV (comma separated values) for Excel output
 +3       ; pass parameter by reference
 +4       ;
 +5        NEW RET,DIR,X,Y,DTOUT,DUOUT,DIRUT,DIROUT
 +6        SET FBIAEXCEL=0
           SET RET=1
 +7        SET DIR(0)="Y"
 +8        SET DIR("A")="Do you want to capture the output in a CSV format"
 +9        SET DIR("B")="NO"
 +10       SET DIR("?",1)="If you want to capture the output from this report in a comma-separated"
 +11       SET DIR("?",2)="values (CSV) format, then answer YES here.  A CSV format is something that"
 +12       SET DIR("?",3)="could be easily imported into a spreadsheet program like Excel."
 +13       SET DIR("?",4)=" "
 +14       SET DIR("?")="If you just want a normal report output, then answer NO here."
 +15       WRITE !
           DO ^DIR
           KILL DIR
 +16       IF $DATA(DIRUT)
               SET RET=0
               WRITE $CHAR(7)
 +17       SET FBIAEXCEL=Y
 +18       QUIT RET
 +19      ;
DEVICE()  ; Device Selection
 +1        NEW ZTRTN,ZTDESC,ZTSAVE,POP,RET,ZTSK,DIR,X,Y
 +2        SET RET=1
 +3        IF 'FBIAEXCEL
               WRITE !!,"This report is 132 characters wide.  Please choose an appropriate device.",!
 +4        IF FBIAEXCEL
               Begin DoDot:1
 +5                WRITE !!,"For CSV output, turn logging or capture on now."
 +6                WRITE !,"To avoid undesired wrapping of the data saved to the file,"
 +7                WRITE !,"please enter ""0;256;99999"" at the ""DEVICE:"" prompt.",!
               End DoDot:1
 +8       ;
 +9        SET ZTRTN="COMPILE^FBAAIAR"
 +10       SET ZTDESC="Fee Basis IPAC Vendor DoD Invoice Report"
 +11       SET ZTSAVE("FBIAVEN(")=""
 +12       SET ZTSAVE("FBIABEG")=""
 +13       SET ZTSAVE("FBIAEND")=""
 +14       SET ZTSAVE("FBIATYPE(")=""
 +15       SET ZTSAVE("FBIAEXCEL")=""
 +16       DO EN^XUTMDEVQ(ZTRTN,ZTDESC,.ZTSAVE,"QM",1)
 +17       IF POP
               SET RET=0
 +18       IF $GET(ZTSK)
               WRITE !!,"Report compilation has started with task# ",ZTSK,".",!
               SET DIR(0)="E"
               DO ^DIR
 +19       QUIT RET
 +20      ;
COMPILE   ; entry point for the compile to build the scratch global
 +1       ; may be background task if job queued
 +2       ;
 +3        KILL ^TMP("FBAAIAR",$JOB)
 +4        IF '$DATA(ZTQUEUED)
               WRITE !!,"Compiling IPAC Vendor DoD Invoice Report.  Please wait ... "
 +5        IF $DATA(FBIATYPE("OUT"))!$DATA(FBIATYPE("ANC"))
               DO COMPOUT
 +6        IF $DATA(FBIATYPE("INP"))
               DO COMPIN
 +7        IF $DATA(FBIATYPE("RX"))
               DO COMPRX^FBAAIARA
 +8       ;
 +9       ; print report
           DO PRINT^FBAAIARA
 +10      ; close the device
           DO ^%ZISC
 +11      ; kill scratch
           KILL ^TMP("FBAAIAR",$JOB)
 +12      ; purge the task
           IF $DATA(ZTQUEUED)
               SET ZTREQ="@"
COMPILX   ;
 +1        QUIT 
 +2       ;
COMPOUT   ; compile Outpatient and Ancillary data
 +1       ;
 +2        NEW DATA,FBDODINV,FBDT,FBJ,FBK,FBL,FBM,FBVENAME,FBVENID,FBY0,FBY2,FBY3,FEEPROG,FBYREJ
 +3        SET FBDT=$ORDER(^FBAAC("AK",FBIABEG),-1)
 +4        FOR 
               SET FBDT=$ORDER(^FBAAC("AK",FBDT))
               if 'FBDT!(FBDT>FBIAEND)
                   QUIT 
               Begin DoDot:1
 +5                SET FBJ=0
                   FOR 
                       SET FBJ=$ORDER(^FBAAC("AK",FBDT,FBJ))
                       if 'FBJ
                           QUIT 
                       Begin DoDot:2
 +6       ; FBK=vendor ien
                           SET FBK=0
                           FOR 
                               SET FBK=$ORDER(^FBAAC("AK",FBDT,FBJ,FBK))
                               if 'FBK
                                   QUIT 
                               Begin DoDot:3
 +7       ; make sure vendor is among the selected vendors for the report
                                   IF '$DATA(FBIAVEN(FBK))
                                       QUIT 
 +8       ; vendor name
                                   SET FBVENAME=$PIECE($GET(^FBAAV(FBK,0)),U,1)
 +9       ; vendor external ID
                                   SET FBVENID=$PIECE($GET(^FBAAV(FBK,0)),U,2)
 +10                               SET FBL=0
                                   FOR 
                                       SET FBL=$ORDER(^FBAAC("AK",FBDT,FBJ,FBK,FBL))
                                       if 'FBL
                                           QUIT 
                                       Begin DoDot:4
 +11                                       SET FBM=0
                                           FOR 
                                               SET FBM=$ORDER(^FBAAC("AK",FBDT,FBJ,FBK,FBL,FBM))
                                               if 'FBM
                                                   QUIT 
                                               Begin DoDot:5
 +12                                               SET FBY0=$GET(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,0))
 +13      ; Fee Program ptr
                                                   SET FEEPROG=+$PIECE(FBY0,U,9)
 +14      ; Outpatient not a chosen type for report
                                                   IF FEEPROG=2
                                                       IF '$DATA(FBIATYPE("OUT"))
                                                           QUIT 
 +15      ; Civil Hosp Ancillary not a chosen type for report
                                                   IF FEEPROG'=2
                                                       IF '$DATA(FBIATYPE("ANC"))
                                                           QUIT 
 +16                                               SET FBY2=$GET(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,2))
 +17                                               SET FBY3=$GET(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,3))
 +18                                               SET FBYREJ=$GET(^FBAAC(FBJ,1,FBK,1,FBL,1,FBM,"FBREJ"))
 +19      ; DoD invoice# must be present
                                                   SET FBDODINV=$PIECE(FBY3,U,7)
                                                   IF FBDODINV=""
                                                       QUIT 
 +20      ; DoD invoice# data already exists
                                                   IF $DATA(^TMP("FBAAIAR",$JOB,FBVENAME,FBDODINV))
                                                       QUIT 
 +21      ; cancellation date exists
                                                   IF $PIECE(FBY2,U,4)
                                                       QUIT 
 +22      ; line has been voided
                                                   IF $PIECE(FBY0,U,21)'=""
                                                       QUIT 
 +23      ; line has been rejected
                                                   IF $PIECE(FBYREJ,U,1)'=""
                                                       QUIT 
 +24      ;
 +25                                               SET DATA=FBK_U_FBVENID_U_FBDT
 +26      ; fee invoice number
                                                   SET $PIECE(DATA,U,7)=$PIECE(FBY0,U,16)
 +27      ; external batch#
                                                   SET $PIECE(DATA,U,9)=$PIECE($GET(^FBAA(161.7,+$PIECE(FBY0,U,8),0)),U,1)
 +28      ; obligation# from the batch file
                                                   SET $PIECE(DATA,U,11)=$PIECE($GET(^FBAA(161.7,+$PIECE(FBY0,U,8),0)),U,2)
 +29      ; Date Paid
                                                   SET $PIECE(DATA,U,13)=$PIECE(FBY0,U,14)
 +30      ; check number
                                                   SET $PIECE(DATA,U,16)=$PIECE(FBY2,U,3)
 +31      ; store new data for this DoD invoice#
                                                   SET ^TMP("FBAAIAR",$JOB,FBVENAME,FBDODINV)=DATA
 +32      ; gather totals for DoD invoice#
                                                   DO GET(FBVENAME,FBDODINV)
 +33                                               QUIT 
                                               End DoDot:5
 +34                                       QUIT 
                                       End DoDot:4
 +35                               QUIT 
                               End DoDot:3
 +36                       QUIT 
                       End DoDot:2
 +37               QUIT 
               End DoDot:1
COMPOUTX  ;
 +1        QUIT 
 +2       ;
COMPIN    ; compile Inpatient data
 +1       ;
 +2        NEW DATA,FBDODINV,FBDT,FBJ,FBV,FBVENAME,FBVENID,FBY0,FBY2,FBY5,FBYREJ
 +3        SET FBDT=$ORDER(^FBAAI("AD",FBIABEG),-1)
 +4        FOR 
               SET FBDT=$ORDER(^FBAAI("AD",FBDT))
               if 'FBDT!(FBDT>FBIAEND)
                   QUIT 
               SET FBJ=0
               FOR 
                   SET FBJ=$ORDER(^FBAAI("AD",FBDT,FBJ))
                   if 'FBJ
                       QUIT 
                   Begin DoDot:1
 +5                    SET FBY0=$GET(^FBAAI(FBJ,0))
 +6                    SET FBY2=$GET(^FBAAI(FBJ,2))
 +7                    SET FBY5=$GET(^FBAAI(FBJ,5))
 +8                    SET FBYREJ=$GET(^FBAAI(FBJ,"FBREJ"))
 +9       ; DoD invoice# must be present
                       SET FBDODINV=$PIECE(FBY5,U,7)
                       IF FBDODINV=""
                           QUIT 
 +10      ; vendor ien
                       SET FBV=+$PIECE(FBY0,U,3)
 +11      ; vendor is not among the selected vendors for the report
                       IF '$DATA(FBIAVEN(FBV))
                           QUIT 
 +12      ; vendor name
                       SET FBVENAME=$PIECE($GET(^FBAAV(FBV,0)),U,1)
 +13      ; vendor external ID
                       SET FBVENID=$PIECE($GET(^FBAAV(FBV,0)),U,2)
 +14      ; DoD invoice# data already exists
                       IF $DATA(^TMP("FBAAIAR",$JOB,FBVENAME,FBDODINV))
                           QUIT 
 +15      ; cancelled
                       IF $PIECE(FBY2,U,5)
                           QUIT 
 +16      ; voided
                       IF $PIECE(FBY0,U,14)'=""
                           QUIT 
 +17      ; rejected
                       IF $PIECE(FBYREJ,U,1)'=""
                           QUIT 
 +18      ;
 +19                   SET DATA=FBV_U_FBVENID_U_FBDT
 +20      ; fee invoice number
                       SET $PIECE(DATA,U,7)=$PIECE(FBY0,U,1)
 +21      ; external batch#
                       SET $PIECE(DATA,U,9)=$PIECE($GET(^FBAA(161.7,+$PIECE(FBY0,U,17),0)),U,1)
 +22      ; obligation# from the batch file
                       SET $PIECE(DATA,U,11)=$PIECE($GET(^FBAA(161.7,+$PIECE(FBY0,U,17),0)),U,2)
 +23      ; Date Paid
                       SET $PIECE(DATA,U,13)=$PIECE(FBY2,U,1)
 +24      ; check number
                       SET $PIECE(DATA,U,16)=$PIECE(FBY2,U,4)
 +25      ; store new data for this DoD invoice#
                       SET ^TMP("FBAAIAR",$JOB,FBVENAME,FBDODINV)=DATA
 +26      ; gather totals for DoD invoice#
                       DO GET(FBVENAME,FBDODINV)
 +27                   QUIT 
                   End DoDot:1
COMPINX   ;
 +1        QUIT 
 +2       ;
GET(FBVENAME,FBDODINV) ; gather totals and other data for all Fee line items for the given vendor and DoD invoice#
 +1       ; update the established scratch global with information
 +2       ;
 +3        NEW ADJTOT,CLAIMED,DISBURSED,FBDISGD,FBG0,FBG2,FBGREJ,K,L,M,N,P,PAID,FBTT
 +4       ;
 +5        IF $GET(FBVENAME)=""
               GOTO GETX
 +6        IF $GET(FBDODINV)=""
               GOTO GETX
 +7       ;
 +8       ; initialize dollar totals to 0
           SET (CLAIMED,PAID,ADJTOT,DISBURSED)=0
 +9       ; current contents of scratch global data
           SET FBDISGD=$GET(^TMP("FBAAIAR",$JOB,FBVENAME,FBDODINV))
 +10      ; current vendor totals
           SET FBTT=$GET(^TMP("FBAAIAR",$JOB,FBVENAME))
 +11      ;
 +12      ; gather outpatient/ancillary totals for this DoD invoice#
 +13       SET K=0
           FOR 
               SET K=$ORDER(^FBAAC("DODI",FBDODINV,K))
               if 'K
                   QUIT 
               SET L=0
               FOR 
                   SET L=$ORDER(^FBAAC("DODI",FBDODINV,K,L))
                   if 'L
                       QUIT 
                   SET M=0
                   FOR 
                       SET M=$ORDER(^FBAAC("DODI",FBDODINV,K,L,M))
                       if 'M
                           QUIT 
                       SET N=0
                       FOR 
                           SET N=$ORDER(^FBAAC("DODI",FBDODINV,K,L,M,N))
                           if 'N
                               QUIT 
                           Begin DoDot:1
 +14                           SET FBG0=$GET(^FBAAC(K,1,L,1,M,1,N,0))
 +15                           SET FBG2=$GET(^FBAAC(K,1,L,1,M,1,N,2))
 +16                           SET FBGREJ=$GET(^FBAAC(K,1,L,1,M,1,N,"FBREJ"))
 +17      ; cancelled
                               IF $PIECE(FBG2,U,4)
                                   QUIT 
 +18      ; voided
                               IF $PIECE(FBG0,U,21)'=""
                                   QUIT 
 +19      ; rejected
                               IF $PIECE(FBGREJ,U,1)'=""
                                   QUIT 
 +20                           SET CLAIMED=CLAIMED+$PIECE(FBG0,U,2)
 +21                           SET PAID=PAID+$PIECE(FBG0,U,3)
 +22                           SET DISBURSED=DISBURSED+$PIECE(FBG2,U,8)
 +23                           SET P=0
                               FOR 
                                   SET P=$ORDER(^FBAAC(K,1,L,1,M,1,N,7,P))
                                   if 'P
                                       QUIT 
                                   SET ADJTOT=ADJTOT+$PIECE($GET(^FBAAC(K,1,L,1,M,1,N,7,P,0)),U,3)
 +24      ;
 +25      ; check for certain fields that may have multiple values or missing values for the same DoD invoice number
 +26      ; across all VistA line items for all payment types.
 +27      ; fee invoice number
                               DO CKMLT($PIECE(FBG0,U,16),7,8)
 +28      ; external batch#
                               DO CKMLT($PIECE($GET(^FBAA(161.7,+$PIECE(FBG0,U,8),0)),U,1),9,10)
 +29      ; obligation# (taken from the batch)
                               DO CKMLT($PIECE($GET(^FBAA(161.7,+$PIECE(FBG0,U,8),0)),U,2),11,12)
 +30      ; date paid (also check for missing values)
                               DO CKMLT($PIECE(FBG0,U,14),13,14,15)
 +31      ; check number (also check for missing values)
                               DO CKMLT($PIECE(FBG2,U,3),16,17,18)
 +32                           QUIT 
                           End DoDot:1
 +33      ;
 +34      ; gather inpatient totals for this DoD invoice#
 +35       SET K=0
           FOR 
               SET K=$ORDER(^FBAAI("DODI",FBDODINV,K))
               if 'K
                   QUIT 
               Begin DoDot:1
 +36               SET FBG0=$GET(^FBAAI(K,0))
 +37               SET FBG2=$GET(^FBAAI(K,2))
 +38               SET FBGREJ=$GET(^FBAAI(K,"FBREJ"))
 +39      ; cancelled
                   IF $PIECE(FBG2,U,5)
                       QUIT 
 +40      ; voided
                   IF $PIECE(FBG0,U,14)'=""
                       QUIT 
 +41      ; rejected
                   IF $PIECE(FBGREJ,U,1)'=""
                       QUIT 
 +42               SET CLAIMED=CLAIMED+$PIECE(FBG0,U,8)
 +43               SET PAID=PAID+$PIECE(FBG0,U,9)
 +44               SET DISBURSED=DISBURSED+$PIECE(FBG2,U,8)
 +45               SET P=0
                   FOR 
                       SET P=$ORDER(^FBAAI(K,8,P))
                       if 'P
                           QUIT 
                       SET ADJTOT=ADJTOT+$PIECE($GET(^FBAAI(K,8,P,0)),U,3)
 +46      ;
 +47      ; check for certain fields that may have multiple values or missing values for the same DoD invoice number
 +48      ; across all VistA line items for all payment types.
 +49      ; fee invoice number
                   DO CKMLT($PIECE(FBG0,U,1),7,8)
 +50      ; external batch#
                   DO CKMLT($PIECE($GET(^FBAA(161.7,+$PIECE(FBG0,U,17),0)),U,1),9,10)
 +51      ; obligation# (taken from the batch)
                   DO CKMLT($PIECE($GET(^FBAA(161.7,+$PIECE(FBG0,U,17),0)),U,2),11,12)
 +52      ; date paid (also check for missing values)
                   DO CKMLT($PIECE(FBG2,U,1),13,14,15)
 +53      ; check number (also check for missing values)
                   DO CKMLT($PIECE(FBG2,U,4),16,17,18)
 +54               QUIT 
               End DoDot:1
 +55      ;
 +56      ; gather pharmacy totals for this DoD invoice#
 +57       SET K=0
           FOR 
               SET K=$ORDER(^FBAA(162.1,"DODI",FBDODINV,K))
               if 'K
                   QUIT 
               SET L=0
               FOR 
                   SET L=$ORDER(^FBAA(162.1,"DODI",FBDODINV,K,L))
                   if 'L
                       QUIT 
                   Begin DoDot:1
 +58                   SET FBG0=$GET(^FBAA(162.1,K,"RX",L,0))
 +59                   SET FBG2=$GET(^FBAA(162.1,K,"RX",L,2))
 +60                   SET FBGREJ=$GET(^FBAA(162.1,K,"RX",L,"FBREJ"))
 +61      ; cancelled
                       IF $PIECE(FBG2,U,11)
                           QUIT 
 +62      ; voided
                       IF $PIECE(FBG2,U,3)'=""
                           QUIT 
 +63      ; rejected
                       IF $PIECE(FBGREJ,U,1)'=""
                           QUIT 
 +64                   SET CLAIMED=CLAIMED+$PIECE(FBG0,U,4)
 +65                   SET PAID=PAID+$PIECE(FBG0,U,16)
 +66                   SET DISBURSED=DISBURSED+$PIECE(FBG2,U,14)
 +67                   SET P=0
                       FOR 
                           SET P=$ORDER(^FBAA(162.1,K,"RX",L,4,P))
                           if 'P
                               QUIT 
                           SET ADJTOT=ADJTOT+$PIECE($GET(^FBAA(162.1,K,"RX",L,4,P,0)),U,3)
 +68      ;
 +69      ; check for certain fields that may have multiple values or missing values for the same DoD invoice number
 +70      ; across all VistA line items for all payment types.
 +71      ; fee invoice number (K is DINUM'd with the .01 field)
                       DO CKMLT(K,7,8)
 +72      ; external batch#
                       DO CKMLT($PIECE($GET(^FBAA(161.7,+$PIECE(FBG0,U,17),0)),U,1),9,10)
 +73      ; obligation# (taken from the batch)
                       DO CKMLT($PIECE($GET(^FBAA(161.7,+$PIECE(FBG0,U,17),0)),U,2),11,12)
 +74      ; date paid (also check for missing values)
                       DO CKMLT($PIECE(FBG2,U,8),13,14,15)
 +75      ; check number (also check for missing values)
                       DO CKMLT($PIECE(FBG2,U,10),16,17,18)
 +76                   QUIT 
                   End DoDot:1
 +77      ;
 +78      ; update scratch global
 +79      ; total amount claimed
           SET $PIECE(FBDISGD,U,4)=CLAIMED
 +80      ; total amount paid
           SET $PIECE(FBDISGD,U,5)=PAID
 +81      ; total adjustment amount
           SET $PIECE(FBDISGD,U,6)=ADJTOT
 +82      ; total disbursed amount
           SET $PIECE(FBDISGD,U,19)=DISBURSED
 +83       SET ^TMP("FBAAIAR",$JOB,FBVENAME,FBDODINV)=FBDISGD
 +84      ;
 +85      ; also update vendor totals
 +86      ; count
           SET $PIECE(FBTT,U,1)=$PIECE(FBTT,U,1)+1
 +87       SET $PIECE(FBTT,U,4)=$PIECE(FBTT,U,4)+CLAIMED
 +88       SET $PIECE(FBTT,U,5)=$PIECE(FBTT,U,5)+PAID
 +89       SET $PIECE(FBTT,U,6)=$PIECE(FBTT,U,6)+ADJTOT
 +90       SET $PIECE(FBTT,U,19)=$PIECE(FBTT,U,19)+DISBURSED
 +91       SET ^TMP("FBAAIAR",$JOB,FBVENAME)=FBTT
 +92      ;
GETX      ;
 +1        QUIT 
 +2       ;
CKMLT(VAL,VALPCE,MLTPCE,NOVPCE) ; check for multiple/missing data values
 +1       ;    Variable FBDISGD is assumed to contain the contents of the scratch global
 +2       ;    VAL - value from the payment file to check
 +3       ; VALPCE - piece# from the scratch global to check for multiple values
 +4       ; MLTPCE - piece# of the scratch global to set if multiple values found
 +5       ; NOVPCE - piece# of the scratch global to set if no data exists in VAL (optional)
 +6       ;
 +7        IF VAL'=""
               IF $PIECE(FBDISGD,U,VALPCE)'=""
                   IF VAL'=$PIECE(FBDISGD,U,VALPCE)
                       SET $PIECE(FBDISGD,U,MLTPCE)=1
 +8        IF $GET(NOVPCE)
               IF VAL=""
                   SET $PIECE(FBDISGD,U,NOVPCE)=1
 +9        QUIT 
 +10      ;