Function

ExecuteSignOrders

Module

fOrdersSign

Last Modified

7/15/2014 3:26:42 PM

Comments

End BillingAware

Forward} function ExecuteSignOrders(SelectedList: TList): Boolean;
ChangeItem: TChangeItem;

Scope

Interfaced

Declaration

{Forward} function ExecuteSignOrders(SelectedList: TList): Boolean;

Calls Hierarchy


ExecuteSignOrders
 ├TfrmBase508Form.Create
 │ ├TfrmBase508Form.UpdateAccessibilityActions
 │ ├UnfocusableControlEnter
 │ └AdjustControls
 │   ├TfrmBase508Form.ModifyUnfocusableControl
 │   └..(rec)..
 ├TSigItems.ResetOrders
 ├IsPendingHold
 ├TSigItems.Add
 ├DEACheckFailedAtSignature
 ├GetOrderableIen
 ├GetPKISite
 ├GetPKIUse
 ├TSigItems.ClearDrawItems
 ├TSigItems.ClearFcb
 ├TSigItems.UpdateListBox
 │ ├notRightOne
 │ ├IsOrderBillable
 │ │ └PrepOrderID
 │ ├RemoveOrderFromDxList
 │ │ └tempDxNodeExists
 │ ├rpcGetSC4Orders
 │ ├OrderRequiresSCEI
 │ ├GetUnsignedOrderFlags
 │ ├GetConsultFlags
 │ │ └SetConsultFlags
 │ ├TSigItems.cbClicked
 │ │ └TSigItems.TagToItem
 │ ├SigItemHeight
 │ ├TSigItems.lbDrawItem
 │ │ └TSigItems.TagToItem
 │ ├CreateCB
 │ │ ├TSigItems.cbClicked...
 │ │ ├TSigItems.cbEnter
 │ │ └TSigItems.cbExit
 │ ├TSigItems.ItemToTag
 │ ├TSigItems.TagInfo
 │ └TSigItems.TagToItem
 ├SignNotRequired
 ├rpcNonBillableOrders
 ├TfrmSignOrders.AnyItemsAreChecked
 │ ├TfrmSignOrders.ItemsAreChecked
 │ └TfrmSignOrders.CSItemsAreChecked
 ├TfrmSignOrders.IsSignatureRequired
 ├UpdateOrderDGIfNeeded
 │ ├TPatient.Clear
 │ └TPatient.SetDFN
 │   ├TPatient.Clear
 │   └SelectPatient
 │     └FormatSSN
 ├SetSAN
 │ ├tCryptography.Create
 │ │ ├tCryptography.Reset
 │ │ ├sCardReady
 │ │ │ ├SCardEstablishContext
 │ │ │ ├SCardListReadersA
 │ │ │ └SCardConnectA
 │ │ ├tCryptography.RaiseErr
 │ │ ├CryptAcquireContext
 │ │ └CryptGetProvParam
 │ └getSANFromCard
 │   ├checkPINValue
 │   │ ├sCardReady...
 │   │ ├CryptAcquireContext
 │   │ ├setPINValue
 │   │ │ ├CryptAcquireContext
 │   │ │ ├CryptSetProvParam
 │   │ │ └CryptReleaseContext
 │   │ └CryptReleaseContext
 │   └tCryptography.FindCert
 │     ├CertOpenSystemStore
 │     ├CertEnumCertificatesInStore
 │     ├CertGetNameString
 │     ├SerialNum
 │     ├CertDateTimeStr
 │     ├tCryptography.SigningCert
 │     │ └CertGetIntendedKeyUsage
 │     ├CertVerifyTimeValidity
 │     ├CertFindExtension
 │     ├tCryptography.CRLDistPoint
 │     │ └CryptDecodeObject
 │     ├Revco
 │     │ └CertVerifyRevocation
 │     └tCryptography.CertChainCheck
 │       ├CertCreateCertificateChainEngine
 │       ├Memo
 │       ├tCryptography.RaiseErr
 │       ├CertGetCertificateChain
 │       └SetStatus
 │         └Memo
 ├checkPINValue...
 ├tCryptography.Create...
 ├Piece2end
 ├tCryptography.Reset
 ├Log2File
 ├StoreDigitalSig
 ├TfrmFrame.GetTimedOut
 │ └TimedOut
 ├TEncounter.GetLocationName
 │ └TEncounter.UpdateText
 │   └GetEncounterText
 ├TEncounter.GetLocationText
 │ └TEncounter.UpdateText...
 ├TEncounter.GetVisitCategory
 ├TfrmPrintLocation.PrintLocation
 │ ├TfrmBase508Form.Create...
 │ ├CurrentLocationForPatient
 │ ├TfrmPrintLocation.ProcessClinicOrders
 │ │ └TfrmPrintLocation.rpcChangeOrderLocation
 │ ├TEncounter.EncounterSwitch
 │ │ ├TEncounter.SetLocation
 │ │ ├TEncounter.SetVisitCategory
 │ │ └TEncounter.SetDateTime
 │ └TfrmFrame.DisplayEncounterText
 │   ├TEncounter.GetLocationText...
 │   ├TEncounter.GetProviderName
 │   │ └TEncounter.UpdateText...
 │   └TfrmFrame.FitToolbar
 ├CurrentLocationForPatient
 ├TSigItems.SaveSettings
 │ └SaveCoPayStatus
 ├SendOrders
 ├FindOrderText
 ├NotifyOtherApps
 │ ├AppNotificationEnabled
 │ │ └TNotifyAppsThread.CreateThread
 │ └TNotifyAppsThread.ResumeIfIdle
 ├GetOrderStatus
 ├CheckForChangeFromLCtoWCOnRelease
 │ └FormatLCtoWCDisplayTextOnRelease
 │   └GetLCtoWCInstructions
 ├ReportBox
 │ └CreateReportBox
 │   └TfrmBase508Form.Create...
 ├PrintOrdersOnSignReleaseMult
 │ └PrintOrdersOnSignRelease
 │   ├CurrentLocationForPatient
 │   ├TEncounter.SetLocation
 │   ├CommonLocationForOrders
 │   ├LookupLocation
 │   │ └TfrmBase508Form.Create...
 │   ├TfrmFrame.DisplayEncounterText...
 │   ├SetupOrdersPrint
 │   │ ├TfrmBase508Form.Create...
 │   │ ├OrderPrintDeviceInfo
 │   │ └TfrmOrdersPrint.SetupControls
 │   │   └TfrmOrdersPrint.SetupPrompting
 │   ├PrintOrdersOnReview
 │   └PrintServiceCopies
 ├PrintOrdersOnSignRelease...
 ├CSRemaining
 │ ├LoadUnsignedOrders
 │ └TextForOrder
 └UnlockOrder

