Index: cprs/branches/foia-cprs/CPRS-Chart/fNoteProps.pas
===================================================================
--- cprs/branches/foia-cprs/CPRS-Chart/fNoteProps.pas	(revision 459)
+++ cprs/branches/foia-cprs/CPRS-Chart/fNoteProps.pas	(revision 460)
@@ -1,3 +1,3 @@
-unit fNoteProps;                           
+unit fNoteProps;
 
 interface
@@ -5,7 +5,27 @@
 uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
-  StdCtrls, ORDtTm, ORCtrls, ExtCtrls, rTIU, uConst, uTIU, ORFn, ORNet;
+  StdCtrls, ORDtTm, ORCtrls, ExtCtrls, rTIU, uConst, uTIU, ORFn, ORNet,
+  ComCtrls, Buttons;
 
 type
+  {This object holds a List of Actions as Returned VIA the RPCBroker}
+  TPRFActions = class(TObject)
+  private
+    FPRFActionList : TStringList;
+  public
+    //procedure to show the Action in a ListView, requires a listview parameter
+    procedure ShowActionsOnList(DisplayList : TCaptionListView);
+    //procedure to load the actions, this will call the RPC
+    procedure Load(TitleIEN : Int64; DFN : String);
+    //returns true if the Action at the Index passed is associated with a note
+    function SelActionHasNote(lstIndex : integer) : boolean;
+    //return the Action IEN at the Index passed
+    function GetActionIEN(lstIndex : integer) : String;
+    //return the PRF IEN at the Index passed
+    function GetPRF_IEN(lstIndex : integer) : integer;
+    constructor Create();
+    destructor Destroy(); override;
+  end;
+
   TfrmNoteProperties = class(TForm)
     lblNewTitle: TLabel;
@@ -42,4 +62,9 @@
     lblProcDateTime: TOROffsetLabel;
     btnShowList: TButton;
