Home   Package List   Routine Alphabetical List   Global Alphabetical List   FileMan Files List   FileMan Sub-Files List   Package Component Lists   Package-Namespace Mapping  
Routine: BARUFEX5

BARUFEX5.m

Go to the documentation of this file.
BARUFEX5 ; IHS/SD/TPF - CONTINUATION OF EXPORT -- BUILD BOB FILE ; 09/17/2008
 ;;1.8;IHS ACCOUNTS RECEIVABLE;**4,5,6,7,23**;OCT 26, 2005
 ;NEW ROUTINE; ITEM 1 OF SCR58 1/2/2008 ;MRS:BAR*1.8*4 DD ITEM 4.1.1
 ;HEAT54733 APR 2012 P.OTT bug fix <SUBSCRIPT>SETORIG+2^BARUFEX5
 ;HEAT93190 DEC 2012 P.OTTIS NOHEAT MARK / IGNORE DUPLICATE GHOST BILLS (SAME PAT, BUT MISSING CLAIM IN 3PB)
 ;NOHEAT MAR 2013 P.OTTIS ADDED NEW VA billing 
 ;
PRE(BARBG,BARDUZ) ;EP;
 ;
 ; ENTERS WITH TRANSACTION BEGIN DATE
 ;  AND BARDUZ = DUZ OF USER
 ;
 K ^BARBOB("BARZ",BARDUZ)
 K ^BARBOB("BARZZ",BARDUZ)
 K ^BARZZZ("BARZ")  ;MRS:BAR*1.8*6
 D NOW^%DTC
 S PRELIVLM=3071001  ;FOR RELEASE
 S ^BARBOB("BARZ",BARDUZ,"BEGIN")=%
 N BARX,BARY,BARZ,BARIDT,BART,BARBDA,BARTX,BAR,BARREV,BARFLG,TRDATE
 S SAFEDUZ=DUZ(2)
 S BARTOTX=0  ;XTMP COUNTER
 ;BEGIN EXCLUSION CHECKS
 D SETZZ(BARDUZ)   ;BUILD FILE OF PAYMENTS IN DATE RANGE
 S DUZ(2)=0
 F  S DUZ(2)=$O(^BARBOB("BARZZ",BARDUZ,DUZ(2))) Q:'DUZ(2)  D
 .S BARBDA=0
 .F  S BARBDA=$O(^BARBOB("BARZZ",BARDUZ,DUZ(2),BARBDA)) Q:'BARBDA  D
 ..S BARBILL=$G(^BARBOB("BARZZ",BARDUZ,DUZ(2),BARBDA))
 ..Q:'BARBILL   ;BILL HAS DA BUT NOT NAME
 ..S BARBL0=$G(^BARBL(DUZ(2),BARBDA,0))
 ..S ABMDUZ2=$P(BARBL0,U,22)
 ..Q:'ABMDUZ2   ;NO 3P DUZ(2)
 ..S ABMIEN=$P(BARBL0,U,17)
 ..Q:'ABMIEN  ;NO 3P BILL IEN
 ..I $$BILL^ABMUEAPI(ABMDUZ2,ABMIEN)<1 Q  ;3P EXCLUSION
 .. Q:'$$IHS^BARUFUT(ABMDUZ2)
 ..;;;Q:'$$IHSERA^BARUFUT(ABMDUZ2) ;P.OTT
 ..S BARACCT=$$GETBACC^BARUFEXU(BARBDA)   ;A/R BILL, A/R ACCOUNT
 ..Q:'BARACCT   ;NO INSURER
 ..S D0=BARACCT
 ..S BARITYP=$$VALI^BARVPM(8)   ;GET 'VIP INSURER TYPE' CODE (NUMBER)
 ..I BARITYP="I"!(BARITYP="T") Q  ;EXCLUDE 'INDIAN' OR 'THIRD PARTY BILLING' PER MEETING OF 5/4/2007
 ..; Check 3P transmitted date
 ..S BARPLOC=$$GETPLOC  ;A/R BILL, PARENT LOCATION
 ..Q:'BARPLOC
 ..;S UFMSSUFC=$$PRELIVE(ABMDUZ2,ABMIEN,BARITYP,BARPLOC)  ;MRS:BAR*1.8*7
 ..;I UFMSSUFC="" Q   ;MRS:BAR*1.8*7
 ..;