Called-By Hierarchy


           ExecuteSignOrders
TfrmOrders.mnuActSignClick┘ 

Calls

Name Declaration Comments
TSigItems.Add procedure Add(ItemType: Integer; const ID: string; Index: integer); -
TfrmSignOrders.AnyItemsAreChecked function AnyItemsAreChecked: Boolean; -
CheckForChangeFromLCtoWCOnRelease procedure CheckForChangeFromLCtoWCOnRelease(Dest: TStrings; ALocation: integer; OrderList: TStringList); -
checkPINValue function checkPINValue(InputForm: TComponent): TPINResult; -
TSigItems.ClearDrawItems procedure ClearDrawItems; -
TSigItems.ClearFcb procedure ClearFcb; -
TfrmBase508Form.Create constructor Create(AOwner: TComponent); override; -
tCryptography.Create constructor Create; -
CSRemaining procedure CSRemaining(orders: TStrings; csorders: TStrings); -
CurrentLocationForPatient procedure CurrentLocationForPatient(const DFN: string; var ALocation: Integer; var AName: string; var ASvc: string); -
DEACheckFailedAtSignature function DEACheckFailedAtSignature(AnOI: Integer; ForInpatient: Boolean): string; -
FindOrderText function FindOrderText(const AnID: string): string; -
TEncounter.GetLocationName function GetLocationName: string; -
TEncounter.GetLocationText function GetLocationText: string; -
GetOrderableIen function GetOrderableIen(AnOrderId:string): integer; -
GetOrderStatus function GetOrderStatus(AnOrderId: string): integer; -
GetPKISite function GetPKISite: Boolean; -
GetPKIUse function GetPKIUse: Boolean; -
TfrmFrame.GetTimedOut function GetTimedOut: boolean; -
TEncounter.GetVisitCategory function GetVisitCategory: Char; -
IsPendingHold function IsPendingHold(OrderID: string): boolean; -
TfrmSignOrders.IsSignatureRequired function IsSignatureRequired:boolean; -
Log2File procedure Log2File( crypto: tCryptography) ; -
NotifyOtherApps procedure NotifyOtherApps(const AppEvent, AppData: string); -
Piece2end function Piece2end (s,del: string): string; -
TfrmPrintLocation.PrintLocation procedure PrintLocation(OrderLst: TStringList; pEncounterLoc: integer; pEncounterLocName, pEncounterLocText: string; pEncounterDT: TFMDateTime; pEncounterVC: Char; var ClinicLst, WardLst: TStringList; var wardIEN: integer; var wardLocation: string; ContainsIMOOrders: boolean; displayEncSwitch: boolean = false); -
PrintOrdersOnSignRelease procedure PrintOrdersOnSignRelease(OrderList: TStringList; Nature: Char; PrintLoc : Integer =0; PrintName: string = ''); -
PrintOrdersOnSignReleaseMult procedure PrintOrdersOnSignReleaseMult(OrderList, ClinicLst, WardLst: TStringList; Nature: Char; EncLoc, WardLoc: integer; EncLocName, WardLocName: string); -
ReportBox procedure ReportBox(ReportText: TStrings; ReportTitle: string; AllowPrint: boolean); -
tCryptography.Reset procedure Reset; -
TSigItems.ResetOrders procedure ResetOrders; -
rpcNonBillableOrders function rpcNonBillableOrders(pOrderList: TStringList): TStringList; -
TSigItems.SaveSettings procedure SaveSettings; -
SendOrders procedure SendOrders(OrderList: TStringList; const ESCode: string); -
SetSAN function SetSAN(Self: TComponent): string; -
SignNotRequired function SignNotRequired: Boolean; -
StoreDigitalSig procedure StoreDigitalSig(AID, AHash: string; AProvider: Int64; ASig, ACrlUrl, DFN: string; var AError: string); -
UnlockOrder procedure UnlockOrder(OrderID: string); -
TSigItems.UpdateListBox function UpdateListBox(lb: TCustomListBox): boolean; -
UpdateOrderDGIfNeeded procedure UpdateOrderDGIfNeeded(AnID: string); -

