BPSNCPDP ;BHAM ISC/LJE/SS - API to submit a claim to ECME ;11/7/07  16:58
 ;;1.0;E CLAIMS MGMT ENGINE;**1,3,4,2,5,6,7,8,10,11,19,20,22,40**;JUN 2004;Build 25
 ;;Per VA Directive 6402, this routine should not be modified.
 ;
 ; Reference to $$PROD^XUPROD in ICR #4440
 ; Reference to $$GETNDC^PSONDCUT in ICR #4705
 ; Reference to Patient file (#2) in ICR #10035
 ;
 ;Input
 ; BRXIEN = Prescription IEN
 ; BFILL  = Fill Number
 ; DOS    = Date of Service
 ; BWHERE (RX Action)
 ;    AREV = Auto-Reversal
 ;    BB   = Back Billing
 ;    CRLB = CMOP/OPAI Release & Rebill
 ;    CRLR = CMOP/OPAI Release & Reverse (successful release)
 ;    CRLX = CMOP/OPAI unsuccessful release & reverse
 ;    CRRL = CMOP/OPAI Release - Original claim not paid, submit
 ;           another claim, no reversal
 ;    DC   = Discontinue - only reverse un-released PAYABLE DC's,
 ;           release date check should be in calling routine.
 ;    DE   = Delete
 ;    ED   = Edit (includes RX release with NDC edit)
 ;    ERES = Resubmit from ECME user screen
 ;    ERWV = Resubmit Without Reversal from ECME user screen
 ;    ERNB = Resubmit of a TRI/CVA non-billable entry from the ECME
 ;           user screen
 ;    EREV = Reversal from ECME user screen
 ;    HLD  = Put prescription on Hold
 ;    OREV = Reversal from Outpatient Pharmacy edit screen
 ;    RSNB = Resubmit Non-Billable TRICARE & CHAMPVA from PSO Reject
 ;           Info Screen
 ;    OF   = Original Fill
 ;    P2   = Original submission from PRO Option, no reversal
 ;    P2S  = Resubmit from PRO Option
 ;    PC   = Pull CMOPs
 ;    PE   = Pull early from suspense
 ;    PL   = Pull local from suspense
 ;    PP   = Pull RX (PP) action from Patient Rx Processing option
 ;    RF   = Refill
 ;    RN   = Renew
 ;    RRL  = Release - Original claim not paid, submit another claim,
 ;           no reversal
 ;    RS   = Return-to-Stock
 ; BILLNDC = Valid NDC# with format 5-4-2
 ; REVREAS = Reversal Reason
 ; DURREC  = String of up to three sets of DUR info. Sets are delimited
 ;           with "~". Each set consists of three "^" pieces:
 ;              Reason for Service Code
 ;              Professional Service Code
 ;              Result of Service Code
 ; BPOVRIEN = Pointer to BPS NCPDP OVERIDE file.  This parameter will 
 ;            only be passed if there are overrides entered by the
 ;            user via the Resubmit with Edits (RED) option in the 
 ;            user screen.
 ; BPSAUTH  = Prior authorization code ^ Prior authorization number
 ; BPSCLARF = Submission Clarification Code (external value from file
 ;            #9002313.25), entered by pharmacist and passed by
 ;            Outpatient Pharmacy to ECME to put into the claim  
 ; BPCOBIND = (optional, default is Primary) for COB indicators - so
 ;            when the API is called for the particular
 ;            COB claim the BPSNCPDP can handle it.
 ; BPJOBFLG = (optional) B - if called by unqueueing logic in background
 ;            F - (default) if called by other (foreground) process
 ; BPREQIEN = (optional) IEN to file #9002313.77, BPS REQUEST, that
 ;            needs to be unqueued 
 ; BPSCLOSE = (optional) local array used with BWHERE="EREV" only, if
 ;            the user had chosen to close the claim after reversal
 ;   If claim needs to be closed then
 ;      BPSCLOSE("CLOSE AFT REV")=1
 ;      BPSCLOSE("CLOSE AFT REV REASON")=<#356.8 ien>
 ;      BPSCLOSE("CLOSE AFT REV COMMENT")=<some text>
 ; BPSPLAN  = (optional) IEN of the entry in file #355.3, GROUP
 ;            INSURANCE PLAN
 ; BPSPRDAT = (optional) local array passed by reference.  Contains
 ;            primary claim data needed to submit a secondary claim.
 ;            Format:  BPSPRDAT(NCPDP field)
 ; BPSRTYPE = (optional) rate type (IEN to file #399.3)
 ; BPSDELAY = Delay Reason Code (IEN to file #9002313.29, BPS NCPDP
 ;            DELAY REASON CODE, entered by user in the Back Billing
 ;            option of Claims Tracking, passed in to be put in claim.
 ; BPSDX    = Diagnosis code (external) to be sent on outgoing claim.
 ; 
 ;Output (RESPONSE^MESSAGE^ELIGIBILITY^CLAIMSTATUS^COB^RXCOB^INSURANCE)
 ; RESPONSE
 ;    0  Submitted through ECME
 ;    1  No submission through ECME
 ;    2  IB not billable
 ;    3  Claim was closed, not submitted (RTS/Deletes)
 ;    4  Unable to queue claim
 ;    5  Incorrect information supplied to ECME
 ;    6  Inactive ECME - Primarily used for TRICARE/CHAMPVA to say
 ;       ok to process rx
 ;    10 Reversal but no resubmit
 ; MESSAGE = Message associated with the response (error/submitted)
 ; ELIGIBILITY = V - Veteran, T - TRICARE, C - CHAMPVA
 ; CLAIMSTATUS = claim status (null or IN PROGRESS/E PAYABLE/etc...)
 ; COB = Coordination Of Benefit indicator of the insurance as it is
 ;       stored in the PATIENT file: 1-primary, 2-secondary, 3-tertiary
 ; RXCOB = the payer sequence indicator of the claim which was sent to
 ;         the payer as a result of this call: 1-primary, 2-secondary)
 ; INSURANCE = Name of the insurance company the claim was sent to
 ;
EN(BRXIEN,BFILL,DOS,BWHERE,BILLNDC,REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPCOBIND,BPJOBFLG,BPREQIEN,BPSCLOSE,BPSPLAN,BPSPRDAT,BPSRTYPE,BPSDELAY,BPSDX) ;
 ;
 N BP77NEW,BPACTTYP,BPLCK,BPNEWCLM,BPPAYABL,BPPREVRQ,BPRESLT,BPRET
 N BPRETV,BPSCOB,BPSELIG,BPSQUIT,BPSSTAT,BPSTART,BRX,CLMSTAT,DFN,IEN59
 N OLDRESP,PNAME,RESPONSE,SITE,TODAY,WFLG
 ;
 ; test not ecme active
 I '$$PROD^XUPROD,'$P($G(^BPS(9002313.99,1,0)),"^",3) Q "1^ECME switch is not on for the site"
 ; Set default values and other required vars
 ; default is foreground ("F")
 S BPJOBFLG=$S($G(BPJOBFLG)="":"F",1:$G(BPJOBFLG))
 S RESPONSE="",CLMSTAT="",BP77NEW=0
 S BPLCK=0 ;0 - default for "B" jobs
 S REVREAS=$G(REVREAS),DURREC=$G(DURREC)
 S BPSCLARF=$G(BPSCLARF),BPSAUTH=$G(BPSAUTH)
 S BPOVRIEN=$G(BPOVRIEN),BPSDELAY=$G(BPSDELAY)
 S BPSDX=$G(BPSDX)
 ; BPCOBIND will be used as a flag to indicate the following
 ;    If BPCOBIND>0 then the API is called for the particular COB claim
 ;    if BPCOBIND=0 then the API is called for a whole RX/RF -
 ;         Outpatient Pharmacy doesn't care about COB when the
 ;         pharmacy user enters, deletes or edits RX/refills
 S BPCOBIND=+$G(BPCOBIND)
 ;
 ; BPSCOB variable will be used to store COB value (default is PRIMARY)
 ; in this function only
 S BPSCOB=$S(BPCOBIND>0:BPCOBIND,1:1)
 ;
 ; Remove semi-colons from reversal reason
 S REVREAS=$TR(REVREAS,";","-")
 ;
 ; Default is original fill
 S BRXIEN=$G(BRXIEN)
 I '$G(BFILL) S BFILL=0
 ;
 ; Get prescription number
 S BRX=$$RXAPI1^BPSUTIL1(BRXIEN,.01,"I")
 ;
 ; Make sure fill date is not in the future or empty
 S TODAY=$$DT^XLFDT
 I '$G(DOS)!($G(DOS)>TODAY) S DOS=$$DOSDATE^BPSSCRRS(BRXIEN,BFILL)
 ;
 ; Get the NDC if it was not passed in
 I $G(BILLNDC)="" S BILLNDC=$$GETNDC^PSONDCUT(BRXIEN,BFILL)
 ;
 ; Patient Info
 S DFN=$$RXAPI1^BPSUTIL1(BRXIEN,2,"I"),PNAME=$$GET1^DIQ(2,DFN,.01)
 ;
 ; Check parameters and vars
 S BPRETV=$$CHCKPAR^BPSOSRX8(BRXIEN,BRX,$G(BWHERE),DFN,PNAME)
 I +BPRETV=0 S CLMSTAT=$P(BPRETV,U,2),RESPONSE=5 G END
 ;
 ; Calculate IEN59
 S IEN59=$$IEN59^BPSOSRX(BRXIEN,BFILL,BPSCOB)
 I IEN59="" S CLMSTAT="BPS Transaction IEN could not be calculated",RESPONSE=1 G END
 ;
 ; Populate COB fields from BPS TRANSACTION to resubmit secondary
 ; claims from the User Screen.  If $G(BPSPRDAT("NEW COB DATA"))=1 then
 ; the resubmit requested from the BPS COB PROCESS SECOND TRICARE and
 ; the user can change the data.
 I BPSCOB=2,$$ACTTYPE^BPSOSRX5(BWHERE)="UC",'$G(BPSPRDAT("NEW COB DATA")) N:$D(BPSRTYPE)=0 BPSRTYPE N:$D(BPSPLAN)=0 BPSPLAN N:$D(BPSPRDAT)=0 BPSPRDAT I $$SECDATA^BPSPRRX6(BRXIEN,BFILL,.BPSPLAN,.BPSPRDAT,.BPSRTYPE)=0 D  G END
 . S CLMSTAT="Insufficient data to resubmit the secondary claim, use Process Secondary/TRICARE Rx to ECME option.",RESPONSE=5
 ;
 ; Initialize log
 D LOG^BPSOSL(IEN59,$T(+0)_"-Start of claim","DT")
 D LOG^BPSOSL(IEN59,$T(+0)_"-BWHERE = "_BWHERE)
 D LOG^BPSOSL(IEN59,$T(+0)_"-Job flag = "_BPJOBFLG_$S(BPJOBFLG="B":" BPS REQUEST ien = "_$G(BPREQIEN),1:""))
 ;
 ; Check if we need to print the messages to the screen (WFLG=1 : YES)
 S WFLG=0
 I BPJOBFLG="F" S WFLG=$$PRINTSCR^BPSOSRX8(BWHERE)
 ;
 ; Lock the Rx and Fill while putting it on the queue to prevent two
 ; jobs from being activated at the same time.  This is only for
 ; foreground jobs.  Background jobs are called from REQST99^BPSOSRX5
 ; and the RX/RF should be already locked by this point.
 I BPJOBFLG="F" D  I 'BPLCK G END
 . S BPLCK=$$LOCKRF^BPSOSRX(BRXIEN,BFILL,10,$G(IEN59),$T(+0))
 . I 'BPLCK S RESPONSE=4,CLMSTAT="Unable to acquire the lock needed to put the RX and fill on the queue"
 ;
 ; Determine the action type.  If foreground job, then action can be
 ; C, U, or UC.
 S BPACTTYP=""
 I BPJOBFLG="F" S BPACTTYP=$$ACTTYPE^BPSOSRX5(BWHERE)
 ; If background/unqueueing job, then action can be C or U.
 I BPJOBFLG="B" D  I RESPONSE=5 G END
 . S BPACTTYP=$P($G(^BPS(9002313.77,+$G(BPREQIEN),1)),U,4)
 . I BPACTTYP="" S RESPONSE=5,CLMSTAT="Unknown Action type in BPS REQUEST ien="_BPREQIEN
 ;
 ; Code to handle "general" submit/reversal as opposed to processing a
 ; claim for a specific payer sequence (primary, secondary).  ECME and
 ; IB always know the payer sequence and should always set the proper
 ; BPCOBIND parameter, thus if BPCOBIND=0 then the API is called by
 ; Pharmacy.  If so then the CLAIM action (not reversal) should be done
 ; for primary only.
 ;
 S BPSQUIT=0
 I BPCOBIND=0 D  I BPSQUIT=1 S CLMSTAT="The secondary claim needs to be reversed first.",RESPONSE=5 G END
 . I BPACTTYP=""!(BPACTTYP="C") S BPCOBIND=1 Q
 . ; code to handle "general" reversal
 . N BPSECLM
 . ; Check if there is a secondary claim.
 . S BPSECLM=$$FINDECLM^BPSPRRX5(BRXIEN,BFILL,2)
 . ; Quit if we have a secondary claim and it is payable or in progress,
 . ; since that claim needs to be reversed first.
 . I BPSECLM=1!(BPSECLM=3) S BPSQUIT=1
 . S BPCOBIND=1
 ;
 ; If BPJOBFLG="F", then determine if there are any scheduled, active,
 ; or in process requests for the RX/RF.  CHKREQST^BPSOSRX7 returns
 ; negative number^message : cannot be accepted for some reason 
 ; 0 : can be accepted because there are NO requests for this RX/RF, 
 ;     we will create a new record in BPS REQUEST and ACTIVATE it.
 ; 1 : there are ACTIVATED/IN PROCESS requests already for this RX/RF
 S BPPREVRQ="-10^Background queuing." ;default
 I BPJOBFLG="F" D  I BPPREVRQ'=0 G STATUS:RESPONSE=0,END:RESPONSE>0
 . S BPPREVRQ=$$CHKREQST^BPSOSRX7(BRXIEN,BFILL,.BPRESLT)
 . D LOG^BPSOSL(IEN59,$T(+0)_"-CHKREQ^BPSOSRX7 result: "_BPPREVRQ)
 . ; if error
 . I BPPREVRQ<0 S RESPONSE=4,CLMSTAT=$P(BPPREVRQ,U,2) D LOG^BPSOSL(IEN59,$T(+0)_"- - Cannot be accepted because of issues with already scheduled requests")
 . ; if there are prior requests for the RX/RF in the queue already
 . ; then schedule additional request(s) for the future and quit since
 . ; we do not know the result of prior requests
 . I BPPREVRQ>0 D
 . . D LOG^BPSOSL(IEN59,$T(+0)_"-There are requests in the queue, do not process - schedule additional request(s)")
 . . I BPACTTYP="U" S BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$G(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BPPREVRQ,"U",.BPSCLOSE,$G(BPSRTYPE),$G(BPSPLAN),.BPSPRDAT,BPSDX)
 . . I BPACTTYP="UC" D
 . . . S BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$G(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BPPREVRQ,"U",.BPSCLOSE,$G(BPSRTYPE),$G(BPSPLAN),.BPSPRDAT,BPSDX)
 . . . I +BPRET=0 S BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$G(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BP77NEW,"C",.BPSCLOSE,$G(BPSRTYPE),$G(BPSPLAN),.BPSPRDAT,BPSDX)
 . . I BPACTTYP="C" S BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$G(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BPPREVRQ,"C",.BPSCLOSE,$G(BPSRTYPE),$G(BPSPLAN),.BPSPRDAT,BPSDX)
 . . I +BPRET=0 S RESPONSE=0,CLMSTAT=$P(BPRET,U,2) D LOG^BPSOSL(IEN59,$T(+0)_"-The new request(s) scheduled. The last one for the RX/RF now is: "_(BP77NEW)) Q
 . . I +BPRET>0 S RESPONSE=+BPRET,CLMSTAT=$P(BPRET,U,2) D LOG^BPSOSL(IEN59,$T(+0)_"-Cannot create request(s)")
 ;
 ; We can continue only if either 
 ;   BPJOBFLG="B" 
 ;     or 
 ;   BPJOBFLG="F" and BPPREVRQ=0
 ;
 ; BPNEWCLM flag = 0 if resubmission, 1 if first submission.
 S BPNEWCLM=$S(+$G(^BPST(IEN59,0)):0,1:1)
 ; get pre-existing RX/RFs status
 S OLDRESP=$P($$STATUS^BPSOSRX(BRXIEN,BFILL,0,,BPSCOB),U,1)
 ; check if the payer IS going to PAY according the last response
 S BPPAYABL=$$PAYABLE^BPSOSRX5(OLDRESP)
 ; set starttime
 S BPSTART=$$STTM^BPSNCPD4()
 ;  
 ; if this is a new RX/RF
 I BPNEWCLM D NEWCLAIM^BPSNCPD6 G STATUS:RESPONSE=0,END:RESPONSE>0
 ;
 ; if we do not have a status for the previous claim AND this is not a
 ; reversal request - treat it as a new claim this will be the case
 ; when resubmitting a non-billable entry
 I (OLDRESP=""),(BPACTTYP'="U") D NEWCLAIM^BPSNCPD6 G STATUS:RESPONSE=0,END:RESPONSE>0
 ;
 ; If we do not have a status for the pre-existing claim, and this is
 ; a reversal request, then do not reverse.
 I (OLDRESP=""),(BPACTTYP="U") D RVNEW^BPSNCPD6 G END
 ;
 ; From here on, all claims with some response (i.e. OLDRESP'="")
 ;
 ; if Back Billing - impossible
 I BWHERE="BB" D BB^BPSNCPD6 G END
 ;
 ; If returning to stock or deleting, and the previous claim was not
 ; paid, then no reversal is needed; close the prescription and quit.
 ; Note: this is inherited "fuzzy logic" - it checks only two statuses
 ; to determine that the claim "was not paid".
 I OLDRESP'["E PAYABLE",OLDRESP'["E REVERSAL REJECTED",(",RS,DE,"[(","_BWHERE_",")) D  G END
 . D CLOSE2^BPSBUTL(BRXIEN,BFILL,BWHERE)
 . S RESPONSE=3
 . S CLMSTAT="Claim was not payable so it has been closed.  No ECME claim created."
 . D DISPL^BPSNCPD4(WFLG,RESPONSE_U_CLMSTAT_"^D^2",$G(BPSELIG))
 . D LOG^BPSOSL(IEN59,$T(+0)_"-"_CLMSTAT)
 ;
 ; Reversals for Payable claims 
 ; (Note: BPSCLOSE can be used in this case only)
 I BPPAYABL,BPACTTYP="U" D RVPAID^BPSNCPD6 G STATUS:RESPONSE=0,END:RESPONSE>0
 ;
 ; Resubmission without doing a Reversal
 I BWHERE="ERWV" D RVRSNPD^BPSNCPD6 G STATUS:RESPONSE=0,END:RESPONSE>0
 ;
 ; Reversals and Resubmission for Payable claims
 I BPPAYABL,BPACTTYP="UC" D RVRSPAID^BPSNCPD6 G STATUS:((RESPONSE=0)!(RESPONSE=10)),END:RESPONSE>0
 ;
 ; Resubmission for Payable claims - DO NOT resubmit
 I BPPAYABL,BPACTTYP="C" D RSPAID^BPSNCPD6 G END
 ;
 ; Reversals for Non-Payable claims - DO NOT reverse
 I 'BPPAYABL,BPACTTYP="U" D RVNPAID^BPSNCPD6 G END
 ;
 ; Resubmission and Reversals and Resubmission for Non-Payable claims
 I 'BPPAYABL,((BPACTTYP="C")!(BPACTTYP="UC")) D RVRSNPD^BPSNCPD6 G STATUS:RESPONSE=0,END:RESPONSE>0
 ;
 S RESPONSE=5,CLMSTAT="Unknown error"
 G END
 ;
 ; Display status
STATUS ;
 ;if successful scheduling or/and activation of the request then make sure the background job is running
 I BPJOBFLG="F",BPLCK D UNLCKRF^BPSOSRX(BRXIEN,BFILL,$G(IEN59),$T(+0)) S BPLCK=0 ;to prevent unlocking in END
 I (RESPONSE=0)!(RESPONSE=10) D LOG^BPSOSL(IEN59,$T(+0)_"-Call RUNNING^BPSOSRX") D RUNNING^BPSOSRX()
 I WFLG W !!,"Processing ",$S(BPSCOB=1:"Primary claim...",BPSCOB=2:"Secondary claim...",1:"claim with Unknown Payer Sequence...")
 I BPJOBFLG="F" D
 . ; If the Write Flag is off and this is TRICARE/CHAMPVA, set Write
 . ; Flag to 2.  STATUS^BPSNCPD1 will not display messages but will
 . ; wait the same amount of time as if it were writing messages.  This
 . ; needs to be done so that TRICARE/CHAMPVA claims get a chance to
 . ; complete before continuing.  Otherwise, the claim will be IN
 . ; PROGRESS, which will create the bulletin (code below) and OP/CMOP
 . ; will not process correctly (keep on suspense queue, etc)
 . I 'WFLG,$G(BPSELIG)="T"!($G(BPSELIG)="C") S WFLG=2
 . I 'WFLG H 1
 . E  D STATUS^BPSNCPD1(BRXIEN,BFILL,+$G(BPPAYABL),$S(BPACTTYP="U":1,1:0),BPSTART,BWHERE,$G(BP77NEW),BPSCOB,$G(BPSELIG),IEN59,WFLG)
 ;
 ; Clean up and quit
END ;
 ; BPSELIG and other variables are established by inference in BPSNCPD6.
 I BPJOBFLG="F",BPLCK D UNLCKRF^BPSOSRX(BRXIEN,BFILL,$G(IEN59),$T(+0)) S BPLCK=0
 ; Get Site in case we send a Bulletin
 S SITE=$$GETSITE^BPSOSRX8(BRXIEN,BFILL)
 ; If foreground, and we can't schedule request for any reason, and
 ; this is not OP, then send a bulletin.
 I BPJOBFLG="F",RESPONSE=4,",AREV,BB,ERES,ERWV,ERNB,EREV,P2,P2S,"'[(","_BWHERE_",") D BULL^BPSNCPD1(BRXIEN,BFILL,$G(SITE),$G(DFN),$G(PNAME),"",$G(CLMSTAT),$G(RESPONSE),$G(BPSCOB))
 I $G(BPSELIG)="" S BPSELIG=""
 ; Send bulletin if TRICARE or CHAMPVA is IN PROGRESS and this is not
 ; a release process
 S BPSSTAT=$S($G(BRXIEN):$P($$STATUS^BPSOSRX(BRXIEN,BFILL,,,BPSCOB),U),1:"")
 ;
 I $G(IEN59) D
 . D LOG^BPSOSL(IEN59,$T(+0)_"-Nearing end of first process, BPSELIG="_BPSELIG_", BPSSTAT="_BPSSTAT)
 . I BPSELIG="T"!(BPSELIG="C"),BPSSTAT="IN PROGRESS",$G(REVREAS)'="RX RELEASE-NDC CHANGE",BWHERE="PC" D LOG^BPSOSL(IEN59,$T(+0)_"-Would have sent bulletin")
 ;
 I BPSELIG="T"!(BPSELIG="C"),BPSSTAT="IN PROGRESS",$G(REVREAS)'="RX RELEASE-NDC CHANGE",",CRLB,CRLR,CRLX,CRRL,PC,RRL,"'[(","_BWHERE_",") D BULL^BPSNCPD1(BRXIEN,BFILL,SITE,$G(DFN),$G(PNAME),BPSELIG,"","",$G(BPSCOB))
 ;
 I '$D(RESPONSE) S RESPONSE=1
 K MOREDATA
 I $G(IEN59) D
 . N MSG
 . S MSG="First Process Complete-RESPONSE="_$G(RESPONSE)
 . I $G(RESPONSE)'=0 S MSG=MSG_", CLMSTAT="_$G(CLMSTAT)
 . D LOG^BPSOSL(IEN59,$T(+0)_"-"_MSG)
 ;
 ; The function $$ECMESND returns the following:
 ;   Response ^ Message ^ Eligibility ^ Claim Status ^ COB ^
 ;   RxCOB ^ Insurance
 ; Response =
 ;   0  Submitted through ECME
 ;   1  No submission through ECME
 ;   2  IB not billable
 ;   3  Claim was closed, not submitted (RTS/Deletes)
 ;   4  Unable to queue claim
 ;   5  Incorrect information supplied to ECME
 ;   6  Inactive ECME - Primarily used for TRICARE/CHAMPVA to
 ;      say ok to process Rx
 ;   10 Reversal but no resubmit
 ; Message = Message indicating whether the claim was submitted
 ; Eligibility = V, Veteran;  T, TRICARE; C, CHAMPVA
 ; Claim Status = IN PROGRESS if incomplete; final status (e.g.
 ;   E PAYABLE or E REJECTED) if complete; null if non-billable
 ; COB  = COB indicator as stored in the INSURANCE TYPE sub-file
 ;   of the PATIENT file
 ; RxCOB = COB indicator sent to the payer and stored in the
 ;   BPS TRANSACTIONS file
 ; Insurance = Name of the insurance company that was billed as
 ;   a result of this call
 ;
 Q RESPONSE_U_$G(CLMSTAT)_U_BPSELIG_U_BPSSTAT_U_$$CLMINFO^BPSUTIL2(+$G(IEN59))
 ;
 ;BPSNCPDP
 
--- Routine Detail   --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HBPSNCPDP   18914     printed  Sep 23, 2025@19:27:32                                                                                                                                                                                                   Page 2
BPSNCPDP  ;BHAM ISC/LJE/SS - API to submit a claim to ECME ;11/7/07  16:58
 +1       ;;1.0;E CLAIMS MGMT ENGINE;**1,3,4,2,5,6,7,8,10,11,19,20,22,40**;JUN 2004;Build 25
 +2       ;;Per VA Directive 6402, this routine should not be modified.
 +3       ;
 +4       ; Reference to $$PROD^XUPROD in ICR #4440
 +5       ; Reference to $$GETNDC^PSONDCUT in ICR #4705
 +6       ; Reference to Patient file (#2) in ICR #10035
 +7       ;
 +8       ;Input
 +9       ; BRXIEN = Prescription IEN
 +10      ; BFILL  = Fill Number
 +11      ; DOS    = Date of Service
 +12      ; BWHERE (RX Action)
 +13      ;    AREV = Auto-Reversal
 +14      ;    BB   = Back Billing
 +15      ;    CRLB = CMOP/OPAI Release & Rebill
 +16      ;    CRLR = CMOP/OPAI Release & Reverse (successful release)
 +17      ;    CRLX = CMOP/OPAI unsuccessful release & reverse
 +18      ;    CRRL = CMOP/OPAI Release - Original claim not paid, submit
 +19      ;           another claim, no reversal
 +20      ;    DC   = Discontinue - only reverse un-released PAYABLE DC's,
 +21      ;           release date check should be in calling routine.
 +22      ;    DE   = Delete
 +23      ;    ED   = Edit (includes RX release with NDC edit)
 +24      ;    ERES = Resubmit from ECME user screen
 +25      ;    ERWV = Resubmit Without Reversal from ECME user screen
 +26      ;    ERNB = Resubmit of a TRI/CVA non-billable entry from the ECME
 +27      ;           user screen
 +28      ;    EREV = Reversal from ECME user screen
 +29      ;    HLD  = Put prescription on Hold
 +30      ;    OREV = Reversal from Outpatient Pharmacy edit screen
 +31      ;    RSNB = Resubmit Non-Billable TRICARE & CHAMPVA from PSO Reject
 +32      ;           Info Screen
 +33      ;    OF   = Original Fill
 +34      ;    P2   = Original submission from PRO Option, no reversal
 +35      ;    P2S  = Resubmit from PRO Option
 +36      ;    PC   = Pull CMOPs
 +37      ;    PE   = Pull early from suspense
 +38      ;    PL   = Pull local from suspense
 +39      ;    PP   = Pull RX (PP) action from Patient Rx Processing option
 +40      ;    RF   = Refill
 +41      ;    RN   = Renew
 +42      ;    RRL  = Release - Original claim not paid, submit another claim,
 +43      ;           no reversal
 +44      ;    RS   = Return-to-Stock
 +45      ; BILLNDC = Valid NDC# with format 5-4-2
 +46      ; REVREAS = Reversal Reason
 +47      ; DURREC  = String of up to three sets of DUR info. Sets are delimited
 +48      ;           with "~". Each set consists of three "^" pieces:
 +49      ;              Reason for Service Code
 +50      ;              Professional Service Code
 +51      ;              Result of Service Code
 +52      ; BPOVRIEN = Pointer to BPS NCPDP OVERIDE file.  This parameter will 
 +53      ;            only be passed if there are overrides entered by the
 +54      ;            user via the Resubmit with Edits (RED) option in the 
 +55      ;            user screen.
 +56      ; BPSAUTH  = Prior authorization code ^ Prior authorization number
 +57      ; BPSCLARF = Submission Clarification Code (external value from file
 +58      ;            #9002313.25), entered by pharmacist and passed by
 +59      ;            Outpatient Pharmacy to ECME to put into the claim  
 +60      ; BPCOBIND = (optional, default is Primary) for COB indicators - so
 +61      ;            when the API is called for the particular
 +62      ;            COB claim the BPSNCPDP can handle it.
 +63      ; BPJOBFLG = (optional) B - if called by unqueueing logic in background
 +64      ;            F - (default) if called by other (foreground) process
 +65      ; BPREQIEN = (optional) IEN to file #9002313.77, BPS REQUEST, that
 +66      ;            needs to be unqueued 
 +67      ; BPSCLOSE = (optional) local array used with BWHERE="EREV" only, if
 +68      ;            the user had chosen to close the claim after reversal
 +69      ;   If claim needs to be closed then
 +70      ;      BPSCLOSE("CLOSE AFT REV")=1
 +71      ;      BPSCLOSE("CLOSE AFT REV REASON")=<#356.8 ien>
 +72      ;      BPSCLOSE("CLOSE AFT REV COMMENT")=<some text>
 +73      ; BPSPLAN  = (optional) IEN of the entry in file #355.3, GROUP
 +74      ;            INSURANCE PLAN
 +75      ; BPSPRDAT = (optional) local array passed by reference.  Contains
 +76      ;            primary claim data needed to submit a secondary claim.
 +77      ;            Format:  BPSPRDAT(NCPDP field)
 +78      ; BPSRTYPE = (optional) rate type (IEN to file #399.3)
 +79      ; BPSDELAY = Delay Reason Code (IEN to file #9002313.29, BPS NCPDP
 +80      ;            DELAY REASON CODE, entered by user in the Back Billing
 +81      ;            option of Claims Tracking, passed in to be put in claim.
 +82      ; BPSDX    = Diagnosis code (external) to be sent on outgoing claim.
 +83      ; 
 +84      ;Output (RESPONSE^MESSAGE^ELIGIBILITY^CLAIMSTATUS^COB^RXCOB^INSURANCE)
 +85      ; RESPONSE
 +86      ;    0  Submitted through ECME
 +87      ;    1  No submission through ECME
 +88      ;    2  IB not billable
 +89      ;    3  Claim was closed, not submitted (RTS/Deletes)
 +90      ;    4  Unable to queue claim
 +91      ;    5  Incorrect information supplied to ECME
 +92      ;    6  Inactive ECME - Primarily used for TRICARE/CHAMPVA to say
 +93      ;       ok to process rx
 +94      ;    10 Reversal but no resubmit
 +95      ; MESSAGE = Message associated with the response (error/submitted)
 +96      ; ELIGIBILITY = V - Veteran, T - TRICARE, C - CHAMPVA
 +97      ; CLAIMSTATUS = claim status (null or IN PROGRESS/E PAYABLE/etc...)
 +98      ; COB = Coordination Of Benefit indicator of the insurance as it is
 +99      ;       stored in the PATIENT file: 1-primary, 2-secondary, 3-tertiary
 +100     ; RXCOB = the payer sequence indicator of the claim which was sent to
 +101     ;         the payer as a result of this call: 1-primary, 2-secondary)
 +102     ; INSURANCE = Name of the insurance company the claim was sent to
 +103     ;
EN(BRXIEN,BFILL,DOS,BWHERE,BILLNDC,REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPCOBIND,BPJOBFLG,BPREQIEN,BPSCLOSE,BPSPLAN,BPSPRDAT,BPSRTYPE,BPSDELAY,BPSDX) ;
 +1       ;
 +2        NEW BP77NEW,BPACTTYP,BPLCK,BPNEWCLM,BPPAYABL,BPPREVRQ,BPRESLT,BPRET
 +3        NEW BPRETV,BPSCOB,BPSELIG,BPSQUIT,BPSSTAT,BPSTART,BRX,CLMSTAT,DFN,IEN59
 +4        NEW OLDRESP,PNAME,RESPONSE,SITE,TODAY,WFLG
 +5       ;
 +6       ; test not ecme active
 +7        IF '$$PROD^XUPROD
               IF '$PIECE($GET(^BPS(9002313.99,1,0)),"^",3)
                   QUIT "1^ECME switch is not on for the site"
 +8       ; Set default values and other required vars
 +9       ; default is foreground ("F")
 +10       SET BPJOBFLG=$SELECT($GET(BPJOBFLG)="":"F",1:$GET(BPJOBFLG))
 +11       SET RESPONSE=""
           SET CLMSTAT=""
           SET BP77NEW=0
 +12      ;0 - default for "B" jobs
           SET BPLCK=0
 +13       SET REVREAS=$GET(REVREAS)
           SET DURREC=$GET(DURREC)
 +14       SET BPSCLARF=$GET(BPSCLARF)
           SET BPSAUTH=$GET(BPSAUTH)
 +15       SET BPOVRIEN=$GET(BPOVRIEN)
           SET BPSDELAY=$GET(BPSDELAY)
 +16       SET BPSDX=$GET(BPSDX)
 +17      ; BPCOBIND will be used as a flag to indicate the following
 +18      ;    If BPCOBIND>0 then the API is called for the particular COB claim
 +19      ;    if BPCOBIND=0 then the API is called for a whole RX/RF -
 +20      ;         Outpatient Pharmacy doesn't care about COB when the
 +21      ;         pharmacy user enters, deletes or edits RX/refills
 +22       SET BPCOBIND=+$GET(BPCOBIND)
 +23      ;
 +24      ; BPSCOB variable will be used to store COB value (default is PRIMARY)
 +25      ; in this function only
 +26       SET BPSCOB=$SELECT(BPCOBIND>0:BPCOBIND,1:1)
 +27      ;
 +28      ; Remove semi-colons from reversal reason
 +29       SET REVREAS=$TRANSLATE(REVREAS,";","-")
 +30      ;
 +31      ; Default is original fill
 +32       SET BRXIEN=$GET(BRXIEN)
 +33       IF '$GET(BFILL)
               SET BFILL=0
 +34      ;
 +35      ; Get prescription number
 +36       SET BRX=$$RXAPI1^BPSUTIL1(BRXIEN,.01,"I")
 +37      ;
 +38      ; Make sure fill date is not in the future or empty
 +39       SET TODAY=$$DT^XLFDT
 +40       IF '$GET(DOS)!($GET(DOS)>TODAY)
               SET DOS=$$DOSDATE^BPSSCRRS(BRXIEN,BFILL)
 +41      ;
 +42      ; Get the NDC if it was not passed in
 +43       IF $GET(BILLNDC)=""
               SET BILLNDC=$$GETNDC^PSONDCUT(BRXIEN,BFILL)
 +44      ;
 +45      ; Patient Info
 +46       SET DFN=$$RXAPI1^BPSUTIL1(BRXIEN,2,"I")
           SET PNAME=$$GET1^DIQ(2,DFN,.01)
 +47      ;
 +48      ; Check parameters and vars
 +49       SET BPRETV=$$CHCKPAR^BPSOSRX8(BRXIEN,BRX,$GET(BWHERE),DFN,PNAME)
 +50       IF +BPRETV=0
               SET CLMSTAT=$PIECE(BPRETV,U,2)
               SET RESPONSE=5
               GOTO END
 +51      ;
 +52      ; Calculate IEN59
 +53       SET IEN59=$$IEN59^BPSOSRX(BRXIEN,BFILL,BPSCOB)
 +54       IF IEN59=""
               SET CLMSTAT="BPS Transaction IEN could not be calculated"
               SET RESPONSE=1
               GOTO END
 +55      ;
 +56      ; Populate COB fields from BPS TRANSACTION to resubmit secondary
 +57      ; claims from the User Screen.  If $G(BPSPRDAT("NEW COB DATA"))=1 then
 +58      ; the resubmit requested from the BPS COB PROCESS SECOND TRICARE and
 +59      ; the user can change the data.
 +60       IF BPSCOB=2
               IF $$ACTTYPE^BPSOSRX5(BWHERE)="UC"
                   IF '$GET(BPSPRDAT("NEW COB DATA"))
                       if $DATA(BPSRTYPE)=0
                           NEW BPSRTYPE
                       if $DATA(BPSPLAN)=0
                           NEW BPSPLAN
                       if $DATA(BPSPRDAT)=0
                           NEW BPSPRDAT
                       IF $$SECDATA^BPSPRRX6(BRXIEN,BFILL,.BPSPLAN,.BPSPRDAT,.BPSRTYPE)=0
                           Begin DoDot:1
 +61                           SET CLMSTAT="Insufficient data to resubmit the secondary claim, use Process Secondary/TRICARE Rx to ECME option."
                               SET RESPONSE=5
                           End DoDot:1
                           GOTO END
 +62      ;
 +63      ; Initialize log
 +64       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Start of claim","DT")
 +65       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-BWHERE = "_BWHERE)
 +66       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Job flag = "_BPJOBFLG_$SELECT(BPJOBFLG="B":" BPS REQUEST ien = "_$GET(BPREQIEN),1:""))
 +67      ;
 +68      ; Check if we need to print the messages to the screen (WFLG=1 : YES)
 +69       SET WFLG=0
 +70       IF BPJOBFLG="F"
               SET WFLG=$$PRINTSCR^BPSOSRX8(BWHERE)
 +71      ;
 +72      ; Lock the Rx and Fill while putting it on the queue to prevent two
 +73      ; jobs from being activated at the same time.  This is only for
 +74      ; foreground jobs.  Background jobs are called from REQST99^BPSOSRX5
 +75      ; and the RX/RF should be already locked by this point.
 +76       IF BPJOBFLG="F"
               Begin DoDot:1
 +77               SET BPLCK=$$LOCKRF^BPSOSRX(BRXIEN,BFILL,10,$GET(IEN59),$TEXT(+0))
 +78               IF 'BPLCK
                       SET RESPONSE=4
                       SET CLMSTAT="Unable to acquire the lock needed to put the RX and fill on the queue"
               End DoDot:1
               IF 'BPLCK
                   GOTO END
 +79      ;
 +80      ; Determine the action type.  If foreground job, then action can be
 +81      ; C, U, or UC.
 +82       SET BPACTTYP=""
 +83       IF BPJOBFLG="F"
               SET BPACTTYP=$$ACTTYPE^BPSOSRX5(BWHERE)
 +84      ; If background/unqueueing job, then action can be C or U.
 +85       IF BPJOBFLG="B"
               Begin DoDot:1
 +86               SET BPACTTYP=$PIECE($GET(^BPS(9002313.77,+$GET(BPREQIEN),1)),U,4)
 +87               IF BPACTTYP=""
                       SET RESPONSE=5
                       SET CLMSTAT="Unknown Action type in BPS REQUEST ien="_BPREQIEN
               End DoDot:1
               IF RESPONSE=5
                   GOTO END
 +88      ;
 +89      ; Code to handle "general" submit/reversal as opposed to processing a
 +90      ; claim for a specific payer sequence (primary, secondary).  ECME and
 +91      ; IB always know the payer sequence and should always set the proper
 +92      ; BPCOBIND parameter, thus if BPCOBIND=0 then the API is called by
 +93      ; Pharmacy.  If so then the CLAIM action (not reversal) should be done
 +94      ; for primary only.
 +95      ;
 +96       SET BPSQUIT=0
 +97       IF BPCOBIND=0
               Begin DoDot:1
 +98               IF BPACTTYP=""!(BPACTTYP="C")
                       SET BPCOBIND=1
                       QUIT 
 +99      ; code to handle "general" reversal
 +100              NEW BPSECLM
 +101     ; Check if there is a secondary claim.
 +102              SET BPSECLM=$$FINDECLM^BPSPRRX5(BRXIEN,BFILL,2)
 +103     ; Quit if we have a secondary claim and it is payable or in progress,
 +104     ; since that claim needs to be reversed first.
 +105              IF BPSECLM=1!(BPSECLM=3)
                       SET BPSQUIT=1
 +106              SET BPCOBIND=1
               End DoDot:1
               IF BPSQUIT=1
                   SET CLMSTAT="The secondary claim needs to be reversed first."
                   SET RESPONSE=5
                   GOTO END
 +107     ;
 +108     ; If BPJOBFLG="F", then determine if there are any scheduled, active,
 +109     ; or in process requests for the RX/RF.  CHKREQST^BPSOSRX7 returns
 +110     ; negative number^message : cannot be accepted for some reason 
 +111     ; 0 : can be accepted because there are NO requests for this RX/RF, 
 +112     ;     we will create a new record in BPS REQUEST and ACTIVATE it.
 +113     ; 1 : there are ACTIVATED/IN PROCESS requests already for this RX/RF
 +114     ;default
           SET BPPREVRQ="-10^Background queuing."
 +115      IF BPJOBFLG="F"
               Begin DoDot:1
 +116              SET BPPREVRQ=$$CHKREQST^BPSOSRX7(BRXIEN,BFILL,.BPRESLT)
 +117              DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-CHKREQ^BPSOSRX7 result: "_BPPREVRQ)
 +118     ; if error
 +119              IF BPPREVRQ<0
                       SET RESPONSE=4
                       SET CLMSTAT=$PIECE(BPPREVRQ,U,2)
                       DO LOG^BPSOSL(IEN59,$TEXT(+0)_"- - Cannot be accepted because of issues with already scheduled requests")
 +120     ; if there are prior requests for the RX/RF in the queue already
 +121     ; then schedule additional request(s) for the future and quit since
 +122     ; we do not know the result of prior requests
 +123              IF BPPREVRQ>0
                       Begin DoDot:2
 +124                      DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-There are requests in the queue, do not process - schedule additional request(s)")
 +125                      IF BPACTTYP="U"
                               SET BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$GET(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BPPREVRQ,"U",.BPSCLOSE,$GET(BPSRTYPE),$GET(BPSPLAN),.BPSPRDAT,BPSDX)
 +126                      IF BPACTTYP="UC"
                               Begin DoDot:3
 +127                              SET BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$GET(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BPPREVRQ,"U",.BPSCLOSE,$GET(BPSRTYPE),$GET(BPSPLAN),.BPSPRDAT,BPSDX)
 +128                              IF +BPRET=0
                                       SET BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$GET(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BP77NEW,"C",.BPSCLOSE,$GET(BPSRTYPE),$GET(BPSPLAN),.BPSPRDAT,BPSDX)
                               End DoDot:3
 +129                      IF BPACTTYP="C"
                               SET BPRET=$$SCHREQ^BPSNCPD5(.BP77NEW,BRXIEN,BFILL,DOS,BWHERE,$GET(BILLNDC),REVREAS,DURREC,BPOVRIEN,BPSCLARF,BPSAUTH,BPSDELAY,IEN59,BPCOBIND,BPPREVRQ,"C",.BPSCLOSE,$GET(BPSRTYPE),$GET(BPSPLAN),.BPSPRDAT,BPSDX)
 +130                      IF +BPRET=0
                               SET RESPONSE=0
                               SET CLMSTAT=$PIECE(BPRET,U,2)
                               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-The new request(s) scheduled. The last one for the RX/RF now is: "_(BP77NEW))
                               QUIT 
 +131                      IF +BPRET>0
                               SET RESPONSE=+BPRET
                               SET CLMSTAT=$PIECE(BPRET,U,2)
                               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Cannot create request(s)")
                       End DoDot:2
               End DoDot:1
               IF BPPREVRQ'=0
                   if RESPONSE=0
                       GOTO STATUS
                   if RESPONSE>0
                       GOTO END
 +132     ;
 +133     ; We can continue only if either 
 +134     ;   BPJOBFLG="B" 
 +135     ;     or 
 +136     ;   BPJOBFLG="F" and BPPREVRQ=0
 +137     ;
 +138     ; BPNEWCLM flag = 0 if resubmission, 1 if first submission.
 +139      SET BPNEWCLM=$SELECT(+$GET(^BPST(IEN59,0)):0,1:1)
 +140     ; get pre-existing RX/RFs status
 +141      SET OLDRESP=$PIECE($$STATUS^BPSOSRX(BRXIEN,BFILL,0,,BPSCOB),U,1)
 +142     ; check if the payer IS going to PAY according the last response
 +143      SET BPPAYABL=$$PAYABLE^BPSOSRX5(OLDRESP)
 +144     ; set starttime
 +145      SET BPSTART=$$STTM^BPSNCPD4()
 +146     ;  
 +147     ; if this is a new RX/RF
 +148      IF BPNEWCLM
               DO NEWCLAIM^BPSNCPD6
               if RESPONSE=0
                   GOTO STATUS
               if RESPONSE>0
                   GOTO END
 +149     ;
 +150     ; if we do not have a status for the previous claim AND this is not a
 +151     ; reversal request - treat it as a new claim this will be the case
 +152     ; when resubmitting a non-billable entry
 +153      IF (OLDRESP="")
               IF (BPACTTYP'="U")
                   DO NEWCLAIM^BPSNCPD6
                   if RESPONSE=0
                       GOTO STATUS
                   if RESPONSE>0
                       GOTO END
 +154     ;
 +155     ; If we do not have a status for the pre-existing claim, and this is
 +156     ; a reversal request, then do not reverse.
 +157      IF (OLDRESP="")
               IF (BPACTTYP="U")
                   DO RVNEW^BPSNCPD6
                   GOTO END
 +158     ;
 +159     ; From here on, all claims with some response (i.e. OLDRESP'="")
 +160     ;
 +161     ; if Back Billing - impossible
 +162      IF BWHERE="BB"
               DO BB^BPSNCPD6
               GOTO END
 +163     ;
 +164     ; If returning to stock or deleting, and the previous claim was not
 +165     ; paid, then no reversal is needed; close the prescription and quit.
 +166     ; Note: this is inherited "fuzzy logic" - it checks only two statuses
 +167     ; to determine that the claim "was not paid".
 +168      IF OLDRESP'["E PAYABLE"
               IF OLDRESP'["E REVERSAL REJECTED"
                   IF (",RS,DE,"[(","_BWHERE_","))
                       Begin DoDot:1
 +169                      DO CLOSE2^BPSBUTL(BRXIEN,BFILL,BWHERE)
 +170                      SET RESPONSE=3
 +171                      SET CLMSTAT="Claim was not payable so it has been closed.  No ECME claim created."
 +172                      DO DISPL^BPSNCPD4(WFLG,RESPONSE_U_CLMSTAT_"^D^2",$GET(BPSELIG))
 +173                      DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-"_CLMSTAT)
                       End DoDot:1
                       GOTO END
 +174     ;
 +175     ; Reversals for Payable claims 
 +176     ; (Note: BPSCLOSE can be used in this case only)
 +177      IF BPPAYABL
               IF BPACTTYP="U"
                   DO RVPAID^BPSNCPD6
                   if RESPONSE=0
                       GOTO STATUS
                   if RESPONSE>0
                       GOTO END
 +178     ;
 +179     ; Resubmission without doing a Reversal
 +180      IF BWHERE="ERWV"
               DO RVRSNPD^BPSNCPD6
               if RESPONSE=0
                   GOTO STATUS
               if RESPONSE>0
                   GOTO END
 +181     ;
 +182     ; Reversals and Resubmission for Payable claims
 +183      IF BPPAYABL
               IF BPACTTYP="UC"
                   DO RVRSPAID^BPSNCPD6
                   if ((RESPONSE=0)!(RESPONSE=10))
                       GOTO STATUS
                   if RESPONSE>0
                       GOTO END
 +184     ;
 +185     ; Resubmission for Payable claims - DO NOT resubmit
 +186      IF BPPAYABL
               IF BPACTTYP="C"
                   DO RSPAID^BPSNCPD6
                   GOTO END
 +187     ;
 +188     ; Reversals for Non-Payable claims - DO NOT reverse
 +189      IF 'BPPAYABL
               IF BPACTTYP="U"
                   DO RVNPAID^BPSNCPD6
                   GOTO END
 +190     ;
 +191     ; Resubmission and Reversals and Resubmission for Non-Payable claims
 +192      IF 'BPPAYABL
               IF ((BPACTTYP="C")!(BPACTTYP="UC"))
                   DO RVRSNPD^BPSNCPD6
                   if RESPONSE=0
                       GOTO STATUS
                   if RESPONSE>0
                       GOTO END
 +193     ;
 +194      SET RESPONSE=5
           SET CLMSTAT="Unknown error"
 +195      GOTO END
 +196     ;
 +197     ; Display status
STATUS    ;
 +1       ;if successful scheduling or/and activation of the request then make sure the background job is running
 +2       ;to prevent unlocking in END
           IF BPJOBFLG="F"
               IF BPLCK
                   DO UNLCKRF^BPSOSRX(BRXIEN,BFILL,$GET(IEN59),$TEXT(+0))
                   SET BPLCK=0
 +3        IF (RESPONSE=0)!(RESPONSE=10)
               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Call RUNNING^BPSOSRX")
               DO RUNNING^BPSOSRX()
 +4        IF WFLG
               WRITE !!,"Processing ",$SELECT(BPSCOB=1:"Primary claim...",BPSCOB=2:"Secondary claim...",1:"claim with Unknown Payer Sequence...")
 +5        IF BPJOBFLG="F"
               Begin DoDot:1
 +6       ; If the Write Flag is off and this is TRICARE/CHAMPVA, set Write
 +7       ; Flag to 2.  STATUS^BPSNCPD1 will not display messages but will
 +8       ; wait the same amount of time as if it were writing messages.  This
 +9       ; needs to be done so that TRICARE/CHAMPVA claims get a chance to
 +10      ; complete before continuing.  Otherwise, the claim will be IN
 +11      ; PROGRESS, which will create the bulletin (code below) and OP/CMOP
 +12      ; will not process correctly (keep on suspense queue, etc)
 +13               IF 'WFLG
                       IF $GET(BPSELIG)="T"!($GET(BPSELIG)="C")
                           SET WFLG=2
 +14               IF 'WFLG
                       HANG 1
 +15              IF '$TEST
                       DO STATUS^BPSNCPD1(BRXIEN,BFILL,+$GET(BPPAYABL),$SELECT(BPACTTYP="U":1,1:0),BPSTART,BWHERE,$GET(BP77NEW),BPSCOB,$GET(BPSELIG),IEN59,WFLG)
               End DoDot:1
 +16      ;
 +17      ; Clean up and quit
END       ;
 +1       ; BPSELIG and other variables are established by inference in BPSNCPD6.
 +2        IF BPJOBFLG="F"
               IF BPLCK
                   DO UNLCKRF^BPSOSRX(BRXIEN,BFILL,$GET(IEN59),$TEXT(+0))
                   SET BPLCK=0
 +3       ; Get Site in case we send a Bulletin
 +4        SET SITE=$$GETSITE^BPSOSRX8(BRXIEN,BFILL)
 +5       ; If foreground, and we can't schedule request for any reason, and
 +6       ; this is not OP, then send a bulletin.
 +7        IF BPJOBFLG="F"
               IF RESPONSE=4
                   IF ",AREV,BB,ERES,ERWV,ERNB,EREV,P2,P2S,"'[(","_BWHERE_",")
                       DO BULL^BPSNCPD1(BRXIEN,BFILL,$GET(SITE),$GET(DFN),$GET(PNAME),"",$GET(CLMSTAT),$GET(RESPONSE),$GET(BPSCOB))
 +8        IF $GET(BPSELIG)=""
               SET BPSELIG=""
 +9       ; Send bulletin if TRICARE or CHAMPVA is IN PROGRESS and this is not
 +10      ; a release process
 +11       SET BPSSTAT=$SELECT($GET(BRXIEN):$PIECE($$STATUS^BPSOSRX(BRXIEN,BFILL,,,BPSCOB),U),1:"")
 +12      ;
 +13       IF $GET(IEN59)
               Begin DoDot:1
 +14               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Nearing end of first process, BPSELIG="_BPSELIG_", BPSSTAT="_BPSSTAT)
 +15               IF BPSELIG="T"!(BPSELIG="C")
                       IF BPSSTAT="IN PROGRESS"
                           IF $GET(REVREAS)'="RX RELEASE-NDC CHANGE"
                               IF BWHERE="PC"
                                   DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-Would have sent bulletin")
               End DoDot:1
 +16      ;
 +17       IF BPSELIG="T"!(BPSELIG="C")
               IF BPSSTAT="IN PROGRESS"
                   IF $GET(REVREAS)'="RX RELEASE-NDC CHANGE"
                       IF ",CRLB,CRLR,CRLX,CRRL,PC,RRL,"'[(","_BWHERE_",")
                           DO BULL^BPSNCPD1(BRXIEN,BFILL,SITE,$GET(DFN),$GET(PNAME),BPSELIG,"","",$GET(BPSCOB))
 +18      ;
 +19       IF '$DATA(RESPONSE)
               SET RESPONSE=1
 +20       KILL MOREDATA
 +21       IF $GET(IEN59)
               Begin DoDot:1
 +22               NEW MSG
 +23               SET MSG="First Process Complete-RESPONSE="_$GET(RESPONSE)
 +24               IF $GET(RESPONSE)'=0
                       SET MSG=MSG_", CLMSTAT="_$GET(CLMSTAT)
 +25               DO LOG^BPSOSL(IEN59,$TEXT(+0)_"-"_MSG)
               End DoDot:1
 +26      ;
 +27      ; The function $$ECMESND returns the following:
 +28      ;   Response ^ Message ^ Eligibility ^ Claim Status ^ COB ^
 +29      ;   RxCOB ^ Insurance
 +30      ; Response =
 +31      ;   0  Submitted through ECME
 +32      ;   1  No submission through ECME
 +33      ;   2  IB not billable
 +34      ;   3  Claim was closed, not submitted (RTS/Deletes)
 +35      ;   4  Unable to queue claim
 +36      ;   5  Incorrect information supplied to ECME
 +37      ;   6  Inactive ECME - Primarily used for TRICARE/CHAMPVA to
 +38      ;      say ok to process Rx
 +39      ;   10 Reversal but no resubmit
 +40      ; Message = Message indicating whether the claim was submitted
 +41      ; Eligibility = V, Veteran;  T, TRICARE; C, CHAMPVA
 +42      ; Claim Status = IN PROGRESS if incomplete; final status (e.g.
 +43      ;   E PAYABLE or E REJECTED) if complete; null if non-billable
 +44      ; COB  = COB indicator as stored in the INSURANCE TYPE sub-file
 +45      ;   of the PATIENT file
 +46      ; RxCOB = COB indicator sent to the payer and stored in the
 +47      ;   BPS TRANSACTIONS file
 +48      ; Insurance = Name of the insurance company that was billed as
 +49      ;   a result of this call
 +50      ;
 +51       QUIT RESPONSE_U_$GET(CLMSTAT)_U_BPSELIG_U_BPSSTAT_U_$$CLMINFO^BPSUTIL2(+$GET(IEN59))
 +52      ;
 +53      ;BPSNCPDP