TX ..;NOW HAVE A BILL TO CHECK
 ..S TRDATE=0
 ..F  S TRDATE=$O(^BARBOB("BARZZ",BARDUZ,DUZ(2),BARBDA,TRDATE)) Q:'TRDATE  D
 ...S IENS=TRDATE_","
 ...S BARPAY=$$GET1^DIQ(90050.03,IENS,3.5,"E")  ;CREDIT - DEBIT
 ...Q:BARPAY=0  ;DON'T LOOK AT 0 PAYMENTS
 ...S BARTX0=$G(^BARTR(DUZ(2),TRDATE,0))
 ...S BARCB=$P(BARTX0,U,14)   ;COLLECTION BATCH IEN;GET EXTERNAL NAME LATER
 ...S BARCI=$P(BARTX0,U,15)   ;COLLECTION ITEM
 ...I BARCB=""!(BARCI="") Q   ;NOT IN BATCH/ITEM
 ...S BARZZ0=^BARBOB("BARZZ",BARDUZ,DUZ(2),BARBDA,TRDATE)
 ...S BARFLG=$S(BARZZ0]"":BARZZ0,1:0)   ;CHECK FOR -25 CODE FLAG
 ...S (BARREV,BAROTX)=""  ;INIT ORIGINAL TX
 ...S BARIPAC=$$IPAC^BARUFEX4(BARCB,BARCI,IENS)  ;SCHEDULE NUMBER/IPAC
 ...D CLEAN
 ...I BARPAY<0,BARFLG'=-25 D
 ....S BARXPAC=$$REV(BARBDA,BARPAY,BARIPAC) ;CHECK REVERSAL
 ....;RETURNS CODE_U_RESCHED_U_ORIGTX
 ....S BARFLG=$P(BARXPAC,U)   ;WHAT FOUND FLAG
 ....S BARIPAC=$P(BARXPAC,U,2)
 ....I BARFLG=2 D   ;SIMPLE REVERSAL
 .....S BAROTX=$P(BARXPAC,U,3)  ;ORIGINAL TRANSACTION
 .....I BAROTX="" QUIT  ;P.OTT
 .....D SETORIG(BAROTX,.BARFLG)  ;RETURNS ORIGINAL REFERENCES
 .....S ^BARBOB("BARZZ",BARDUZ,DUZ(2),BARBDA,TRDATE)=2
 ....I $E(BARFLG)="-" D CODE(BARBDA,BARFLG)
 ...S ^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI,TRDATE)=BARPAY_U_BARIPAC_U_BARBDA_U_BARBILL_U_BARFLG
 D COLCK   ;CHECK BATCH/ITEM FOR PAIRS
 D NOW^%DTC
 S ^BARBOB("BARZ",BARDUZ,"END")=%
 S DUZ(2)=SAFEDUZ
 Q
 ;
SETORIG(TRDT,BARFLG) ;FLAG ORIGINAL PAYMENT WHEN SIMPLE REVERSAL IS FOUND
 N BARO0,BAROCB,BAROCI
 S BARO0=$G(^BARTR(DUZ(2),TRDT,0))
 S BAROCB=$P(BARO0,U,14)   ;COLLECTION BATCH IEN;GET EXTERNAL NAME LATER
 Q:'BAROCB
 S BAROCI=$P(BARO0,U,15)   ;COLLECTION ITEM
 Q:'BAROCI
 S BARFLG=BARFLG_U_BARCB_U_BARCI_U_TRDATE
 S $P(^BARBOB("BARZ",BARDUZ,DUZ(2),BAROCB,BAROCI,TRDT),U,5)=BARFLG
 S BARFLG=BARFLG_U_BAROCB_U_BAROCI_U_TRDT
 S ^BARBOB("BARZZ",BARDUZ,DUZ(2),BARBDA,TRDT)=2
 Q
 ;
