VWPSSXPD	; VW/SMH - Update the Drug File and friends... ; Wed Apr 12 2012
	;;WorldVista Modifications;1.0;;;Build 3
	; (C) Sam Habiel
	; Licensed under AGPL
	;
	; This routine contains utilities to remove a drug file and install a new drug
	; file in VISTA.
	; Workflow:
	; 1. Call KILLDRUG to remove all Drug Data
	; 2. Restore a global containing a Drug File (^PSDRUG) and Pharmacy Orderable 
	;    Item file (^PS(50.6)). Can use a KIDS global transport.
	; 3. Call RESTOCK to sync CPRS files back with drug files.
	;
	; PEPs: KILLDRUG to remove all Drugs
	;       RESTOCK to add the drugs back to CPRS
	; 
	; KIDS EPs:
	;	ENV -> Environment Check
	;	TRAN -> Pre-Transport Routine (to get data from original system)
	;	PRE -> Pre-Init for the Destination System
	;	POST -> Post-Init for the Destination System
	;
	; Environment Check falls through from the top
	; 
	; As of Build 3, this routine supports transporting a drug file using
	; -> plain jane KIDS builds rather than a manual workflow outlined above.
	; 
	; You do it by creating a KIDS build that calls the follwing:
	; - ENV is the Environment Check for the Build
	; - TRAN is the Pre-Transport Routine for your originating system drug data
	; - PRE is the Pre-Init for the Destination System
	; - POST is the Post-Init for the Destination System
	; 
	; -- START KIDS HOOKS --
	;
ENV	; Environment Check Routine; KIDS EP; top fallthough
	N VWPROD S VWPROD=$$PROD^XUPROD() ; Check if a production acc; +ve val is yes
	W $$CJ^XLFSTR("------   WARNING WARNING WARNING   -----",80),!!  ; Center Justify
	W "This package will delete your drug file and add a new drug file contained",!
	W "in the distribution.",!!
	W "If you have patient pharmacy data, this will CORRUPT your database.",!!
	W "You are running in a "_$S(VWPROD:"Production",1:"Test")_" Environment.",!!
	W "Are you sure you want to continue? Type a full YES/yes if you want to proceed)",!
	N X R "ANSWER: ",X
	S X=$$UP^XLFSTR(X) ; uppercase
	I X'="YES" S XPDQUIT=1 ; Quit if the user doesn't say yes
	QUIT
	;
TRAN	; Pre-Transport routine for KIDS to ship off the 50 and 50.7 globals
	M @XPDGREF@("VWPSSXPD",50)=^PSDRUG
	M @XPDGREF@("VWPSSXPD",50.7)=^PS(50.7)
	QUIT
	;
PRE	; Pre-Init routine for KIDS
	DO KILLDRUG ; Remove old drugs from system.
	QUIT
	;
POST	; Post-Init routine for KIDS
	; Load new drugs into system
	M ^PSDRUG=@XPDGREF@("VWPSSXPD",50)
	M ^PS(50.7)=@XPDGREF@("VWPSSXPD",50.7)
	; Restock the CPRS files from the new drug files
	DO RESTOCK
	QUIT
	;
	; -- END KIDS HOOKS --
	;
	; -- BEGIN Public Entry Points --
KILLDRUG	; Remove all Drug Data. PEP. Use this to call the routine.
	D DT^DICRW ; Min FM Vars
	D MES^XPDUTL("Killing Drug File (50)") D DRUG
	D MES^XPDUTL("Killing Pharmacy Orderable Item (OI) File (50.7)") D PO
	D MES^XPDUTL("Removing Pharmacy OIs from the Orderable Item file (101.43)") D O
	D MES^XPDUTL("Syncing the Order Quick View file (101.44)") D CPRS
	QUIT
	;
RESTOCK	; Restock CPRS Orderable Items from new Drug & Pharmacy Orderable Item 
	; File. Public Entry Point. 
	; Call this after repopulating the drug file (50) and the pharmacy orderable 
	; item file (50.7)
	N PSOIEN ; Looper variable
	D DT^DICRW ; Establish FM Basic Variables
	; 
	; Loop through Orderable Item file and call the protocol file updater to CPRS 
	; Files
	S PSOIEN=0 F  S PSOIEN=$O(^PS(50.7,PSOIEN)) Q:'PSOIEN  D 
	. D MES^XPDUTL("Updating Pharamcy Orderable Item "_PSOIEN)
	. D EN^PSSPOIDT(PSOIEN),EN2^PSSHL1(PSOIEN,"MUP")
	D CPRS ; Update Orderable Item View files 
	QUIT
	;
	; -- END Public Entry Points --
	; 
	; -- The rest is private --
DRUG	; Kill Drug File; Private
	N %1 S %1=^PSDRUG(0)
	K ^PSDRUG
	S ^PSDRUG(0)=%1
	S $P(^PSDRUG(0),"^",3,4)=""
	QUIT
	;
PO	; Kill Pharmacy Orderable Items; Private
	N %1 S %1=^PS(50.7,0)
	K ^PS(50.7)
	S ^PS(50.7,0)=%1
	S $P(^PS(50.7,0),"^",3,4)=""
	QUIT
	;
O	; Kill off Pharamcy Order Items (Only!) in the Orderable Item file; Private
	N DA ; Used in For loop below
	N DIK S DIK="^ORD(101.43,"
	N I S I=0
	FOR  S I=$O(^ORD(101.43,"ID",I)) QUIT:I=""  DO
	. I I["PSP" S DA=$O(^ORD(101.43,"ID",I,"")) D ^DIK
	QUIT
	;
CPRS	; Now, update the CPRS lists (sync with Orderable Item file) - 
	; Uses a CPRS API to do this; Private
	; Next 3 variables are required as inputs
	N ATTEMPT S ATTEMPT=0 ; Attempt to Update
	N UPDTIME S UPDTIME=$HOROLOG ; Update Time
	N DGNM ; Dialog Name
	; IVA RX -> Additives; IVB RX -> Solutions
	; IVM RX -> Inpatient Meds for Outpatients
	; NV RX -> Non-VA Meds ; O RX -> Outpatient
	; UD RX -> Unit Dose
	FOR DGNM="IVA RX","IVB RX","IVM RX","NV RX","O RX","UD RX" DO
	. D MES^XPDUTL(" --> Rebuilding "_DGNM)
	. D FVBLD^ORWUL
	QUIT
	;
