BPSOSRB ;BHAM ISC/FCS/DRS/FLS - Process claim on processing queue ;06/01/2004
 ;;1.0;E CLAIMS MGMT ENGINE;**1,5,7,8,10,11,20,23**;JUN 2004;Build 44
 ;;Per VA Directive 6402, this routine should not be modified.
 ;
 Q
BACKGR ;
 I '$$LOCKNOW^BPSOSRX("BACKGROUND") Q
 N TYPE,KEY1,KEY2,IEN59,IEN59PR,BPNOW,BPUNTIL
 N BPIEN77,BPLCKRX,BPQ,BPCOBIND,GRPLAN
 S BPNOW=$$NOW^BPSOSRX()
 ;go through all ACTIVATED
 S KEY1="" F  S KEY1=$O(^BPS(9002313.77,"AC",1,KEY1)) Q:KEY1=""  D
 . S KEY2="" F  S KEY2=$O(^BPS(9002313.77,"AC",1,KEY1,KEY2)) Q:KEY2=""  D
 . . S IEN59PR=+$$IEN59^BPSOSRX(KEY1,KEY2,0)
 . . S BPLCKRX=$$LOCKRF^BPSOSRX(KEY1,KEY2,10,IEN59PR,$T(+0)) I BPLCKRX=0 D  Q
 . . . D LOG^BPSOSL(IEN59PR,$T(+0)_"-Failed to $$LOCKRF^BPSOSRX.  Will retry later.")
 . . S BPQ=0
 . . S BPIEN77="" F  S BPIEN77=$O(^BPS(9002313.77,"AC",1,KEY1,KEY2,BPIEN77)) Q:(+BPIEN77=0)!(BPQ=1)  D
 . . . ;check DONT PROCESS UNTIL field #.08 and if it is greater than NOW then DO NOT process it
 . . . S BPUNTIL=+$P($G(^BPS(9002313.77,BPIEN77,0)),U,8) I BPUNTIL>BPNOW S BPQ=1 Q  ;D LOG^BPSOSL(IEN59,$T(+0)_"-The request cannot be processed until."_BPUNTIL_" Will retry later.") Q
 . . . ;check if PROCESS FLAG is IN PROCESS - if the earlier record for this RX refill is in progress - 
 . . . ;we should not process the next operation queued - go to the next refill (BPQ=1)
 . . . I $P($G(^BPS(9002313.77,BPIEN77,0)),U,4)=2 S BPQ=1 D  Q
 . . . . D LOG^BPSOSL(IEN59,$T(+0)_"-Status is 'IN PROCESS'.  Will retry later.")
 . . . S BPCOBIND=$P(^BPS(9002313.77,BPIEN77,0),U,3)
 . . . S IEN59=$$IEN59^BPSOSRX(KEY1,KEY2,BPCOBIND)
 . . . ; Removed code to check Insurer Asleep
 . . . S TYPE=$P($G(^BPS(9002313.77,+BPIEN77,1)),U,4),TYPE=$S(TYPE="C":"CLAIM",TYPE="U":"UNCLAIM",TYPE="E":"ELIGIBILITY",1:"UNKNW")
 . . . I TYPE="UNKNW" D ERROR(+BPIEN77,IEN59,"Request Type is unknown. Cannot be processed.") Q
 . . . D LOG^BPSOSL(IEN59,$T(+0)_"-Processing the Activated request "_BPIEN77)
 . . . D LOG77(IEN59,BPIEN77)  ; Log entire contents of the request.
 . . . D LOG^BPSOSL(IEN59,$T(+0)_"-Dequeuing.  Type is "_TYPE)
 . . . ; if this is ACTIVATED then make it IN PROCESS (see SETPRFLG below)
 . . . N TIME,MOREDATA
 . . . S TIME=$P($G(^BPS(9002313.77,+BPIEN77,6)),U,1) ; time requested
 . . . D READMORE^BPSOSRX4(BPIEN77,.MOREDATA)
 . . . ;now it is IN PROCESS - i.e. goes to BACKGR1 (as K ^XTMP("BPS-PROC",TYPE,KEY1,KEY2 in old logic) 
 . . . I +$$INPROCES^BPSOSRX4(BPIEN77)=0 D ERROR(+BPIEN77,IEN59,"Cannot change the PROCESS FLAG to IN PROCESS. Cannot be processed.") Q
 . . . D LOG^BPSOSL(IEN59,$T(+0)_"-The request "_BPIEN77_" has been changed to IN PROCESS")
 . . . I +$$BACKGR1(TYPE,KEY1,KEY2,TIME,.MOREDATA,IEN59,+BPIEN77)=0 Q
 . . . S BPQ=1 ; This will skip requests with the same keys as the one just processed
 . . I BPLCKRX D UNLCKRF^BPSOSRX(KEY1,KEY2,IEN59PR,$T(+0))
 D UNLOCK^BPSOSRX("BACKGROUND")
 Q
 ;
LOG77(IEN59,BPIEN77) ; Log entire contents of the request.
 N A
 M A=^BPS(9002313.77,BPIEN77)
 D LOG^BPSOSL(IEN59,$T(+0)_"-Contents of ^BPS(9002313.77,"_BPIEN77_"), BPS REQUEST:")
 D LOGARRAY^BPSOSL(IEN59,"A")
 Q
 ;
 ; BACKGR1 - Further processing of the claim
 ; Besides the parameter below, IEN59 also needs to be defined
 ; TYPE - "CLAIM", "UNCLAIM", or "ELIGIBILITY"
 ; KEY1 - First key of the request
 ; KEY2 - Second key of the request
 ; TIME - time requested
 ; MOREDATA - array with claim data
 ; IEN59 - BPS TRANSACTION ien
 ; BPS77 - BPS REQUEST file ien
BACKGR1(TYPE,KEY1,KEY2,TIME,MOREDATA,IEN59,BPS77) ;
 ; Resolve multiple requests
 N SKIP,SKIPREAS,BPCOBIND,RESULT,PAYABLE
 D LOG^BPSOSL(IEN59,$T(+0)_"-Processing request "_BPS77_" with keys "_KEY1_", "_KEY2_" and type "_TYPE)
 S SKIP=0
 S BPCOBIND=$$COB59^BPSUTIL2(IEN59)
 S RESULT=$P($$STATUS^BPSOSRX(KEY1,KEY2,0,,BPCOBIND),U)
 S PAYABLE=$$PAYABLE^BPSOSRX5(RESULT)
 I TYPE="CLAIM" D
 . I $$RXDEL^BPSOS(KEY1,KEY2) S SKIP=1,SKIPREAS="Prescription is marked as DELETED or CANCELLED in Outpatient Pharmacy" Q
 . ;
 . ; Allow resubmit w/o reversal to proceed no matter the claim status (bps*1*20)
 . I $P($G(^BPS(9002313.77,BPS77,1)),U,1)="ERWV" Q
 . ;
 . I PAYABLE S SKIP=1,SKIPREAS="Prescription is currently paid.  Previous Result is "_RESULT Q
 ;
 I TYPE="UNCLAIM",'PAYABLE S SKIP=1,SKIPREAS="Cannot reverse - previous result was "_RESULT
 ;
 ; If the SKIP flag message is set, handle error and quit
 I SKIP D ERROR(BPS77,IEN59,SKIPREAS) Q 0
 ;
 ; Submit claim
 S MOREDATA("SUBMIT TIME")=TIME
 ;
 ; If reversal, execute reversal code and quit
 I TYPE="UNCLAIM" D REVERSE(IEN59,.MOREDATA,$G(BPS77)) Q 1
 ;
 ; Claims and Eligibility will fall through to here
 D LOG^BPSOSL(IEN59,$T(+0)_"-Initiating Claim")
 D EN^BPSOSIZ(IEN59,.MOREDATA,$G(BPS77))
 Q 1
 ;
 ; Error handling - If a record can not be processed, then it needs to be
 ; inactivated and the next request activated
 ; 
 ; This is also called by ERROR^BPSOSIZ
ERROR(BPS77,IEN59,ERROR) ;
 I '$G(BPS77) Q
 I $G(ERROR)="" S ERROR="Unknown"
 N BPNXT77,BPRETV
 D LOG^BPSOSL(IEN59,$T(+0)_"-Skipping "_BPS77_" because of ERROR: "_ERROR)
 ;
 ; Inactivate the current request
 S BPRETV=$$INACTIVE^BPSOSRX4(BPS77,ERROR)
 I 'BPRETV D LOG^BPSOSL(IEN59,$T(+0)_"-Could not inactivate the request: "_BPRETV) Q
 D LOG^BPSOSL(IEN59,$T(+0)_"-Request is inactivated")
 ;
 ; See if there is a next request linked to this one
 ; If there is, activate it
 S BPNXT77=+$$GETNXREQ^BPSOSRX6(BPS77,0,0,$G(IEN59))
 I BPNXT77 D
 . S BPRETV=$$ACTIVATE^BPSOSRX4(BPNXT77)
 . D LOG^BPSOSL(IEN59,$T(+0)_"-The next request "_BPNXT77_" has "_$S('BPRETV:"not ",1:"")_"been activated")
 Q
 ;
 ; Process the reversal
REVERSE(IEN59,MOREDATA,BP77) ;
 N MSG,RETVAL,REV
 ;
 ; Update BPS REQUEST with the BPS TRANSACTION IEN
 I $G(BP77)>0 D UPD7759^BPSOSRX4(BP77,IEN59)
 ;
 ; Log Reversal or Reversal/Resubmit message.
 ; Note that the reversal/resubmit message is needed
 ;   for Turn-Around Stats - Do NOT delete/alter!!
 S MSG=$T(+0)_"-Initiating Reversal"
 D LOG^BPSOSL(IEN59,MSG)
 ;
 ; Change status to 0% (Waiting to Start), which will reset START TIME,
 ;   and then to 10% (Building transaction)
 D SETSTAT^BPSOSU(IEN59,0)
 D SETSTAT^BPSOSU(IEN59,10)
 ;
 ; Update specific fields of the BPS Transaction record - Submit Date (#6),
 ;   User (#13), Request Type (#19), Reversal Claim (#401), Reversal Response (#402),
 ;   Reversal Reason (#404), Reversal Request (#405), Reversal Request Date and Time (#406),
 ;   and RX Action (#1201)
 N DIE,DR,DA
 S DIE=9002313.59,DA=IEN59
 S DR="6////"_$G(MOREDATA("SUBMIT TIME"))_";13////"_$G(MOREDATA("USER"))
 S DR=DR_";404////"_$G(MOREDATA("REVERSAL REASON"))_";1201////"_$G(MOREDATA("RX ACTION"))
 S DR=DR_";19////"_$G(MOREDATA("REQ TYPE"))_";405////"_$G(MOREDATA("REQ IEN"))
 S DR=DR_";406////"_MOREDATA("REQ DTTM")_";401////@;402////@"
 ;
 D ^DIE
 ;
 ; Store the Payer Sequence in the log
 N BPSCOB
 S BPSCOB=$$COB59^BPSUTIL2(IEN59),BPSCOB=$S(BPSCOB=2:"-Secondary",BPSCOB=3:"-Tertiary",1:"-Primary"),BPSCOB=BPSCOB_" Insurance"
 D LOG^BPSOSL(IEN59,$T(+0)_BPSCOB)
 ;
 ; Store contents of BPST in the Log
 D LOG^BPSOSL(IEN59,$T(+0)_"-Contents of ^BPST("_IEN59_") :")
 D LOG59^BPSOSQA(IEN59) ; Log contents of 9002313.59
 ;
 ; Add semi-colon to result text
 D PREVISLY^BPSOSIZ(IEN59)
 ;
 ; Construct reversal claim
 ;   If no reversal claim is returned, log error and quit.
 S REV=$$REVERSE^BPSECA8(IEN59)
 I REV=0 D  Q
 . D LOG^BPSOSL(IEN59,$T(+0)_"-Reversal claim not created for "_IEN59)
 . D ERROR^BPSOSU($T(+0),IEN59,100,"Reversal Claim not created")
 ;
 ; Update Reversal Field in the transaction
 S DIE=9002313.59,DA=IEN59,DR="401////"_REV
 D ^DIE
 ;
 ; Update Log
 D LOG^BPSOSL(IEN59,$T(+0)_"-Reversal claim "_$P(^BPSC(REV,0),U)_" ("_REV_")")
 ;
 ; Update status to 30% (Building the claim)
 D SETSTAT^BPSOSU(IEN59,30)
 ;
 ; Fire off task to get this on the HL7 queue
 D TASK^BPSOSQA
 Q
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HBPSOSRB   7979     printed  Sep 23, 2025@19:28:11                                                                                                                                                                                                     Page 2
BPSOSRB   ;BHAM ISC/FCS/DRS/FLS - Process claim on processing queue ;06/01/2004
 +1       ;;1.0;E CLAIMS MGMT ENGINE;**1,5,7,8,10,11,20,23**;JUN 2004;Build 44
 +2       ;;Per VA Directive 6402, this routine should not be modified.
 +3       ;
 +4        QUIT 
BACKGR    ;
 +1        IF '$$LOCKNOW^BPSOSRX("BACKGROUND")
               QUIT 
 +2        NEW TYPE,KEY1,KEY2,IEN59,IEN59PR,BPNOW,BPUNTIL
 +3        NEW BPIEN77,BPLCKRX,BPQ,BPCOBIND,GRPLAN
 +4        SET BPNOW=$$NOW^BPSOSRX()
 +5       ;go through all ACTIVATED
 +6        SET KEY1=""
           FOR 
               SET KEY1=$ORDER(^BPS(9002313.77,"AC",1,KEY1))
               if KEY1=""
                   QUIT 
               Begin DoDot:1
 +7                SET KEY2=""
                   FOR 
                       SET KEY2=$ORDER(^BPS(9002313.77,"AC",1,KEY1,KEY2))
                       if KEY2=""
                           QUIT 
                       Begin DoDot:2
 +8                        SET IEN59PR=+$$IEN59^BPSOSRX(KEY1,KEY2,0)
 +9                        SET BPLCKRX=$$LOCKRF^BPSOSRX(KEY1,KEY2,10,IEN59PR,$TEXT(+0))
                           IF BPLCKRX=0
                               Begin DoDot:3
 +10                               DO LOG^BPSOSL(IEN59PR,$TEXT(+0)_"-Failed to $$LOCKRF^BPSOSRX.  Will retry later.")
                               End DoDot:3
                               QUIT 
 +11                       SET BPQ=0
 +12                       SET BPIEN77=""
                           FOR 
                               SET BPIEN77=$ORDER(^BPS(9002313.77,"AC",1,KEY1,KEY2,BPIEN77))
                               if (+BPIEN77=0)!(BPQ=1)
                                   QUIT 
                               Begin DoDot:3
 +13      ;check DONT PROCESS UNTIL field #.08 and if it is greater than NOW then DO NOT process it
 +14      ;D LOG^BPSOSL(IEN59,$T(+0)_"-The request cannot be processed until."_BPUNTIL_" Will retry later.") Q
                                   SET BPUNTIL=+$PIECE($GET(^BPS(9002313.77,BPIEN77,0)),U,8)
                                   IF BPUNTIL>BPNOW
                                       SET BPQ=1
                                       QUIT 
 +15      ;check if PROCESS FLAG is IN PROCESS - if the earlier record for this RX refill is in progress - 
 +16      ;we should not process the next operation queued - go to the next refill (BPQ=1)
 +17                               IF $PIECE($GET(^BPS(9002313.77,BPIEN77,0)),U,4)=2
                                       SET BPQ=1
                                       Begin DoDot:4
 +18                                       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Status is 'IN PROCESS'.  Will retry later.")
                                       End DoDot:4
                                       QUIT 
 +19                               SET BPCOBIND=$PIECE(^BPS(9002313.77,BPIEN77,0),U,3)
 +20                               SET IEN59=$$IEN59^BPSOSRX(KEY1,KEY2,BPCOBIND)
 +21      ; Removed code to check Insurer Asleep
 +22                               SET TYPE=$PIECE($GET(^BPS(9002313.77,+BPIEN77,1)),U,4)
                                   SET TYPE=$SELECT(TYPE="C":"CLAIM",TYPE="U":"UNCLAIM",TYPE="E":"ELIGIBILITY",1:"UNKNW")
 +23                               IF TYPE="UNKNW"
                                       DO ERROR(+BPIEN77,IEN59,"Request Type is unknown. Cannot be processed.")
                                       QUIT 
 +24                               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Processing the Activated request "_BPIEN77)
 +25      ; Log entire contents of the request.
                                   DO LOG77(IEN59,BPIEN77)
 +26                               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Dequeuing.  Type is "_TYPE)
 +27      ; if this is ACTIVATED then make it IN PROCESS (see SETPRFLG below)
 +28                               NEW TIME,MOREDATA
 +29      ; time requested
                                   SET TIME=$PIECE($GET(^BPS(9002313.77,+BPIEN77,6)),U,1)
 +30                               DO READMORE^BPSOSRX4(BPIEN77,.MOREDATA)
 +31      ;now it is IN PROCESS - i.e. goes to BACKGR1 (as K ^XTMP("BPS-PROC",TYPE,KEY1,KEY2 in old logic) 
 +32                               IF +$$INPROCES^BPSOSRX4(BPIEN77)=0
                                       DO ERROR(+BPIEN77,IEN59,"Cannot change the PROCESS FLAG to IN PROCESS. Cannot be processed.")
                                       QUIT 
 +33                               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-The request "_BPIEN77_" has been changed to IN PROCESS")
 +34                               IF +$$BACKGR1(TYPE,KEY1,KEY2,TIME,.MOREDATA,IEN59,+BPIEN77)=0
                                       QUIT 
 +35      ; This will skip requests with the same keys as the one just processed
                                   SET BPQ=1
                               End DoDot:3
 +36                       IF BPLCKRX
                               DO UNLCKRF^BPSOSRX(KEY1,KEY2,IEN59PR,$TEXT(+0))
                       End DoDot:2
               End DoDot:1
 +37       DO UNLOCK^BPSOSRX("BACKGROUND")
 +38       QUIT 
 +39      ;
LOG77(IEN59,BPIEN77) ; Log entire contents of the request.
 +1        NEW A
 +2        MERGE A=^BPS(9002313.77,BPIEN77)
 +3        DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Contents of ^BPS(9002313.77,"_BPIEN77_"), BPS REQUEST:")
 +4        DO LOGARRAY^BPSOSL(IEN59,"A")
 +5        QUIT 
 +6       ;
 +7       ; BACKGR1 - Further processing of the claim
 +8       ; Besides the parameter below, IEN59 also needs to be defined
 +9       ; TYPE - "CLAIM", "UNCLAIM", or "ELIGIBILITY"
 +10      ; KEY1 - First key of the request
 +11      ; KEY2 - Second key of the request
 +12      ; TIME - time requested
 +13      ; MOREDATA - array with claim data
 +14      ; IEN59 - BPS TRANSACTION ien
 +15      ; BPS77 - BPS REQUEST file ien
BACKGR1(TYPE,KEY1,KEY2,TIME,MOREDATA,IEN59,BPS77) ;
 +1       ; Resolve multiple requests
 +2        NEW SKIP,SKIPREAS,BPCOBIND,RESULT,PAYABLE
 +3        DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Processing request "_BPS77_" with keys "_KEY1_", "_KEY2_" and type "_TYPE)
 +4        SET SKIP=0
 +5        SET BPCOBIND=$$COB59^BPSUTIL2(IEN59)
 +6        SET RESULT=$PIECE($$STATUS^BPSOSRX(KEY1,KEY2,0,,BPCOBIND),U)
 +7        SET PAYABLE=$$PAYABLE^BPSOSRX5(RESULT)
 +8        IF TYPE="CLAIM"
               Begin DoDot:1
 +9                IF $$RXDEL^BPSOS(KEY1,KEY2)
                       SET SKIP=1
                       SET SKIPREAS="Prescription is marked as DELETED or CANCELLED in Outpatient Pharmacy"
                       QUIT 
 +10      ;
 +11      ; Allow resubmit w/o reversal to proceed no matter the claim status (bps*1*20)
 +12               IF $PIECE($GET(^BPS(9002313.77,BPS77,1)),U,1)="ERWV"
                       QUIT 
 +13      ;
 +14               IF PAYABLE
                       SET SKIP=1
                       SET SKIPREAS="Prescription is currently paid.  Previous Result is "_RESULT
                       QUIT 
               End DoDot:1
 +15      ;
 +16       IF TYPE="UNCLAIM"
               IF 'PAYABLE
                   SET SKIP=1
                   SET SKIPREAS="Cannot reverse - previous result was "_RESULT
 +17      ;
 +18      ; If the SKIP flag message is set, handle error and quit
 +19       IF SKIP
               DO ERROR(BPS77,IEN59,SKIPREAS)
               QUIT 0
 +20      ;
 +21      ; Submit claim
 +22       SET MOREDATA("SUBMIT TIME")=TIME
 +23      ;
 +24      ; If reversal, execute reversal code and quit
 +25       IF TYPE="UNCLAIM"
               DO REVERSE(IEN59,.MOREDATA,$GET(BPS77))
               QUIT 1
 +26      ;
 +27      ; Claims and Eligibility will fall through to here
 +28       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Initiating Claim")
 +29       DO EN^BPSOSIZ(IEN59,.MOREDATA,$GET(BPS77))
 +30       QUIT 1
 +31      ;
 +32      ; Error handling - If a record can not be processed, then it needs to be
 +33      ; inactivated and the next request activated
 +34      ; 
 +35      ; This is also called by ERROR^BPSOSIZ
ERROR(BPS77,IEN59,ERROR) ;
 +1        IF '$GET(BPS77)
               QUIT 
 +2        IF $GET(ERROR)=""
               SET ERROR="Unknown"
 +3        NEW BPNXT77,BPRETV
 +4        DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Skipping "_BPS77_" because of ERROR: "_ERROR)
 +5       ;
 +6       ; Inactivate the current request
 +7        SET BPRETV=$$INACTIVE^BPSOSRX4(BPS77,ERROR)
 +8        IF 'BPRETV
               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Could not inactivate the request: "_BPRETV)
               QUIT 
 +9        DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Request is inactivated")
 +10      ;
 +11      ; See if there is a next request linked to this one
 +12      ; If there is, activate it
 +13       SET BPNXT77=+$$GETNXREQ^BPSOSRX6(BPS77,0,0,$GET(IEN59))
 +14       IF BPNXT77
               Begin DoDot:1
 +15               SET BPRETV=$$ACTIVATE^BPSOSRX4(BPNXT77)
 +16               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-The next request "_BPNXT77_" has "_$SELECT('BPRETV:"not ",1:"")_"been activated")
               End DoDot:1
 +17       QUIT 
 +18      ;
 +19      ; Process the reversal
REVERSE(IEN59,MOREDATA,BP77) ;
 +1        NEW MSG,RETVAL,REV
 +2       ;
 +3       ; Update BPS REQUEST with the BPS TRANSACTION IEN
 +4        IF $GET(BP77)>0
               DO UPD7759^BPSOSRX4(BP77,IEN59)
 +5       ;
 +6       ; Log Reversal or Reversal/Resubmit message.
 +7       ; Note that the reversal/resubmit message is needed
 +8       ;   for Turn-Around Stats - Do NOT delete/alter!!
 +9        SET MSG=$TEXT(+0)_"-Initiating Reversal"
 +10       DO LOG^BPSOSL(IEN59,MSG)
 +11      ;
 +12      ; Change status to 0% (Waiting to Start), which will reset START TIME,
 +13      ;   and then to 10% (Building transaction)
 +14       DO SETSTAT^BPSOSU(IEN59,0)
 +15       DO SETSTAT^BPSOSU(IEN59,10)
 +16      ;
 +17      ; Update specific fields of the BPS Transaction record - Submit Date (#6),
 +18      ;   User (#13), Request Type (#19), Reversal Claim (#401), Reversal Response (#402),
 +19      ;   Reversal Reason (#404), Reversal Request (#405), Reversal Request Date and Time (#406),
 +20      ;   and RX Action (#1201)
 +21       NEW DIE,DR,DA
 +22       SET DIE=9002313.59
           SET DA=IEN59
 +23       SET DR="6////"_$GET(MOREDATA("SUBMIT TIME"))_";13////"_$GET(MOREDATA("USER"))
 +24       SET DR=DR_";404////"_$GET(MOREDATA("REVERSAL REASON"))_";1201////"_$GET(MOREDATA("RX ACTION"))
 +25       SET DR=DR_";19////"_$GET(MOREDATA("REQ TYPE"))_";405////"_$GET(MOREDATA("REQ IEN"))
 +26       SET DR=DR_";406////"_MOREDATA("REQ DTTM")_";401////@;402////@"
 +27      ;
 +28       DO ^DIE
 +29      ;
 +30      ; Store the Payer Sequence in the log
 +31       NEW BPSCOB
 +32       SET BPSCOB=$$COB59^BPSUTIL2(IEN59)
           SET BPSCOB=$SELECT(BPSCOB=2:"-Secondary",BPSCOB=3:"-Tertiary",1:"-Primary")
           SET BPSCOB=BPSCOB_" Insurance"
 +33       DO LOG^BPSOSL(IEN59,$TEXT(+0)_BPSCOB)
 +34      ;
 +35      ; Store contents of BPST in the Log
 +36       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Contents of ^BPST("_IEN59_") :")
 +37      ; Log contents of 9002313.59
           DO LOG59^BPSOSQA(IEN59)
 +38      ;
 +39      ; Add semi-colon to result text
 +40       DO PREVISLY^BPSOSIZ(IEN59)
 +41      ;
 +42      ; Construct reversal claim
 +43      ;   If no reversal claim is returned, log error and quit.
 +44       SET REV=$$REVERSE^BPSECA8(IEN59)
 +45       IF REV=0
               Begin DoDot:1
 +46               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Reversal claim not created for "_IEN59)
 +47               DO ERROR^BPSOSU($TEXT(+0),IEN59,100,"Reversal Claim not created")
               End DoDot:1
               QUIT 
 +48      ;
 +49      ; Update Reversal Field in the transaction
 +50       SET DIE=9002313.59
           SET DA=IEN59
           SET DR="401////"_REV
 +51       DO ^DIE
 +52      ;
 +53      ; Update Log
 +54       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Reversal claim "_$PIECE(^BPSC(REV,0),U)_" ("_REV_")")
 +55      ;
 +56      ; Update status to 30% (Building the claim)
 +57       DO SETSTAT^BPSOSU(IEN59,30)
 +58      ;
 +59      ; Fire off task to get this on the HL7 queue
 +60       DO TASK^BPSOSQA
 +61       QUIT