COLCK ; NOW BEGIN PAIRS CHECK IN COLLECTION BATCH/ITEM
 ;
 N BARCB,BARCI,TRDATE,BARNEG,BAR,BARPAY,BAR7
 S DUZ(2)=0
 F  S DUZ(2)=$O(^BARBOB("BARZ",BARDUZ,DUZ(2))) Q:'DUZ(2)  D
 .S BARCB=0
 .F  S BARCB=$O(^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB)) Q:'BARCB  D
 ..S BARCI=0
 ..F  S BARCI=$O(^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI)) Q:'BARCI  D
 ...S (TRDATE,BARNEG,BAR7)=0
 ...K BAR   ;CLEAR LOCAL ARRAY
 ...F  S TRDATE=$O(^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI,TRDATE)) Q:'TRDATE  D
 ....S BAR0=^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI,TRDATE)
 ....S BARDA=$P(BAR0,U,3)
 ....S BARFLG=$P(BAR0,U,5)
 ....S BARFLGZ=$G(^BARBOB("BARZZ",BARDUZ,DUZ(2),BARDA,TRDATE))
 ....I BARFLG=0,BARFLGZ'="" S BARFLG=BARFLGZ
 ....I $E(BARFLG)="-" D
 .....S $P(^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI,TRDATE),U,5)=BARFLG
 .....S $P(BAR0,U,5)=BARFLG
 .....S BAR7=3
 ....S BARAMT=$P(BAR0,U,1)
 ....I BARAMT<0 S BARNEG=1
 ....S BAR(TRDATE)=BAR0
 ...I BARNEG D PAIRS(.BAR,BAR7) Q   ;REVERSAL FOUND
 ...S BARFLG=$S(BAR7'=0:2,1:1)
 ...S ^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI)=BARFLG
 Q
 ;
PAIRS(BAR,BAR7) ;LOOK FOR OK PAIRS OF PAYMENTS AND REVERSALS
 ;BAR STRING = BARPAY_U_BARIPAC_U_BARBDA_U_BARBILL_U_BARFLG
 ;   I BARFLG=-4 or -7  ;MULTIPLE MATCHES -- DON'T PAIR
 ;   I BARFLG=-3 or -6  ;ORIGINAL NOT FOUND -- PAIR
 ;ADDED CHECK FOR PERMANENT IGNORABLE PAIRS
 ;  DEF: FIRST PAYMENT IS A REVERSAL
 ;   SECOND PAYMENT MATCHES REVERSAL
 ;   AND BOTH ARE IN SAME BILL AND BATCH/ITEM
 ;
 N BARI,BARJ,BARCNT
 S ^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI)=BAR7 ;REVERSALS IN BATCH
 S BARCNT=0
 ;
AGAIN S BARI=0
I S BARI=$O(BAR(BARI))
 Q:'BARI
 S BARCNT=BARCNT+1
 S BARIFLG=$P(BAR(BARI),U,5)
 I BARIFLG=2 K BAR(BARI) G I   ;FLAGGED AS SIMPLE TX/REV
 I BARIFLG=-4!(BARIFLG=-7)!(BARIFLG=-25) K BAR(BARI) G I ;COMPLEX OR DUPLICATE
 S BARJ=0