Called-By

Name Declaration Comments
TfrmOrders.mnuActSignClick procedure mnuActSignClick(Sender: TObject); -


Source

222   function ExecuteSignOrders(SelectedList: TList): Boolean;
223   const
224     VERT_SPACING = 6;
225   
226   var
227     i, k, cidx,cnt, theSts, WardIEN: Integer;
228     ShrinkHeight,oheight,newheight{,TotalSH},temph1,temph2,oltemptop,csoltemptop,deatemptop,esigtemptop,oltemph,csoltemph,deatemph,esigtemph: integer;
229     SignList: TStringList;
230     CSSignList: TStringList;
231     Obj: TOrder;
232     DigSigErr, DigStoreErr, ContainsIMOOrders, DoNotPrint, t1, t2,t3: Boolean;
233     x, SigData, SigUser, SigDrugSch, SigDEA: string;
234     cSignature, cHashData, cCrlUrl, cErr, WardName: string;
235     UsrAltName, IssuanceDate, PatientName, PatientAddress, DetoxNumber, ProviderName, ProviderAddress: string;
236     DrugName, Quantity, Directions: string;
237     OrderText, ASvc: string;
238     PrintLoc: Integer;
239     AList, ClinicList, OrderPrintList, WardList: TStringList;
240     EncLocName, EncLocText: string;
241     EncLocIEN: integer;
242     EncDT: TFMDateTime;
243     EncVC: Char;
244     CSSelectedList: TList;
245     CSOrder,PINRetrieved : boolean;
246     PINResult : TPINResult;
247     PINLock : string;
248     DEACheck: string;
249     //ChangeItem: TChangeItem;
250   
251     function FindOrderText(const AnID: string): string;
252     var
253       i: Integer;
254     begin
255       Result := '';
256       fOrdersSign.tempList := SelectedList;
257       fOrdersSign.tempCSList := CSSelectedList;
258       with SelectedList do for i := 0 to Count - 1 do
259         with TOrder(Items[i]) do if ID = AnID then
260         begin
261           Result := Text;
262           Break;
263         end;
264       with CSSelectedList do for i := 0 to Count - 1 do
265         with TOrder(Items[i]) do if ID = AnID then
266         begin
267           Result := Text;
268           Break;
269         end;
270     end;
271   
272     function SignNotRequired: Boolean;
273     var
274       i: Integer;
275     begin
276       Result := True;
277       tempList := SelectedList;
278       tempCSList := CSSelectedList;
279       with SelectedList do for i := 0 to Pred(Count) do
280       begin
281         with TOrder(Items[i]) do if Signature <> OSS_NOT_REQUIRE then Result := False;
282       end;
283   //    with CSSelectedList do for i := 0 to Pred(Count) do
284   //    begin
285   //      with TOrder(Items[i]) do if Signature <> OSS_NOT_REQUIRE then Result := False;
286   //    end;
287     end;
288   
289     function DigitalSign: Boolean;
290     var
291       i: Integer;
292     begin
293       Result := False;
294   
295       with SelectedList do for i := 0 to Pred(Count) do
296       begin
297         with TOrder(Items[i]) do if Copy(DigSigReq,1,1) = '2' then Result := True;
298       end;
299       with CSSelectedList do for i := 0 to Pred(Count) do
300       begin
301         with TOrder(Items[i]) do if Copy(DigSigReq,1,1) = '2' then Result := True;
302       end;
303     end;
304   
305     function Piece2end (s,del: string): string;
306     var
307       i: Integer;
308     begin
309       i := Pos(del,s);
310       Result := copy(s,i+1,length(s));
311     end;
312   
313     procedure Log2File(  crypto: tCryptography) ;
314     var
315       f: TextFile;
316     begin
317       AssignFile(f,'c:\PKISignError\hashLog.txt');
318       if not(FileExists('c:\PKISignError\hashLog.txt')) then
319       begin
320         ReWrite(f) ;
321         Write(f,'cprs 29 hash log' + CRLF + CRLF);
322         CloseFile(f);
323       end;
324   
325       Append(f);
326       WriteLn(f, '==============' + crypto.OrderNumber + CRLF +
327                         'UsrName: "' + crypto.UsrName + '"' + CRLF +
328                         'UsrAltName: "' + crypto.UsrAltName + '"' + CRLF +
329                         'IssuanceDate: "' + crypto.IssuanceDate + '"' + CRLF +
330                         'PatientName: "' + crypto.PatientName + '"' + CRLF +
331                         'PatientAddress: "' + crypto.PatientAddress + '"' + CRLF +
332                         'DrugName: "' + crypto.DrugName + '"' + CRLF +
333                         'ProviderName: "' + crypto.ProviderName + '"' + CRLF +
334                         'ProviderAddress: "' + crypto.ProviderAddress + '"' + CRLF +
335                         'IssuanceDate: "' + crypto.IssuanceDate + '"' + CRLF +
336                         'Quantity: "' + crypto.Quantity + '"' + CRLF +
337                         'Directions: "' + crypto.Directions + '"' + CRLF +
338                         'DetoxNumber: "' + crypto.DetoxNumber + '"' + CRLF +
339                         'DeaNumber: "' + crypto.DeaNumber + '"' + CRLF +
340                         'OrderNumber: "' + crypto.OrderNumber + '"' + CRLF +
341                         CRLF);
342       CloseFile(f);
343     end;
344   
345   begin
346   //  DigSigErr := True;
347     CSSelectedList := SelectedList;
348     Result := False;
349     PrintLoc := 0;
350     EncLocIEN := 0;
351     DoNotPrint := False;
352     if BILLING_AWARE then
353     begin
354        tempOrderList := TStringList.Create;
355        tempOrderList.Clear;
356     end;
357     frmSignOrders := TfrmSignOrders.Create(Application);
358   
359     CallV('ORDEA DEATEXT', []);
360     frmSignOrders.lblDEAText.Caption := '';
361     for i := 0 to RPCBrokerV.Results.Count -1 do
362        frmSignOrders.lblDEAText.Caption := frmSignOrders.lblDEAText.Caption + ' ' + RPCBrokerV.Results.Strings[i];
363   
364     CallV('ORDEA SIGINFO', [Patient.DFN, User.DUZ]);
365     frmSignOrders.lblProvInfo.Caption := '';
366     for i := 0 to RPCBrokerV.Results.Count -1 do
367        frmSignOrders.lblProvInfo.Caption := frmSignOrders.lblProvInfo.Caption + #13#10 + RPCBrokerV.Results.Strings[i];
368     try
369   //    ResizeAnchoredFormToFont(frmSignOrders);
370       SigItems.ResetOrders;
371       SigItemsCS.ResetOrders;
372       with SelectedList do for i := 0 to Count - 1 do
373         begin
374           obj := TOrder(Items[i]);
375           if ((obj.IsControlledSubstance=false) or IsPendingHold(Obj.ID)) then
376             begin
377               cidx := frmSignOrders.clstOrders.Items.AddObject(Obj.Text,Obj);
378               SigItems.Add(CH_ORD,Obj.ID, cidx);
379               if BILLING_AWARE then
380                 tempOrderList.Add(Obj.ID);
381               frmSignOrders.clstOrders.Checked[cidx] := TRUE;
382   
383               if (TOrder(Items[i]).DGroupName) = NonVAMedGroup then
384                 frmSignOrders.clstOrders.State[cidx] := cbGrayed ;
385             end;
386         end;
387       with CSSelectedList do for i := 0 to Count - 1 do
388         begin
389           obj := TOrder(Items[i]);
390           if (obj.IsControlledSubstance and obj.IsOrderPendDC) then
391           begin
392             cidx := frmSignOrders.clstOrders.Items.AddObject(Obj.Text,Obj);
393             SigItems.Add(CH_ORD,Obj.ID, cidx);
394             if BILLING_AWARE then
395               tempOrderList.Add(Obj.ID);
396             frmSignOrders.clstOrders.Checked[cidx] := TRUE;
397   
398             if (TOrder(Items[i]).DGroupName) = NonVAMedGroup then
399               frmSignOrders.clstOrders.State[cidx] := cbGrayed ;
400           end
401           else if (obj.IsControlledSubstance and not(IsPendingHold(Obj.ID))) then
402             begin
403               DEACheck := DEACheckFailedAtSignature(GetOrderableIen(Piece(obj.ID,';',1)),False);
404               if not(DEACheck='0') then ShowMsg('You are not authorized to Digitally Sign order: '+CRLF+obj.Text)
405               else
406               begin
407                 cidx := frmSignOrders.clstCSOrders.Items.AddObject(Obj.Text,Obj);
408                 SigItemsCS.Add(CH_ORD,Obj.ID, cidx);
409                 if BILLING_AWARE then
410                   tempOrderList.Add(Obj.ID);
411                 if TOrder(Items[i]).IsOrderPendDC then frmSignOrders.clstCSOrders.Checked[cidx] := TRUE
412                 else frmSignOrders.clstCSOrders.Checked[cidx] := FALSE;
413   
414                 if (TOrder(Items[i]).DGroupName) = NonVAMedGroup then
415                   frmSignOrders.clstCSOrders.State[cidx] := cbGrayed ;
416                 end;
417             end;
418         end;
419   
420       if frmSignOrders.clstCSOrders.Count>0 then
421       begin
422         if not(GetPKISite) then
423         begin
424           ShowMsg('Digital Signing of Controlled Substances is currently disabled for your site.');
425           if frmSignOrders.clstOrders.Count>0 then
426           begin
427             frmSignOrders.clstCSOrders.Clear;
428             SigItemsCS.ResetOrders;
429           end
430           else Exit;
431         end;
432         if not(GetPKIUse ) then
433         begin
434           ShowMsg('You are not currently permitted to digitally sign Controlled Substances.');
435           if frmSignOrders.clstOrders.Count>0 then
436           begin
437             frmSignOrders.clstCSOrders.Clear;
438             SigItemsCS.ResetOrders;
439           end
440           else Exit;
441         end;
442       end;
443       
444       
445   
446       SigItems.ClearDrawItems;
447       SigItems.ClearFcb;
448       SigItemsCS.ClearDrawItems;
449       SigItemsCS.ClearFcb;
450       t1 := SigItems.UpdateListBox(frmSignOrders.clstOrders);
451       t2 := SigItemsCS.UpdateListBox(frmSignOrders.clstCSOrders);
452       if t1 or t2 then
453         frmSignOrders.fraCoPay.Visible := TRUE
454       else
455         begin
456         {Begin BillingAware}
457           if  BILLING_AWARE then
458              frmSignOrders.gbDxLookup.Visible := FALSE;
459          {End BillingAware}
460         end;
461       if SignNotRequired then
462         begin
463           frmSignOrders.lblESCode.Visible := False;
464           frmSignOrders.txtESCode.Visible := False;
465         end;
466       if BILLING_AWARE then
467       begin
468        //  build list of orders that are not billable based on order type
469           UBAGlobals.NonBillableOrderList := rpcNonBillableOrders(tempOrderList);
470       end;
471   
472   
473   
474   
475       with frmSignOrders do
476       begin
477         lblDeaText.Visible := TRUE;
478         txtEScode.Text := '';
479         if ((clstOrders.Count = 0) and (clstCSOrders.Count = 0)) then  Exit;
480         pnlProvInfo.Height := lblProvInfo.Height+5+lblProvInfo.Top;
481   //      TotalSH := 0;
482   
483         if clstCSOrders.Count = 0 then
484         begin      
485           oheight := pnlOrderList.height;
486           pnlProvInfo.Visible := False;
487           if fraCoPay.Visible = FALSE then
488           begin
489             pnlTop.Visible := False;
490           end;
491           lblDeaText.Visible := False;   
492           pnlCSOrderlist.Visible := False;
493           pnlOrderlist.Align := alClient; 
494           newheight := Height - pnlOrderList.height + oheight;
495           if newheight < Constraints.MinHeight then Constraints.MinHeight := newheight;
496           Height := newheight;
497         end
498         else if clstOrders.Count = 0 then
499         begin   
500           oheight := pnlCSOrderList.height;
501           pnlOrderlist.Visible := False;
502           pnlCSOrderlist.Align := alClient;  
503           newheight := Height - pnlCSOrderlist.height + oheight;
504           if newheight < Constraints.MinHeight then Constraints.MinHeight := newheight;
505           Height := newheight;
506           txtESCode.Visible := FALSE;
507           lblESCode.Visible := FALSE;
508   
509         end
510         else if fraCoPay.Visible = FALSE then
511         begin
512           fraCoPay.Visible := TRUE;
513           ShrinkHeight := fraCoPay.Height - pnlProvInfo.Height;
514           fraCoPay.Visible := FALSE;
515   
516   		    pnlTop.Height := pnlTop.Height - ShrinkHeight;
517           pnlCombined.Top := pnlCombined.Top - ShrinkHeight; 
518   		    pnlCombined.Height := pnlCombined.Height + ShrinkHeight;
519   
520         end;
521         
522   
523   
524         lblDEAText.Visible := FALSE;
525         lblSmartCardNeeded.Visible := FALSE;
526       end;
527       if frmSignOrders.AnyItemsAreChecked then
528       begin
529         frmSignOrders.lblESCode.Visible := frmSignOrders.IsSignatureRequired;
530         frmSignOrders.txtESCode.Visible := frmSignOrders.IsSignatureRequired
531       end ;
532       if frmSignOrders.txtESCode.Visible then  frmSignOrders.ActiveControl := frmSignOrders.txtESCode;
533       
534        frmSignOrders.ShowModal;
535         if frmSignOrders.OKPressed then
536         begin
537           PINResult := prOK;
538           Result := True;
539           SignList := TStringList.Create;
540           CSSignList := TStringList.Create;
541           ClinicList := TStringList.Create;
542           OrderPrintList := TStringList.Create;
543           WardList := TStringList.Create;
544           ContainsIMOOrders := false;
545           try
546             with SelectedList do for i := 0 to Count - 1 do with TOrder(Items[i]) do
547             begin     
548               CSOrder := False;
549               cErr := '';
550               cidx := frmSignOrders.clstOrders.Items.IndexOfObject(TOrder(Items[i]));
551               if cidx<0 then
552               begin
553                 cidx := frmSignOrders.clstCSOrders.Items.IndexOfObject(TOrder(Items[i]));
554                 CSOrder := True;
555               end;
556   
557   
558               if (cidx > -1 ) and (CSOrder=False) and (cErr = '') then
559                 begin
560                   if TOrder(Items[i]).DGroupName = NonVAMedGroup  then  frmSignOrders.clstOrders.Checked[cidx] := True;    //Non VA MEDS
561                   if frmSignOrders.clstOrders.Checked[cidx] then
562                   begin
563                     UpdateOrderDGIfNeeded(ID);
564                     SignList.Add(ID + U + SS_ESIGNED + U + RS_RELEASE + U + NO_PROVIDER);
565                     BAOrderList.Add(TOrder(Items[i]).ID);
566                   end;
567                 end;
568   
569   
570               if (cidx > -1) and (CSOrder) and (cErr = '') then
571                 begin
572                   if frmSignOrders.clstCSOrders.Checked[cidx] then
573                   begin
574                     if IsOrderPendDC then
575                     begin
576                       UpdateOrderDGIfNeeded(ID);
577                       SignList.Add(ID + U + SS_DIGSIG + U + RS_RELEASE + U + NO_PROVIDER);
578                       BAOrderList.Add(TOrder(Items[i]).ID);
579                     end
580                     else
581                     begin
582   //                    UpdateOrderDGIfNeeded(ID);
583                       CSSignList.Add(ID + U + SS_DIGSIG + U + RS_RELEASE + U + NO_PROVIDER);
584   //                    BAOrderList.Add(TOrder(Items[i]).ID);
585                     end;
586                   end;
587                 end;
588             end;
589   
590             if CSSignList.Count > 0 then
591             begin
592             LastPINvalue := '';
593             //isXuDSigSLogging := true;
594             PINRetrieved := False;
595             //get altusername
596             UsrAltName := sCallV('XUS PKI GET UPN', []);
597             //if SAN is blank then attempt to retrieve from card to set it
598             if UsrAltName='' then
599             begin
600               UsrAltName := SetSAN(fOrdersSign.frmSignOrders);
601               if not(UsrAltName='')  then
602                 PINRetrieved := True;
603             end;
604             //if still blank then cancel digital signing
605             if UsrAltName='' then   
606               PINResult := prCancel
607             else if not(PINRetrieved) then
608               try
609                 PINLock := sCallV('ORDEA PINLKCHK', []);
610                 if PINLock = '1' then 
611                   PINResult := prLocked
612                 else 
613                   PINResult := checkPINValue(fOrdersSign.frmSignOrders);
614               except
615                 PINResult := prError;
616               end;
617   
618             if PINResult = PRError then 
619               ShowMsg('Problem getting PIN.  Cannot Digitally Sign.')
620             else if PINResult = prLocked then begin
621               if PINLock = '0' then sCallV('ORDEA PINLKSET', []);
622               ShowMsg('Card has been locked.  Cannot Digitally Sign.')
623             end else if PINResult=prCancel then 
624               ShowMsg('Digital Signing has been cancelled.')
625             else begin
626               try
627                 crypto := tCryptography.Create;
628                 //call rpc to get hash fields other than drug info
629                 CallV('ORDEA HASHINFO', [Patient.DFN, User.DUZ]);
630                 for i := 0 to RPCBrokerV.Results.Count -1 do
631                 begin
632                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'IssuanceDate' then IssuanceDate := Piece2end(RPCBrokerV.Results.Strings[i],':');
633                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'PatientName' then PatientName := Piece2end(RPCBrokerV.Results.Strings[i],':');
634                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'PatientAddress' then PatientAddress := Piece2end(RPCBrokerV.Results.Strings[i],':');
635                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'DetoxNumber' then DetoxNumber := Piece2end(RPCBrokerV.Results.Strings[i],':');
636                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'ProviderName' then ProviderName := Piece2end(RPCBrokerV.Results.Strings[i],':');
637                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'ProviderAddress' then ProviderAddress := Piece2end(RPCBrokerV.Results.Strings[i],':');
638                   if Piece(RPCBrokerV.Results.Strings[i],':',1) = 'DeaNumber' then SigDEA := Piece2end(RPCBrokerV.Results.Strings[i],':');
639                 end;
640   
641                 for i := 0 to CSSignList.Count - 1 do
642                 begin
643                   CallV('ORDEA ORDHINFO', [Piece(Piece(CSSignList.strings[i],U,1),';',1)]);
644                   DrugName := '';
645                   Quantity := '';
646                   Directions := '';
647                   for k := 0 to RPCBrokerV.Results.Count -1 do
648                   begin
649                     if Piece(RPCBrokerV.Results.Strings[k],':',1) = 'DrugName' then DrugName := Piece2end(RPCBrokerV.Results.Strings[k],':');
650                     if Piece(RPCBrokerV.Results.Strings[k],':',1) = 'Quantity' then Quantity := Piece2end(RPCBrokerV.Results.Strings[k],':');
651                     if Piece(RPCBrokerV.Results.Strings[k],':',1) = 'Directions' then Directions := Piece(Piece2end(RPCBrokerV.Results.Strings[k],':'),U,1);
652                   end;
653                   try
654                     crypto.Reset;
655                     crypto.isDEAsig := true;
656                     crypto.UsrName := User.Name;
657                     crypto.UsrAltName := UsrAltName;
658                     crypto.IssuanceDate :=  IssuanceDate;
659                     crypto.PatientName :=   PatientName;
660                     crypto.PatientAddress :=  PatientAddress;
661                     crypto.DrugName :=     DrugName;
662                     crypto.Quantity :=   Quantity;
663                     crypto.Directions :=  Directions;
664                     crypto.DetoxNumber :=  ''; //DetoxNumber;  don't include detox in hash calc
665                     crypto.ProviderName :=  ProviderName;
666                     crypto.ProviderAddress :=  ProviderAddress;
667                     crypto.DeaNumber :=      SigDEA;
668                     crypto.OrderNumber := Piece(Piece(CSSignList.strings[i],U,1),';',1);
669   
670                     if false then Log2File(crypto);
671   
672                     if crypto.SignData = true then
673                     begin
674                       cSignature := crypto.SignatureStr;
675                       cHashData := crypto.HashStr;
676                       cCrlUrl := crypto.CrlUrl;
677                     end
678                     else
679                     begin
680                       ShowMsg('Could not digitally sign. An error has occurred: Hash generation failed'+CRLF+CRLF+crypto.Reason);
681                       DigStoreErr := true;
682                     end;
683   
684                   except
685                     on  E: Exception do
686                       begin
687                         ShowMsg('Could not digitally sign. An error has occurred: '+ E.Message);
688                         DigStoreErr := true;
689                       end;
690                   end;
691   
692                   if DigStoreErr then
693                    //messages shown above
694                   else
695                   begin
696                     cErr := '';
697                     StoreDigitalSig(Piece(CSSignList.Strings[i],U,1), cHashData, User.DUZ, cSignature, cCrlUrl, Patient.DFN, cErr);
698                     if cErr = '' then
699                     begin
700                       UpdateOrderDGIfNeeded(Piece(CSSignList.Strings[i],U,1));
701                       SignList.Add(CSSignList.Strings[i]);
702                       BAOrderList.Add(Piece(CSSignList.Strings[i],U,1));
703                     end;
704                   end;
705                 end;
706               finally
707                   if not(crypto=nil) then
708                   begin
709                     crypto.Free;
710                     crypto := nil;
711                   end;
712                 end;
713               end;
714             end;
715             StatusText('Sending Orders to Service(s)...');
716             if SignList.Count > 0 then
717             begin
718               //hds7591  Clinic/Ward movement.  Patient Admission IMO
719               if not frmFrame.TimedOut then
720               begin
721                 if (Patient.Inpatient = True) and (Encounter.Location <> Patient.Location) then
722                 begin
723                   EncLocName := Encounter.LocationName;
724                   EncLocIEN  := Encounter.Location;
725                   EncLocText := Encounter.LocationText;
726                   EncDT := Encounter.DateTime;
727                   EncVC := Encounter.VisitCategory;
728                   for i := 0 to SelectedList.Count - 1 do
729                   begin
730                     CSOrder := False;
731                     cidx := frmSignOrders.clstOrders.Items.IndexOfObject(TOrder(SelectedList.Items[i]));
732                     //selected order isn't from non-CS list -> check CS List
733                     if cidx = -1 then
734                     begin
735                       cidx := frmSignOrders.clstCSOrders.Items.IndexOfObject(TOrder(SelectedList.Items[i]));
736                       //selected order found in CS List -> CSOrder := True
737                       if cidx <> -1 then
738                         CSOrder := True;
739                     end;
740                     if ((CSOrder=False) and (frmSignOrders.clstOrders.Checked[cidx]=False)) or (CSOrder and (frmSignOrders.clstCSOrders.Checked[cidx]=False)) then continue;
741                     if TOrder(SelectedList.Items[i]).DGroupName = 'Clinic Orders' then ContainsIMOOrders := true;
742                     if TOrder(SelectedList.Items[i]).DGroupName = '' then continue;
743                     if (Pos('DC', TOrder(SelectedList.Items[i]).ActionOn) > 0) or
744                        (TOrder(SelectedList.Items[i]).IsOrderPendDC = true) then
745                     begin
746                       WardList.Add(TOrder(SelectedList.Items[i]).ID);
747                       Continue;
748                     end;
749                     //ChangeItem := Changes.Locate(20,TOrder(SelectedList.Items[i]).ID);
750                     //if ChangeItem = nil then continue;
751                     //if ChangeItem.Delay = true then continue;
752                     if TOrder(SelectedList.Items[i]).IsDelayOrder = true then Continue;
753                     OrderPrintList.Add(TOrder(SelectedList.Items[i]).ID + ':' + TOrder(SelectedList.Items[i]).Text);
754                   end;
755                   if OrderPrintList.Count > 0 then
756                   begin
757                     frmPrintLocation.PrintLocation(OrderPrintList, EncLocIEN, EncLocName, EncLocText, EncDT, EncVC, ClinicList,
758                                                    WardList, WardIen,WardName, ContainsIMOOrders, true);
759                     //fframe.frmFrame.OrderPrintForm := false;
760                   end
761                   else if (clinicList.count = 0) and (wardList.Count = 0) then
762                     DoNotPrint := True;
763                   if (WardIEN = 0) and (WardName = '') then
764                     CurrentLocationForPatient(Patient.DFN, WardIEN, WardName, ASvc);
765                 end;
766               end;
767               uCore.TempEncounterLoc := 0;
768               uCore.TempEncounterLocName := '';
769               //hds7591  Clinic/Ward movement  Patient Admission IMO
770   
771               SigItems.SaveSettings; // Save CoPay FIRST!
772               SigItemsCS.SaveSettings;
773               SendOrders(SignList, frmSignOrders.ESCode);
774             end;
775   
776             //CQ #15813 Modified code to look for error string mentioned in CQ and change strings to conts - JCS
777             //CQ #15813 Adjusted code to handle error message properly - TDP
778               with SignList do if Count > 0 then for i := 0 to Count - 1 do
779               begin
780                 if Pos('E', Piece(SignList[i], U, 2)) > 0 then
781                   begin
782                     OrderText := FindOrderText(Piece(SignList[i], U, 1));
783                     if Piece(SignList[i],U,4) = TX_SAVERR_PHARM_ORD_NUM_SEARCH_STRING then
784                     InfoBox(TX_SAVERR1 + Piece(SignList[i], U, 4) + TX_SAVERR2 + OrderText + CRLF + CRLF +
785                           TX_SAVERR_PHARM_ORD_NUM, TC_SAVERR, MB_OK)
786                     else if AnsiContainsStr(Piece(SignList[i],U,4), TX_SAVERR_IMAGING_PROC_SEARCH_STRING) then
787                     InfoBox(TX_SAVERR1 + Piece(SignList[i], U, 4) + TX_SAVERR2 + OrderText + CRLF + CRLF +
788                           TX_SAVERR_IMAGING_PROC, TC_SAVERR, MB_OK)
789                     else
790                     InfoBox(TX_SAVERR1 + Piece(SignList[i], U, 4) + TX_SAVERR2 + OrderText,
791                           TC_SAVERR, MB_OK);
792                   end;
793                 if Pos('R', Piece(SignList[i], U, 2)) > 0 then
794                   NotifyOtherApps(NAE_ORDER, 'RL' + U + Piece(SignList[i], U, 1));
795               end;
796             StatusText('');
797             for cnt := SignList.Count - 1 downto 0 do
798             begin
799                 if Pos('E', Piece(SignList[cnt], U, 2)) > 0 then
800                 begin
801                   SignList.Delete(cnt);
802                   Continue;
803                 end;
804                 theSts := GetOrderStatus(Piece(SignList[cnt],U,1));
805                 if theSts = 10 then  SignList.Delete(cnt);  //signed delayed order should not be printed.
806             end;
807             //  CQ 10226, PSI-05-048 - advise of auto-change from LC to WC on lab orders
808             AList := TStringList.Create;
809             try
810               CheckForChangeFromLCtoWCOnRelease(AList, Encounter.Location, SignList);
811               if AList.Text <> '' then
812                 ReportBox(AList, 'Changed Orders', TRUE);
813             finally
814               AList.Free;
815             end;
816             if(ClinicList.Count > 0) or (WardList.count > 0) then
817                 PrintOrdersOnSignReleaseMult(SignList, CLinicList, WardList, NO_PROVIDER, EncLocIEN, WardIEN, EncLocName, wardName)
818               else if DoNotPrint = False then PrintOrdersOnSignRelease(SignList, NO_PROVIDER, PrintLoc);
819           finally
820             CSRemaining(frmSignOrders.clstOrders.items,frmSignOrders.clstCSOrders.items);
821             LastPINvalue := '';
822             SignList.Free;
823             OrderPrintList.free;
824             WardList.free;
825             ClinicList.free;
826           end;
827         end; {if frmSignOrders.OKPressed}
828     finally
829       frmSignOrders.Free;
830   
831       with SelectedList do for i := 0 to Count - 1 do UnlockOrder(TOrder(Items[i]).ID);
832     end;
833       if not(crypto=nil) then
834       begin
835         crypto.Free;
836         crypto := nil;
837       end;
838   end;