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

Last change on this file since 346 was 341, checked in by George Lilly, 15 years ago

Fixed intervals in CCRMEDS6

File size: 13.8 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,MINXML
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 MEDS(MEDCNT)=MEDS1(J)
59 K MEDS1
60 S MEDMAP=$NA(^TMP("GPLCCR",$J,"MEDMAP")) ; this is the variable map
61 S @MEDMAP@(0)=0 ; Initial count of meds
62 S MEDCNT="" ; Initialize for $Order
63 F S MEDCNT=$O(MEDS(MEDCNT)) Q:MEDCNT="" D ; for each medication in the list
64 . I DEBUG W "MEDCNT IS ",MEDCNT,!
65 . S MAP=$NA(^TMP("GPLCCR",$J,"MEDMAP",MEDCNT))
66 . ; K @MAP DO NOT KILL HERE, WAS CLEARED IN CCRMEDS
67 . S @MEDMAP@(0)=@MEDMAP@(0)+1 ; increment total meds in var array
68 . I DEBUG W "MAP= ",MAP,!
69 . S @MAP@("MEDOBJECTID")="MED"_MEDCNT ; MEDCNT FOR ID
70 . S @MAP@("MEDISSUEDATETXT")="Issue Date"
71 . S @MAP@("MEDISSUEDATE")=$$FMDTOUTC^CCRUTIL($P(MEDS(MEDCNT),U,13))
72 . S @MAP@("MEDLASTFILLDATETXT")="Last Fill Date"
73 . S @MAP@("MEDLASTFILLDATE")=$$FMDTOUTC^CCRUTIL($P(MEDS(MEDCNT),U,11))
74 . S @MAP@("MEDRXNOTXT")="Prescription Number"
75 . S @MAP@("MEDRXNO")=$P(MEDS(MEDCNT),U,14)
76 . S @MAP@("MEDTYPETEXT")="Medication"
77 . S @MAP@("MEDDETAILUNADORNED")="" ; Leave blank, field has its uses
78 . S @MAP@("MEDSTATUSTEXT")=$P(MEDS(MEDCNT),U,10)
79 . ; Provider only provided in API as text, not DUZ.
80 . ; We need to get DUZ from filman file 52 (Prescription)
81 . ; Field 4; IEN is Piece 2 of Meds stripped of trailing characters.
82 . ; Note that I will use RXIEN several times later
83 . N RXIEN S RXIEN=+$P(MEDS(MEDCNT),U,2)
84 . S @MAP@("MEDSOURCEACTORID")="ACTORPROVIDER_"_$$GET1^DIQ(52,RXIEN,4,"I")
85 . S @MAP@("MEDPRODUCTNAMETEXT")=$P(MEDS(MEDCNT),U,3)
86 . ; --- RxNorm Stuff
87 . ; 176.001 is the file for Concepts; 176.003 is the file for
88 . ; sources (i.e. for RxNorm Version)
89 . ;
90 . ; I use 176.001 for the Vista version of this routine (files 1-3)
91 . ; Since IHS does not have VUID's, I will be getting RxNorm codes
92 . ; using NDCs. My specially crafted index (sounds evil) named "NDC"
93 . ; is in file 176.002. The file is called RxNorm NDC to VUID.
94 . ; Except that I don't need the VUID, but it's there if I need it.
95 . ;
96 . ; We obviously need the NDC. That is easily obtained from the prescription.
97 . ; Field 27 in file 52
98 . ; N NDC S NDC=$$GET1^DIQ(52,RXIEN,27,"I")
99 . ; S NDC=$TR(NDC,"-") ; Remove dashes
100 . ; NDC="0"_NDC ; Add an extra zero to front; don't ask, that's how RxNorm has it
101 . N NDC S NDC=0 ; TODO:Remove this line after I fix the RxNorm 176.002 file.
102 . N RXNIEN,RXNORM,SRCIEN,RXNNAME,RXNVER
103 . I +NDC,$D(^C0CRXN) D ; $Data is for Systems that don't have our RxNorm file yet.
104 . . S RXNIEN=$$FIND1^DIC(176.002,,,NDC,"NDC")
105 . . S RXNORM=$$GET1^DIQ(176.002,RXNIEN,.01)
106 . . S SRCIEN=$$FIND1^DIC(176.003,,,"RXNORM","B")
107 . . S RXNNAME=$$GET1^DIQ(176.003,SRCIEN,6)
108 . . S RXNVER=$$GET1^DIQ(176.003,SRCIEN,7)
109 . ;
110 . E S (RXNORM,RXNNAME,RXNVER)=""
111 . ; End if/else block
112 . S @MAP@("MEDPRODUCTNAMECODEVALUE")=RXNORM
113 . S @MAP@("MEDPRODUCTNAMECODINGINGSYSTEM")=RXNNAME
114 . S @MAP@("MEDPRODUCTNAMECODEVERSION")=RXNVER
115 . ; --- End RxNorm section
116 . ;
117 . ; Brand name is 52 field 6.5
118 . S @MAP@("MEDBRANDNAMETEXT")=$$GET1^DIQ(52,RXIEN,6.5)
119 . ;
120 . ; Next I need Med Form (tab, cap etc), strength (250mg)
121 . ; concentration for liquids (250mg/mL)
122 . ; Since IHS does not have any of the new calls that
123 . ; Vista has, I will be doing a crosswalk:
124 . ; File 52, field 6 is Drug IEN in file 50
125 . ; File 50, field 22 is VA Product IEN in file 50.68
126 . ; In file 50.68, I will get the following:
127 . ; -- 1: Dosage Form
128 . ; -- 2: Strength
129 . ; -- 3: Units
130 . ; -- 8: Dispense Units
131 . ; -- Conc is 2 concatenated with 3
132 . ;
133 . ; *** If Drug is not matched to NDF, then VA Product will be "" ***
134 . ;
135 . N MEDIEN S MEDIEN=$$GET1^DIQ(52,RXIEN,6,"I") ; Drug IEN in 50
136 . N VAPROD S VAPROD=$$GET1^DIQ(50,MEDIEN,22,"I") ; VA Product in file 50.68
137 . I +VAPROD D
138 . . S @MAP@("MEDSTRENGTHVALUE")=$$GET1^DIQ(50.68,VAPROD,2)
139 . . S @MAP@("MEDSTRENGTHUNIT")=$$GET1^DIQ(50.68,VAPROD,3)
140 . . S @MAP@("MEDFORMTEXT")=$$GET1^DIQ(50.68,VAPROD,1)
141 . . S @MAP@("MEDCONCVALUE")=@MAP@("MEDSTRENGTHVALUE")
142 . . S @MAP@("MEDCONCUNIT")=@MAP@("MEDSTRENGTHUNIT")
143 . E D
144 . . S @MAP@("MEDSTRENGTHVALUE")=""
145 . . S @MAP@("MEDSTRENGTHUNIT")=""
146 . . S @MAP@("MEDFORMTEXT")=""
147 . . S @MAP@("MEDCONCVALUE")=""
148 . . S @MAP@("MEDCONCUNIT")=""
149 . ; End Strengh/Conc stuff
150 . ;
151 . ; Quantity is in the prescription, field 7
152 . S @MAP@("MEDQUANTITYVALUE")=$$GET1^DIQ(52,RXIEN,7)
153 . ; Dispense unit is in the drug file, field 14.5
154 . S @MAP@("MEDQUANTITYUNIT")=$$GET1^DIQ(50,MEDIEN,14.5)
155 . ;
156 . ; --- START OF DIRECTIONS ---
157 . ; Sig data not in any API :-( Oh yes, you can get the whole thing, but...
158 . ; we want the components.
159 . ; It's in multiple 113 in the Prescription File (52)
160 . ; #.01 DOSAGE ORDERED [1F] "20"
161 . ; #1 DISPENSE UNITS PER DOSE [2N] "1"
162 . ; #2 UNITS [3P:50.607] "MG"
163 . ; #3 NOUN [4F] "TABLET"
164 . ; #4 DURATION [5F] "10D"
165 . ; #5 CONJUNCTION [6S] "AND"
166 . ; #6 ROUTE [7P:51.2] "ORAL"
167 . ; #7 SCHEDULE [8F] "BID"
168 . ; #8 VERB [9F] "TAKE"
169 . ;
170 . ; Will use GETS^DIQ to get fields.
171 . ; Data comes out like this:
172 . ; SAMINS(52.0113,"1,23,",.01)=20
173 . ; SAMINS(52.0113,"1,23,",1)=1
174 . ; SAMINS(52.0113,"1,23,",2)="MG"
175 . ; SAMINS(52.0113,"1,23,",3)="TABLET"
176 . ; SAMINS(52.0113,"1,23,",4)="5D"
177 . ; SAMINS(52.0113,"1,23,",5)="THEN"
178 . ;
179 . N RAWDATA
180 . D GETS^DIQ(52,RXIEN,"113*",,"RAWDATA")
181 . N FMSIG M FMSIG=RAWDATA(52.0113) ; Merge into subfile...
182 . K RAWDATA
183 . N FMSIGNUM S FMSIGNUM="" ; Sigline number in fileman.
184 . ; FMSIGNUM gets outputted as "IEN,RXIEN,".
185 . ; DIRCNT is the proper Sigline numer.
186 . ; SIGDATA is the simplfied array.
187 . F S FMSIGNUM=$O(FMSIG(FMSIGNUM)) Q:FMSIGNUM="" D
188 . . N DIRCNT S DIRCNT=$P(FMSIGNUM,",")
189 . . N SIGDATA M SIGDATA=FMSIG(FMSIGNUM)
190 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRECTIONDESCRIPTIONTEXT")="" ; This is reserved for systems not able to generate the sig in components.
191 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDOSEINDICATOR")="1" ; means that we are specifying it. See E2369-05.
192 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDELIVERYMETHOD")=SIGDATA(8)
193 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDOSEVALUE")=SIGDATA(.01)
194 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDOSEUNIT")=SIGDATA(2)
195 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDRATEVALUE")="" ; For inpatient
196 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDRATEUNIT")="" ; For inpatient
197 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDVEHICLETEXT")="" ; For inpatient
198 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRECTIONROUTETEXT")=SIGDATA(6)
199 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDFREQUENCYVALUE")=SIGDATA(7)
200 . . ; Invervals... again another call.
201 . . ; In the wisdom of the original programmers, the schedule is a free text field
202 . . ; However, it gets translated by a call to the administration schedule file
203 . . ; to see if that schedule exists.
204 . . ; That's the same thing I am going to do.
205 . . ; Search B index of 51.1 (Admin Schedule) with schedule
206 . . ; First, remove "PRN" if it exists (don't ask, that's how the file
207 . . ; works; I wouldn't do it that way).
208 . . N SCHNOPRN S SCHNOPRN=SIGDATA(7)
209 . . I SCHNOPRN["PRN" S SCHNOPRN=$E(SCHNOPRN,1,$F(SCHNOPRN,"PRN")-5)
210 . . ; Super call below:
211 . . ; 1=File 51.1 3=Field 2 (Frequency in Minutes)
212 . . ; 4=Packed format, Exact Match 5=Lookup Value
213 . . ; 6=# of entries to return 7=Index 10=Return Array
214 . . ;
215 . . ; I do not account for the fact that two schedules can be
216 . . ; spelled identically (ie duplicate entry). In that case,
217 . . ; I get the first. That's just a bad pharmacy pkg maintainer.
218 . . N C0C515
219 . . D FIND^DIC(51.1,,"@;2","PX",SCHNOPRN,1,"B",,,"C0C515")
220 . . N INTERVAL S INTERVAL="" ; Default
221 . . ; If there are entries found, get it
222 . . I +$G(C0C515("DILIST",0)) S INTERVAL=$P(C0C515("DILIST",1,0),U,2)
223 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDINTERVALVALUE")=INTERVAL
224 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDINTERVALUNIT")="Minute"
225 . . ; Duration is 10M minutes, 10H hours, 10D for Days
226 . . ; 10W for weeks, 10L for months. I smell $Select
227 . . ; But we don't need to do that if there isn't a duration
228 . . I +SIGDATA(4) D
229 . . . N DURUNIT S DURUNIT=$E(SIGDATA(4),$L(SIGDATA(4))) ; get last char
230 . . . N DURTXT S DURTXT=$S(DURUNIT="M":"Minutes",DURUNIT="H":"Hours",DURUNIT="D":"Days",DURUNIT="W":"Weeks",DURUNIT="L":"Months",1:"Days")
231 . . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDURATIONVALUE")=+SIGDATA(4)
232 . . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDURATIONUNIT")=DURTXT
233 . . E D
234 . . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDURATIONVALUE")=""
235 . . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDURATIONUNIT")=""
236 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPRNFLAG")=SIGDATA(4)["PRN"
237 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMOBJECTID")="" ; when avail
238 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMTYPETXT")=""
239 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMDESCRIPTION")=""
240 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMCODEVALUE")=""
241 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMCODINGSYSTEM")=""
242 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMCODINGVERSION")=""
243 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDPROBLEMSOURCEACTORID")=""
244 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDSTOPINDICATOR")="" ; not stored
245 . . ; Another confusing line; I am pretty bad:
246 . . ; If there is another entry in the FMSIG array (i.e. another line
247 . . ; in the sig), set the direction count indicator.
248 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRSEQ")="" ; Default
249 . . S:+$O(FMSIG(FMSIGNUM)) @MAP@("M","DIRECTIONS",DIRCNT,"MEDDIRSEQ")=DIRCNT
250 . . S @MAP@("M","DIRECTIONS",DIRCNT,"MEDMULDIRMOD")=SIGDATA(5)
251 . ;
252 . ; --- END OF DIRECTIONS ---
253 . ;
254 . ; Med instructions is a WP field, thus the acrobatics
255 . ; Notice buffer overflow protection set at 10,000 chars
256 . ; -- 1. Med Patient Instructions
257 . N MEDPTIN1 S MEDPTIN1=$$GET1^DIQ(52,RXIEN,115,,"MEDPTIN1")
258 . N MEDPTIN2,J S (MEDPTIN2,J)=""
259 . I $L(MEDPTIN1) F S J=$O(@MEDPTIN1@(J)) Q:J="" Q:$L(MEDPTIN2)>10000 S MEDPTIN2=MEDPTIN2_@MEDPTIN1@(J)_" "
260 . S @MAP@("MEDPTINSTRUCTIONS")=MEDPTIN2
261 . K J
262 . ; -- 2. Med Provider Instructions
263 . N MEDPVIN1 S MEDPVIN1=$$GET1^DIQ(52,RXIEN,39,,"MEDPVIN1")
264 . N MEDPVIN2,J S (MEDPVIN2,J)=""
265 . I $L(MEDPVIN1) F S J=$O(@MEDPVIN1@(J)) Q:J="" Q:$L(MEDPVIN2)>10000 S MEDPVIN2=MEDPVIN2_@MEDPVIN1@(J)_" "
266 . S @MAP@("MEDFULLFILLMENTINSTRUCTIONS")=MEDPVIN2
267 . ;
268 . ; Remaining refills
269 . S @MAP@("MEDRFNO")=$P(MEDS(MEDCNT),U,6)
270 . ; ------ END OF MAPPING
271 . ;
272 . ; ------ BEGIN XML INSERTION
273 . N RESULT S RESULT=$NA(^TMP("GPLCCR",$J,"MAPPED"))
274 . K @RESULT
275 . D MAP^GPLXPATH(MINXML,MAP,RESULT)
276 . ; D PARY^GPLXPATH(RESULT)
277 . ; MAPPING DIRECTIONS
278 . N MEDDIR1,DIRXML1 S DIRXML1="MEDDIR1" ; VARIABLE AND NAME VARIABLE TEMPLATE
279 . N MEDDIR2,DIRXML2 S DIRXML2="MEDDIR2" ; VARIABLE AND NAME VARIABLE RESULT
280 . D QUERY^GPLXPATH(MINXML,"//Medications/Medication/Directions",DIRXML1)
281 . D REPLACE^GPLXPATH(RESULT,"","//Medications/Medication/Directions")
282 . ; N MDZ1,MDZNA
283 . N DIRCNT S DIRCNT=""
284 . I +$O(@MAP@("M","DIRECTIONS",DIRCNT)) D ; IF THERE ARE DIRCTIONS
285 . . F DIRCNT=$O(@MAP@("M","DIRECTIONS",DIRCNT)) D ; FOR EACH DIRECTION
286 . . . S MDZNA=$NA(@MAP@("M","DIRECTIONS",DIRCNT))
287 . . . D MAP^GPLXPATH(DIRXML1,MDZNA,DIRXML2)
288 . . . D INSERT^GPLXPATH(RESULT,DIRXML2,"//Medications/Medication")
289 . D:MEDCNT=1 CP^GPLXPATH(RESULT,OUTXML) ; First one is a copy
290 . D:MEDCNT>1 INSINNER^GPLXPATH(OUTXML,RESULT) ; AFTER THE FIRST, INSERT INNER XML
291 N MEDTMP,MEDI
292 D MISSING^GPLXPATH(OUTXML,"MEDTMP") ; SEARCH XML FOR MISSING VARS
293 I MEDTMP(0)>0 D ; IF THERE ARE MISSING VARS - MARKED AS @@X@@
294 . W "MEDICATION MISSING ",!
295 . F MEDI=1:1:MEDTMP(0) W MEDTMP(MEDI),!
296 Q
297 ;
Note: See TracBrowser for help on using the repository browser.