J S BARJ=$O(BAR(BARJ))
 I 'BARJ G I
 S BARJFLG=$P(BAR(BARJ),U,5)
 I BARJFLG=2 K BAR(BARJ) G J   ;FLAGGED AS ORIGINAL TX OR REV
 I BARJFLG=-4!(BARJFLG=-7)!(BARJFLG=-25) K BAR(BARI) G I ;COMPLEX OR DUPLICATE
 I $P(BAR(BARI),U,1,3)=$P(BAR(BARJ),U,1,3) G J   ;DON'T CHECK SELF
 ;CHECK AMOUNT
 S BARIAMT=$P(BAR(BARI),U)
 S BARJAMT=$P(BAR(BARJ),U)
 I BARIAMT'=-BARJAMT G J   ;ABSOLUTE AMOUNTS DON'T MATCH
 ;CHECK TREAS SCHED #
 I $P(BAR(BARI),U,2)=$P(BAR(BARJ),U,2) D  G AGAIN  ;KEEP CHECKING
 .I BARCNT=1 D  Q:'$D(BAR(BARI))
 ..I $P(BAR(BARI),U,3)'=$P(BAR(BARJ),U,3) Q  ;NOT MATCHING BILLS
 ..I BARIAMT<0,BARJAMT>0 D PAIR("-I")  ;FOUND IGNORABLE PAIR
 .D PAIR(1)
 G J
 ;
PAIR(Z) ;FOUND A MATCH -- FLAG TRANSACTIONS AS GOOD TO GO OR CODE "-I"
 N FLG
 S FLG=Z_U_BARCB_U_BARCI_U_BARJ
 S $P(^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI,BARI),U,5)=FLG
 K BAR(BARI)   ;REMOVE ENTRY
 S FLG=Z_U_BARCB_U_BARCI_U_BARI
 S $P(^BARBOB("BARZ",BARDUZ,DUZ(2),BARCB,BARCI,BARJ),U,5)=FLG
 K BAR(BARJ)
 Q
 ;
REV(BARBDA,BARPAY,TIPAC) ; FIND SIMPLE REVERSAL SCHEDULE NUMBER ;HEAVILY MODIFIED FOR BAR*1.8*5 IM29449 
 N Y,Z,SCHED,CODE,BARRDT,BARRTDN,BARR1
 S Y=""
 S BARR1=$G(^BARTR(DUZ(2),TRDATE,1))
 I $P(BARR1,U,12)="I" Q "I"  ;IGNORE TRANSACTION MRS:BAR*1.8*6 IM29571
 S BARRDT=$P(BARR1,U,10)   ;REVERSAL DATE/TIME
 I BARRDT]"" D  ;GET MATCHED TRANSACTION   ;NEW CHECK FOR MATCHES BAR*1.8*5 IM29571
 .I BARRDT'["." S BARRDT="" Q  ;IMCOMPLETE TX DATE; CHECK FURTHER
 .S BARRTDN=$P(BARR1,U,11)   ;REVERSAL TDN/IPAC
 .;S:BARRTDN="" BARRTDN="PRE MODIFICATIONS"  ;LEAVE BLANK;MRS:BAR*1.8*6
 .I '$G(^BARTR(DUZ(2),BARRDT,0)) S REV=0 Q
 .S TAMT=$$GET1^DIQ(90050.03,BARRDT_",",3.5,"E")
 .I +$G(BARPAY)'=-TAMT S REV=0 Q
 .S REV=$P(BARRDT,".")_U_BARRTDN_U_BARRDT
 ;
 I BARRDT="" D
 .S REV=$$FINDTRAN(BARBDA,BARPAY) ;FIND ORIGINAL RECEIPT
 .; RETURNS REVDATE_U_RESCHED_U_ORIGTX, NUMBER OF MULTIPLES, OR 0 IF CAN'T FIND
 S SCHED=$P(REV,U,2)
 S Y=$P($G(^BARTR(DUZ(2),TRDATE,1)),U,6)
 ;
 I 'REV D  Q CODE_U_TIPAC  ;CAN'T FIND THE ORIGINAL DATE
 .S CODE=$S(Y="e":-3,1:-6)
 I REV'[U D  Q CODE_U_TIPAC  ;MATCHES/REV ARE>1, NOT STRING
 .S CODE=$S(Y="e":-4,1:-7)
 Q 2_U_$P(REV,U,2,3)   ;RETURN SCHED AND ORIGINAL TX
 ;
