 ; ACSPNT2.m --> PROCESS RECEIVED HL7 MESSAGE (ADT-A31) FROM ACS COMPUMD.
 ; ...continued from ACSPNT.m to process insurance company
 ;
 ; DBS CALLS:
 ; D FILE^DIE   REPLACES EXISTING RECORDS
 ; D UPDATE^DIE ADDS NEW RECORDS
 ;
 ; GLOBAL/LOCAL VARIABLES:
 ; COUNT        COUNTER FOR INSURANCE INDEX
 ; INSRESULT    RESULT FROM INSURANCE SEARCH
 ; INSFINDERR   ERR FROM INSURANCE SEARCH
 ; INSROOT      FDA_ROOT FOR INSURANCE DBS CALLS
 ; VALINSROOT   VALIDATED INSROOT
 ; INSERR       ERR MESSAGES FROM INSURANCE DBS CALLS
 ; INSIEN       IEN FOR DBS CALLS
 ; ^CHECK       TEMP STORAGE FOR ERR MESSAGES
 ;
 ; COMMENTS:
 ; ;)           TEMPORARY COMMENT/LINE OF CODE
 ;
 ; FILEMAN FILES:
 ; ^DIC(36,     INSURANCE COMPANY FILE (#36)
 ;
 ; last update 5.6.2007 1128
 ;
EN  ;entry point, init
 N COUNT
 N INSRESULT,INSFINDERR
 N VALINSROOT
 S COUNT=0
LOOP    ;loop through each INS segment
 I COUNT>INSCOUNT G REXIT
FINDINS ;
 D FIND^DIC(36,,"@","X",INSSEG(COUNT,"INS"),,"ACSINSNO",,,"INSRESULT","INSFINDERR")
 M ^CHECK("4INSFINDERR")=INSFINDERR ;)
 M ^CHECK("4INSFINDRESULT")=INSRESULT ;)
 I $P(INSRESULT("DILIST",0),"^",1)=0 G DOUPDATEDIE ;if no entries for ACSINSNO found, go to update (add)
 ;fall through if insurance company found
 ;
DOFILEDIE   ;
 N INSROOT,INSERR
 N INSIEN
 S INSIEN=INSRESULT("DILIST",2,1)_"," ;set IEN from returned array plus comma
 S INSROOT(36,INSIEN,.01)=$G(INSSEG(COUNT,"NAME"))
 S INSROOT(36,INSIEN,.05)="NO" ;inactive flag
 S INSROOT(36,INSIEN,.111)=$G(INSSEG(COUNT,"ADDR1"))
 S INSROOT(36,INSIEN,.112)=$G(INSSEG(COUNT,"ADDR2"))
 S INSROOT(36,INSIEN,.114)=$G(INSSEG(COUNT,"CITY"))
 S INSROOT(36,INSIEN,.115)=$G(INSSEG(COUNT,"STATE"))
 S INSROOT(36,INSIEN,.116)=$G(INSSEG(COUNT,"ZIP"))
 S INSROOT(36,INSIEN,.131)=$G(INSSEG(COUNT,"PHONE"))
 S INSROOT(36,INSIEN,1)="Y" ;REIMBURSE?
 S INSROOT(36,INSIEN,2)="0" ;SIGNATURE REQUIRED ON BILL?
 S INSIEN=INSRESULT("DILIST",2,1)
 ;do validation
 D VALS^DIE("","INSROOT","VALINSROOT","VALERR")
 N INDEX,ERRNUM,ERRCOUNT
 S INDEX=""
 S ERRCOUNT=1
 F  S INDEX=$O(VALINSROOT(36,INSIEN_",",INDEX)) Q:INDEX=""  D
 . I VALINSROOT(36,INSIEN_",",INDEX)="^" S ERRNUM=$P(VALERR("DIERR"),"^",1)
 . I  S ^ACSERR(ERRDATE,"ACSPNT2","FILE ERRNUM")=ERRNUM
 . I  D LOGERR
 . I  Q
 K INDEX,ERRNUM,ERRCOUNT
 ;
 ;begin file locks
FILELOCKDIC ;
 L +^DIC(36,INSIEN):1 ;try lock
 I $T G DOFILEDIEFILER ;if lock, continue
 E  G FILELOCKDIC ;if lock fails, keep trying
 ;end locks
 ;
DOFILEDIEFILER  ;
 D FILE^DIE("S","VALINSROOT","INSERR")
 L -^DIC(36,INSIEN)
 K INSROOT,INSERR
 K INSIEN
 G NEXTSEGMENT
DOUPDATEDIE ;
 N INSROOT,INSERR
 N INSIEN
 S INSROOT(36,"?+1,",.01)=$G(INSSEG(COUNT,"NAME"))
 S INSROOT(36,"?+1,",.05)="NO" ;inactive flag
 S INSROOT(36,"?+1,",.111)=$G(INSSEG(COUNT,"ADDR1"))
 S INSROOT(36,"?+1,",.112)=$G(INSSEG(COUNT,"ADDR2"))
 S INSROOT(36,"?+1,",.114)=$G(INSSEG(COUNT,"CITY"))
 S INSROOT(36,"?+1,",.115)=$G(INSSEG(COUNT,"STATE"))
 S INSROOT(36,"?+1,",.116)=$G(INSSEG(COUNT,"ZIP"))
 S INSROOT(36,"?+1,",.131)=$G(INSSEG(COUNT,"PHONE"))
 S INSROOT(36,"?+1,",1)="Y" ;REIMBURSE?
 S INSROOT(36,"?+1,",2)="0" ;SIGNATURE REQUIRED ON BILL?
 ;do validation
 D VALS^DIE("","INSROOT","VALINSROOT","VALERR")
 N INDEX,ERRCOUNT,ERRNUM
 S INDEX=""
 S ERRCOUNT=1
 F  S INDEX=$O(VALINSROOT(36,"?+1,",INDEX)) Q:INDEX=""  D
 . I VALINSROOT(36,"?+1,",INDEX)="^" S ERRNUM=$P(VALERR("DIERR"),"^",1)
 . I  S ^ACSERR(ERRDATE,"ACSPNT2","UPDATE ERRNUM")=ERRNUM
 . I  D LOGERR
 . I  S VALFLAG=1
 . I  Q
 K INDEX,ERRCOUNT,ERRNUM
 I VALFLAG Q ;if validation fails, return to ACSPNT
DOUPDATEDIEFILER    ;
 ;)S INSERR("DIERR")=""
 D UPDATE^DIE("S","VALINSROOT","INSIEN","INSERR")
 I $G(INSIEN(1))'="" S ^DIC(36,"ACSINSNO",INSSEG(COUNT,"INS"),INSIEN(1))="ACTIVE" ;set up my cross-reference
 K INSROOT,INSERR
 K INSIEN
NEXTSEGMENT ;
 S COUNT=COUNT+1
 G LOOP
REXIT   ;
 K COUNT
 K INSRESULT,INSFINDERR
 K VALINSROOT
 Q
LOGERR  ;
 F  Q:ERRCOUNT>ERRNUM  D
 . M ^ACSERR(ERRDATE,"ERR TEXT")=VALERR("DIERR",ERRCOUNT,"TEXT")
 . S ERRCOUNT=ERRCOUNT+1
 Q