source: cprs/trunk/CPRS-Chart/Orders/rODMeds.pas@ 839

Last change on this file since 839 was 829, checked in by Kevin Toppenberg, 14 years ago

Upgrade to version 27

File size: 17.9 KB
Line 
1unit rODMeds;
2
3interface
4
5uses SysUtils, Classes, ORNet, ORFn, uCore, uConst, Windows;
6
7type
8 TAdminTimeHelpText = record
9 HelpText: string;
10 end;
11
12 TInpatientClozapineText = record
13 dispText: string;
14 end;
15
16 TDrugHasMaxData = record
17 CaptureMaxData: boolean;
18 MaxSupply: integer;
19 MaxQuantity: integer;
20 MaxRefills: integer;
21 end;
22
23function DEACheckFailed(AnOI: Integer; ForInpatient: Boolean): Boolean;
24function DEACheckFailedForIVOnOutPatient(AnOI: Integer; AnOIType: Char): boolean;
25procedure ListForOrderable(var AListIEN, ACount: Integer; const DGrpNm: string);
26procedure SubsetOfOrderable(Dest: TStringList; Append: Boolean; ListIEN, First, Last: Integer);
27function IndexOfOrderable(ListIEN: Integer; From: string): Integer;
28procedure IsActivateOI(var AMsg: string; theOI: integer);
29procedure ListForQuickOrders(var AListIEN, ACount: Integer; const DGrpNm: string);
30procedure SubsetOfQuickOrders(Dest: TStringList; AListIEN, First, Last: Integer);
31function IndexOfQuickOrder(AListIEN: Integer; From: string): Integer;
32procedure LoadFormularyAltOI(AList: TStringList; AnIEN: Integer; ForInpatient: Boolean);
33procedure LoadFormularyAltDose(AList: TStringList; DispDrug, OI: Integer; ForInpatient: Boolean);
34procedure LoadAdminInfo(const Schedule: string; OrdItem: Integer; var StartText: string;
35 var AdminTime: TFMDateTime; var Duration: string; Admin: string = '');
36function GetAdminTime(const StartText, Schedule: string; OrdItem: Integer): TFMDateTime;
37procedure LoadSchedules(Dest: TStrings; IsInptDlg: boolean = False);
38procedure LoadDOWSchedules(Dest: TStrings);
39procedure LoadAllIVRoutes(Dest: TStrings);
40procedure LoadDosageFormIVRoutes(Dest: TStrings; OrderIDs: TStringList; Default: boolean);
41function QtyToDays(Quantity: Double; const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
42function DaysToQty(DaysSupply: Integer; const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
43function DurToQty(DaysSupply: Integer; const UnitStr, SchedStr: string): Integer;
44function DefaultDays(const ADrug, UnitStr, SchedStr: string): Integer;
45function CalcMaxRefills(const Drug: string; Days, OrdItem: Integer; Discharge: Boolean): Integer;
46function ScheduleRequired(OrdItem: Integer; const ARoute, ADrug: string): Boolean;
47function ODForMedsIn: TStrings;
48function ODForMedsOut: TStrings;
49function OIForMed(AnIEN: Integer; ForInpatient: Boolean; HavePI: boolean = True; PKIActive: Boolean = False): TStrings;
50function GetPickupForLocation(const Loc: string): string;
51function QOHasRouteDefined(AQOID: integer): boolean;
52procedure CheckExistingPI(AOrderId: string; var APtI: string);
53function PassDrugTest(OI: integer; OrderType: string; InptOrder: boolean; CheckForClozapineOnly: boolean = false): boolean;
54function AdminTimeHelpText(): string;
55//function ValidateDaySupplyandQuantity(DaySupply, Quantity: integer): boolean;
56//function ValidateMaxQuantity(Quantity: integer): boolean;
57function ValidateDrugAutoAccept(tempDrug, tempUnit, tempSch, tempDur: string; OI, tempSupply, tempQuantity, tempRefills: integer): boolean;
58function ValidateDaySupplyandQuantityErrorMsg(DaySupply, quantity: integer): String;
59procedure ClearMaxData;
60function DifferentOrderLocations(ID: string; Loc: integer): boolean;
61function IsClozapineOrder: boolean;
62//function ValidateQuantityErrorMsg(Quantity: integer): String;
63function GetQOOrderableItem(DialogIEN: string): integer;
64
65
66implementation
67 var
68 uAdminTimeHelpText: TAdminTimeHelpText;
69 uDrugHasMaxData: TDrugHasMaxData;
70 uInpatientClozapineText : TInpatientClozapineText;
71
72function DEACheckFailed(AnOI: Integer; ForInpatient: Boolean): Boolean;
73var
74 PtType: Char;
75begin
76 if ForInpatient then PtType := 'I' else PtType := 'O';
77 Result := sCallV('ORWDPS1 FAILDEA', [AnOI, Encounter.Provider, PtType]) = '1';
78end;
79
80function DEACheckFailedForIVOnOutPatient(AnOI: Integer; AnOIType: Char): boolean;
81begin
82 Result := sCallV('ORWDPS1 IVDEA',[AnOI,AnOIType,Encounter.Provider]) = '1';
83end;
84
85procedure ListForOrderable(var AListIEN, ACount: Integer; const DGrpNm: string);
86begin
87 CallV('ORWUL FV4DG', [DGrpNm]);
88 AListIEN := StrToIntDef(Piece(RPCBrokerV.Results[0], U, 1), 0);
89 ACount := StrToIntDef(Piece(RPCBrokerV.Results[0], U, 2), 0);
90end;
91
92procedure SubsetOfOrderable(Dest: TStringList; Append: Boolean; ListIEN, First, Last: Integer);
93var
94 i: Integer;
95begin
96 CallV('ORWUL FVSUB', [ListIEN, First+1, Last+1]); // M side not 0-based
97 if Append then FastAddStrings(RPCBrokerV.Results, Dest) else
98 begin
99 for i := Pred(RPCBrokerV.Results.Count) downto 0 do Dest.Insert(0, RPCBrokerV.Results[i]);
100 end;
101end;
102
103function IndexOfOrderable(ListIEN: Integer; From: string): Integer;
104var
105 x: string;
106begin
107 Result := -1;
108 if From = '' then Exit;
109 // decrement last char & concat '~' for $ORDER on M side, limit string length
110 x := UpperCase(Copy(From, 1, 220));
111 x := Copy(x, 1, Length(x) - 1) + Pred(x[Length(x)]) + '~';
112 x := sCallV('ORWUL FVIDX', [ListIEN, x]);
113 // use Pred to make the index 0-based (first value = 1 on M side)
114 if CompareText(Copy(Piece(x, U, 2), 1, Length(From)), From) = 0
115 then Result := Pred(StrToIntDef(Piece(x, U, 1), 0));
116end;
117
118procedure IsActivateOI(var AMsg: string; theOI: integer);
119begin
120 AMsg := SCallV('ORWDXA ISACTOI', [theOI]);
121end;
122
123procedure ListForQuickOrders(var AListIEN, ACount: Integer; const DGrpNm: string);
124begin
125 CallV('ORWUL QV4DG', [DGrpNm]);
126 AListIEN := StrToIntDef(Piece(RPCBrokerV.Results[0], U, 1), 0);
127 ACount := StrToIntDef(Piece(RPCBrokerV.Results[0], U, 2), 0);
128end;
129
130procedure SubsetOfQuickOrders(Dest: TStringList; AListIEN, First, Last: Integer);
131var
132 i: Integer;
133begin
134 CallV('ORWUL QVSUB', [AListIEN,'','']);
135 for i := 0 to RPCBrokerV.Results.Count -1 do
136 Dest.Add(RPCBrokerV.Results[i]);
137end;
138
139function IndexOfQuickOrder(AListIEN: Integer; From: string): Integer;
140var
141 x: string;
142begin
143 Result := -1;
144 if From = '' then Exit;
145 // decrement last char & concat '~' for $ORDER on M side, limit string length
146 x := UpperCase(Copy(From, 1, 220));
147 x := Copy(x, 1, Length(x) - 1) + Pred(x[Length(x)]) + '~';
148 x := sCallV('ORWUL QVIDX', [AListIEN, x]);
149 // use Pred to made the index 0-based (first value = 1 on M side)
150 if CompareText(Copy(Piece(x, U, 2), 1, Length(From)), From) = 0
151 then Result := Pred(StrToIntDef(Piece(x, U, 1), 0));
152end;
153
154procedure LoadFormularyAltOI(AList: TStringList; AnIEN: Integer; ForInpatient: Boolean);
155var
156 PtType: Char;
157begin
158 if ForInpatient then PtType := 'I' else PtType := 'O';
159 CallV('ORWDPS1 FORMALT', [AnIEN, PtType]);
160 FastAssign(RPCBrokerV.Results, AList);
161end;
162
163procedure LoadFormularyAltDose(AList: TStringList; DispDrug, OI: Integer; ForInpatient: Boolean);
164var
165 PtType: Char;
166begin
167 if ForInpatient then PtType := 'I' else PtType := 'O';
168 CallV('ORWDPS1 DOSEALT', [DispDrug, OI, PtType]);
169 FastAssign(RPCBrokerV.Results, AList);
170end;
171
172procedure LoadAdminInfo(const Schedule: string; OrdItem: Integer; var StartText: string;
173 var AdminTime: TFMDateTime; var Duration: string; Admin: string = '');
174var
175 x: string;
176begin
177 x := sCallV('ORWDPS2 ADMIN', [Patient.DFN, Schedule, OrdItem, Encounter.Location, Admin]);
178 StartText := Piece(x, U, 1);
179 AdminTime := MakeFMDateTime(Piece(x, U, 4));
180 Duration := Piece(x, U, 3);
181end;
182
183function GetAdminTime(const StartText, Schedule: string; OrdItem: Integer): TFMDateTime;
184var
185 x: string;
186begin
187 x := sCallV('ORWDPS2 REQST', [Patient.DFN, Schedule, OrdItem, Encounter.Location, StartText]);
188 Result := MakeFMDateTime(x);
189end;
190
191procedure LoadSchedules(Dest: TStrings; IsInptDlg: boolean);
192begin
193 // if uMedSchedules = nil then CallV('ORWDPS ALLSCHD', [nil]); uMedSchedules.Assign(...);
194 CallV('ORWDPS1 SCHALL', [patient.dfn, patient.location]);
195 FastAssign(RPCBrokerV.Results, Dest);
196 If (Dest.IndexOfName('OTHER') < 0) and IsInptDlg then
197 Dest.Add('OTHER');
198end;
199
200procedure LoadAllIVRoutes(Dest: TStrings);
201begin
202 CallV('ORWDPS32 ALLIVRTE', []);
203 FastAssign(RPCBrokerV.Results, Dest);
204end;
205
206procedure LoadDosageFormIVRoutes(Dest: TStrings; OrderIDs: TStringList; Default: boolean);
207begin
208 CallV('ORWDPS33 IVDOSFRM', [OrderIDs, Default, False]);
209 FastAssign(RPCBrokerV.Results, Dest);
210end;
211procedure LoadDOWSchedules(Dest: TStrings);
212begin
213 // if uMedSchedules = nil then CallV('ORWDPS ALLSCHD', [nil]); uMedSchedules.Assign(...);
214 CallV('ORWDPS1 DOWSCH', [patient.dfn, patient.location]);
215 FastAssign(RPCBrokerV.Results, Dest);
216end;
217
218function QtyToDays(Quantity: Double; const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
219begin
220 Result := StrToIntDef(sCallV('ORWDPS2 QTY2DAY',
221 [Quantity, UnitsPerDose, Schedule, Duration, Patient.DFN, Drug]), 0);
222end;
223
224function DaysToQty(DaysSupply: Integer; const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
225begin
226 Result := StrToIntDef(sCallV('ORWDPS2 DAY2QTY',
227 [DaysSupply, UnitsPerDose, Schedule, Duration, Patient.DFN, Drug]), 0);
228 if uDrugHasMaxData.CaptureMaxData = True then uDrugHasMaxData.MaxQuantity := Result;
229end;
230
231function DurToQty(DaysSupply: Integer; const UnitStr, SchedStr: string): Integer;
232begin
233 Result := StrToIntDef(sCallV('ORWDPS2 DAY2QTY', [DaysSupply, UnitStr, SchedStr]), 0);
234end;
235
236function DefaultDays(const ADrug, UnitStr, SchedStr: string): Integer;
237begin
238 Result := StrToIntDef(sCallV('ORWDPS1 DFLTSPLY', [UnitStr, SchedStr, Patient.DFN, ADrug]), 0);
239 if uDrugHasMaxData.CaptureMaxData = True then uDrugHasMaxData.MaxSupply := Result;
240end;
241
242function CalcMaxRefills(const Drug: string; Days, OrdItem: Integer; Discharge: Boolean): Integer;
243begin
244 Result := StrToIntDef(sCallV('ORWDPS2 MAXREF', [Patient.DFN, Drug, Days, OrdItem, Discharge]), 0);
245 if uDrugHasMaxData.CaptureMaxData = True then uDrugHasMaxData.MaxRefills := Result;
246end;
247
248function ScheduleRequired(OrdItem: Integer; const ARoute, ADrug: string): Boolean;
249begin
250 Result := sCallV('ORWDPS2 SCHREQ', [OrdItem, ARoute, ADrug]) = '1';
251end;
252
253function ODForMedsIn: TStrings;
254{ Returns init values for inpatient meds dialog. The results must be used immediately. }
255begin
256 CallV('ORWDPS1 ODSLCT', [PST_UNIT_DOSE, Patient.DFN, Encounter.Location]);
257 Result := RPCBrokerV.Results;
258end;
259
260function ODForMedsOut: TStrings;
261{ Returns init values for outpatient meds dialog. The results must be used immediately. }
262begin
263 CallV('ORWDPS1 ODSLCT', [PST_OUTPATIENT, Patient.DFN, Encounter.Location]);
264 Result := RPCBrokerV.Results;
265end;
266
267function OIForMed(AnIEN: Integer; ForInpatient: Boolean; HavePI: Boolean; PKIActive: Boolean): TStrings;
268var
269 PtType: Char;
270 NeedPI: Char;
271 IsPKIActive: Char;
272begin
273 if HavePI then NeedPI := 'Y' else NeedPI := 'N';
274 if ForInpatient then PtType := 'U' else PtType := 'O';
275 if PKIActive then IsPKIActive := 'Y' else IsPKIActive := 'N';
276 CallV('ORWDPS2 OISLCT', [AnIEN, PtType, Patient.DFN, NeedPI, IsPKIActive]);
277 Result := RPCBrokerV.Results;
278end;
279
280function GetPickupForLocation(const Loc: string): string;
281begin
282 Result := sCallV('ORWDPS1 LOCPICK',[Loc]);
283end;
284
285function QOHasRouteDefined(AQOID: integer): boolean;
286begin
287 Result := False;
288 if ( sCallV('ORWDPS1 HASROUTE',[AQOID])='1' ) then
289 Result := True;
290end;
291
292procedure CheckExistingPI(AOrderId: string; var APtI: string);
293begin
294 APtI := sCallV('ORWDPS2 CHKPI', [AOrderId]);
295end;
296
297function PassDrugTest(OI: integer; OrderType: string; InptOrder: boolean; CheckForClozapineOnly: boolean = false): boolean;
298var
299MessCap, MessText: string;
300i: integer;
301begin
302 result := false;
303 MessText := '';
304 uDrugHasMaxData.CaptureMaxData := false;
305 uDrugHasMaxData.MaxSupply := 0;
306 uDrugHasMaxData.MaxQuantity := 0;
307 uDrugHasMaxData.MaxRefills := 0;
308 CallV('ORALWORD ALLWORD', [Patient.DFN, OI, OrderType, Encounter.Provider]);
309 for i := 0 to RPCBrokerV.Results.Count -1 do
310 begin
311 if i = 0 then
312 begin
313 MessCap := Piece(RPCBrokerV.Results.strings[i],U,1);
314 if Piece(RPCBrokerV.Results.strings[i],U,2) = '1' then uDrugHasMaxData.CaptureMaxData := True;
315 end;
316 if i >0 then MessText := MessText + RPCBrokerV.Results.Strings[i] + CRLF;
317 end;
318 if CheckForClozapineOnly = True then
319 begin
320 Result := uDrugHasMaxData.CaptureMaxData = True;
321 Exit;
322 end;
323 if (MessText = '') and (MessCap = '') then
324 begin
325 Result := True;
326 if (InptOrder = true) and (uDrugHasMaxData.CaptureMaxData = true) then
327 begin
328 uDrugHasMaxData.CaptureMaxData := false;
329 if uInpatientClozapineText.dispText = '' then
330 begin
331 CallV('ORDDPAPI CLOZMSG', []);
332 for i := 0 to RPCBrokerV.Results.Count -1 do
333 if i = 0 then uInpatientClozapineText.dispText := RPCBrokerV.Results.Strings[i]
334 else uInpatientClozapineText.dispText := uInpatientClozapineText.dispText + CRLF + RPCBrokerV.Results.Strings[i];
335 end;
336 if uInpatientClozapineText.dispText <> '' then infoBox(uInpatientClozapineText.dispText, 'Inpatient Drug Warning', MB_OK);
337 end;
338 exit;
339 end;
340 infoBox(MessText, MessCap,MB_OK);
341end;
342
343function AdminTimeHelpText(): string;
344var
345i: integer;
346begin
347 if uAdminTimeHelpText.HelpText = '' then
348 begin
349 CallV('ORDDPAPI ADMTIME',[]);
350 for I := 0 to RPCBrokerV.Results.Count - 1 do
351 if I = 0 then uAdminTimeHelpText.HelpText := RPCBrokerV.Results.Strings[i]
352 else uAdminTimeHelpText.HelpText := uAdminTimeHelpText.HelpText + CRLF +RPCBrokerV.Results.Strings[i];
353 end;
354 Result := uAdminTimeHelpText.helpText
355end;
356
357function ValidateDrugAutoAccept(tempDrug, tempUnit, tempSch, tempDur: string; OI, tempSupply, tempQuantity, tempRefills: integer): boolean;
358var
359daySupply, Quantity, Refills: integer;
360begin
361 Result := True;
362 if uDrugHasMaxData.CaptureMaxData = false then exit;
363 daySupply := DefaultDays(tempDrug, tempUnit, tempSch);
364 if (tempSupply > daySupply) and (uDrugHasMaxData.MaxSupply > 0) then
365 begin
366 infoBox('For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxSupply), 'Cannot Save Error', MB_OK);
367 Result := false;
368 uDrugHasMaxData.CaptureMaxData := false;
369 Exit;
370 end;
371 Quantity := DaysToQty(daySupply, tempUnit, tempSch, tempDur, tempDrug);
372 if (tempQuantity > Quantity) and (uDrugHasMaxData.MaxQuantity > 0) then
373 begin
374 infoBox('For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity), 'Cannot Save Error', MB_OK);
375 Result := false;
376 uDrugHasMaxData.CaptureMaxData := false;
377 Exit;
378 end;
379 Refills := CalcMaxRefills(tempDrug, daySupply, OI, false);
380 if tempRefills > Refills then
381 begin
382 infoBox('For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxRefills), 'Cannot Save Error', MB_OK);
383 Result := false;
384 uDrugHasMaxData.CaptureMaxData := false;
385 Exit;
386 end;
387end;
388
389function ValidateDaySupplyandQuantity(DaySupply, Quantity: integer): boolean;
390var
391str: string;
392begin
393 Result := True;
394 str := '';
395 if uDrugHasMaxData.CaptureMaxData = false then exit;
396 if (daySupply > uDrugHasMaxData.MaxSupply) and (uDrugHasMaxData.MaxSupply > 0) then
397 begin
398 str := 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxSupply);
399 Result := false;
400 end;
401 if (Quantity > uDrugHasMaxData.MaxQuantity) and (uDrugHasMaxData.MaxQuantity > 0) then
402 begin
403 if str <> '' then str := str + CRLF + 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity)
404 else str := 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity);
405 result := false;
406 end;
407 if str <> '' then infoBox(str, 'Cannot Save Error', MB_OK);
408 //uDrugHasMaxData.CaptureMaxData := false;
409end;
410
411function ValidateMaxQuantity(Quantity: integer): boolean;
412begin
413 Result := True;
414 if uDrugHasMaxData.CaptureMaxData = false then exit;
415 if uDrugHasMaxData.MaxQuantity = 0 then exit;
416 if Quantity > uDrugHasMaxData.MaxQuantity then
417 begin
418 infoBox('For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity), 'Cannot Save Error', MB_OK);
419 Result := false;
420 end;
421end;
422
423function ValidateDaySupplyandQuantityErrorMsg(DaySupply, quantity: integer): String;
424begin
425 Result := '';
426 if uDrugHasMaxData.CaptureMaxData = false then exit;
427 if (daySupply > uDrugHasMaxData.MaxSupply) and (uDrugHasMaxData.MaxSupply > 0) then
428 begin
429 Result := 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxSupply);
430 end;
431 if (Quantity > uDrugHasMaxData.MaxQuantity) and (uDrugHasMaxData.MaxQuantity > 0) then
432 begin
433 if Result <> '' then Result := Result + CRLF + 'For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity)
434 else Result := 'For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity);
435 end;
436 //uDrugHasMaxData.CaptureMaxData := false;
437end;
438
439function ValidateQuantityErrorMsg(Quantity: integer): String;
440begin
441 Result := '';
442 if uDrugHasMaxData.CaptureMaxData = false then exit;
443 if uDrugHasMaxData.MaxQuantity = 0 then exit;
444 if Quantity > uDrugHasMaxData.MaxQuantity then
445 begin
446 Result := 'For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity);
447 end;
448end;
449
450procedure ClearMaxData;
451begin
452 uDrugHasMaxData.CaptureMaxData := false;
453end;
454
455function DifferentOrderLocations(ID: string; Loc: integer): boolean;
456begin
457 Result := (sCallV('ORWDPS33 COMPLOC', [ID, Loc])='1');
458end;
459
460function IsClozapineOrder: boolean;
461begin
462 if uDrugHasMaxData.CaptureMaxData = true then result := true
463 else result := false;
464end;
465
466function GetQOOrderableItem(DialogIEN: string): integer;
467begin
468 Result := StrtoInt(SCallV('ORWDPS1 QOMEDALT',[DialogIEN]))
469end;
470
471end.
Note: See TracBrowser for help on using the repository browser.