PRELIVE(ABMDUZ2,ABMIEN,BARITYP,BARPLOC) ;
 Q   ;MRS:BAR*1.8*7
 N TPBAPDT,TPBEXDT
 S UFMSSUFC=""
 S TPBAPDT=$$APPRDTTM^ABMUEAPI(ABMDUZ2,ABMIEN)  ;API 3P APPROVAL DATE
 I TPBAPDT="" Q UFMSSUFC
 ;
 ;Prelive logic for 'APPLY TO' or ASUFACASUFAC3PIEN string
 ;If Date/Time Approved < 10/1/2007 then UFMSSUFC=$$PRELIVE instead
 ;If this is true there will be no delay send at all
 ;
 ;I TPBAPDT<PRELIVLM D  Q UFMSSUFC  ;IHS/SD/SDR bar*1.8*4 SCR100
 S BAR08DT=$P($G(^BAR(90052.06,DUZ(2),DUZ(2),15)),U,5)  ;IHS/SD/SDR bar*1.8*4 SCR100
 I TPBAPDT<PRELIVLM!(TPBAPDT<BAR08DT) D  Q UFMSSUFC  ;IHS/SD/SDR bar*1.8*4 SCR100
 .S BARAREA=$$GET1^DIQ(9999999.06,BARPLOC_",",.04,"I") ;LOCATION, AREA PTR
 .S PRELIV=$$PRELIVE^BARUFUT1(BARAREA,BARITYP)
 .S UFMSSUFC=PRELIV
 ;
 S UFMSSUFC=$$TRANSMIT^ABMUEAPI(ABMDUZ2,ABMIEN)
 S:UFMSSUFC=-1 UFMSSUFC=""
 Q UFMSSUFC
 ;
GETPLOC() ;
 N BARPLOC,BARVLOC
 S BARPLOC=$P(BARBL0,U,8)   ;A/R BILL, PARENT LOCATION
 I BARPLOC Q BARPLOC
 I 'BARPLOC D   ;Not found/or bad location
 .S BARVLOC=$P($G(^BARBL(DUZ(2),BARBDA,1)),U,8) ;A/R BILL, VISIT LOCATION
 .S BARPLOC=$$PARENT^BARUFEXU
 Q BARPLOC
 ;
SETZZ(BARDUZ) ;CHECK FOR AT LEAST ONE PAYMENT IN DATE RANGE
 N X,Y,Z,BARBILL,DUP,BARNUM,TRANTYPE
 S DUZ(2)=0 F  S DUZ(2)=$O(^BARTR(DUZ(2))) Q:'DUZ(2)  D
 . Q:'$$IHS^BARUFUT(DUZ(2))
 . ;;;Q:'$$IHSERA^BARUFUT(DUZ(2)) ;P.OTT
 .S BARBEG=$$BARBEG(DUZ(2))
 .S X=0
 .F  S X=$O(^BARTR(DUZ(2),"AC",X)) Q:'X  D
 ..S Y=$O(^BARTR(DUZ(2),"AC",X,""),-1)
 ..I Y'>BARBEG Q   ;NO TRANSACTIONS IN DATE RANGE
 ..S Y=0
 ..F  S Y=$O(^BARTR(DUZ(2),"AC",X,Y)) Q:'Y  D
 ...I $P($G(^BARTR(DUZ(2),Y,1)),U,12)="I" D  Q  ;FLAGGED AS IGNORABLE:MRS:BAR*1.8*6 IM29571
 ....S ^BARBOB("BARZZ",BARDUZ,DUZ(2),"I",X)=""  ;MRS:BAR*1.8*6 IM29571
 ...S IENS=Y_","
 ...S TRANTYPE=$$GET1^DIQ(90050.03,IENS_",",101,"E")  ;TRANSACTION TYPE
 ...Q:TRANTYPE'="PAYMENT"   ;ONLY WANT PAYMENTS
 ...S BARBILL=$$GET1^DIQ(90050.03,IENS,4,"E")   ;A/R BILL
 ...S DUP=$$DUP(BARBILL)  ;CHECK FOR DUPLICATE BILLS
 ...S ^BARBOB("BARZZ",BARDUZ,DUZ(2),X)=BARBILL
 ...S ^BARBOB("BARZZ",BARDUZ,DUZ(2),X,Y)=DUP
 Q
 ;
