Index: cprs/trunk/CPRS-Chart/fPrintLocation.pas
===================================================================
--- cprs/trunk/CPRS-Chart/fPrintLocation.pas	(revision 830)
+++ cprs/trunk/CPRS-Chart/fPrintLocation.pas	(revision 1679)
@@ -25,5 +25,4 @@
     cboEncLoc: TComboBox;
     procedure pnlFieldsResize(Sender: TObject);
-    //procedure FormCreate(Sender: TObject);
     procedure orderGridMouseDown(Sender: TObject; Button: TMouseButton;
       Shift: TShiftState; X, Y: Integer);
@@ -38,13 +37,16 @@
     procedure FormResize(Sender: TObject);
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
+    procedure FormDestroy(Sender: TObject);
   private
     { Private declarations }
+    CLoc,WLoc: string;
+    CIEN,WIEN: integer;
     function ValFor(FieldID, ARow: Integer): string;
     procedure ShowEditor(ACol, ARow: Integer; AChar: Char);
     procedure ProcessClinicOrders(WardList, ClinicList: TStringList; WardIEN, ClinicIEN: integer; ContainsIMO: boolean);
     procedure rpcChangeOrderLocation(pOrderList:TStringList; ContainsIMO: boolean);
+    function ClinicText(ALoc: integer): string;
   public
      { Public declarations }
-  CLoc,WLoc: string;
   CloseOK: boolean;
   DisplayOrders: boolean;
@@ -53,4 +55,5 @@
              var wardIEN: integer; var wardLocation: string; ContainsIMOOrders: boolean; displayEncSwitch: boolean = false);
   procedure SwitchEncounterLoction(pEncounterLoc: integer; pEncounterLocName, pEncounterLocText: string; pEncounterDT: TFMDateTime; pEncounterVC: Char);
+  function rpcIsPatientOnWard(Patient: string): string;
   end;
 
@@ -73,8 +76,9 @@
 COL_LOCATION  = 2;
 TAB           = #9;
-  LOCATION_CHANGE_1 = 'This patient is currently admitted to ward';
-  LOCATION_CHANGE_2 = 'These orders are written at clinic';
-  LOCATION_CHANGE_3 = 'What Location are these orders associated with?';
-  LOCATION_CHANGE_4 = 'The encounter location is currently at clinic';
+  LOCATION_CHANGE_1  = 'The patient is admitted to ward';
+  LOCATION_CHANGE_2  = 'You have the chart open to a clinic location of';
+  LOCATION_CHANGE_2W = 'You have the chart open with the patient still on ward';
+  LOCATION_CHANGE_3  = 'What Location are these orders associated with?';
+  LOCATION_CHANGE_4  = 'The patient has now been admitted to ward: ';
 
 
@@ -179,4 +183,10 @@
 end;
 
+procedure TfrmPrintLocation.FormDestroy(Sender: TObject);
+begin
+  inherited;
+  frmPrintLocation := nil;
+end;
+
 procedure TfrmPrintLocation.FormResize(Sender: TObject);
 begin
@@ -185,21 +195,9 @@
 end;
 
