source: ccr/trunk/p/CCRMEDS6.m@ 330

Last change on this file since 330 was 324, checked in by Sam Habiel, 15 years ago

Finished initial draft of CCRMEDS6

File size: 12.6 KB
Line 
1CCRMEDS6 ; WV/CCDCCR/SMH - Meds from RPMS: Outpatient Meds;01/10/09
2 ;;0.1;CCDCCR;;JUL 16,2008;
3 ; Copyright 2008 WorldVistA. Licensed under the terms of the GNU
4 ; General Public License See attached copy of the License.
5 ;
6 ; This program is free software; you can redistribute it and/or modify
7 ; it under the terms of the GNU General Public License as published by
8 ; the Free Software Foundation; either version 2 of the License, or
9 ; (at your option) any later version.
10 ;
11 ; This program is distributed in the hope that it will be useful,
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ; GNU General Public License for more details.
15 ;
16 ; You should have received a copy of the GNU General Public License along
17 ; with this program; if not, write to the Free Software Foundation, Inc.,
18 ; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ;
20 W "NO ENTRY FROM TOP",!
21 Q
22 ;
23EXTRACT(MINXML,DFN,OUTXML) ; EXTRACT MEDICATIONS INTO PROVIDED XML TEMPLATE
24 ;
25 ; MINXML and OUTXML are passed by name so globals can be used
26 ; MINXML will contain only the medications skeleton of the overall template
27 ;
28 ; MEDS is return array from RPC.
29 ; MAP is a mapping variable map (store result) for each med
30 ; MED is holds each array element from MEDS(J), one medicine
31 ; J is a counter.
32 ;
33 ; GETRXS^BEHORXFN(ARRAYNAME,DFN,DAYS) will be the the API used.
34 ; This API has been developed by Medsphere for IHS for getting
35 ; Medications from RPMS. It has most of what we need.
36 ; API written by Doug Martin when he worked for Medsphere (thanks Doug!)
37 ; -- ARRAYNAME is passed by name (required)
38 ; -- DFN is passed by value (required)
39 ; -- DAYS is passed by value (optional; if not passed defaults to 365)
40 ;
41 ; Return:
42 ; ~Type^PharmID^Drug^InfRate^StopDt^RefRem^TotDose^UnitDose^OrderID
43 ; ^Status^LastFill^Chronic^Issued^Rx #^Provider^
44 ; Status Reason^DEA Handling
45 ;
46 N MEDS,MEDS1,MAP
47 D GETRXS^BEHORXFN("MEDS1",DFN,365) ; Days hard set to 365
48 ; If MEDS1 is not defined, then no meds
49 I '$D(MEDS1) S @OUTXML@(0)=0 QUIT
50 I DEBUG ZWR MEDS1
51 N MEDCNT S MEDCNT=0 ; Med Count
52 ; The next line is a super line. It goes through the array return
53 ; and if the first characters are ~OP, it grabs the line.
54 ; This means that line is for a dispensed Outpatient Med.
55 ; That line has the metadata about the med that I need.
56 ; The next lines, however many, are the med and the sig.
57 ; I won't be using those because I have to get the sig parsed exactly.
58 N J S J="" F S J=$O(MEDS1(J)) Q:J="" I $E(MEDS1(J),1,3)="~OP" S MEDCNT=MEDCNT+1 S MED(MEDCNT)=MEDS1(J)
59 S MEDMAP=$NA(^TMP("GPLCCR",$J,"MEDMAP")) ; this is the variable map
60 S @MEDMAP@(0)=0 ; Initial count of meds
61 F S MEDCNT=$O(MEDS(MEDCNT)) Q:MEDCNT="" D ; for each medication in the list
62 . I DEBUG W "MEDCNT IS ",MEDCNT,!
63 . S MAP=$NA(^TMP("GPLCCR",$J,"MEDMAP",MEDCNT))
64 . ; K @MAP DO NOT KILL HERE, WAS CLEARED IN CCRMEDS
65 . S @MEDMAP@(0)=@MEDMAP@(0)+1 ; increment total meds in var array
66 . I DEBUG W "MAP= ",MAP,!
67 . S @MAP@("MEDOBJECTID")="MED"_MEDCNT ; MEDCNT FOR ID
68 . S @MAP@("MEDISSUEDATETXT")="Issue Date"
69 . S @MAP@("MEDISSUEDATE")=$$FMDTOUTC^CCRUTIL($P(MEDS(MEDCNT),U,13))
70 . S @MAP@("MEDLASTFILLDATETXT")="Last Fill Date"
71 . S @MAP@("MEDLASTFILLDATE")=$$FMDTOUTC^CCRUTIL($P(MEDS(MEDCNT),U,11))
72 . S @MAP@("MEDRXNOTXT")="Prescription Number"
73 . S @MAP@("MEDRXNO")=$P(MEDS(MEDCNT),U,14)
74 . S @MAP@("MEDTYPETEXT")="Medication"
75 . S @MAP@("MEDDETAILUNADORNED")="" ; Leave blank, field has its uses
76 . S @MAP@("MEDSTATUSTEXT")=$P(MEDS(MEDCNT),U,10)
77 . ; Provider only provided in API as text, not DUZ.
78 . ; We need to get DUZ from filman file 52 (Prescription)
79 . ; Field 4;; IEN is Piece 1 of Meds stripped of trailing characters.
80 . N RXIEN S RXIEN=+$P(MEDS(MEDCNT),U)
81 . S @MAP@("MEDSOURCEACTORID")="ACTORPROVIDER_"_$$GET1^DIQ(52,RXIEN,4,"I")
82 . S @MAP@("MEDPRODUCTNAMETEXT")=$P(MEDS(MEDCNT),U,3)
83 . ; --- RxNorm Stuff
84 . ; 176.001 is the file for Concepts; 176.003 is the file for
85 . ; sources (i.e. for RxNorm Version)
86 . ;
87 . ; I use 176.001 for the Vista version of this routine (files 1-3)
88 . ; Since IHS does not have VUID's, I will be getting RxNorm codes
89 . ; using NDCs. My specially crafted index (sounds evil) named "NDC"
90 . ; is in file 176.002. The file is called RxNorm NDC to VUID.
91 . ; Except that I don't need the VUID, but it's there if I need it.
92 . ;
93 . ; We obviously need the NDC. That is easily obtained from the prescription.
94 . ; Field 27 in file 52
95 . ; N NDC S NDC=$$GET1^DIQ(52,RXIEN,27,"I")
96 . ; S NDC=$TR(NDC,"-") ; Remove dashes
97 . ; NDC="0"_NDC ; Add an extra zero to front; don't ask, that's how RxNorm has it
98 . N NDC S NDC=0 ; TODO:Remove this line after I fix the RxNorm 176.002 file.
99 . N RXNIEN,RXNORM,SRCIEN,RXNNAME,RXNVER
100 . I +NDC,$D(^C0CRXN) D ; $Data is for Systems that don't have our RxNorm file yet.
101 . . S RXNIEN=$$FIND1^DIC(176.002,,,NDC,"NDC")
102 . . S RXNORM=$$GET1^DIQ(176.002,RXNIEN,.01)
103 . . S SRCIEN=$$FIND1^DIC(176.003,,"B","RXNORM")
104 . . S RXNNAME=$$GET1^DIQ(176.003,SRCIEN,6)
105 . . S RXNVER=$$GET1^DIQ(176.003,SRCIEN,7)
106 . ;
107 . E S (RXNORM,RXNNAME,RXNVER)=""
108 . ; End if/else block
109 . S @MAP@("MEDPRODUCTNAMECODEVALUE")=RXNORM
110 . S @MAP@("MEDPRODUCTNAMECODINGINGSYSTEM")=RXNNAME
111 . S @MAP@("MEDPRODUCTNAMECODEVERSION")=RXNVER
112 . ; --- End RxNorm section
113 . ;
114 . ; Brand name is 52 field 6.5
115 . S @MAP@("MEDBRANDNAMETEXT")=$$GET1^DIQ(52,RXIEN,6.5,"I")
116 . ;
117 . ; Next I need Med Form (tab, cap etc), strength (250mg)
118 . ; concentration for liquids (250mg/mL)
119 . ; Since IHS does not have any of the new calls that
120 . ; Vista has, I will be doing a crosswalk:
121 . ; File 52, field 6 is Drug IEN in file 50
122 . ; File 50, field 22 is VA Product IEN in file 50.68
123 . ; In file 50.68, I will get the following:
124 . ; -- 1: Dosage Form
125 . ; -- 2: Strength
126 . ; -- 3: Units
127 . ; -- 8: Dispense Units
128 . ; -- Conc is 2 concatenated with 3
129 . ;
130 . ; *** If Drug is not matched to NDF, then VA Product will be "" ***
131 . ;
132 . N MEDIEN S MEDIEN=$$GET1^DIQ(52,RXIEN,6,"I") ; Drug IEN in 50
133 . N VAPROD S VAPROD=$$GET1^DIQ(50,MEDIEN,22,"I") ; VA Product in file 50.68
134 . I +VAPROD D
135 . . S @MAP@("MEDSTRENGTHVALUE")=$$GET1^DIQ(50.68,VAPROD,2)
136 . . S @MAP@("MEDSTRENGTHUNIT")=$$GET1^DIQ(50.68,VAPROD,3)
137 . . S @MAP@("MEDFORMTEXT")=$$GET1^DIQ(50.68,VAPROD,1)
138 . . S @MAP@("MEDCONCVALUE")=@MAP@("MEDSTRENGTHVALUE")
139 . . S @MAP@("MEDCONCUNIT")=@MAP@("MEDSTRENGTHUNIT")
140 . E D
141 . . S @MAP@("MEDSTRENGTHVALUE")=""
142 . . S @MAP@("MEDSTRENGTHUNIT")=""
143 . . S @MAP@("MEDFORMTEXT")=""
144 . . S @MAP@("MEDCONCVALUE")=""
145 . . S @MAP@("MEDCONCUNIT")=""
146 . ; End Strengh/Conc stuff
147 . ;
148 . ; Quantity is in the prescription, field 7
149 . S @MAP@("MEDQUANTITYVALUE")=$$GET1^DIQ(52,RXIEN,7)
150 . ; Dispense unit is in the drug file, field 14.5
151 . S @MAP@("MEDQUANTITYUNIT")=$$GET1^DIQ(50,MEDIEN,14.5)
152 . ;
153 . ; --- START OF DIRECTIONS ---
154 . ; Sig data not in any API :-( Oh yes, you can get the whole thing, but...
155 . ; we want the compoenents.
156 . ; It's in multiple 113 in the Prescription File (52)
157 . ; #.01 DOSAGE ORDERED [1F] "20"
158 . ; #1 DISPENSE UNITS PER DOSE [2N] "1"
159 . ; #2 UNITS [3P:50.607] "MG"
160 . ; #3 NOUN [4F] "TABLET"
161 . ; #4 DURATION [5F] "10D"
162 . ; #5 CONJUNCTION [6S] "AND"
163 . ; #6 ROUTE [7P:51.2] "ORAL"
164 . ; #7 SCHEDULE [8F] "BID"
165 . ; #8 VERB [9F] "TAKE"
166 . ;
167 . ; Will use GETS^DIQ to get fields.
168 . ; Data comes out like this:
169 . ; SAMINS(52.0113,"1,23,",.01)=20
170 . ; SAMINS(52.0113,"1,23,",1)=1
171 . ; SAMINS(52.0113,"1,23,",2)="MG"
172 . ; SAMINS(52.0113,"1,23,",3)="TABLET"
173 . ; SAMINS(52.0113,"1,23,",4)="5D"
174 . ; SAMINS(52.0113,"1,23,",5)="THEN"
175 . ;
176 . N RAWDATA
177 . D GETS^DIQ(52,RXIEN,"113*",,"RAWDATA")
178 . N FMSIG M FMSIG=RAWDATA(52.0113) ; Merge into subfile...
179 . K RAWDATA
180 . N FMSIGNUM S FMSIGNUM="" ; Sigline number in fileman.
181 . ; FMSIGNUM gets outputted as "IEN,RXIEN,".
182 . ; DIRCNT is the proper Sigline numer.
183 . ; SIGDATA is the simplfied array.
184 . F S FMSIGNUM=$O(FMSIG(FMSIGNUM)) Q:FMSIGNUM="" D
185 . . N DIRCNT S DIRCNT=$P(FMSIGNUM,",")
186 . . N SIGDATA M SIGDATA=FMSIG(FMSIGNUM)
187 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRECTIONDESCRIPTIONTEXT")="" ; This is reserved for systems not able to generate the sig in components.
188 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDOSEINDICATOR")="1" ; means that we are specifying it. See E2369-05.
189 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDELIVERYMETHOD")=SIGDATA(8)
190 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDOSEVALUE")=SIGDATA(.01)
191 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDOSEUNIT")=SIGDATA(2)
192 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDRATEVALUE")="" ; For inpatient
193 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDRATEUNIT")="" ; For inpatient
194 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDVEHICLETEXT")="" ; For inpatient
195 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRECTIONROUTETEXT")=SIGDATA(6)
196 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDFREQUENCYVALUE")=SIGDATA(7)
197 . . ; Invervals... again another call.
198 . . ; In the wisdom of the original programmers, the schedule is a free text field
199 . . ; However, it gets translated by a call to the administration schedule file
200 . . ; to see if that schedule exists.
201 . . ; That's the same thing I am going to do.
202 . . ; Search B index of 51.1 (Admin Schedule) with first abbr of schedule
203 . . N SCHEDIEN=$$FIND^DIC(51.1,,,$P(SIGDATA(7)," "),"B")
204 . . N INTERVAL S INTERVAL="" ; Default
205 . . S:SCEHDIEN INTERVAL=$$GET1^DIQ(51.1,SCHEDIEN,2)
206 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDINTERVALVALUE")=INTERVAL
207 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDINTERVALUNIT")="Minute"
208 . . ; Duration is 10M minutes, 10H hours, 10D for Days
209 . . ; 10W for weeks, 10L for months. I smell $Select
210 . . N DURUNIT S DURUNIT=$E(SIGDATA(4),$L(SIGDATA(4))) ; get last char
211 . . N DURTXT S DURTXT=$S(DURUNIT="M":"Minutes",DURUNIT="H":"Hours",DURUNIT="D":"Days",DURUNIT="W":"Weeks",DURUNIT="L":"Months",1:"Days")
212 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDURATIONVALUE")=+SIGDATA(4)
213 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDURATIONUNIT")=DURTXT
214 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPRNFLAG")=SIGDATA(4)["PRN"
215 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMOBJECTID")="" ; when avail
216 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMTYPETXT")=""
217 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMDESCRIPTION")=""
218 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMCODEVALUE")=""
219 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMCODINGSYSTEM")=""
220 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMCODINGVERSION")=""
221 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMSOURCEACTORID")=""
222 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDSTOPINDICATOR")="" ; not stored
223 . . ; Another confusing line; I am pretty bad:
224 . . ; If there is another entry in the FMSIG array (i.e. another line
225 . . ; in the sig), set the direction count indicator.
226 . . S:+$O(FMSIG(FMSIGNUM)) @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRSEQ")=DIRCNT
227 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDMULDIRMOD")=SIGDATA(5)
228 . ;
229 . ; --- END OF DIRECTIONS ---
230 . ;
231 . ; Med instructions is a WP field, thus the acrobatics
232 . ; Notice buffer overflow protection set at 10,000 chars
233 . ; -- 1. Med Patient Instructions
234 . N MEDPTIN1 S MEDPTIN1=$$GET1^DIQ(52,RXIEN,115,,"MEDPTIN1")
235 . N MEDPTIN2,J S (MEDPTIN2,J)=""
236 . F S J=$O(@MEDPTIN1@(J)) Q:J="" Q:$L(MEDPTIN2)>10000 S MEDPTIN2=MEDPTIN2_@MEDPTIN1@(J)_" "
237 . S @MAP@("MEDPTINSTRUCTIONS")=MEDPTIN2
238 . K J
239 . ; -- 2. Med Provider Instructions
240 . N MEDPVIN1 S MEDPVIN1=$$GET1^DIQ(52,RXIEN,39,,"MEDPVIN1")
241 . N MEDPVIN2,J S (MEDPVIN2,J)=""
242 . F S J=$O(@MEDPVIN1@(J)) Q:J="" Q:$L(MEDPVIN2)>10000 S MEDPVIN2=MEDPVIN2_@MEDPVIN1@(J)_" "
243 . S @MAP@("MEDFULLFILLMENTINSTRUCTIONS")=MEDPVIN2
244 . ;
245 . ; Remaining refills
246 . S @MAP@("MEDRFNO")=$P(MEDS(MEDCNT),U,6)
247 . ; ------ END OF MAPPING
248 . ;
249 . ; ------ BEGIN XML INSERTION
250 . N RESULT S RESULT=$NA(^TMP("GPLCCR",$J,"MAPPED"))
251 . K @RESULT
252 . D MAP^GPLXPATH(MINXML,MAP,RESULT)
253 . ; D PARY^GPLXPATH(RESULT)
254 . ; MAPPING DIRECTIONS
255 . N MEDDIR1,DIRXML1 S DIRXML1="MEDDIR1" ; VARIABLE AND NAME VARIABLE TEMPLATE
256 . N MEDDIR2,DIRXML2 S DIRXML2="MEDDIR2" ; VARIABLE AND NAME VARIABLE RESULT
257 . D QUERY^GPLXPATH(MINXML,"//Medications/Medication/Directions",DIRXML1)
258 . D REPLACE^GPLXPATH(RESULT,"","//Medications/Medication/Directions")
259 . ; N MDZ1,MDZNA
260 . I DIRCNT>0 D ; IF THERE ARE DIRCTIONS
261 . . F MDZ1=1:1:DIRCNT D ; FOR EACH DIRECTION
262 . . . S MDZNA=$NA(@MAP@("M","DIRECTIONS",MDZ1))
263 . . . D MAP^GPLXPATH(DIRXML1,MDZNA,DIRXML2)
264 . . . D INSERT^GPLXPATH(RESULT,DIRXML2,"//Medications/Medication")
265 . D:MEDCNT=1 CP^GPLXPATH(RESULT,OUTXML) ; First one is a copy
266 . D:MEDCNT>1 INSINNER^GPLXPATH(OUTXML,RESULT) ; AFTER THE FIRST, INSERT INNER XML
267 N MEDTMP,MEDI
268 D MISSING^GPLXPATH(OUTXML,"MEDTMP") ; SEARCH XML FOR MISSING VARS
269 I MEDTMP(0)>0 D ; IF THERE ARE MISSING VARS - MARKED AS @@X@@
270 . W "MEDICATION MISSING ",!
271 . F MEDI=1:1:MEDTMP(0) W MEDTMP(MEDI),!
272 Q
273 ;
Note: See TracBrowser for help on using the repository browser.