CODE(X,Z) ;FLAG ALL TRANSACTIONS FOR BILL WITH CODE -7 AND -4
 ; ENTERS WITH X = BARBILL DA
 ;   Z = CODE = -7 = MULTIPLE MAN TX W/IN BILL
 ;  -6 = CANNOT FIND ORIG TX W/IN BILL
 ;  -3 = SAME AS -6 FOR ERA TX
 ;  -4 = SAME AS -7 FOR ERA TX
 N Y
 S Y=0
 F  S Y=$O(^BARBOB("BARZZ",BARDUZ,DUZ(2),X,Y)) Q:'Y  D
 .I Z'=-25 Q:^BARBOB("BARZZ",BARDUZ,DUZ(2),X,Y)=2   ;HAS SIMPLE REVERSAL
 .Q:^BARBOB("BARZZ",BARDUZ,DUZ(2),X,Y)=-25   ;DUPLICATE BILL
 .S ^BARBOB("BARZZ",BARDUZ,DUZ(2),X,Y)=Z
 Q
DUP(ZZ) ;EP-CHECK FOR DUPLICATE BILLS
 ;  ENTERS WITH BILL NUMBER
 I ZZ="" Q ""
 N YY,CNT
 I '$D(^BARBL(DUZ(2),"B",ZZ)) Q ""
 S (CNT,YY)=0
 F  S YY=$O(^BARBL(DUZ(2),"B",ZZ,YY)) Q:'YY  D
 . I $D(^BARBOB("BARZZ",BARDUZ,DUZ(2),"I",YY)) Q   ;IGNORABLE;MRS:BAR*1.8*6 IM29571
 . S CNT=CNT+1
 ;------------>P.OTT: RETURNS DUP="" FOR MANAGABLE DUP, -26 FOR GHOST
 ;;;***** NOTE: TMP DISABLED FOR P23 DEC 30 2012 ***
 ;;;I CNT=2 D FNDD Q QVAL ;CHECK IF ONE OF THESE 2 IS 'GHOST' (NONEX IN 3PB) ?
 ;;;***** NOTE: TMP DISABLED FOR P23 DEC 30 2012 ***
 ;<------------P.OTT
 I CNT>1 Q -25 ;DUPLICATE
 Q ""
FNDD ;
 NEW YY1,YY2,BAR3PEIN,BARDUZ3P,QVAL D
 . S YY1=$O(^BARBL(DUZ(2),"B",ZZ,0))
 . S BAR3PEIN=$P($G(^BARBL(DUZ(2),YY1,0)),"^",17)
 . S BARDUZ3P=$P($G(^BARBL(DUZ(2),YY1,0)),"^",22)
 . I BAR3PEIN="" S QVAL="" Q
 . I BARDUZ3P="" S QVAL="" Q
 . I '$D(^ABMDBILL(BARDUZ3P,BAR3PEIN,0)) S QVAL=-26 Q  ;GHOST
 . S YY2=$O(^BARBL(DUZ(2),"B",ZZ,YY1))
 . S BAR3PEIN=$P($G(^BARBL(DUZ(2),YY2,0)),"^",17)
 . S BARDUZ3P=$P($G(^BARBL(DUZ(2),YY2,0)),"^",22)
 . I BAR3PEIN="" S QVAL="" Q
 . I BARDUZ3P="" S QVAL="" Q
 . I '$D(^ABMDBILL(BARDUZ3P,BAR3PEIN,0)) S QVAL=-26 Q  ;GHOST
 . S QVAL=""
 . QUIT
 Q
BARBEG(X) ;SET BEGINING DATE FROM NOT SET CROSS-REFERENCE
 ;
 N Z
 S:BARBG="" BARBG=3070930
 S Z=$O(^BARSESS(X,"NS",""))
 I Z=""!(Z>BARBG) S Z=BARBG  ;IF NO NS X-REF, OR BARBG<NS, USE SESS DATE
 Q Z-1