+    pnlPRF: TORAutoPanel;
+    lblPRF: TLabel;
+    Bevel1: TBevel;
+    lvPRF: TCaptionListView;
+    btnDetails: TButton;
     procedure FormShow(Sender: TObject);
     procedure cboNewTitleNeedData(Sender: TObject; const StartFrom: String;
@@ -63,4 +88,7 @@
     procedure FormResize(Sender: TObject);
     procedure calNoteEnter(Sender: TObject);
+    procedure FormDestroy(Sender: TObject);
+    procedure btnDetailsClick(Sender: TObject);
+    procedure lstRequestsChange(Sender: TObject);
   private
     FCosignIEN: Int64;      // store cosigner that was passed in
@@ -81,13 +109,19 @@
     FProcDateTime: TFMDateTime;
     FCPStatusFlag: integer;
+    FPRFActions : TPRFActions;
+    FStarting: boolean;
     procedure SetCosignerRequired(DoSetup: boolean);
     procedure FormatRequestList;
     procedure ShowRequestList(ShouldShow: Boolean);
     procedure ShowSurgCaseList(ShouldShow: Boolean);
+    procedure ShowPRFList(ShouldShow: Boolean);
     procedure ShowClinProcFields(YesNo: boolean);
+    procedure SetGenericFormSize;
     procedure UMDelayEvent(var Message: TMessage); message UM_DELAYEVENT;
   public
     { Public declarations }
+
   end;
+
 
 function ExecuteNoteProperties(var ANote: TEditNoteRec; CallingTab: integer; IDNoteTitlesOnly,
@@ -101,9 +135,11 @@
  TC_INACTIVE_USER     = 'Inactive User Alert';
 
+ PIXEL_SPACE = 6;
+
 implementation
 
 {$R *.DFM}
 
-uses uCore, rCore, rConsults, uConsults, rSurgery, uAccessibleListBox;
+uses uCore, rCore, rConsults, uConsults, rSurgery, uAccessibleListBox, fRptBox;
 
 { Initial values in ANote
@@ -133,4 +169,6 @@
   TX_REQ_REQUEST       = CRLF + 'This title requires the selection of an associated consult request.';
   TX_REQ_SURGCASE      = CRLF + 'This title requires the selection of an associated surgery case.';
+  TX_REQ_PRF_ACTION    = CRLF + 'Notes of this title require the selection of a patient record flag action.';
+  TX_REQ_PRF_NOTE      = CRLF + 'This action has already been assigned to another note.';
   TX_NO_FUTURE         = CRLF + 'A reference date/time in the future is not allowed.';
   TX_COS_SELF          = CRLF + 'You cannot make yourself a cosigner.';
@@ -153,4 +191,14 @@
   ACTIVE_STATUS = 'ACTIVE,PENDING,SCHEDULED';
 
+  PRF_LABEL = 'Which Patient Record Flag Action should this Note be linked to?';
+
+  FLAG_NAME = 1;
+  PRF_IEN = 2;
+  ACTION_NAME = 3;
+  ACTION_IEN = 4;
+  ACTION_DATE_I = 5;
+  ACTION_DATE = 6;
+  NOTE_IEN = 7;
+
 
 var
@@ -176,5 +224,6 @@
       FIsClinProcNote := (AClassName = DCL_CLINPROC);
       FCPStatusFlag := CPStatusFlag;
-      uShowUnresolvedOnly := False;
+      //uShowUnresolvedOnly := False;                      //v26.5 (RV)
+      uShowUnresolvedOnly := True;                         //v26.5 (RV)
       if ANote.DocType <> TYP_ADDENDUM then
         begin
@@ -210,7 +259,8 @@
            CT_NOTES:      begin
                             Caption := 'Progress Note Properties';
-                            if ANote.IsNewNote and UnresolvedConsultsExist then
-                              uShowUnresolvedOnly := InfoBox(TX_UNRESOLVED_CONSULTS, 'Unresolved Consults Exist',
-                                MB_YESNO or MB_ICONQUESTION) = IDYES;
+                            if ANote.IsNewNote then
+                            begin
+                              GetUnresolvedConsultsInfo; // v26.5 (RV) removed nag screen
+                            end;
                             cboNewTitle.InitLongList('');
                             cboAuthor.InitLongList(ANote.AuthorName);
@@ -235,7 +285,9 @@
         end;
       ShowClinProcFields(FIsClinProcNote);
+      FStarting := True;
       if ANote.Title > 0 then cboNewTitle.SelectByIEN(ANote.Title);
       if (ANote.Title > 0) and (cboNewTitle.ItemIndex < 0)
         then cboNewTitle.SetExactByIEN(ANote.Title, ANote.TitleName);
+      FStarting := False;
       calNote.FMDateTime := ANote.DateTime;
       // setup cosigner fields
@@ -248,29 +300,22 @@
       SetCosignerRequired(True);
       // setup package fields
+      SetGenericFormSize;
       case FCallingTab of
         CT_CONSULTS:  begin
                         ShowRequestList(False);
                         ShowSurgCaseList(False);
+                        ShowPRFList(False);
                       end;
         CT_SURGERY :  begin
                         ShowRequestList(False);
                         ShowSurgCaseList(False);
+                        ShowPRFList(False);
                       end;
         CT_NOTES   :  begin
-                        if IsConsultTitle(ANote.Title) then
-                          begin
-                            ShowRequestList(True);
-                            ShowSurgCaseList(False);
-                          end
-                        else if IsSurgeryTitle(ANote.Title) then
-                          begin
-                            ShowSurgCaseList(True);
-                            ShowRequestList(False);
-                          end
-                        else
-                          begin
-                            ShowRequestList(False);
-                            ShowSurgCaseList(False);
-                          end;
+                        with uUnresolvedConsults do                          // v26.5 (RV)
+                          ShowRequestList(IsConsultTitle(ANote.Title) or
+                            (UnresolvedConsultsExist and ShowNagScreen));    // v26.5 (RV)
+                        ShowSurgCaseList(IsSurgeryTitle(ANote.Title));
+                        ShowPRFList(IsPRFTitle(ANote.Title));
                       end;
       end;
@@ -283,8 +328,10 @@
       end;
       cboNewTitle.Caption := lblNewTitle.Caption;
+      FStarting := True;
       cboNewTitleExit(frmNoteProperties);        // force display of request/case list
+      FStarting := False;
       if uShowUnresolvedOnly then                // override previous display if SHOW ME clicked on entrance
       begin
-        cboNewTitle.ItemIndex := -1;
+        //cboNewTitle.ItemIndex := -1;      CQ#7587, v26.25 - RV
         uShowUnresolvedOnly := not uShowUnresolvedOnly;
         FormatRequestList;
@@ -351,4 +398,9 @@
                               PkgPtr := PKG_SURGERY;
                               PkgRef := IntToStr(PkgIEN) + ';' + PkgPtr;
+                            end
+                          else if pnlPRF.Visible then //PRF
+                            begin
+                              PRF_IEN := FPRFActions.GetPRF_IEN(lvPRF.ItemIndex);
+                              ActionIEN := FPRFActions.GetActionIEN(lvPRF.ItemIndex);
                             end;
                         end;
@@ -392,6 +444,6 @@
   begin
     if cboNewTitle.ItemIEN = 0
-      then lblCosigner.Visible := AskCosignerForTitle(FDocType,            cboAuthor.ItemIEN)
-      else lblCosigner.Visible := AskCosignerForTitle(cboNewTitle.ItemIEN, cboAuthor.ItemIEN);
+      then lblCosigner.Visible := AskCosignerForTitle(FDocType,            cboAuthor.ItemIEN, calNote.FMDateTime)
+      else lblCosigner.Visible := AskCosignerForTitle(cboNewTitle.ItemIEN, cboAuthor.ItemIEN, calNote.FMDateTime);
   end;
   cboCosigner.Visible := lblCosigner.Visible;
@@ -427,12 +479,11 @@
   SavedIEN: integer;
 begin
+  ShouldShow := ShouldShow and (FCallingTab = CT_NOTES);
   if FDocType = TYP_ADDENDUM then ShouldShow := False;
+  pnlConsults.Visible := ShouldShow;
   if ShouldShow then
   begin
     SavedIEN := lstRequests.ItemIEN;
-    for i := 0 to Pred(ControlCount) do
-      if Controls[i].Tag = 1 then Controls[i].Visible := True;
-    pnlConsults.Align := alBottom;
-    ClientHeight := cboCosigner.Top + cboCosigner.Height + 6 + pnlConsults.Height;
+    ClientHeight := cboCosigner.Top + cboCosigner.Height + PIXEL_SPACE + pnlConsults.Height;
     lstRequests.Items.Clear;
     if uConsultsList.Count = 0 then ListConsultRequests(uConsultsList);
@@ -451,31 +502,18 @@
     lblConsult1.Visible := (cboNewTitle.ItemIndex > -1);
     lstRequests.SelectByIEN(SavedIEN);
-  end else
-  begin
-    for i := 0 to Pred(ControlCount) do
-      if Controls[i].Tag = 1 then Controls[i].Visible := False;
-    ClientHeight := cboCosigner.Top + cboCosigner.Height + 6;
-  end;
+    btnDetails.Enabled := (lstRequests.ItemIndex > -1);
+  end
 end;
 
 procedure TfrmNoteProperties.ShowSurgCaseList(ShouldShow: Boolean);
 { called initially & whenever title changes }
-var
-  i: Integer;
-begin
+begin
+  pnlSurgery.Visible := ShouldShow;
   if FDocType = TYP_ADDENDUM then ShouldShow := False;
   if ShouldShow then
   begin
-    for i := 0 to Pred(ControlCount) do
-      if Controls[i].Tag = 2 then Controls[i].Visible := True;
-    pnlSurgery.Align := alBottom;
-    ClientHeight := cboCosigner.Top + cboCosigner.Height + 6 + pnlSurgery.Height;
+    ClientHeight := cboCosigner.Top + cboCosigner.Height + PIXEL_SPACE + pnlSurgery.Height;
     if lstSurgery.Items.Count = 0 then ListSurgeryCases(lstSurgery.Items);
-  end else
-  begin
-    for i := 0 to Pred(ControlCount) do
-      if Controls[i].Tag = 2 then Controls[i].Visible := False;
-    ClientHeight := cboCosigner.Top + cboCosigner.Height + 6;
-  end;
+  end
 end;
 
@@ -503,4 +541,15 @@
 
 procedure TfrmNoteProperties.cboNewTitleMouseClick(Sender: TObject);
+const
+  TX_NEED_CONSULT_TITLE = 'You currently have unresolved consults awaiting completion.' + CRLF +
+                          'The selected title cannot be used to complete consults.' + CRLF +
+                          'You must select a Consults title to complete a consult.' + CRLF + CRLF +
+                          'Answer "YES" to continue with this title and not complete a consult.' + CRLF +
+                          'Answer "NO" to select a different title.' + CRLF + CRLF +
+                          'Do you want to use this title and continue?';                          
+  TC_NOT_CONSULT_TITLE = 'Not a consult title';
+var
+  WantsToCompleteConsult: boolean;
+  ConsultTitle: boolean;
 begin
   with cboNewTitle do
@@ -515,19 +564,21 @@
       CT_CONSULTS:  ;  // no action
       CT_SURGERY :  ;  // no action
-      CT_NOTES   :  if IsConsultTitle(cboNewTitle.ItemIEN) then
-                      begin
-                        ShowSurgCaseList(False);
-                        ShowRequestList(True);
-                      end
-                    else if IsSurgeryTitle(cboNewTitle.ItemIEN) then
-                      begin
-                        ShowRequestList(False);
-                        ShowSurgCaseList(True);
-                      end
-                    else
-                      begin
-                        ShowRequestList(False);
-                        ShowSurgCaseList(False);
-                      end;
+      CT_NOTES   :  begin            // v26.5 (RV) main changes here
+                      WantsToCompleteConsult := False;
+                      ConsultTitle := IsConsultTitle(cboNewTitle.ItemIEN);
+                      if (pnlConsults.Visible) and
+                         (lstRequests.Items.Count > 0) and
+                         (not FStarting) and
+                         (*(lstRequests.ItemID <> '') and*)
+                         (not ConsultTitle) then
+                          WantsToCompleteConsult := (InfoBox(TX_NEED_CONSULT_TITLE,
+                                                            TC_NOT_CONSULT_TITLE,
+                                                            MB_ICONWARNING or MB_YESNO or MB_DEFBUTTON2) = IDNO);
+                      if WantsToCompleteConsult and (not ConsultTitle) then cboNewTitle.ItemIndex := -1;
+                      SetGenericFormSize;
+                      ShowRequestList(WantsToCompleteConsult or ConsultTitle);
+                      ShowSurgCaseList(IsSurgeryTitle(cboNewTitle.ItemIEN));
+                      ShowPRFList(IsPRFTitle(cboNewTitle.ItemIEN));
+                    end;
     end;
   SetCosignerRequired(True);
@@ -573,5 +624,11 @@
 //var x: string;
 begin
-  with cboCosigner do if Text = '' then ItemIndex := -1;
+  with cboCosigner do if ((Text = '') or (ItemIEN = 0)) then
+  begin
+    ItemIndex := -1;
+    FCosignIEN := 0;
+    FCosignName := '';
+    exit;
+  end;
   FCosignIEN := cboCosigner.ItemIEN;
   FCosignName := Piece(cboCosigner.Items[cboCosigner.ItemIndex], U, 2);
@@ -602,16 +659,27 @@
                        ShowRequestList(False);
                        ShowSurgCaseList(False);
+                       ShowPRFList(False);
                     end;
   end;*)
   SetCosignerRequired(False);
   ErrMsg := '';
-   if cboNewTitle.ItemIEN = 0 then
-    ErrMsg := ErrMsg + TX_REQ_TITLE
-    //code added 12/2002  check note parm - one per visit  GRE
-  else if OneNotePerVisit(CboNewTitle.ItemIEN, Patient.DFN, Encounter.VisitStr)then
-      ErrMsg := ErrMsg + TX_ONE_NOTE_PER_VISIT1
-                       + Piece(cboNewTitle.Items[cboNewTitle.ItemIndex],U,2)
-                       + TX_ONE_NOTE_PER_VISIT2
-    else
+  if cboNewTitle.ItemIEN = 0 then
+    ErrMsg := ErrMsg + TX_REQ_TITLE ;
+  if ErrMsg = '' then
+    begin
+      if FDocType = TYP_ADDENDUM then
+        begin
+          if OneNotePerVisit(TYP_ADDENDUM, Patient.DFN, Encounter.VisitStr)then
+            ErrMsg := ErrMsg + TX_ONE_NOTE_PER_VISIT1
+                     + 'Addendum to ' + Piece(cboNewTitle.Items[cboNewTitle.ItemIndex],U,2)
+                     + TX_ONE_NOTE_PER_VISIT2;
+        end
+        //code added 12/2002  check note parm - one per visit  GRE
+      else if OneNotePerVisit(CboNewTitle.ItemIEN, Patient.DFN, Encounter.VisitStr)then
+            ErrMsg := ErrMsg + TX_ONE_NOTE_PER_VISIT1
+                     + Piece(cboNewTitle.Items[cboNewTitle.ItemIndex],U,2)
+                     + TX_ONE_NOTE_PER_VISIT2;
+    end;
+  if ErrMsg = '' then
     begin
       if FIDNoteTitlesOnly then
@@ -626,4 +694,11 @@
           else if ((pnlSurgery.Visible) and (lstSurgery.ItemIndex < 0)) then
             ErrMsg := ErrMsg + TX_REQ_SURGCASE
+          else if (pnlPRF.Visible) then
+          begin
+            if (lvPRF.ItemIndex < 0) then
+              ErrMsg := ErrMsg + TX_REQ_PRF_ACTION
+            else if FPRFActions.SelActionHasNote(lvPRF.ItemIndex) then
+              ErrMsg := ErrMsg + TX_REQ_PRF_NOTE;
+          end;
         end;
     end;
@@ -635,5 +710,5 @@
        if (cboCosigner.ItemIEN = 0)     then ErrMsg := ErrMsg + TX_REQ_COSIGNER;
       //if (cboCosigner.ItemIEN = User.DUZ) then ErrMsg := TX_COS_SELF;  // (CanCosign will do this check)
-      if (cboCosigner.ItemIEN > 0) and not CanCosign(cboNewTitle.ItemIEN, FDocType, cboCosigner.ItemIEN)
+      if (cboCosigner.ItemIEN > 0) and not CanCosign(cboNewTitle.ItemIEN, FDocType, cboCosigner.ItemIEN, calNote.FMDateTime)
         then ErrMsg := cboCosigner.Text + TX_COS_AUTH;
         //code added 02/2003  check if User is Inactive   GRE
@@ -719,5 +794,7 @@
     else
       Caption := SHOW_UNRESOLVED;
-  ShowRequestList(True);
+  with uUnresolvedConsults do if (UnresolvedConsultsExist and ShowNagScreen) then pnlConsults.Visible := TRUE;  //v26.27 (RV)
+  ShowRequestList(pnlConsults.Visible);      //v26.5 (RV)
+  //ShowRequestList(True);                   //v26.5 (RV)
 end;
 
@@ -739,3 +816,112 @@
 end;
 
+procedure TfrmNoteProperties.ShowPRFList(ShouldShow: Boolean);
+begin
+  pnlPRF.Visible := ShouldShow and not (FDocType = TYP_ADDENDUM);
+  if pnlPRF.Visible then
+  begin
+    ClientHeight := cboCosigner.Top + cboCosigner.Height + PIXEL_SPACE + pnlPRF.Height;
+    if FPRFActions = nil then
+      FPRFActions := TPRFActions.Create;
+    FPRFActions.Load(cboNewTitle.ItemIEN,Patient.DFN);
+    if RPCBrokerV.Results.Count <> 0 then
+      lblPRF.Caption := PRF_LABEL
+    else
+      lblPRF.Caption := 'No Linkable Actions for this Patient and/or Title.';
+    FPRFActions.ShowActionsOnList(lvPRF);
+    //Fix for CQ: 6926
+    lvPRF.Columns.BeginUpdate;
+    lvPRF.Columns.EndUpdate;
+    //End Fix for CQ: 6926
+  end
+end;
+
+procedure TfrmNoteProperties.SetGenericFormSize;
+begin
+  ClientHeight := cboCosigner.Top + cboCosigner.Height + PIXEL_SPACE;
+end;
+
+{ TPRFActions }
+
+constructor TPRFActions.Create;
+begin
+  inherited;
+  FPRFActionList := TStringList.Create;
+end;
+
+destructor TPRFActions.Destroy;
+begin
+  FPRFActionList.Free;
+  inherited;
+end;
+
+function TPRFActions.GetActionIEN(lstIndex: integer): String;
+begin
+  Result := Piece(FPRFActionList[lstIndex],U,ACTION_IEN);
+end;
+
+function TPRFActions.GetPRF_IEN(lstIndex: integer): integer;
+begin
+  Result := StrToInt(Piece(FPRFActionList[lstIndex],U,PRF_IEN));
+end;
+
+procedure TPRFActions.Load(TitleIEN : Int64; DFN : String);
+begin
+  CallV('TIU GET PRF ACTIONS', [TitleIEN,DFN]);
+  FPRFActionList.Assign(RPCBrokerV.Results);
+end;
+
+function TPRFActions.SelActionHasNote(lstIndex: integer): boolean;
+begin
+  Result := false;
+  if Piece(FPRFActionList[lstIndex],U,NOTE_IEN) <> '' then
+    Result := true;
+end;
+
+procedure TPRFActions.ShowActionsOnList(DisplayList: TCaptionListView);
+var
+  i : integer;
+  ListItem: TListItem;
+begin
+  DisplayList.Clear;
+  for i := 0 to FPRFActionList.Count-1 do
+  begin
+    //Caption="Text for Screen Reader" SubItem1=Flag SubItem2=Date SubItem3=Action SubItem4=Note
+    ListItem := DisplayList.Items.Add;
+    ListItem.Caption := PRF_LABEL; //Screen readers don't read the first column title on a listview.
+    ListItem.SubItems.Add(Piece(FPRFActionList[i],U,FLAG_NAME));
+    ListItem.SubItems.Add(Piece(FPRFActionList[i],U,ACTION_DATE));
+    ListItem.SubItems.Add(Piece(FPRFActionList[i],U,ACTION_NAME));
+    if SelActionHasNote(i) then
+      ListItem.SubItems.Add('Yes')
+    else
+      ListItem.SubItems.Add('No');
+  end;
+end;
+
+procedure TfrmNoteProperties.FormDestroy(Sender: TObject);
+begin
+  FPRFActions.Free;
+end;
+
+procedure TfrmNoteProperties.btnDetailsClick(Sender: TObject);
+var
+  ConsultDetail: TStringList;
+begin
+  if lstRequests.ItemIEN <= 0 then exit;
+  ConsultDetail := TStringList.Create;
+  try
+    LoadConsultDetail(ConsultDetail, lstRequests.ItemIEN) ;
+    ReportBox(ConsultDetail, 'Consult Details: #' + lstRequests.ItemID + ' - ' +
+               Piece(lstRequests.Items[lstRequests.ItemIndex], U, 3), TRUE);
+  finally
+    ConsultDetail.Free;
+  end;
+end;
+
+procedure TfrmNoteProperties.lstRequestsChange(Sender: TObject);
+begin
+  btnDetails.Enabled := (lstRequests.ItemIEN > 0);
+end;
+
 end.
