Index: cprs/trunk/CPRS-Chart/Orders/rODMeds.pas
===================================================================
--- cprs/trunk/CPRS-Chart/Orders/rODMeds.pas	(revision 456)
+++ cprs/trunk/CPRS-Chart/Orders/rODMeds.pas	(revision 829)
@@ -3,5 +3,21 @@
 interface
 
-uses SysUtils, Classes, ORNet, ORFn, uCore, uConst;
+uses SysUtils, Classes, ORNet, ORFn, uCore, uConst, Windows;
+
+type
+   TAdminTimeHelpText = record
+    HelpText: string;
+  end;
+
+  TInpatientClozapineText = record
+    dispText: string;
+  end;
+
+  TDrugHasMaxData = record
+    CaptureMaxData: boolean;
+    MaxSupply: integer;
+    MaxQuantity: integer;
+    MaxRefills: integer;
+  end;
 
 function DEACheckFailed(AnOI: Integer; ForInpatient: Boolean): Boolean;
@@ -17,7 +33,10 @@
 procedure LoadFormularyAltDose(AList: TStringList; DispDrug, OI: Integer; ForInpatient: Boolean);
 procedure LoadAdminInfo(const Schedule: string; OrdItem: Integer; var StartText: string;
-  var AdminTime: TFMDateTime; var Duration: string);
+  var AdminTime: TFMDateTime; var Duration: string; Admin: string = '');
 function GetAdminTime(const StartText, Schedule: string; OrdItem: Integer): TFMDateTime;
 procedure LoadSchedules(Dest: TStrings; IsInptDlg: boolean = False);