FINDTRAN(BARBLIEN,AMOUNT) ;EP -HEAVILY MODIFIED FOR NEW MATCHING;MRS:BAR*1.8*6 IM29571
 ;AMOUNT SHOULD BE A NEGATIVE NUMBER
 Q:'BARBLIEN 0
 N TRANS,TAMOUNT,FOUND,REVDATE,COLDA,ITEMDA,REVSCHED,REVERSAL,BARFLG
 S BARFLG=^BARBOB("BARZZ",DUZ,DUZ(2),BARBLIEN,TRDATE)
 I BARFLG=2 D  Q $P(REVDATE,".")_U_REVSCHED  ;ALREADY MATCHED
 .S BARTR0=^BARTR(DUZ(2),TRDATE,1)
 .S REVDATE=$P(BARTR0,U,10)
 .S REVSCHED=$P(BARTR0,U,11)
 S AMOUNT=-AMOUNT  ;SET TO ABSOLUTE VALUE
 S FOUND=0
 S REVERSAL=0  ;NUMBER OF REVERSALS ALREADY SET UP FOR THIS AMOUNT. if even one sent to "ns"
 S TRANS=TRDATE  ;WORK BACK TO EARLIER TRANSACTIONS
 ;CHECK FOR MULTIPLES; REVERSAL CAN NOT APPLY TO A FUTURE PAYMENT
 F  S TRANS=$O(^BARBOB("BARZZ",DUZ,DUZ(2),BARBLIEN,TRANS),-1) Q:'TRANS  D  ;CHECK FOR MULTIPLES; REVERSAL CAN NOT APPLY TO A FUTURE PAYMENT
 .S BARFLG=^BARBOB("BARZZ",DUZ,DUZ(2),BARBLIEN,TRANS)
 .Q:BARFLG=2  ;ALREADY MATCHED
 .S TAMOUNT=$$GET1^DIQ(90050.03,TRANS_",",3.5,"E")
 .I TAMOUNT=-AMOUNT,$$GET1^DIQ(90050.03,TRANS_",",110,"I") S REVERSAL=REVERSAL+1
 .I TAMOUNT=AMOUNT S FOUND=FOUND+1 D
 ..S REVDATE=$$GET1^DIQ(90050.03,TRANS_",",.01,"I")
 ..S COLDA=$$GET1^DIQ(90050.03,TRANS_",",14,"I")
 ..S ITEMDA=$$GET1^DIQ(90050.03,TRANS_",",15,"I")
 ..S REVSCHED=$$GET1^DIQ(90051.1101,ITEMDA_","_COLDA_",",20,"I")
 ..S:REVSCHED="" REVSCHED="PRE-UFMS_COLLECTIONS"
 ..S REVSCHED=REVSCHED_U_TRANS   ;CAPTURE ORIGINAL TX
 I REVERSAL>0 S FOUND=0
 I FOUND=1 Q $P(REVDATE,".")_U_REVSCHED
 Q FOUND
 ;
CLEAN ;CHECK FOR AND CLEAN POSSIBLE BAD DATA
 ;FIX ANY TRANSACTION HAS A 0 REVERSAL DATE
 I $P($G(^BARTR(DUZ(2),TRDATE,1)),U,10)=0 D DEPOP Q
 ;
 ; FIX TRANTYPE = PAYMENT WITH A REVERSAL DATE AND A POSITIVE AMT
 I (BARPAY>0!(BARPAY=0)),$P($G(^BARTR(DUZ(2),TRDATE,1)),U,10) D
 .D DEPOP
 Q
DEPOP ;EP - DE POPULATE IF REVERSAL DATES NOT FOUND
 ;ONLY FOR BETA SITES
 K DIR,DIE,DIC,DA,DR
 S DA=TRDATE
 S DR="110///@;111///@"
 S DIE="^BARTR("_DUZ(2)_","
 D ^DIE
 K DIR,DIE,DIC,DA,DR
 Q