PSAUP5 ;BIR/JMB-Upload and Process Prime Vendor Invoice Data - CONT'D ;10/9/97
;;3.0; DRUG ACCOUNTABILITY/INVENTORY INTERFACE;**3,21,37,48**; 10/24/97
;References to ^DIC(51.5 are covered by IA #1931
;References to ^PSDRUG( are covered by IA #2095
;This routine looks in the DRUG file for each line item on the order.
;It looks for the NDC then VSN. If it is not found, no data is stored.
;
S PSACTRL=0 F S PSACTRL=$O(^XTMP("PSAPV",PSACTRL)) Q:PSACTRL="" D
.I '$D(^XTMP("PSAPV",PSACTRL,"IN"))!($P($G(^XTMP("PSAPV",PSACTRL,"IN")),"^",8)="P") Q
.S PSAIN=^XTMP("PSAPV",PSACTRL,"IN"),PSAORD=$P(PSAIN,"^",4),PSAINV=$P(PSAIN,"^",2),PSACS=0
.W !,"Order#: "_PSAORD_" Invoice#: "_PSAINV_" Invoice Date: "_$$FMTE^XLFDT(+PSAIN)
.;PSA*3*31 check/fix possible string too long errors
.S PSAUPLD=1,PSALINE=0 D PSAP31^PSAVSN K PSAUPLD
.S PSALINE=0 F S PSALINE=$O(^XTMP("PSAPV",PSACTRL,"IT",PSALINE)) Q:'PSALINE D
..Q:'$D(^XTMP("PSAPV",PSACTRL,"IT",PSALINE)) Q:$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",18)="P"
..S PSAOK=1,PSADATA=^XTMP("PSAPV",PSACTRL,"IT",PSALINE),PSANDC=$P(PSADATA,"^",4),PSAVSN=$P(PSADATA,"^",5)
..DO GETDRUG W "."
..S:PSAOK $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",18)="OK"
..D:PSAOK OUAUTO^PSAUP8 ;*48 ORDER UNIT AUTO ADJUST
.S (PSACNT,PSALLCS,PSALLOK,PSASUP)=0
.S:PSACS $P(^XTMP("PSAPV",PSACTRL,"IN"),"^",9)="CS"
.S PSALINE=0 F S PSALINE=+$O(^XTMP("PSAPV",PSACTRL,"IT",PSALINE)) Q:'PSALINE D
..S PSACNT=PSACNT+1
..S:$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="CS" PSALLCS=PSALLCS+1
..S:$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",18)="OK" PSALLOK=PSALLOK+1
..S:'+$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6) PSASUP=PSASUP+1
.I PSACNT=PSASUP S $P(^XTMP("PSAPV",PSACTRL,"IN"),"^",13)="SUP"
.I PSACNT=PSALLCS S $P(^XTMP("PSAPV",PSACTRL,"IN"),"^",10)="ALL CS"
.I PSACNT=PSALLOK S $P(^XTMP("PSAPV",PSACTRL,"IN"),"^",8)="OK"
Q
;
GETDRUG ;Looks for NDC then VSNs in DRUG file.
;PSA*3*21 (DAVE B 3MAR2000)
;Make NDC vendor proof
I $G(PSANDC)'="",$L(PSANDC)>12 S PSANDC=$E(PSANDC,1,12)
I $G(PSANDC)'="",$L(PSANDC)<12 D
.I $G(PSANDC)'="",'$D(^PSDRUG("NDC",PSANDC)) S X=12,X1=$L(PSANDC) F X=1:1:(12-X1) S PSANDC="0"_PSANDC
.S NDC0=1 F X=1:1:$L(PSANDC) I $E(PSANDC,X)'=0&($E(PSANDC,X)'="-") K NDC0
.I $G(NDC0)=1 S PSANDC=""
;Dave B (PSA*3*2)
I $D(PSANDC) S PSANDC=$P(PSANDC,"~")
I $D(PSAVSN) S PSAVSN=$P(PSAVSN,"~")
G:PSANDC=0 GETVSN
I PSANDC'="" S PSAIEN=+$O(^PSDRUG("C",PSANDC,0)) I PSAIEN S PSASUB=+$O(^PSDRUG("C",PSANDC,PSAIEN,0)) G FOUND
;
GETVSN ;Looks for Vendor Stock Number then NDC.
I PSAVSN'="" S PSAIEN=+$O(^PSDRUG("AVSN",PSAVSN,0)) I PSAIEN S PSASUB=+$O(^PSDRUG("AVSN",PSAVSN,PSAIEN,0)) G FOUND
;
I (PSANDC=""&PSAVSN=""),$P(PSADATA,"^",26)'="" D ^PSAUP6
;
S:'+$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6) PSAOK=0
G UOM
;
FOUND ;Store line item data if ordered item was found in DRUG file.
I $P($G(^PSDRUG(PSAIEN,2)),"^",3)["N" S PSACS=1,$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="CS"
S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN,$P(^(PSALINE),"^",7)=PSASUB
;
NDC ;If >1 NDC in DRUG file, store how many.
;^XTMP $P4=NDC ~ # of nodes with same VSN (if >1 NDC) ~ VSN if different
; than one on SYNONYM multiple (if 1 NDC)
I PSANDC'="",$O(^PSDRUG("C",PSANDC,0)) D
.S (PSACNT,PSACNT1,PSAFND,PSAFND1,PSAIEN50)=0
.;
.;DAVE B (PSA*3*2)
.F S PSAIEN50=+$O(^PSDRUG("C",PSANDC,PSAIEN50)) Q:'PSAIEN50 I '$D(^PSDRUG(PSAIEN50,"I")) S PSASYN=0 F S PSASYN=+$O(^PSDRUG("C",PSANDC,PSAIEN50,PSASYN)) Q:'PSASYN D
..Q:'$D(^PSDRUG(PSAIEN50,1,PSASYN,0))
..I $P(^PSDRUG(PSAIEN50,1,PSASYN,0),"^",4)=PSAVSN S PSAFND=PSAFND+1,PSAFND1=PSAIEN50_"^"_PSASYN Q
..I $P(^PSDRUG(PSAIEN50,1,PSASYN,0),"^",4)'=PSAVSN S PSACNT=PSACNT+1,PSACNT1=PSAIEN50_"^"_PSASYN
.;
.;If NDC & VSN match, set ^XTMP
.I PSAFND=1 S PSAIEN=$P(PSAFND1,"^"),$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN,PSASUB=$P(PSAFND1,"^",2),$P(^(PSALINE),"^",7)=PSASUB,$P(^(PSALINE),"^",4)=PSANDC Q
.;
.;If >1 with same NDC & VSN, set # with same NDC & VSN in ^XTMP & flag
.I PSAFND>1 S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)_"~"_PSAFND,PSAOK=0 Q
.;
.;If 1 NDC and ...
.I PSACNT=1 S PSAIEN=$P(PSACNT1,"^"),$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN,PSASUB=$P(PSACNT1,"^",2),$P(^(PSALINE),"^",7)=PSASUB D Q
..;VSN is null, accept as found & set ^XTMP
..I $P(^PSDRUG(PSAIEN,1,PSASUB,0),"^",4)="" S PSAIEN=$P(PSACNT1,"^"),$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN,PSASUB=$P(PSACNT1,"^",2),$P(^(PSALINE),"^",7)=PSASUB,$P(^(PSALINE),"^",4)=PSANDC Q
..;Different VSN, set VSN in NDC piece in ^XTMP
..S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)_"~~"_$P(^PSDRUG(PSAIEN,1,PSASUB,0),"^",4),PSAOK=0
.;
.;If >1 NDC with differnt VSN, set flag in NDC piece of ^XTMP
.I PSACNT>1 S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)_"~"_PSACNT,PSAOK=0
;
VSN ;If there >1 VSN with same VSN, store how many.
;^XTMP $P5=VSN ~ # of nodes with same UOM (if >1 VSN) ~ NDC if different
; than one on SYNONYM multiple (if 1 VSN)
I PSAVSN'="",$O(^PSDRUG("AVSN",PSAVSN,0)) D
.S (PSACNT,PSACNT1,PSAFND,PSAFND1,PSAIEN50)=0
.;
.;DAVE B (PSA*3*2)
.F S PSAIEN50=+$O(^PSDRUG("AVSN",PSAVSN,PSAIEN50)) Q:'PSAIEN50 I '$D(^PSDRUG(PSAIEN50,"I")) S PSASYN=0 F S PSASYN=+$O(^PSDRUG("AVSN",PSAVSN,PSAIEN50,PSASYN)) Q:'PSASYN D
..Q:'$D(^PSDRUG(PSAIEN50,1,PSASYN,0))
..I $P(^PSDRUG(PSAIEN50,1,PSASYN,0),"^")=PSANDC S PSAFND=PSAFND+1,PSAFND1=PSAIEN50_"^"_PSASYN Q
..I $P(^PSDRUG(PSAIEN50,1,PSASYN,0),"^")'=PSANDC S PSACNT=PSACNT+1,PSACNT1=PSAIEN50_"^"_PSASYN
.;
.;If VSN & NDC match, set ^XTMP
.I PSAFND=1 D Q
..I '+$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6) D
...S PSAIEN=$P(PSAFND1,"^"),$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN,PSASUB=$P(PSAFND1,"^",2),$P(^(PSALINE),"^",7)=PSASUB
...S:$P($G(^PSDRUG(PSAIEN,2)),"^",3)["N" $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="CS",PSACS=1
...S:$P($G(^PSDRUG(PSAIEN,2)),"^",3)'["N" $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="",PSACS=0
..;DAVE B (PSA*3*21) Ensure VSN = one number
..S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=+$G(PSAVSN)
.;
.;If >1 with same VSN & NDC, set # with same NDC & VSN in ^XTMP & flag
.I PSAFND>1 S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)_"~"_PSAFND,PSAOK=0 Q
.;
.;If 1 VSN and ...
.I PSACNT=1 D Q
..I '+$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6) S PSAIEN=$P(PSACNT1,"^"),$P(^(PSALINE),"^",6)=PSAIEN,PSASUB=$P(PSACNT1,"^",2),$P(^(PSALINE),"^",7)=PSASUB
..;NDC is null, accept as found & set ^XTMP
..I $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)="" S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=PSAVSN,$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$P(^PSDRUG(PSAIEN,1,PSASUB,0),"^") Q
..;Different VSN, set VSN in NDC piece in ^XTMP
..;PSA*3*31 - Check to ensure VSN not already stored
..;result is string too long error
..I $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)'[$P(^PSDRUG(PSAIEN,1,PSASUB,0),"^") S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)_"~~"_$P(^PSDRUG(PSAIEN,1,PSASUB,0),"^"),PSAOK=0
.;
.;If >1 VSN with different NDC, set flag in NDC piece of ^XTMP
.I PSACNT>1 S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)_"~"_PSACNT,PSAOK=0
;
UOM ;Locates X12 Unit of Measure Code in ORDER UNIT file.
;^XTMP $P2=Alpha OU ~ 51.5 IEN
K PSAUOM I $P(PSADATA,"^",2)="" S PSAOK=0 G QTY
;
;If order unit is standard order unit, set IEN in ^XTMP
S PSAUOM=$O(^DIC(51.5,"B",$P($P(PSADATA,"^",2),"~"),0))
I 'PSAUOM D Q:'PSAOK
.I +$P(PSADATA,"^",6),+$P(PSADATA,"^",7),+$P($G(^PSDRUG(PSAIEN,1,+$P(PSADATA,"^",7),0)),"^",5) D
..S PSAUOM=+$P($G(^PSDRUG(PSAIEN,1,+$P(PSADATA,"^",7),0)),"^",5),PSAUOMN=$P($P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2),"~")
..S $P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2)=PSAUOMN_"~"_PSAUOM K PSAUOMN
.S:'PSAUOM PSAOK=0
I PSAUOM S PSAUOMN=$P($P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2),"~"),$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2)=PSAUOMN_"~"_PSAUOM K PSAUOMN
;
QTY ;If qty is 0 or blank, set flag
I '+$P(PSADATA,"^") S PSAOK=0
;
DUOU ;If no dispense units per order unit, set flag.
S PSADRG=$P(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6),PSASYN=$P(^(PSALINE),"^",7)
S:'PSASYN PSAOK=0
I PSASYN,'$P($G(^PSDRUG(PSADRG,1,PSASYN,0)),"^",7) S PSAOK=0
Q
PSAUP5 ;BIR/JMB-Upload and Process Prime Vendor Invoice Data - CONT'D ;10/9/97
+1 ;;3.0; DRUG ACCOUNTABILITY/INVENTORY INTERFACE;**3,21,37,48**; 10/24/97
+2 ;References to ^DIC(51.5 are covered by IA #1931
+3 ;References to ^PSDRUG( are covered by IA #2095
+4 ;This routine looks in the DRUG file for each line item on the order.
+5 ;It looks for the NDC then VSN. If it is not found, no data is stored.
+6 ;
+7 SET PSACTRL=0
FOR
SET PSACTRL=$ORDER(^XTMP("PSAPV",PSACTRL))
IF PSACTRL=""
QUIT
Begin DoDot:1
+8 IF '$DATA(^XTMP("PSAPV",PSACTRL,"IN"))!($PIECE($GET(^XTMP("PSAPV",PSACTRL,"IN")),"^",8)="P")
QUIT
+9 SET PSAIN=^XTMP("PSAPV",PSACTRL,"IN")
SET PSAORD=$PIECE(PSAIN,"^",4)
SET PSAINV=$PIECE(PSAIN,"^",2)
SET PSACS=0
+10 WRITE !,"Order#: "_PSAORD_" Invoice#: "_PSAINV_" Invoice Date: "_$$FMTE^XLFDT(+PSAIN)
+11 ;PSA*3*31 check/fix possible string too long errors
+12 SET PSAUPLD=1
SET PSALINE=0
DO PSAP31^PSAVSN
KILL PSAUPLD
+13 SET PSALINE=0
FOR
SET PSALINE=$ORDER(^XTMP("PSAPV",PSACTRL,"IT",PSALINE))
IF 'PSALINE
QUIT
Begin DoDot:2
+14 IF '$DATA(^XTMP("PSAPV",PSACTRL,"IT",PSALINE))
QUIT
IF $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",18)="P"
QUIT
+15 SET PSAOK=1
SET PSADATA=^XTMP("PSAPV",PSACTRL,"IT",PSALINE)
SET PSANDC=$PIECE(PSADATA,"^",4)
SET PSAVSN=$PIECE(PSADATA,"^",5)
+16 DO GETDRUG
WRITE "."
+17 IF PSAOK
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",18)="OK"
+18 ;*48 ORDER UNIT AUTO ADJUST
IF PSAOK
DO OUAUTO^PSAUP8
End DoDot:2
+19 SET (PSACNT,PSALLCS,PSALLOK,PSASUP)=0
+20 IF PSACS
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IN"),"^",9)="CS"
+21 SET PSALINE=0
FOR
SET PSALINE=+$ORDER(^XTMP("PSAPV",PSACTRL,"IT",PSALINE))
IF 'PSALINE
QUIT
Begin DoDot:2
+22 SET PSACNT=PSACNT+1
+23 IF $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="CS"
SET PSALLCS=PSALLCS+1
+24 IF $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",18)="OK"
SET PSALLOK=PSALLOK+1
+25 IF '+$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)
SET PSASUP=PSASUP+1
End DoDot:2
+26 IF PSACNT=PSASUP
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IN"),"^",13)="SUP"
+27 IF PSACNT=PSALLCS
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IN"),"^",10)="ALL CS"
+28 IF PSACNT=PSALLOK
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IN"),"^",8)="OK"
End DoDot:1
+29 QUIT
+30 ;
GETDRUG ;Looks for NDC then VSNs in DRUG file.
+1 ;PSA*3*21 (DAVE B 3MAR2000)
+2 ;Make NDC vendor proof
+3 IF $GET(PSANDC)'=""
IF $LENGTH(PSANDC)>12
SET PSANDC=$EXTRACT(PSANDC,1,12)
+4 IF $GET(PSANDC)'=""
IF $LENGTH(PSANDC)<12
Begin DoDot:1
+5 IF $GET(PSANDC)'=""
IF '$DATA(^PSDRUG("NDC",PSANDC))
SET X=12
SET X1=$LENGTH(PSANDC)
FOR X=1:1:(12-X1)
SET PSANDC="0"_PSANDC
+6 SET NDC0=1
FOR X=1:1:$LENGTH(PSANDC)
IF $EXTRACT(PSANDC,X)'=0&($EXTRACT(PSANDC,X)'="-")
KILL NDC0
+7 IF $GET(NDC0)=1
SET PSANDC=""
End DoDot:1
+8 ;Dave B (PSA*3*2)
+9 IF $DATA(PSANDC)
SET PSANDC=$PIECE(PSANDC,"~")
+10 IF $DATA(PSAVSN)
SET PSAVSN=$PIECE(PSAVSN,"~")
+11 IF PSANDC=0
GOTO GETVSN
+12 IF PSANDC'=""
SET PSAIEN=+$ORDER(^PSDRUG("C",PSANDC,0))
IF PSAIEN
SET PSASUB=+$ORDER(^PSDRUG("C",PSANDC,PSAIEN,0))
GOTO FOUND
+13 ;
GETVSN ;Looks for Vendor Stock Number then NDC.
+1 IF PSAVSN'=""
SET PSAIEN=+$ORDER(^PSDRUG("AVSN",PSAVSN,0))
IF PSAIEN
SET PSASUB=+$ORDER(^PSDRUG("AVSN",PSAVSN,PSAIEN,0))
GOTO FOUND
+2 ;
+3 IF (PSANDC=""&PSAVSN="")
IF $PIECE(PSADATA,"^",26)'=""
DO ^PSAUP6
+4 ;
+5 IF '+$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)
SET PSAOK=0
+6 GOTO UOM
+7 ;
FOUND ;Store line item data if ordered item was found in DRUG file.
+1 IF $PIECE($GET(^PSDRUG(PSAIEN,2)),"^",3)["N"
SET PSACS=1
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="CS"
+2 SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN
SET $PIECE(^(PSALINE),"^",7)=PSASUB
+3 ;
NDC ;If >1 NDC in DRUG file, store how many.
+1 ;^XTMP $P4=NDC ~ # of nodes with same VSN (if >1 NDC) ~ VSN if different
+2 ; than one on SYNONYM multiple (if 1 NDC)
+3 IF PSANDC'=""
IF $ORDER(^PSDRUG("C",PSANDC,0))
Begin DoDot:1
+4 SET (PSACNT,PSACNT1,PSAFND,PSAFND1,PSAIEN50)=0
+5 ;
+6 ;DAVE B (PSA*3*2)
+7 FOR
SET PSAIEN50=+$ORDER(^PSDRUG("C",PSANDC,PSAIEN50))
IF 'PSAIEN50
QUIT
IF '$DATA(^PSDRUG(PSAIEN50,"I"))
SET PSASYN=0
FOR
SET PSASYN=+$ORDER(^PSDRUG("C",PSANDC,PSAIEN50,PSASYN))
IF 'PSASYN
QUIT
Begin DoDot:2
+8 IF '$DATA(^PSDRUG(PSAIEN50,1,PSASYN,0))
QUIT
+9 IF $PIECE(^PSDRUG(PSAIEN50,1,PSASYN,0),"^",4)=PSAVSN
SET PSAFND=PSAFND+1
SET PSAFND1=PSAIEN50_"^"_PSASYN
QUIT
+10 IF $PIECE(^PSDRUG(PSAIEN50,1,PSASYN,0),"^",4)'=PSAVSN
SET PSACNT=PSACNT+1
SET PSACNT1=PSAIEN50_"^"_PSASYN
End DoDot:2
+11 ;
+12 ;If NDC & VSN match, set ^XTMP
+13 IF PSAFND=1
SET PSAIEN=$PIECE(PSAFND1,"^")
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN
SET PSASUB=$PIECE(PSAFND1,"^",2)
SET $PIECE(^(PSALINE),"^",7)=PSASUB
SET $PIECE(^(PSALINE),"^",4)=PSANDC
QUIT
+14 ;
+15 ;If >1 with same NDC & VSN, set # with same NDC & VSN in ^XTMP & flag
+16 IF PSAFND>1
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)_"~"_PSAFND
SET PSAOK=0
QUIT
+17 ;
+18 ;If 1 NDC and ...
+19 IF PSACNT=1
SET PSAIEN=$PIECE(PSACNT1,"^")
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN
SET PSASUB=$PIECE(PSACNT1,"^",2)
SET $PIECE(^(PSALINE),"^",7)=PSASUB
Begin DoDot:2
+20 ;VSN is null, accept as found & set ^XTMP
+21 IF $PIECE(^PSDRUG(PSAIEN,1,PSASUB,0),"^",4)=""
SET PSAIEN=$PIECE(PSACNT1,"^")
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN
SET PSASUB=$PIECE(PSACNT1,"^",2)
SET $PIECE(^(PSALINE),"^",7)=PSASUB
SET $PIECE(^(PSALINE),"^",4)=PSANDC
QUIT
+22 ;Different VSN, set VSN in NDC piece in ^XTMP
+23 SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)_"~~"_$PIECE(^PSDRUG(PSAIEN,1,PSASUB,0),"^",4)
SET PSAOK=0
End DoDot:2
QUIT
+24 ;
+25 ;If >1 NDC with differnt VSN, set flag in NDC piece of ^XTMP
+26 IF PSACNT>1
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)_"~"_PSACNT
SET PSAOK=0
End DoDot:1
+27 ;
VSN ;If there >1 VSN with same VSN, store how many.
+1 ;^XTMP $P5=VSN ~ # of nodes with same UOM (if >1 VSN) ~ NDC if different
+2 ; than one on SYNONYM multiple (if 1 VSN)
+3 IF PSAVSN'=""
IF $ORDER(^PSDRUG("AVSN",PSAVSN,0))
Begin DoDot:1
+4 SET (PSACNT,PSACNT1,PSAFND,PSAFND1,PSAIEN50)=0
+5 ;
+6 ;DAVE B (PSA*3*2)
+7 FOR
SET PSAIEN50=+$ORDER(^PSDRUG("AVSN",PSAVSN,PSAIEN50))
IF 'PSAIEN50
QUIT
IF '$DATA(^PSDRUG(PSAIEN50,"I"))
SET PSASYN=0
FOR
SET PSASYN=+$ORDER(^PSDRUG("AVSN",PSAVSN,PSAIEN50,PSASYN))
IF 'PSASYN
QUIT
Begin DoDot:2
+8 IF '$DATA(^PSDRUG(PSAIEN50,1,PSASYN,0))
QUIT
+9 IF $PIECE(^PSDRUG(PSAIEN50,1,PSASYN,0),"^")=PSANDC
SET PSAFND=PSAFND+1
SET PSAFND1=PSAIEN50_"^"_PSASYN
QUIT
+10 IF $PIECE(^PSDRUG(PSAIEN50,1,PSASYN,0),"^")'=PSANDC
SET PSACNT=PSACNT+1
SET PSACNT1=PSAIEN50_"^"_PSASYN
End DoDot:2
+11 ;
+12 ;If VSN & NDC match, set ^XTMP
+13 IF PSAFND=1
Begin DoDot:2
+14 IF '+$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)
Begin DoDot:3
+15 SET PSAIEN=$PIECE(PSAFND1,"^")
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)=PSAIEN
SET PSASUB=$PIECE(PSAFND1,"^",2)
SET $PIECE(^(PSALINE),"^",7)=PSASUB
+16 IF $PIECE($GET(^PSDRUG(PSAIEN,2)),"^",3)["N"
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)="CS"
SET PSACS=1
+17 IF $PIECE($GET(^PSDRUG(PSAIEN,2)),"^",3)'["N"
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",19)=""
SET PSACS=0
End DoDot:3
+18 ;DAVE B (PSA*3*21) Ensure VSN = one number
+19 SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=+$GET(PSAVSN)
End DoDot:2
QUIT
+20 ;
+21 ;If >1 with same VSN & NDC, set # with same NDC & VSN in ^XTMP & flag
+22 IF PSAFND>1
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)_"~"_PSAFND
SET PSAOK=0
QUIT
+23 ;
+24 ;If 1 VSN and ...
+25 IF PSACNT=1
Begin DoDot:2
+26 IF '+$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)
SET PSAIEN=$PIECE(PSACNT1,"^")
SET $PIECE(^(PSALINE),"^",6)=PSAIEN
SET PSASUB=$PIECE(PSACNT1,"^",2)
SET $PIECE(^(PSALINE),"^",7)=PSASUB
+27 ;NDC is null, accept as found & set ^XTMP
+28 IF $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=""
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=PSAVSN
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",4)=$PIECE(^PSDRUG(PSAIEN,1,PSASUB,0),"^")
QUIT
+29 ;Different VSN, set VSN in NDC piece in ^XTMP
+30 ;PSA*3*31 - Check to ensure VSN not already stored
+31 ;result is string too long error
+32 IF $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)'[$PIECE(^PSDRUG(PSAIEN,1,PSASUB,0),"^")
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)_"~~"_$PIECE(^PSDRUG(PSAIEN,1,PSASUB,0),"^")
SET PSAOK=0
End DoDot:2
QUIT
+33 ;
+34 ;If >1 VSN with different NDC, set flag in NDC piece of ^XTMP
+35 IF PSACNT>1
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",5)_"~"_PSACNT
SET PSAOK=0
End DoDot:1
+36 ;
UOM ;Locates X12 Unit of Measure Code in ORDER UNIT file.
+1 ;^XTMP $P2=Alpha OU ~ 51.5 IEN
+2 KILL PSAUOM
IF $PIECE(PSADATA,"^",2)=""
SET PSAOK=0
GOTO QTY
+3 ;
+4 ;If order unit is standard order unit, set IEN in ^XTMP
+5 SET PSAUOM=$ORDER(^DIC(51.5,"B",$PIECE($PIECE(PSADATA,"^",2),"~"),0))
+6 IF 'PSAUOM
Begin DoDot:1
+7 IF +$PIECE(PSADATA,"^",6)
IF +$PIECE(PSADATA,"^",7)
IF +$PIECE($GET(^PSDRUG(PSAIEN,1,+$PIECE(PSADATA,"^",7),0)),"^",5)
Begin DoDot:2
+8 SET PSAUOM=+$PIECE($GET(^PSDRUG(PSAIEN,1,+$PIECE(PSADATA,"^",7),0)),"^",5)
SET PSAUOMN=$PIECE($PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2),"~")
+9 SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2)=PSAUOMN_"~"_PSAUOM
KILL PSAUOMN
End DoDot:2
+10 IF 'PSAUOM
SET PSAOK=0
End DoDot:1
IF 'PSAOK
QUIT
+11 IF PSAUOM
SET PSAUOMN=$PIECE($PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2),"~")
SET $PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",2)=PSAUOMN_"~"_PSAUOM
KILL PSAUOMN
+12 ;
QTY ;If qty is 0 or blank, set flag
+1 IF '+$PIECE(PSADATA,"^")
SET PSAOK=0
+2 ;
DUOU ;If no dispense units per order unit, set flag.
+1 SET PSADRG=$PIECE(^XTMP("PSAPV",PSACTRL,"IT",PSALINE),"^",6)
SET PSASYN=$PIECE(^(PSALINE),"^",7)
+2 IF 'PSASYN
SET PSAOK=0
+3 IF PSASYN
IF '$PIECE($GET(^PSDRUG(PSADRG,1,PSASYN,0)),"^",7)
SET PSAOK=0
+4 QUIT