+procedure LoadDOWSchedules(Dest: TStrings);
+procedure LoadAllIVRoutes(Dest: TStrings);
+procedure LoadDosageFormIVRoutes(Dest: TStrings; OrderIDs: TStringList; Default: boolean);
 function QtyToDays(Quantity: Double;   const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
 function DaysToQty(DaysSupply: Integer; const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
@@ -32,6 +51,22 @@
 function QOHasRouteDefined(AQOID: integer): boolean;
 procedure CheckExistingPI(AOrderId: string; var APtI: string);
+function PassDrugTest(OI: integer; OrderType: string; InptOrder: boolean; CheckForClozapineOnly: boolean = false): boolean;
+function AdminTimeHelpText(): string;
+//function ValidateDaySupplyandQuantity(DaySupply, Quantity: integer): boolean;
+//function ValidateMaxQuantity(Quantity: integer): boolean;
+function ValidateDrugAutoAccept(tempDrug, tempUnit, tempSch, tempDur: string; OI, tempSupply, tempQuantity, tempRefills: integer): boolean;
+function ValidateDaySupplyandQuantityErrorMsg(DaySupply, quantity: integer): String;
+procedure ClearMaxData;
+function DifferentOrderLocations(ID: string; Loc: integer): boolean;
+function IsClozapineOrder: boolean;
+//function ValidateQuantityErrorMsg(Quantity: integer): String;
+function GetQOOrderableItem(DialogIEN: string): integer;
+
 
 implementation
+ var
+  uAdminTimeHelpText: TAdminTimeHelpText;
+  uDrugHasMaxData: TDrugHasMaxData;
+  uInpatientClozapineText : TInpatientClozapineText;
 
 function DEACheckFailed(AnOI: Integer; ForInpatient: Boolean): Boolean;
@@ -60,5 +95,5 @@
 begin
   CallV('ORWUL FVSUB', [ListIEN, First+1, Last+1]);  // M side not 0-based
-  if Append then Dest.AddStrings(RPCBrokerV.Results) else
+  if Append then FastAddStrings(RPCBrokerV.Results, Dest) else
   begin
     for i := Pred(RPCBrokerV.Results.Count) downto 0 do Dest.Insert(0, RPCBrokerV.Results[i]);
@@ -123,5 +158,5 @@
   if ForInpatient then PtType := 'I' else PtType := 'O';
   CallV('ORWDPS1 FORMALT', [AnIEN, PtType]);
-  AList.Assign(RPCBrokerV.Results);
+  FastAssign(RPCBrokerV.Results, AList);
 end;
 
@@ -132,13 +167,13 @@
   if ForInpatient then PtType := 'I' else PtType := 'O';
   CallV('ORWDPS1 DOSEALT', [DispDrug, OI, PtType]);
-  AList.Assign(RPCBrokerV.Results);
+  FastAssign(RPCBrokerV.Results, AList);
 end;
 
 procedure LoadAdminInfo(const Schedule: string; OrdItem: Integer; var StartText: string;
-  var AdminTime: TFMDateTime; var Duration: string);
+  var AdminTime: TFMDateTime; var Duration: string; Admin: string = '');
 var
   x: string;
 begin
-  x := sCallV('ORWDPS2 ADMIN', [Patient.DFN, Schedule, OrdItem, Encounter.Location]);
+  x := sCallV('ORWDPS2 ADMIN', [Patient.DFN, Schedule, OrdItem, Encounter.Location, Admin]);
   StartText := Piece(x, U, 1);
   AdminTime := MakeFMDateTime(Piece(x, U, 4));
@@ -157,10 +192,28 @@
 begin
   // if uMedSchedules = nil then CallV('ORWDPS ALLSCHD', [nil]); uMedSchedules.Assign(...);
-  CallV('ORWDPS1 SCHALL', [nil]);
-  Dest.Assign(RPCBrokerV.Results);
+  CallV('ORWDPS1 SCHALL', [patient.dfn, patient.location]);
+  FastAssign(RPCBrokerV.Results, Dest);
   If (Dest.IndexOfName('OTHER') < 0) and IsInptDlg then
     Dest.Add('OTHER');
 end;
 
+procedure LoadAllIVRoutes(Dest: TStrings);
+begin
+  CallV('ORWDPS32 ALLIVRTE', []);
+  FastAssign(RPCBrokerV.Results, Dest);
+end;
+
+procedure LoadDosageFormIVRoutes(Dest: TStrings; OrderIDs: TStringList; Default: boolean);
+begin
+  CallV('ORWDPS33 IVDOSFRM', [OrderIDs, Default, False]);
+  FastAssign(RPCBrokerV.Results, Dest);
+end;
+procedure LoadDOWSchedules(Dest: TStrings);
+begin
+  // if uMedSchedules = nil then CallV('ORWDPS ALLSCHD', [nil]); uMedSchedules.Assign(...);
+  CallV('ORWDPS1 DOWSCH', [patient.dfn, patient.location]);
+  FastAssign(RPCBrokerV.Results, Dest);
+end;
+
 function QtyToDays(Quantity: Double;   const UnitsPerDose, Schedule, Duration, Drug: string): Integer;
 begin
@@ -173,4 +226,5 @@
   Result := StrToIntDef(sCallV('ORWDPS2 DAY2QTY',
     [DaysSupply, UnitsPerDose, Schedule, Duration, Patient.DFN, Drug]), 0);
+  if uDrugHasMaxData.CaptureMaxData = True then uDrugHasMaxData.MaxQuantity := Result;
 end;
 
@@ -183,4 +237,5 @@
 begin
   Result := StrToIntDef(sCallV('ORWDPS1 DFLTSPLY', [UnitStr, SchedStr, Patient.DFN, ADrug]), 0);
+  if uDrugHasMaxData.CaptureMaxData = True then uDrugHasMaxData.MaxSupply := Result;
 end;
 
@@ -188,4 +243,5 @@
 begin
   Result := StrToIntDef(sCallV('ORWDPS2 MAXREF', [Patient.DFN, Drug, Days, OrdItem, Discharge]), 0);
+  if uDrugHasMaxData.CaptureMaxData = True then uDrugHasMaxData.MaxRefills := Result;
 end;
 
@@ -239,3 +295,177 @@
 end;
 
+function PassDrugTest(OI: integer; OrderType: string; InptOrder: boolean; CheckForClozapineOnly: boolean = false): boolean;
+var
+MessCap, MessText: string;
+i: integer;
+begin
+  result := false;
+  MessText := '';
+  uDrugHasMaxData.CaptureMaxData := false;
+  uDrugHasMaxData.MaxSupply := 0;
+  uDrugHasMaxData.MaxQuantity := 0;
+  uDrugHasMaxData.MaxRefills := 0;
+  CallV('ORALWORD ALLWORD', [Patient.DFN, OI, OrderType, Encounter.Provider]);
+  for i := 0 to RPCBrokerV.Results.Count -1 do
+    begin
+      if i = 0 then
+        begin
+          MessCap := Piece(RPCBrokerV.Results.strings[i],U,1);
+          if Piece(RPCBrokerV.Results.strings[i],U,2) = '1' then uDrugHasMaxData.CaptureMaxData := True;
+        end;
+      if i >0 then MessText := MessText + RPCBrokerV.Results.Strings[i] + CRLF;
+    end;
+  if CheckForClozapineOnly = True then
+    begin
+      Result := uDrugHasMaxData.CaptureMaxData = True;
+      Exit;
+    end;
+  if (MessText = '') and (MessCap = '') then
+    begin
+      Result := True;
+      if (InptOrder = true) and (uDrugHasMaxData.CaptureMaxData = true) then
+        begin
+          uDrugHasMaxData.CaptureMaxData := false;
+          if uInpatientClozapineText.dispText = '' then
+            begin
+              CallV('ORDDPAPI CLOZMSG', []);
+              for i := 0 to RPCBrokerV.Results.Count -1 do
+                 if i = 0 then uInpatientClozapineText.dispText := RPCBrokerV.Results.Strings[i]
+                 else uInpatientClozapineText.dispText := uInpatientClozapineText.dispText + CRLF + RPCBrokerV.Results.Strings[i];
+            end;
+          if uInpatientClozapineText.dispText <> '' then infoBox(uInpatientClozapineText.dispText, 'Inpatient Drug Warning', MB_OK);
+        end;
+      exit;
+    end;
+  infoBox(MessText, MessCap,MB_OK);
+end;
+
+function AdminTimeHelpText(): string;
+var
+i: integer;
+begin
+      if uAdminTimeHelpText.HelpText = '' then
+       begin
+          CallV('ORDDPAPI ADMTIME',[]);
+          for I := 0 to RPCBrokerV.Results.Count - 1 do
+            if I = 0 then uAdminTimeHelpText.HelpText := RPCBrokerV.Results.Strings[i]
+            else uAdminTimeHelpText.HelpText := uAdminTimeHelpText.HelpText + CRLF +RPCBrokerV.Results.Strings[i];
+       end;
+   Result := uAdminTimeHelpText.helpText
+end;
+
+function ValidateDrugAutoAccept(tempDrug, tempUnit, tempSch, tempDur: string; OI, tempSupply, tempQuantity, tempRefills: integer): boolean;
+var
+daySupply, Quantity, Refills: integer;
+begin
+  Result := True;
+  if uDrugHasMaxData.CaptureMaxData = false then exit;
+  daySupply := DefaultDays(tempDrug, tempUnit, tempSch);
+  if (tempSupply > daySupply) and (uDrugHasMaxData.MaxSupply > 0) then
+    begin
+      infoBox('For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxSupply), 'Cannot Save Error', MB_OK);
+      Result := false;
+      uDrugHasMaxData.CaptureMaxData := false;
+      Exit;
+    end;
+  Quantity := DaysToQty(daySupply, tempUnit, tempSch, tempDur, tempDrug);
+  if (tempQuantity > Quantity) and (uDrugHasMaxData.MaxQuantity > 0) then
+    begin
+      infoBox('For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity), 'Cannot Save Error', MB_OK);
+      Result := false;
+      uDrugHasMaxData.CaptureMaxData := false;
+      Exit;
+    end;
+  Refills := CalcMaxRefills(tempDrug, daySupply, OI, false);
+  if tempRefills > Refills then
+    begin
+      infoBox('For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxRefills), 'Cannot Save Error', MB_OK);
+      Result := false;
+      uDrugHasMaxData.CaptureMaxData := false;
+      Exit;
+    end;
+end;
+
+function ValidateDaySupplyandQuantity(DaySupply, Quantity: integer): boolean;
+var
+str: string;
+begin
+  Result := True;
+  str := '';
+  if uDrugHasMaxData.CaptureMaxData = false then exit;
+  if (daySupply > uDrugHasMaxData.MaxSupply) and (uDrugHasMaxData.MaxSupply > 0) then
+    begin
+      str := 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxSupply);
+      Result := false;
+    end;
+  if (Quantity > uDrugHasMaxData.MaxQuantity) and (uDrugHasMaxData.MaxQuantity > 0) then
+    begin
+      if str <> '' then str := str + CRLF + 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity)
+      else str := 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity);
+      result := false;
+    end;
+ if str <> '' then infoBox(str, 'Cannot Save Error', MB_OK);
+ //uDrugHasMaxData.CaptureMaxData := false;
+end;
+
+function ValidateMaxQuantity(Quantity: integer): boolean;
+begin
+  Result := True;
+  if uDrugHasMaxData.CaptureMaxData = false then exit;
+  if uDrugHasMaxData.MaxQuantity = 0 then exit;
+  if Quantity > uDrugHasMaxData.MaxQuantity then
+    begin
+      infoBox('For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity), 'Cannot Save Error', MB_OK);
+      Result := false;
+    end;
+end;
+
+function ValidateDaySupplyandQuantityErrorMsg(DaySupply, quantity: integer): String;
+begin
+  Result := '';
+  if uDrugHasMaxData.CaptureMaxData = false then exit;
+  if (daySupply > uDrugHasMaxData.MaxSupply) and (uDrugHasMaxData.MaxSupply > 0) then
+    begin
+      Result := 'For this medication Day Supply cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxSupply);
+    end;
+  if (Quantity > uDrugHasMaxData.MaxQuantity) and (uDrugHasMaxData.MaxQuantity > 0) then
+    begin
+      if Result <> '' then Result := Result + CRLF + 'For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity)
+      else Result := 'For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity);
+    end;
+  //uDrugHasMaxData.CaptureMaxData := false;
+end;
+
+function ValidateQuantityErrorMsg(Quantity: integer): String;
+begin
+  Result := '';
+  if uDrugHasMaxData.CaptureMaxData = false then exit;
+  if uDrugHasMaxData.MaxQuantity = 0 then exit;
+  if Quantity > uDrugHasMaxData.MaxQuantity then
+    begin
+      Result := 'For this medication Quantity cannot be greater then ' + InttoStr(uDrugHasMaxData.MaxQuantity);
+    end;
+end;
+
+procedure ClearMaxData;
+begin
+  uDrugHasMaxData.CaptureMaxData := false;
+end;
+
+function DifferentOrderLocations(ID: string; Loc: integer): boolean;
+begin
+   Result := (sCallV('ORWDPS33 COMPLOC', [ID, Loc])='1');
+end;
+
+function IsClozapineOrder: boolean;
+begin
+   if uDrugHasMaxData.CaptureMaxData = true then result := true
+   else result := false;
+end;
+
+function GetQOOrderableItem(DialogIEN: string): integer;
+begin
+  Result := StrtoInt(SCallV('ORWDPS1 QOMEDALT',[DialogIEN]))
+end;
+
 end.
