PSJPADIT ;BIR/JCH-INPATIENT PADE INVENTORY FILE UTILITIES ;25 SEP 97 / 7:41 AM
;;5.0;INPATIENT MEDICATIONS ;**317,356**;16 DEC 97;Build 7
;Per VHA Directive 2004-038, this routine should not be modified.
;
; Reference to ^XLFDT is supported by DBIA 10103.
; Reference to ^PSDRUG is supported by DBIA 2192.
;
FIL58601(PADATA,PSJOMS) ; File PADE data into PADE INVENTORY file #58.601
; PADE HL7 messages filed into the PADE IN TRANSACTION (#58.6) trigger new-style 'AC' action cross reference, entering here.
;
;INPUT:PADATA array - Passed in X(n) array from ACTION cross reference on PADE INBOUND TRANSACTIONS (#58.6) file.
; Array Node File# Field# Field Name Description
; ---------- ------ ------ ------------------ -------------
; PADATA(1) 58.6 1.1 PADE VENDOR IEN of PADE system from file 58.7
; PADATA(2) 58.6 1 DISPENSING DEVICE Name (text) of PADE DEVICE
; PADATA(3) 58.6 1.2 DRAWER Name (text) of PADE DRAWER
; PADATA(4) 58.6 2 DRUG Pointer to DRUG (#50) file.
; PADATA(5) 58.6 4 TYPE OF TRANSACTION Set of codes describing transaction (L=LOAD,U=UNLOAD,D=DISPENSE,R=RETURN,X=DESTROY).
; PADATA(6) 58.6 3 QUANTITY Quantity (item count) of drugs documented in transaction.
; PADATA(7) 58.6 10 POCKET Most specific storage location within drawer or subdrawer.
; PADATA(8) 58.6 13 ACTUAL BEGIN BALANCE Actual item count performed at PADE device prior to transaction.
; PADATA(9) 58.6 .01 TRANSACTION DATE/TIME Date/Time the activity occurred at the dispensing device
; PADATA(10) 58.6 11 SUBDRAWER Another storage division of a cabinet
; PADATA(12) 58.6 12 EXPECTED BEGIN COUNT Expected Quantity of Drug in Bin/Pocket at start of transaction
; PADATA(16) 58.6 16 POCKET BALANCE Item count of pocket/bin
;
;INPUT:PSJOMS array - Additional PADE HL7 information
; PSJOMS("DRGITM") - Drug ID
; PSJOMS("CABID") - PADE Cabinet ID
; PSJOMS("DISPSYS")- PADE System
;
N ERRMSG,PADNOW,PSJNOTE
Q:'$D(PADATA)>1 ; No data passed in
; Don't allow future Last Pocket Update date/times (could suppress actual updates)
S PADNOW=$$NOW^XLFDT()
I $G(PADATA(9))>PADNOW S PADATA(9)=PADNOW
;
N INVDATA ; Array to hold data returned from GETS^DIQ calls (re-initialized and re-used)
N ERR ; Array to hold messages returned from DIE and DIC calls. (re-initialized and re-used).
N FDA ; Array containing the name of the root of a VA FileMan Data Array
;
N PS586IEN S PS586IEN=+$G(DA) I PS586IEN I $P($G(^PS(58.6,PS586IEN,3)),"^")]"" S $P(^PS(58.6,PS586IEN,3),"^")="I"
S PSJNOTE=$$UPPER^HLFNC($G(^PS(58.6,+$G(PS586IEN),2)))
I PSJNOTE["CANCEL" Q ; Cancel Inventory Update if comments contain 'CANCEL'
S PADATA(12)=$P($G(^PS(58.6,+$G(PS586IEN),0)),"^",13)
S PADATA(16)=$P($G(^PS(58.6,+$G(PS586IEN),1)),"^",2)
;S PADATA(4)=$P($G(^PS(58.6,+$G(PS586IEN),0)),"^",3) ;UNCOMMENT THIS LINE IN TEST SYSTEM ONLY TO MOCK TEST CREATING AN INVENTORY UPDATE IN FILEMAN
Q:$E($G(PADATA(5)))="W"&(PSJNOTE'["RETURN BIN") ; Waste Transaction should not update inventory, unless RETURN BIN
;
; Quit if this transaction is older than the most recent update date/time for the pocket-drug
N TMPARRAY M PSTMPAR=PADATA
I $$OLDPKUP^PSJPAD70(.PSTMPAR,.ERR,PS586IEN) D LOGERR(.ERR) Q
K PSTMPAR
;
; File PADE System from PADE INBOUND TRANSACTION file to PADE INVENTORY file
I '$$FILSYS(.PADATA,.ERR) S ERR="CANNOT FILE SYSTEM "_ERR D LOGERR(.ERR) Q
;
; File PADE Device from PADE INBOUND TRANSACTION file to PADE INVENTORY file
I '$$FILDEV^PSJPADIU(.PADATA,.ERR) S ERR="PADE DEVICE NOT UPDATED "_ERR D LOGERR(.ERR) Q
;
I ($TR(PADATA(3),"~")="")&($TR(PADATA(7),"~")="") Q ; No drawer, no pocket = no inventory update
;
; Add PADE Drawer from PADE INBOUND TRANSACTION file to PADE System Dispensing Device in PADE INVENTORY file
I '$$FILDRWR(.PADATA,.ERR) S ERR="DRAWER NOT UPDATED "_ERR D LOGERR(.ERR) Q
;
; Add drug from PADE INBOUND TRANSACTION file to PADE System Dispensing Device in PADE INVENTORY file.
I '$$FILDRUG(.PADATA,.ERR) S ERR="DRUG/DEVICE NOT UPDATED "_ERR D Q
.Q:'$G(PADATA(4)) ; Consider drug 'NOT ON FILE' only if a valid VistA IEN (all numeric). Otherwise, the ID was never intended for VistA.
.I $L($G(PSJOMS("DRGITM"))) Q:PSJOMS("DRGITM")'?1.N
.S:$L($G(PSJOMS("CABID"))) ERR=ERR_":CABINET="_PSJOMS("CABID")
.S:$L($G(PSJOMS("DISPSYS"))) ERR=ERR_":SYSTEM="_PSJOMS("DISPSYS")
.D LOGERR(.ERR) Q
;
; Add drug from PADE INBOUND TRANSACTION file to Drug sub-file in Drawer multiple of PADE INVENTORY file
I '$$DRGDRWR(.PADATA,.ERR) S ERR="DRUG/DRAWER NOT UPDATED "_ERR D LOGERR(.ERR) Q
;
; Add Pocket sub-file entry to Drawer multiple in PADE INVENTORY file
I '$$UPCKDRW(.PADATA,.ERR) S ERR="POCKET NOT UPDATED "_ERR D LOGERR(.ERR) Q
;
; Update Pocket balance
I '$$UPCKBAL(.PADATA,.ERR) S ERR="POCKET BALANCE NOT UPDATED "_ERR D LOGERR(.ERR)
; Update Drug (Drawer) balance in PADE INVENTORY file
I '$$UPDRWBAL(.PADATA,.ERR) S ERR="DRUG/DRAWER BALANCE NOT UPDATED "_ERR D LOGERR(.ERR)
;
I '($G(PSJNOTE)["PATIENT SPECIFIC BIN")&'($G(PSJNOTE)["RETURN BIN") D
.; Update Drug (Device) balance in PADE INVENTORY file
.I '$$UPDEVBAL(.PADATA,.ERR) S ERR="DRUG/DEVICE BALANCE NOT UPDATED "_ERR D LOGERR(.ERR)
;
; Delete (Unload) drug from device
I $$DELCHK(.PADATA) D
.N SYS,DEV,DRW,DRGDEV,DRG,PCK
.S SYS=$G(PADATA("SYS IEN")) ; IEN of system in file 58.601
.S DEV=$G(PADATA("DEVICE IEN")) ; IEN of device from subfile 58.6011
.S DRW=$G(PADATA("DRAWER IEN")) ; IEN of drawer from subfile 58.60112
.S DRGDEV=$G(PADATA("DRUG DEV IEN")) ; IEN of drug (device) from subfile 58.60111
.S DRG=$G(PADATA("DRUG IEN")) ; IEN of drug (drawer) from subfile 58.601121
.S PCK=$G(PADATA("POCK/SUB IEN")) ; IEN of pocket/subdrawer from subfile 58.601122
.D UNLOAD^PSJPAD70(SYS,DEV,DRW,DRG,DRGDEV,PCK)
;
; Transaction processing complete
I $G(PS586IEN) I $P($G(^PS(58.6,PS586IEN,3)),"^")]"" S $P(^PS(58.6,PS586IEN,3),"^")="C"
Q
;
DELCHK(PADATA) ; Check if incoming transaction represents a drug removed from the device
N PSJTTRAN
S PSJTTRAN=$G(PADATA(5)) I PSJTTRAN="U"!(PSJTTRAN="B") Q 1
Q 0
;
FILSYS(PADATA,ERRMSG) ; Check for PADE Inbound System in PADE INVENTORY file
K DIERR,ERR S PADATA("SYS IEN")=$$FIND1^DIC(58.601,"","MXQ",$G(PADATA(1)),,,"ERR") K DIERR ;*356
I '$G(PADATA("SYS IEN")) S ERRMSG="UNABLE TO FIND PADE SYSTEM "_PADATA(1) Q 0
Q 1
;
FILEDEV(XARRAY) ; File device from PADE DISPENSING DEVICE (#58.63) file
N PADATA0
I $G(XARRAY(2)) S PADATA("SYS IEN")=XARRAY(2)
I '$G(XARRAY(2)),$G(XARRAY("SYS IEN"))]"" S PADATA(2)=$P($G(^PS(58.601,+XARRAY("SYS IEN"),0)),"^")
I $G(XARRAY(1))]"" S PADATA(2)=XARRAY(1)
D FILDEV^PSJPADIU(.PADATA)
Q
;
FILDRWR(PADATA,ERRMSG) ; Add PADE Drawer to PADE System's DISPENSING DEVICE in PADE INVENTORY file
I $G(PADATA(3))="" S PADATA(3)="zz"
K ERR,DIERR S PADATA("DRAWER IEN")=$$FIND1^DIC(58.60112,","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",PADATA(3),,,"ERR") K DIERR ;*356
I '$G(PADATA("DRAWER IEN")) D
.K FDA,ERR,DIERR S FDA(58.60112,"?+1,"_+PADATA("DEVICE IEN")_","_+PADATA("SYS IEN")_",",.01)=PADATA(3) D UPDATE^DIE("E","FDA","","ERR") K DIERR ;*356
.K ERR,DIERR S PADATA("DRAWER IEN")=$$FIND1^DIC(58.60112,","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",PADATA(3),,,"ERR") K DIERR ;*356
I '$G(PADATA("DRAWER IEN")) S ERRMSG="Unable to file PADE drawer"_PADATA(3) Q 0
Q 1
;
FILDRUG(PADATA,ERRMSG) ; Add drug from PADE INBOUND TRANSACTION file to PADE System Dispensing Device in PADE INVENTORY file
N DRUG,DEVIEN,SYSIEN
S DRUG=$G(PADATA(4)) ; Drug
S DEVIEN=$G(PADATA("DEVICE IEN")) ; Dispensing Device IEN
S SYSIEN=$G(PADATA("SYS IEN")) ; PADE System IEN
I '(DRUG]"") S ERRMSG="MISSING DRUG" Q 0 ; No drug, no go
;
I '$D(^PSDRUG(+DRUG,0)) D Q 0
.N DRGNAME,DRGID,DRGNODE
.S DRGNODE=$G(^PS(58.6,+$G(PS586IEN),1))
.S DRGNAME=$P(DRGNODE,"^",6)
.S DRGID=$P(DRGNODE,"^",7)
.S ERRMSG="DRUG NOT ON FILE|" ; No LAYGO - Unknown DRUG (#50) file IEN
.S ERRMSG=ERRMSG_"DRG ID="_DRGID_"|NAME="_DRGNAME
; Must have PADE system and Dispensing Device
I '$G(DEVIEN) S ERRMSG="MISSING DEVICE IEN" Q 0
I '$G(SYSIEN) S ERRMSG="MISSING SYSTEM IEN" Q 0
N DA,FDA,X,Y,DIC,DIE,DR,D0,ERR,D,DD,DICR,DICRS,DO
K ERR,DIERR S PADATA("DRUG DEV IEN")=$$FIND1^DIC(58.60111,","_DEVIEN_","_SYSIEN_",","MXQ",DRUG,,,"ERR") K DIERR ;*356
I 'PADATA("DRUG DEV IEN") D
.K ERR,DIERR S FDA(58.60111,"?+1,"_+DEVIEN_","_+SYSIEN_",",.01)=DRUG D UPDATE^DIE("E","FDA","ERR") K DIERR ;*356
.K ERR,DIERR S PADATA("DRUG DEV IEN")=$$FIND1^DIC(58.60111,","_DEVIEN_","_SYSIEN_",","MXQ",DRUG,,,"ERR") K DIERR ;*356
K ERR,DIERR S PADATA("DRUG DEV IEN")=$$FIND1^DIC(58.60111,","_DEVIEN_","_SYSIEN_",","MXQ",DRUG,,,"ERR") K DIERR ;*356
I '$G(PADATA("DRUG DEV IEN")) S ERRMSG="UNABLE TO FILE DRUG "_DRUG Q 0
Q 1
;
DRGDRWR(PADATA,ERRMSG) ; Add drug to drawer in PADE INVENTORY file
N FDA,ERR
K ERR,DIERR S PADATA("DRUG IEN")=$$FIND1^DIC(58.601121,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MXQ",PADATA(4),,,"ERR") K DIERR ;*356
I '$G(PADATA("DRUG IEN")) D
.S FDA(58.601121,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",.01)=PADATA(4) K ERR,DIERR D UPDATE^DIE("","FDA","","ERR") K DIERR ;*356
.K DIERR,ERR S PADATA("DRUG IEN")=$$FIND1^DIC(58.601121,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MXQ",PADATA(4),,,"ERR") K DIERR ;*356
.Q:'PADATA("DRUG IEN") N DBALIENS,TRNSIGN,TRNSAMT
.; If a new drug was added to drawer, initialize drug's drawer balance to Actual Begin Count (PADATA(8)) from HL7 message
.S DBALIENS=PADATA("DRUG IEN")_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
.S FDA(58.601121,DBALIENS,1)=$G(PADATA(8))
.; File the updated drawer total for this drug
.K DIERR,ERR D FILE^DIE("","FDA","ERR") K DIERR ;*356
I '$G(PADATA("DRUG IEN")) S ERRMSG="Unable to file PADE drug (drawer) IEN"_PADATA(4) Q 0
Q 1
;
UPDEVBAL(PADATA,ERRMSG) ; Update Drug (Device) balance
; Get current device total count of drug
N DBALIENS,FDA,INVDATA,BALANCE,TRERR,DOSFORM
;
I $E($G(PADATA(5)))="B" Q 1 ; "BIN" transaction does not update device balance
I $E($G(PADATA(5)))="W" Q 1 ; "WASTE" transaction does not update device balance
;
; Get Device Balance from transaction, if sent
S BALANCE=+$P($G(^PS(58.6,+$G(PS586IEN),1)),"^",5)
S:BALANCE<0 BALANCE=0
;
; Done with 58.6, now set IENS to refer to 58.60111
S DBALIENS=PADATA("DRUG DEV IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
;
; Reported Device Balance. Do this before Calculated Balance.
; File transaction's Device Balance for current device and drug to the Reported Device Balance
N TRERR,FDA K DIERR S FDA(58.60111,DBALIENS,2)=BALANCE D FILE^DIE("","FDA","TRERR") K DIERR ;*356
;
; Add transaction input quantity to Drug's calculated Device balance
N TRERR,FDA,DOSFORM
; Update Drug Dosage Form from transaction
S DOSFORM=$P($G(^PS(58.6,+$G(PS586IEN),0)),"^",6)
S FDA(58.60111,DBALIENS,1)=$$DEVBAL(PADATA("SYS IEN"),PADATA("DEVICE IEN"),PADATA(4))
I DOSFORM]"" S FDA(58.60111,DBALIENS,4)=DOSFORM
K DIERR,TRERR D FILE^DIE("","FDA","TRERR") K DIERR ;*356
Q 1
;
UPDRWBAL(PADATA,ERRMSG) ; Update Drug (Drawer) balance
; Get current drawer total count of drug
N DBALIENS,INVDATA,FDA,BALANCE,II,PSYS,PDEV,PDRW,PDRG,POCK,PTOT,PNOD
S PSYS=+$G(PADATA("SYS IEN")),PDEV=+$G(PADATA("DEVICE IEN")),PDRW=+$G(PADATA("DRAWER IEN")),PDRG=+$G(PADATA("DRUG IEN"))
S (POCK,PTOT)=0 F S POCK=$O(^PS(58.601,PSYS,"DEVICE",PDEV,"DRAWER",PDRW,"SUB",POCK)) Q:'POCK S PNOD=^(POCK,0) D
.Q:$P(PNOD,"^",5)'=$G(PADATA(4))
.S PTOT=$G(PTOT)+$P(PNOD,"^",2)
N ERR
; Add the updated pocket totals to the drawer total for this drug
S DBALIENS=PDRG_","_PDRW_","_PDEV_","_PSYS_","
S FDA(58.601121,DBALIENS,1)=PTOT
; File the updated drawer total for this drug
K DIERR,ERR D FILE^DIE("","FDA","ERR") K DIERR ;*356
Q 1
;
UPCKDRW(PADATA,ERRMSG) ; Add pocket/subdrawer sub-file entry to Drawer multiple
N FDA ; Fileman data array
N ERR ; Fileman error message array
N POCKSUB ; POCKET_SUBDRAWER concatenated
N DRGIENS ; IEN string for Drug field
N INVDATA ; Data returned from PADE INVENTORY (#58.601) file
N DOSFORM ; Dose Form from HL7 message from PADE vendor
N PADATA6 ; Signed value of inbound Quantity; positive for PADATA(5)=LOAD or RETURN, negative for PADATA(5)=DISPENSE/VEND/ISSUE and UNLOAD
N PSPRVDT ; The last transaction date/time (date/time of the activity at the cabinet) this pocket was updated
N PSPRVDIE ; The IEN of the last transaction date/time in the "PKUPDT" multiple
;
S DOSFORM=$P($G(^PS(58.6,+$G(PS586IEN),0)),"^",6)
S PADATA(10)=$TR(PADATA(10),"~~")_"~~"_+$G(PADATA(4)) ; Append subdrawer unique drug IEN suffix to handle different drugs in same subdr-pocket combo
S:$G(PADATA(10))="" PADATA(10)=$P($G(^PS(58.6,+$G(PS586IEN),0)),"^",12) S:PADATA(10)="" PADATA(10)="~~"_+$G(PADATA(4))
S POCKSUB=$G(PADATA(7))_"_"_$G(PADATA(10)) ; "POCKET_SUBDRAWER" storage location
K ERR,DIERR S PADATA("POCK/SUB IEN")=$$FIND1^DIC(58.601122,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",POCKSUB,,,"ERR") K DIERR ;*356
; Get the last date/time this drawer/subdrawer~drug/pocket was updated
S PSPRVDIE=$O(^PS(58.601,+$G(PADATA("SYS IEN")),"DEVICE",+$G(PADATA("DEVICE IEN")),"DRAWER",+$G(PADATA("DRAWER IEN")),"PKUPDT","B",POCKSUB,""))
;
S PSPRVDT=$P($G(^PS(58.601,+$G(PADATA("SYS IEN")),"DEVICE",+$G(PADATA("DEVICE IEN")),"DRAWER",+$G(PADATA("DRAWER IEN")),"PKUPDT",+$G(PSPRVDIE),0)),"^",2)
; If this current update contains a transaction date/time (i.e., activity date/time) older than the last update, don't update inventory
I $G(PSPRVDT)&$G(PADATA(9)) I PADATA(9)<PSPRVDT D Q 0
.S ERRMSG="- OUTDATED TRANSACTION - "_$G(PADATA(1))_"."_$G(PADATA(2))_".DRUG="_$G(PADATA(4))_".POCKET="_$G(PADATA(7))
.S ERRMSG=ERRMSG_".LAST UPDATED="_PSPRVDT_".TRANS DT="_PADATA(9)
I 'PADATA("POCK/SUB IEN") D
.S FDA(58.601122,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",.01)=POCKSUB K DIERR,ERR D UPDATE^DIE("","FDA","","ERR") K DIERR ;*356
.K ERR,DIERR S PADATA("POCK/SUB IEN")=$$FIND1^DIC(58.601122,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",POCKSUB,,,"ERR") K DIERR ;*356
I PADATA("POCK/SUB IEN") D ; File Pocket and Subdrawer references in the POCK/SUB sub-file to ensure distinguishing between the two
.N PKSBIENS S PKSBIENS=PADATA("POCK/SUB IEN")_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
.S FDA(58.601122,PKSBIENS,1)=PADATA(4) ; DRUG
.N PKBAL S PKBAL=PADATA(8)+PADATA(6) S FDA(58.601122,PKSBIENS,2)=$S(PKBAL<0:0,1:PKBAL) ; NEW BALANCE (BALANCE FORWARD)
.S FDA(58.601122,PKSBIENS,3)=PADATA(7) ; POCKET
.S FDA(58.601122,PKSBIENS,4)=PADATA(10) ; SUBDRAWER
.I DOSFORM]"" S FDA(58.601122,PKSBIENS,5)=DOSFORM ; DRUG DOSE FORM
.K ERR,DIERR D FILE^DIE("","FDA","ERR") K DIERR ;*356
.I '$G(PSPRVDT),($G(POCKSUB)]"") D
..N FDA S FDA(58.601123,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",.01)=POCKSUB
..S FDA(58.601123,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",1)=PADATA(9)
..K DIERR,ERR D UPDATE^DIE("","FDA","","ERR") K DIERR ;*356
.K DIERR,ERR S PSPRVDIE=$$FIND1^DIC(58.601123,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",POCKSUB,,,"ERR") K DIERR ;*356
.I PSPRVDIE S DBALIENS=PSPRVDIE_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_"," D
..I $G(PADATA(9))?7N.".".8N S FDA(58.601123,DBALIENS,1)=PADATA(9) ; TRANSACTION DATE/TIME
..Q:'$G(FDA(58.601123,DBALIENS,1))
..K DIERR,ERR D FILE^DIE("","FDA","ERR") K DIERR ;*356
I 'PADATA("POCK/SUB IEN") S ERRMSG="Unable to file PADE pocket/subdrawer "_POCKSUB Q 0
Q 1
;
UPCKBAL(PADATA,ERRMSG) ; Update pocket / subdrawer balance
; Add transaction Quantity to Actual Begin Balance
;
N PCK ; Pocket ID
N PCKBEG ; Total of Actual Begin Balance
N FDA ; Fileman data array
N TRNSAMT ; Transaction amount
N PCKTOT ; New balance of pocket
N DRW ; Drawer ID
N SUBDRW ; Subdrawer ID
N DBALIENS ; IENS to System, Cabinet, Drawer, Drug, Pocket, Subdrawer
;
S PCK=$G(PADATA(10))
S TRNSAMT=$G(PADATA(6))
S:$E(TRNSAMT)="-" TRNSAMT=+$P(TRNSAMT,"-",2)
S TRNSAMT=$$TSIGN(.PADATA)_TRNSAMT
S PCKBEG=$S($G(PADATA(8))&($G(PADATA(5))="C"):$G(PADATA(8)),1:$G(PADATA(12)))
S PCKTOT=$S($E($G(PADATA(5)))="C":PCKBEG,1:PCKBEG+TRNSAMT)
S PCKTOT=$S(PCKTOT<0:0,1:PCKTOT)
S DRW=$G(PADATA(1.2))
S SUBDRW=$G(PADATA(10))
S DBALIENS=PADATA("POCK/SUB IEN")_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
S FDA(58.601122,DBALIENS,2)=PCKTOT
; File the updated drawer total for this drug
K DIERR,ERR D FILE^DIE("","FDA","ERR") K DIERR ;*356
Q 1
;
LOGERR(ERRMSG) ; Log error ERRMSG
D ERROR^PSJPAD7U(.ERRMSG)
Q
;
ORDIX(PS586IEN) ; Computed ORDER to handle blank orders in primary patient sort PADE cross reference - "zz" = patient transaction types without order
N ORDER,RESULTS,RESULT
K DIERR,ERROR D GETS^DIQ(58.6,PS586IEN_",","4;15","","RESULT","ERROR") K DIERR ;*356
S ORDER=$G(RESULT(58.6,PS586IEN_",",15))
Q:$G(ORDER) ORDER
Q:(",LOAD,UNLOAD,"[(","_$G(RESULT(58.6,PS586IEN_",",4))_",")) "INVENTORY"
Q "zz"
;
DRWRBAL(PADESYS,PADEDEV,PADEDRWR,DRUGIEN) ; Calculate drawer balance for PADE device=PADEDEV, drawer=PADEDRWR, drug=DRUGIEN
; PADESYS = PADE vendor system IEN from file 58.601
; PADEDEV = PADE device IEN from file 58.601
; PADEDRWR = PADE drawer from file 58.601
; DRUGIEN = Drug IEN from file 58.601 (points to file 50)
K DRWBAL S DRWBAL="" ; Initialize returned balance
N SUBDRW ; Pocket_subdrawer IEN
N OUT ; Return array from LIST^DIC
;
Q:'$G(PADESYS)!'$G(PADEDEV)!'$G(PADEDRWR) "" ; We need system, device, and drawer to find a drawer balance
Q:'DRUGIEN ""
D LIST^DIC(58.601121,","_PADEDRWR_","_PADEDEV_","_PADESYS_",","1IE;2","","","","","","","","OUT","WTF")
S SUBDRW=0 F S SUBDRW=$O(OUT("DILIST","ID",SUBDRW)) Q:'SUBDRW D
.N DRUG S DRUG=$G(OUT("DILIST","ID",SUBDRW,1,"I")) I DRUG,(DRUG=DRUGIEN) S DRWBAL=DRWBAL+$G(OUT("DILIST","ID",SUBDRW,2))
Q DRWBAL
;
DEVBAL(PADESYS,PADEDEV,DRUGIEN) ; Calculate Device BALANCE for PADE device=PADEDEV drug=DRUGIEN
Q $$DEVBAL^PSJPDRU1(PADESYS,PADEDEV,DRUGIEN)
;
TSIGN(PADATA) ; Determine if the transaction amount needs to be added or subtracted, depending on the transaction type
Q $$TSIGN^PSJPDRU1(.PADATA)
--- Routine Detail --- with STRUCTURED ROUTINE LISTING ---[H[J[2J[HPSJPADIT 19521 printed Dec 13, 2024@02:08:36 Page 2
PSJPADIT ;BIR/JCH-INPATIENT PADE INVENTORY FILE UTILITIES ;25 SEP 97 / 7:41 AM
+1 ;;5.0;INPATIENT MEDICATIONS ;**317,356**;16 DEC 97;Build 7
+2 ;Per VHA Directive 2004-038, this routine should not be modified.
+3 ;
+4 ; Reference to ^XLFDT is supported by DBIA 10103.
+5 ; Reference to ^PSDRUG is supported by DBIA 2192.
+6 ;
FIL58601(PADATA,PSJOMS) ; File PADE data into PADE INVENTORY file #58.601
+1 ; PADE HL7 messages filed into the PADE IN TRANSACTION (#58.6) trigger new-style 'AC' action cross reference, entering here.
+2 ;
+3 ;INPUT:PADATA array - Passed in X(n) array from ACTION cross reference on PADE INBOUND TRANSACTIONS (#58.6) file.
+4 ; Array Node File# Field# Field Name Description
+5 ; ---------- ------ ------ ------------------ -------------
+6 ; PADATA(1) 58.6 1.1 PADE VENDOR IEN of PADE system from file 58.7
+7 ; PADATA(2) 58.6 1 DISPENSING DEVICE Name (text) of PADE DEVICE
+8 ; PADATA(3) 58.6 1.2 DRAWER Name (text) of PADE DRAWER
+9 ; PADATA(4) 58.6 2 DRUG Pointer to DRUG (#50) file.
+10 ; PADATA(5) 58.6 4 TYPE OF TRANSACTION Set of codes describing transaction (L=LOAD,U=UNLOAD,D=DISPENSE,R=RETURN,X=DESTROY).
+11 ; PADATA(6) 58.6 3 QUANTITY Quantity (item count) of drugs documented in transaction.
+12 ; PADATA(7) 58.6 10 POCKET Most specific storage location within drawer or subdrawer.
+13 ; PADATA(8) 58.6 13 ACTUAL BEGIN BALANCE Actual item count performed at PADE device prior to transaction.
+14 ; PADATA(9) 58.6 .01 TRANSACTION DATE/TIME Date/Time the activity occurred at the dispensing device
+15 ; PADATA(10) 58.6 11 SUBDRAWER Another storage division of a cabinet
+16 ; PADATA(12) 58.6 12 EXPECTED BEGIN COUNT Expected Quantity of Drug in Bin/Pocket at start of transaction
+17 ; PADATA(16) 58.6 16 POCKET BALANCE Item count of pocket/bin
+18 ;
+19 ;INPUT:PSJOMS array - Additional PADE HL7 information
+20 ; PSJOMS("DRGITM") - Drug ID
+21 ; PSJOMS("CABID") - PADE Cabinet ID
+22 ; PSJOMS("DISPSYS")- PADE System
+23 ;
+24 NEW ERRMSG,PADNOW,PSJNOTE
+25 ; No data passed in
if '$DATA(PADATA)>1
QUIT
+26 ; Don't allow future Last Pocket Update date/times (could suppress actual updates)
+27 SET PADNOW=$$NOW^XLFDT()
+28 IF $GET(PADATA(9))>PADNOW
SET PADATA(9)=PADNOW
+29 ;
+30 ; Array to hold data returned from GETS^DIQ calls (re-initialized and re-used)
NEW INVDATA
+31 ; Array to hold messages returned from DIE and DIC calls. (re-initialized and re-used).
NEW ERR
+32 ; Array containing the name of the root of a VA FileMan Data Array
NEW FDA
+33 ;
+34 NEW PS586IEN
SET PS586IEN=+$GET(DA)
IF PS586IEN
IF $PIECE($GET(^PS(58.6,PS586IEN,3)),"^")]""
SET $PIECE(^PS(58.6,PS586IEN,3),"^")="I"
+35 SET PSJNOTE=$$UPPER^HLFNC($GET(^PS(58.6,+$GET(PS586IEN),2)))
+36 ; Cancel Inventory Update if comments contain 'CANCEL'
IF PSJNOTE["CANCEL"
QUIT
+37 SET PADATA(12)=$PIECE($GET(^PS(58.6,+$GET(PS586IEN),0)),"^",13)
+38 SET PADATA(16)=$PIECE($GET(^PS(58.6,+$GET(PS586IEN),1)),"^",2)
+39 ;S PADATA(4)=$P($G(^PS(58.6,+$G(PS586IEN),0)),"^",3) ;UNCOMMENT THIS LINE IN TEST SYSTEM ONLY TO MOCK TEST CREATING AN INVENTORY UPDATE IN FILEMAN
+40 ; Waste Transaction should not update inventory, unless RETURN BIN
if $EXTRACT($GET(PADATA(5)))="W"&(PSJNOTE'["RETURN BIN")
QUIT
+41 ;
+42 ; Quit if this transaction is older than the most recent update date/time for the pocket-drug
+43 NEW TMPARRAY
MERGE PSTMPAR=PADATA
+44 IF $$OLDPKUP^PSJPAD70(.PSTMPAR,.ERR,PS586IEN)
DO LOGERR(.ERR)
QUIT
+45 KILL PSTMPAR
+46 ;
+47 ; File PADE System from PADE INBOUND TRANSACTION file to PADE INVENTORY file
+48 IF '$$FILSYS(.PADATA,.ERR)
SET ERR="CANNOT FILE SYSTEM "_ERR
DO LOGERR(.ERR)
QUIT
+49 ;
+50 ; File PADE Device from PADE INBOUND TRANSACTION file to PADE INVENTORY file
+51 IF '$$FILDEV^PSJPADIU(.PADATA,.ERR)
SET ERR="PADE DEVICE NOT UPDATED "_ERR
DO LOGERR(.ERR)
QUIT
+52 ;
+53 ; No drawer, no pocket = no inventory update
IF ($TRANSLATE(PADATA(3),"~")="")&($TRANSLATE(PADATA(7),"~")="")
QUIT
+54 ;
+55 ; Add PADE Drawer from PADE INBOUND TRANSACTION file to PADE System Dispensing Device in PADE INVENTORY file
+56 IF '$$FILDRWR(.PADATA,.ERR)
SET ERR="DRAWER NOT UPDATED "_ERR
DO LOGERR(.ERR)
QUIT
+57 ;
+58 ; Add drug from PADE INBOUND TRANSACTION file to PADE System Dispensing Device in PADE INVENTORY file.
+59 IF '$$FILDRUG(.PADATA,.ERR)
SET ERR="DRUG/DEVICE NOT UPDATED "_ERR
Begin DoDot:1
+60 ; Consider drug 'NOT ON FILE' only if a valid VistA IEN (all numeric). Otherwise, the ID was never intended for VistA.
if '$GET(PADATA(4))
QUIT
+61 IF $LENGTH($GET(PSJOMS("DRGITM")))
if PSJOMS("DRGITM")'?1.N
QUIT
+62 if $LENGTH($GET(PSJOMS("CABID")))
SET ERR=ERR_":CABINET="_PSJOMS("CABID")
+63 if $LENGTH($GET(PSJOMS("DISPSYS")))
SET ERR=ERR_":SYSTEM="_PSJOMS("DISPSYS")
+64 DO LOGERR(.ERR)
QUIT
End DoDot:1
QUIT
+65 ;
+66 ; Add drug from PADE INBOUND TRANSACTION file to Drug sub-file in Drawer multiple of PADE INVENTORY file
+67 IF '$$DRGDRWR(.PADATA,.ERR)
SET ERR="DRUG/DRAWER NOT UPDATED "_ERR
DO LOGERR(.ERR)
QUIT
+68 ;
+69 ; Add Pocket sub-file entry to Drawer multiple in PADE INVENTORY file
+70 IF '$$UPCKDRW(.PADATA,.ERR)
SET ERR="POCKET NOT UPDATED "_ERR
DO LOGERR(.ERR)
QUIT
+71 ;
+72 ; Update Pocket balance
+73 IF '$$UPCKBAL(.PADATA,.ERR)
SET ERR="POCKET BALANCE NOT UPDATED "_ERR
DO LOGERR(.ERR)
+74 ; Update Drug (Drawer) balance in PADE INVENTORY file
+75 IF '$$UPDRWBAL(.PADATA,.ERR)
SET ERR="DRUG/DRAWER BALANCE NOT UPDATED "_ERR
DO LOGERR(.ERR)
+76 ;
+77 IF '($GET(PSJNOTE)["PATIENT SPECIFIC BIN")&'($GET(PSJNOTE)["RETURN BIN")
Begin DoDot:1
+78 ; Update Drug (Device) balance in PADE INVENTORY file
+79 IF '$$UPDEVBAL(.PADATA,.ERR)
SET ERR="DRUG/DEVICE BALANCE NOT UPDATED "_ERR
DO LOGERR(.ERR)
End DoDot:1
+80 ;
+81 ; Delete (Unload) drug from device
+82 IF $$DELCHK(.PADATA)
Begin DoDot:1
+83 NEW SYS,DEV,DRW,DRGDEV,DRG,PCK
+84 ; IEN of system in file 58.601
SET SYS=$GET(PADATA("SYS IEN"))
+85 ; IEN of device from subfile 58.6011
SET DEV=$GET(PADATA("DEVICE IEN"))
+86 ; IEN of drawer from subfile 58.60112
SET DRW=$GET(PADATA("DRAWER IEN"))
+87 ; IEN of drug (device) from subfile 58.60111
SET DRGDEV=$GET(PADATA("DRUG DEV IEN"))
+88 ; IEN of drug (drawer) from subfile 58.601121
SET DRG=$GET(PADATA("DRUG IEN"))
+89 ; IEN of pocket/subdrawer from subfile 58.601122
SET PCK=$GET(PADATA("POCK/SUB IEN"))
+90 DO UNLOAD^PSJPAD70(SYS,DEV,DRW,DRG,DRGDEV,PCK)
End DoDot:1
+91 ;
+92 ; Transaction processing complete
+93 IF $GET(PS586IEN)
IF $PIECE($GET(^PS(58.6,PS586IEN,3)),"^")]""
SET $PIECE(^PS(58.6,PS586IEN,3),"^")="C"
+94 QUIT
+95 ;
DELCHK(PADATA) ; Check if incoming transaction represents a drug removed from the device
+1 NEW PSJTTRAN
+2 SET PSJTTRAN=$GET(PADATA(5))
IF PSJTTRAN="U"!(PSJTTRAN="B")
QUIT 1
+3 QUIT 0
+4 ;
FILSYS(PADATA,ERRMSG) ; Check for PADE Inbound System in PADE INVENTORY file
+1 ;*356
KILL DIERR,ERR
SET PADATA("SYS IEN")=$$FIND1^DIC(58.601,"","MXQ",$GET(PADATA(1)),,,"ERR")
KILL DIERR
+2 IF '$GET(PADATA("SYS IEN"))
SET ERRMSG="UNABLE TO FIND PADE SYSTEM "_PADATA(1)
QUIT 0
+3 QUIT 1
+4 ;
FILEDEV(XARRAY) ; File device from PADE DISPENSING DEVICE (#58.63) file
+1 NEW PADATA0
+2 IF $GET(XARRAY(2))
SET PADATA("SYS IEN")=XARRAY(2)
+3 IF '$GET(XARRAY(2))
IF $GET(XARRAY("SYS IEN"))]""
SET PADATA(2)=$PIECE($GET(^PS(58.601,+XARRAY("SYS IEN"),0)),"^")
+4 IF $GET(XARRAY(1))]""
SET PADATA(2)=XARRAY(1)
+5 DO FILDEV^PSJPADIU(.PADATA)
+6 QUIT
+7 ;
FILDRWR(PADATA,ERRMSG) ; Add PADE Drawer to PADE System's DISPENSING DEVICE in PADE INVENTORY file
+1 IF $GET(PADATA(3))=""
SET PADATA(3)="zz"
+2 ;*356
KILL ERR,DIERR
SET PADATA("DRAWER IEN")=$$FIND1^DIC(58.60112,","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",PADATA(3),,,"ERR")
KILL DIERR
+3 IF '$GET(PADATA("DRAWER IEN"))
Begin DoDot:1
+4 ;*356
KILL FDA,ERR,DIERR
SET FDA(58.60112,"?+1,"_+PADATA("DEVICE IEN")_","_+PADATA("SYS IEN")_",",.01)=PADATA(3)
DO UPDATE^DIE("E","FDA","","ERR")
KILL DIERR
+5 ;*356
KILL ERR,DIERR
SET PADATA("DRAWER IEN")=$$FIND1^DIC(58.60112,","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",PADATA(3),,,"ERR")
KILL DIERR
End DoDot:1
+6 IF '$GET(PADATA("DRAWER IEN"))
SET ERRMSG="Unable to file PADE drawer"_PADATA(3)
QUIT 0
+7 QUIT 1
+8 ;
FILDRUG(PADATA,ERRMSG) ; Add drug from PADE INBOUND TRANSACTION file to PADE System Dispensing Device in PADE INVENTORY file
+1 NEW DRUG,DEVIEN,SYSIEN
+2 ; Drug
SET DRUG=$GET(PADATA(4))
+3 ; Dispensing Device IEN
SET DEVIEN=$GET(PADATA("DEVICE IEN"))
+4 ; PADE System IEN
SET SYSIEN=$GET(PADATA("SYS IEN"))
+5 ; No drug, no go
IF '(DRUG]"")
SET ERRMSG="MISSING DRUG"
QUIT 0
+6 ;
+7 IF '$DATA(^PSDRUG(+DRUG,0))
Begin DoDot:1
+8 NEW DRGNAME,DRGID,DRGNODE
+9 SET DRGNODE=$GET(^PS(58.6,+$GET(PS586IEN),1))
+10 SET DRGNAME=$PIECE(DRGNODE,"^",6)
+11 SET DRGID=$PIECE(DRGNODE,"^",7)
+12 ; No LAYGO - Unknown DRUG (#50) file IEN
SET ERRMSG="DRUG NOT ON FILE|"
+13 SET ERRMSG=ERRMSG_"DRG ID="_DRGID_"|NAME="_DRGNAME
End DoDot:1
QUIT 0
+14 ; Must have PADE system and Dispensing Device
+15 IF '$GET(DEVIEN)
SET ERRMSG="MISSING DEVICE IEN"
QUIT 0
+16 IF '$GET(SYSIEN)
SET ERRMSG="MISSING SYSTEM IEN"
QUIT 0
+17 NEW DA,FDA,X,Y,DIC,DIE,DR,D0,ERR,D,DD,DICR,DICRS,DO
+18 ;*356
KILL ERR,DIERR
SET PADATA("DRUG DEV IEN")=$$FIND1^DIC(58.60111,","_DEVIEN_","_SYSIEN_",","MXQ",DRUG,,,"ERR")
KILL DIERR
+19 IF 'PADATA("DRUG DEV IEN")
Begin DoDot:1
+20 ;*356
KILL ERR,DIERR
SET FDA(58.60111,"?+1,"_+DEVIEN_","_+SYSIEN_",",.01)=DRUG
DO UPDATE^DIE("E","FDA","ERR")
KILL DIERR
+21 ;*356
KILL ERR,DIERR
SET PADATA("DRUG DEV IEN")=$$FIND1^DIC(58.60111,","_DEVIEN_","_SYSIEN_",","MXQ",DRUG,,,"ERR")
KILL DIERR
End DoDot:1
+22 ;*356
KILL ERR,DIERR
SET PADATA("DRUG DEV IEN")=$$FIND1^DIC(58.60111,","_DEVIEN_","_SYSIEN_",","MXQ",DRUG,,,"ERR")
KILL DIERR
+23 IF '$GET(PADATA("DRUG DEV IEN"))
SET ERRMSG="UNABLE TO FILE DRUG "_DRUG
QUIT 0
+24 QUIT 1
+25 ;
DRGDRWR(PADATA,ERRMSG) ; Add drug to drawer in PADE INVENTORY file
+1 NEW FDA,ERR
+2 ;*356
KILL ERR,DIERR
SET PADATA("DRUG IEN")=$$FIND1^DIC(58.601121,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MXQ",PADATA(4),,,"ERR")
KILL DIERR
+3 IF '$GET(PADATA("DRUG IEN"))
Begin DoDot:1
+4 ;*356
SET FDA(58.601121,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",.01)=PADATA(4)
KILL ERR,DIERR
DO UPDATE^DIE("","FDA","","ERR")
KILL DIERR
+5 ;*356
KILL DIERR,ERR
SET PADATA("DRUG IEN")=$$FIND1^DIC(58.601121,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MXQ",PADATA(4),,,"ERR")
KILL DIERR
+6 if 'PADATA("DRUG IEN")
QUIT
NEW DBALIENS,TRNSIGN,TRNSAMT
+7 ; If a new drug was added to drawer, initialize drug's drawer balance to Actual Begin Count (PADATA(8)) from HL7 message
+8 SET DBALIENS=PADATA("DRUG IEN")_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
+9 SET FDA(58.601121,DBALIENS,1)=$GET(PADATA(8))
+10 ; File the updated drawer total for this drug
+11 ;*356
KILL DIERR,ERR
DO FILE^DIE("","FDA","ERR")
KILL DIERR
End DoDot:1
+12 IF '$GET(PADATA("DRUG IEN"))
SET ERRMSG="Unable to file PADE drug (drawer) IEN"_PADATA(4)
QUIT 0
+13 QUIT 1
+14 ;
UPDEVBAL(PADATA,ERRMSG) ; Update Drug (Device) balance
+1 ; Get current device total count of drug
+2 NEW DBALIENS,FDA,INVDATA,BALANCE,TRERR,DOSFORM
+3 ;
+4 ; "BIN" transaction does not update device balance
IF $EXTRACT($GET(PADATA(5)))="B"
QUIT 1
+5 ; "WASTE" transaction does not update device balance
IF $EXTRACT($GET(PADATA(5)))="W"
QUIT 1
+6 ;
+7 ; Get Device Balance from transaction, if sent
+8 SET BALANCE=+$PIECE($GET(^PS(58.6,+$GET(PS586IEN),1)),"^",5)
+9 if BALANCE<0
SET BALANCE=0
+10 ;
+11 ; Done with 58.6, now set IENS to refer to 58.60111
+12 SET DBALIENS=PADATA("DRUG DEV IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
+13 ;
+14 ; Reported Device Balance. Do this before Calculated Balance.
+15 ; File transaction's Device Balance for current device and drug to the Reported Device Balance
+16 ;*356
NEW TRERR,FDA
KILL DIERR
SET FDA(58.60111,DBALIENS,2)=BALANCE
DO FILE^DIE("","FDA","TRERR")
KILL DIERR
+17 ;
+18 ; Add transaction input quantity to Drug's calculated Device balance
+19 NEW TRERR,FDA,DOSFORM
+20 ; Update Drug Dosage Form from transaction
+21 SET DOSFORM=$PIECE($GET(^PS(58.6,+$GET(PS586IEN),0)),"^",6)
+22 SET FDA(58.60111,DBALIENS,1)=$$DEVBAL(PADATA("SYS IEN"),PADATA("DEVICE IEN"),PADATA(4))
+23 IF DOSFORM]""
SET FDA(58.60111,DBALIENS,4)=DOSFORM
+24 ;*356
KILL DIERR,TRERR
DO FILE^DIE("","FDA","TRERR")
KILL DIERR
+25 QUIT 1
+26 ;
UPDRWBAL(PADATA,ERRMSG) ; Update Drug (Drawer) balance
+1 ; Get current drawer total count of drug
+2 NEW DBALIENS,INVDATA,FDA,BALANCE,II,PSYS,PDEV,PDRW,PDRG,POCK,PTOT,PNOD
+3 SET PSYS=+$GET(PADATA("SYS IEN"))
SET PDEV=+$GET(PADATA("DEVICE IEN"))
SET PDRW=+$GET(PADATA("DRAWER IEN"))
SET PDRG=+$GET(PADATA("DRUG IEN"))
+4 SET (POCK,PTOT)=0
FOR
SET POCK=$ORDER(^PS(58.601,PSYS,"DEVICE",PDEV,"DRAWER",PDRW,"SUB",POCK))
if 'POCK
QUIT
SET PNOD=^(POCK,0)
Begin DoDot:1
+5 if $PIECE(PNOD,"^",5)'=$GET(PADATA(4))
QUIT
+6 SET PTOT=$GET(PTOT)+$PIECE(PNOD,"^",2)
End DoDot:1
+7 NEW ERR
+8 ; Add the updated pocket totals to the drawer total for this drug
+9 SET DBALIENS=PDRG_","_PDRW_","_PDEV_","_PSYS_","
+10 SET FDA(58.601121,DBALIENS,1)=PTOT
+11 ; File the updated drawer total for this drug
+12 ;*356
KILL DIERR,ERR
DO FILE^DIE("","FDA","ERR")
KILL DIERR
+13 QUIT 1
+14 ;
UPCKDRW(PADATA,ERRMSG) ; Add pocket/subdrawer sub-file entry to Drawer multiple
+1 ; Fileman data array
NEW FDA
+2 ; Fileman error message array
NEW ERR
+3 ; POCKET_SUBDRAWER concatenated
NEW POCKSUB
+4 ; IEN string for Drug field
NEW DRGIENS
+5 ; Data returned from PADE INVENTORY (#58.601) file
NEW INVDATA
+6 ; Dose Form from HL7 message from PADE vendor
NEW DOSFORM
+7 ; Signed value of inbound Quantity; positive for PADATA(5)=LOAD or RETURN, negative for PADATA(5)=DISPENSE/VEND/ISSUE and UNLOAD
NEW PADATA6
+8 ; The last transaction date/time (date/time of the activity at the cabinet) this pocket was updated
NEW PSPRVDT
+9 ; The IEN of the last transaction date/time in the "PKUPDT" multiple
NEW PSPRVDIE
+10 ;
+11 SET DOSFORM=$PIECE($GET(^PS(58.6,+$GET(PS586IEN),0)),"^",6)
+12 ; Append subdrawer unique drug IEN suffix to handle different drugs in same subdr-pocket combo
SET PADATA(10)=$TRANSLATE(PADATA(10),"~~")_"~~"_+$GET(PADATA(4))
+13 if $GET(PADATA(10))=""
SET PADATA(10)=$PIECE($GET(^PS(58.6,+$GET(PS586IEN),0)),"^",12)
if PADATA(10)=""
SET PADATA(10)="~~"_+$GET(PADATA(4))
+14 ; "POCKET_SUBDRAWER" storage location
SET POCKSUB=$GET(PADATA(7))_"_"_$GET(PADATA(10))
+15 ;*356
KILL ERR,DIERR
SET PADATA("POCK/SUB IEN")=$$FIND1^DIC(58.601122,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",POCKSUB,,,"ERR")
KILL DIERR
+16 ; Get the last date/time this drawer/subdrawer~drug/pocket was updated
+17 SET PSPRVDIE=$ORDER(^PS(58.601,+$GET(PADATA("SYS IEN")),"DEVICE",+$GET(PADATA("DEVICE IEN")),"DRAWER",+$GET(PADATA("DRAWER IEN")),"PKUPDT","B",POCKSUB,""))
+18 ;
+19 SET PSPRVDT=$PIECE($GET(^PS(58.601,+$GET(PADATA("SYS IEN")),"DEVICE",+$GET(PADATA("DEVICE IEN")),"DRAWER",+$GET(PADATA("DRAWER IEN")),"PKUPDT",+$GET(PSPRVDIE),0)),"^",2)
+20 ; If this current update contains a transaction date/time (i.e., activity date/time) older than the last update, don't update inventory
+21 IF $GET(PSPRVDT)&$GET(PADATA(9))
IF PADATA(9)<PSPRVDT
Begin DoDot:1
+22 SET ERRMSG="- OUTDATED TRANSACTION - "_$GET(PADATA(1))_"."_$GET(PADATA(2))_".DRUG="_$GET(PADATA(4))_".POCKET="_$GET(PADATA(7))
+23 SET ERRMSG=ERRMSG_".LAST UPDATED="_PSPRVDT_".TRANS DT="_PADATA(9)
End DoDot:1
QUIT 0
+24 IF 'PADATA("POCK/SUB IEN")
Begin DoDot:1
+25 ;*356
SET FDA(58.601122,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",.01)=POCKSUB
KILL DIERR,ERR
DO UPDATE^DIE("","FDA","","ERR")
KILL DIERR
+26 ;*356
KILL ERR,DIERR
SET PADATA("POCK/SUB IEN")=$$FIND1^DIC(58.601122,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",POCKSUB,,,"ERR")
KILL DIERR
End DoDot:1
+27 ; File Pocket and Subdrawer references in the POCK/SUB sub-file to ensure distinguishing between the two
IF PADATA("POCK/SUB IEN")
Begin DoDot:1
+28 NEW PKSBIENS
SET PKSBIENS=PADATA("POCK/SUB IEN")_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
+29 ; DRUG
SET FDA(58.601122,PKSBIENS,1)=PADATA(4)
+30 ; NEW BALANCE (BALANCE FORWARD)
NEW PKBAL
SET PKBAL=PADATA(8)+PADATA(6)
SET FDA(58.601122,PKSBIENS,2)=$SELECT(PKBAL<0:0,1:PKBAL)
+31 ; POCKET
SET FDA(58.601122,PKSBIENS,3)=PADATA(7)
+32 ; SUBDRAWER
SET FDA(58.601122,PKSBIENS,4)=PADATA(10)
+33 ; DRUG DOSE FORM
IF DOSFORM]""
SET FDA(58.601122,PKSBIENS,5)=DOSFORM
+34 ;*356
KILL ERR,DIERR
DO FILE^DIE("","FDA","ERR")
KILL DIERR
+35 IF '$GET(PSPRVDT)
IF ($GET(POCKSUB)]"")
Begin DoDot:2
+36 NEW FDA
SET FDA(58.601123,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",.01)=POCKSUB
+37 SET FDA(58.601123,"?+1,"_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",",1)=PADATA(9)
+38 ;*356
KILL DIERR,ERR
DO UPDATE^DIE("","FDA","","ERR")
KILL DIERR
End DoDot:2
+39 ;*356
KILL DIERR,ERR
SET PSPRVDIE=$$FIND1^DIC(58.601123,","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_",","MX",POCKSUB,,,"ERR")
KILL DIERR
+40 IF PSPRVDIE
SET DBALIENS=PSPRVDIE_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
Begin DoDot:2
+41 ; TRANSACTION DATE/TIME
IF $GET(PADATA(9))?7N.".".8N
SET FDA(58.601123,DBALIENS,1)=PADATA(9)
+42 if '$GET(FDA(58.601123,DBALIENS,1))
QUIT
+43 ;*356
KILL DIERR,ERR
DO FILE^DIE("","FDA","ERR")
KILL DIERR
End DoDot:2
End DoDot:1
+44 IF 'PADATA("POCK/SUB IEN")
SET ERRMSG="Unable to file PADE pocket/subdrawer "_POCKSUB
QUIT 0
+45 QUIT 1
+46 ;
UPCKBAL(PADATA,ERRMSG) ; Update pocket / subdrawer balance
+1 ; Add transaction Quantity to Actual Begin Balance
+2 ;
+3 ; Pocket ID
NEW PCK
+4 ; Total of Actual Begin Balance
NEW PCKBEG
+5 ; Fileman data array
NEW FDA
+6 ; Transaction amount
NEW TRNSAMT
+7 ; New balance of pocket
NEW PCKTOT
+8 ; Drawer ID
NEW DRW
+9 ; Subdrawer ID
NEW SUBDRW
+10 ; IENS to System, Cabinet, Drawer, Drug, Pocket, Subdrawer
NEW DBALIENS
+11 ;
+12 SET PCK=$GET(PADATA(10))
+13 SET TRNSAMT=$GET(PADATA(6))
+14 if $EXTRACT(TRNSAMT)="-"
SET TRNSAMT=+$PIECE(TRNSAMT,"-",2)
+15 SET TRNSAMT=$$TSIGN(.PADATA)_TRNSAMT
+16 SET PCKBEG=$SELECT($GET(PADATA(8))&($GET(PADATA(5))="C"):$GET(PADATA(8)),1:$GET(PADATA(12)))
+17 SET PCKTOT=$SELECT($EXTRACT($GET(PADATA(5)))="C":PCKBEG,1:PCKBEG+TRNSAMT)
+18 SET PCKTOT=$SELECT(PCKTOT<0:0,1:PCKTOT)
+19 SET DRW=$GET(PADATA(1.2))
+20 SET SUBDRW=$GET(PADATA(10))
+21 SET DBALIENS=PADATA("POCK/SUB IEN")_","_PADATA("DRAWER IEN")_","_PADATA("DEVICE IEN")_","_PADATA("SYS IEN")_","
+22 SET FDA(58.601122,DBALIENS,2)=PCKTOT
+23 ; File the updated drawer total for this drug
+24 ;*356
KILL DIERR,ERR
DO FILE^DIE("","FDA","ERR")
KILL DIERR
+25 QUIT 1
+26 ;
LOGERR(ERRMSG) ; Log error ERRMSG
+1 DO ERROR^PSJPAD7U(.ERRMSG)
+2 QUIT
+3 ;
ORDIX(PS586IEN) ; Computed ORDER to handle blank orders in primary patient sort PADE cross reference - "zz" = patient transaction types without order
+1 NEW ORDER,RESULTS,RESULT
+2 ;*356
KILL DIERR,ERROR
DO GETS^DIQ(58.6,PS586IEN_",","4;15","","RESULT","ERROR")
KILL DIERR
+3 SET ORDER=$GET(RESULT(58.6,PS586IEN_",",15))
+4 if $GET(ORDER)
QUIT ORDER
+5 if (",LOAD,UNLOAD,"[(","_$GET(RESULT(58.6,PS586IEN_",",4))_","))
QUIT "INVENTORY"
+6 QUIT "zz"
+7 ;
DRWRBAL(PADESYS,PADEDEV,PADEDRWR,DRUGIEN) ; Calculate drawer balance for PADE device=PADEDEV, drawer=PADEDRWR, drug=DRUGIEN
+1 ; PADESYS = PADE vendor system IEN from file 58.601
+2 ; PADEDEV = PADE device IEN from file 58.601
+3 ; PADEDRWR = PADE drawer from file 58.601
+4 ; DRUGIEN = Drug IEN from file 58.601 (points to file 50)
+5 ; Initialize returned balance
KILL DRWBAL
SET DRWBAL=""
+6 ; Pocket_subdrawer IEN
NEW SUBDRW
+7 ; Return array from LIST^DIC
NEW OUT
+8 ;
+9 ; We need system, device, and drawer to find a drawer balance
if '$GET(PADESYS)!'$GET(PADEDEV)!'$GET(PADEDRWR)
QUIT ""
+10 if 'DRUGIEN
QUIT ""
+11 DO LIST^DIC(58.601121,","_PADEDRWR_","_PADEDEV_","_PADESYS_",","1IE;2","","","","","","","","OUT","WTF")
+12 SET SUBDRW=0
FOR
SET SUBDRW=$ORDER(OUT("DILIST","ID",SUBDRW))
if 'SUBDRW
QUIT
Begin DoDot:1
+13 NEW DRUG
SET DRUG=$GET(OUT("DILIST","ID",SUBDRW,1,"I"))
IF DRUG
IF (DRUG=DRUGIEN)
SET DRWBAL=DRWBAL+$GET(OUT("DILIST","ID",SUBDRW,2))
End DoDot:1
+14 QUIT DRWBAL
+15 ;
DEVBAL(PADESYS,PADEDEV,DRUGIEN) ; Calculate Device BALANCE for PADE device=PADEDEV drug=DRUGIEN
+1 QUIT $$DEVBAL^PSJPDRU1(PADESYS,PADEDEV,DRUGIEN)
+2 ;
TSIGN(PADATA) ; Determine if the transaction amount needs to be added or subtracted, depending on the transaction type
+1 QUIT $$TSIGN^PSJPDRU1(.PADATA)