-(*procedure TfrmPrintLocation.FormCreate(Sender: TObject);
-var
-  ListCount: Integer;
-  x: string;
-begin
-   inherited;
-   with OrderGrid do
-     begin
-       ColWidths[0] := 8;
-       ColWidths[1] := 50;
-       ColWidths[2] := 50;
-       Cells[1,0] := 'Order';
-       Cells[2,0] := 'Location';
-     end;
-   //TAccessibleStringGrid.WrapControl(OrderGrid);
-
-end;  *)
+function TfrmPrintLocation.ClinicText(ALoc: integer): string;
+begin
+  if SCallV('ORIMO ISCLOC', [ALoc]) = '1' then Result := LOCATION_CHANGE_2
+  else Result := LOCATION_CHANGE_2W
+end;
 
 procedure TfrmPrintLocation.OrderGridDrawCell(Sender: TObject; ACol,
@@ -251,5 +249,11 @@
         if frmPrintLocation.DisplayOrders = false then
           begin
-            lblText.Caption := lblText.Caption + LOCATION_CHANGE_4 + ': ' + frmPrintLocation.CLoc + CRLF;
+            if frmPrintlocation.CLoc = '' then
+              begin
+                lblText.Caption := LOCATION_CHANGE_4 + frmPrintLocation.WLoc + CRLF;
+                cboEncLoc.Enabled := false;
+                lblEncounter.Enabled := false;
+              end
+            else lblText.Caption := lblText.Caption + ClinicText(frmPrintLocation.CIEN) + ': ' + frmPrintLocation.CLoc + CRLF;
             btnClinic.Visible := false;
             btnWard.Visible := false;
@@ -265,7 +269,7 @@
         else
           begin
-            lblText.Caption := lblText.Caption + LOCATION_CHANGE_2 + ': ' + frmPrintLocation.CLoc + CRLF + CRLF;
+            lblText.Caption := lblText.Caption + ClinicText(frmPrintLocation.CIEN) + ': ' + frmPrintLocation.CLoc + CRLF + CRLF;
             lblText.Caption := lblText.Caption + LOCATION_CHANGE_3;
-            lblText.Caption := lblText.Caption + CRLF + 'One clinic location allowed; ' + frmPrintLocation.CLoc + ' will be used';
+            //lblText.Caption := lblText.Caption + CRLF + 'One clinic location allowed; ' + frmPrintLocation.CLoc + ' will be used';
             btnClinic.Caption := 'All ' + frmPrintLocation.CLoc;
             btnWard.Caption := 'All ' + frmPrintLocation.WLoc;
@@ -335,4 +339,5 @@
 begin
   frmPrintLocation := TfrmPrintLocation.Create(Application);
+  try
   frmPrintLocation.DisplayOrders := true;
   frmPrintLocation.CloseOK := false;
@@ -345,4 +350,6 @@
   frmPrintLocation.Cloc := pEncounterLocName;
   frmPrintLocation.WLoc := WardLocation;
+  frmPrintLocation.CIEN := pEncounterLoc;
+  frmPrintLocation.WIEN := wardIEN;
   ResizeAnchoredFormToFont(frmPrintLocation);
   frmPrintLocation.orderGrid.DefaultRowHeight := frmPrintLocation.cbolocation.Height;
@@ -369,24 +376,16 @@
   if frmPrintLocation.cboEncLoc.ItemIndex = cidx then
     begin
-      //Encounter.Location := pEncounterLoc;
-      //Encounter.LocationName := pEncounterLocName;
-      //Encounter.LocationText := pEncounterLocText;
       uCore.Encounter.EncounterSwitch(pEncounterLoc, pEncounterLocName, pEncounterLocText, pEncounterDT, pEncounterVC);
       fframe.frmFrame.DisplayEncounterText;
-      //fframe.frmFrame.DoNotChangeEncWindow := true;
       fframe.frmFrame.OrderPrintForm := True;
     end
   else if frmPrintLocation.cboEncLoc.ItemIndex = widx then
     begin
-    uCore.Encounter.EncounterSwitch(WardIEN, WardLocation, '', Patient.AdmitTime, 'H');
-   (* Encounter.Location := WardIEN;
-    Encounter.DateTime := Patient.AdmitTime;
-    Encounter.VisitCategory := 'H';
-    Encounter.Location := WardIEN;
-    Encounter.LocationName := WardLocation; *)
-      //fframe.frmFrame.DoNotChangeEncWindow := false;
+    uCore.Encounter.EncounterSwitch(WardIEN, WardLocation, WardLocation, Patient.AdmitTime, 'H');
       fFrame.frmFrame.DisplayEncounterText;
     end;
-  frmPrintLocation.Close;
+  finally
+   frmPrintLocation.Destroy;
+  end;
 end;
 
@@ -422,8 +421,13 @@
 
 
+function TfrmPrintLocation.rpcIsPatientOnWard(Patient: string): string;
+begin
+  //Ward Loction Name^Ward Location IEN
+  result := sCallV('ORWDX1 PATWARD',[Patient]);
+end;
+
 procedure TfrmPrintLocation.ShowEditor(ACol, ARow: Integer; AChar: Char);
 var
   tmpText: string;
-  //Index: integer;
 
   procedure PlaceControl(AControl: TWinControl);
@@ -455,8 +459,5 @@
 begin
   inherited;
-  //if AChar = ' ' then AChar := #0;
   if ARow = 0 then Exit;
-  //with OrderGrid do if (Acol > COL_ORDER) and (ValFor(COL_ORDER, ARow) = '') then Exit;
-  //if (ACol = COL_Order) and (ARow > 1) and (ValFor(COL_Order, ARow-1) = '') then Exit;
   Case ACol of
   COL_LOCATION: begin
@@ -475,6 +476,6 @@
 Asvc, WardLocation: string;
 begin
-  //frmPrintLocation.Caption := 'Refresh Encounter Location Form';
   frmPrintLocation := TfrmPrintLocation.Create(Application);
+  try
   frmPrintLocation.DisplayOrders := false;
   frmPrintLocation.CloseOK := false;
@@ -484,4 +485,6 @@
   frmPrintLocation.Cloc := pEncounterLocName;
   frmPrintLocation.WLoc := WardLocation;
+  frmPrintLocation.CIEN := pEncounterLoc;
+  frmPrintLocation.WIEN := wardIEN;
   ResizeAnchoredFormToFont(frmPrintLocation);
   frmPrintLocation.cboEncLoc.Items.Add(frmPrintLocation.CLoc);
@@ -491,4 +494,6 @@
   cidx := frmPrintLocation.cboEncLoc.Items.IndexOf(frmPrintLocation.CLoc);
   widx := frmPrintLocation.cboEncLoc.Items.IndexOf(frmPrintLocation.WLoc);
+  if frmPrintLocation.cboEncLoc.Enabled = FALSE then frmPrintLocation.cboEncLoc.ItemIndex := widx;
+  
   if frmPrintLocation.cboEncLoc.ItemIndex = cidx then
     begin
@@ -497,15 +502,13 @@
       Encounter.LocationText := pEncounterLocText;
       fframe.frmFrame.DisplayEncounterText;
-      //fframe.frmFrame.DoNotChangeEncWindow := true;
     end
   else if frmPrintLocation.cboEncLoc.ItemIndex = widx then
     begin
       uCore.Encounter.EncounterSwitch(WardIEN, WardLocation, '', Patient.AdmitTime, 'H');
-      (*Encounter.Location := WardIEN;
-      Encounter.LocationName := WardLocation; *)
-      //fframe.frmFrame.DoNotChangeEncWindow := false;
       fFrame.frmFrame.DisplayEncounterText;
     end;
-  frmPrintLocation.Close;
+  finally
+    frmPrintLocation.Destroy;
+  end;
 end;
 
