Function

TfrmODMeds.FindCommonDrug

Module

fODMeds

Last Modified

7/15/2014 3:26:42 PM

Comments

DoseList[n] = DoseText ^ Dispense Drug Pointer

Visibility

Private

Owner

TfrmODMeds

Declaration

function FindCommonDrug(DoseList: TStringList): string;

Calls Hierarchy


TfrmODMeds.FindCommonDrug
 ├TfrmODMeds.FindDoseFields
 ├SaveDrug
 └KillDrug

Called-By Hierarchy


               TfrmODMeds.FindCommonDrug
              TfrmODMeds.ControlChange┘ 
              TfrmODMeds.SetupDialog┤   
                 TfrmODMeds.Validate┤   
           TfrmODMeds.btnSelectClick┤   
           TfrmODMeds.UMDelayClick┘ │   
            TfrmODMeds.tabDoseChange┤   
            TfrmODMeds.FormKeyDown┘ │   
           TfrmODMeds.cboRouteChange┤   
         TfrmODMeds.SetOnMedSelect┘ │   
          TfrmODMeds.SetupDialog┤   │   
    TfrmODMeds.btnSelectClick...┤   │   
     TfrmODMeds.CheckFormAltDose┘   │   
     TfrmODMeds.cboDosageClick┘     │   
   TfrmODMeds.cboDosageKeyUp┘       │   
         TfrmODMeds.popDurationClick┤   
          TfrmODMeds.cboXRouteChange┤   
           TfrmODMeds.cboXRouteClick┤   
          TfrmODMeds.cboXRouteExit┘ │   
       TfrmODMeds.txtXDurationChange┤   
       TfrmODMeds.popDurationClick┘ │   
          TfrmODMeds.btnXRemoveClick┤   
        TfrmODMeds.grdDosesKeyDown┘ │   
     TfrmODMeds.timCheckChangesTimer┤   
          TfrmODMeds.UpdateRelated┘ │   
          TfrmODMeds.SetupDialog┤   │   
    TfrmODMeds.btnSelectClick...┤   │   
    TfrmODMeds.cboDosageClick...┤   │   
      TfrmODMeds.cboDosageChange┤   │   
        TfrmODMeds.cboDosageExit┤   │   
     TfrmODMeds.cboScheduleClick┤   │   
     TfrmODMeds.cmdAcceptClick┤ │   │   
        TfrmODMeds.chkPRNClick┤ │   │   
        TfrmODMeds.txtNSSClick┤ │   │   
   TfrmODMeds.cboScheduleEnter┤ │   │   
   TfrmODMeds.UMShowNSSBuilder┘ │   │   
    TfrmODMeds.cboScheduleChange┤   │   
      TfrmODMeds.txtSupplyChange┤   │   
    TfrmODMeds.txtQuantityChange┤   │   
         TfrmODMeds.grdDosesExit┤   │   
     TfrmODMeds.cboXDosageChange┤   │   
      TfrmODMeds.cboXDosageClick┤   │   
     TfrmODMeds.cboXDosageExit┤ │   │   
    TfrmODMeds.cboXDosageKeyUp┘ │   │   
       TfrmODMeds.cboXDosageExit┤   │   
   TfrmODMeds.cboXScheduleChange┤   │   
        TfrmODMeds.txtNSSClick┘ │   │   
    TfrmODMeds.cboXScheduleClick┤   │   
       TfrmODMeds.chkXPRNClick┤ │   │   
   TfrmODMeds.pnlXScheduleExit┘ │   │   
     TfrmODMeds.pnlXScheduleExit┤   │   
TfrmODMeds.txtXDurationChange...┤   │   
     TfrmODMeds.pnlXDurationExit┤   │   
          TfrmODMeds.chkPRNClick┤   │   
      TfrmODMeds.cboScheduleExit┤   │   
       TfrmODMeds.cboDosageKeyUp┤   │   
      TfrmODMeds.cboXDosageKeyUp┘   │   
       TfrmODMeds.chkPtInstructClick┤   
          TfrmODMeds.chkDoseNowClick┤   
        TfrmODMeds.SetOnQuickOrder┘ │   
          TfrmODMeds.SetupDialog┤   │   
    TfrmODMeds.btnSelectClick...┘   │   
              TfrmODMeds.chkPRNClick┤   
       TfrmODMeds.cboXSequenceChange┤   
         TfrmODMeds.txtRefillsChange┘   

Calls

Name Declaration Comments
TfrmODMeds.FindDoseFields function FindDoseFields(const Drug, ADose: string): string; -
KillDrug procedure KillDrug(const ADrug: string); -
SaveDrug procedure SaveDrug(const ADrug: string; UnitsPerDose: Extended); -

Called-By

Name Declaration Comments
TfrmODMeds.ControlChange procedure ControlChange(Sender: TObject); -


Source

2953  function TfrmODMeds.FindCommonDrug(DoseList: TStringList): string;
2954  // DoseList[n] = DoseText ^ Dispense Drug Pointer
2955  var
2956    i, j, UnitIndex: Integer;
2957    DrugStrength, DoseValue, UnitsPerDose: Extended;
2958    DrugOK, PossibleDoses, SplitTab: Boolean;
2959    ADrug, ADose, DoseFields, DoseUnits, DrugUnits: string;
2960    FoundDrugs: TStringList;
2961  
2962    procedure SaveDrug(const ADrug: string; UnitsPerDose: Extended);
2963    var
2964      i, DrugIndex: Integer;
2965      CurUnits: Extended;
2966    begin
2967      DrugIndex := -1;
2968      for i := 0 to Pred(FoundDrugs.Count) do
2969        if AnsiSameText(Piece(FoundDrugs[i], U, 1), ADrug) then DrugIndex := i;
2970      if DrugIndex = -1 then FoundDrugs.Add(ADrug + U + FloatToStr(UnitsPerDose)) else
2971      begin
2972        CurUnits := StrToFloatDef(Piece(FoundDrugs[DrugIndex], U, 2), 0);
2973        if UnitsPerDose > CurUnits
2974          then FoundDrugs[DrugIndex] := ADrug + U + FloatToStr(UnitsPerDose);
2975      end;
2976    end;
2977  
2978    procedure KillDrug(const ADrug: string);
2979    var
2980      i, DrugIndex: Integer;
2981    begin
2982      DrugIndex := -1;
2983      for i := 0 to Pred(FoundDrugs.Count) do
2984        if AnsiSameText(Piece(FoundDrugs[i], U, 1), ADrug) then DrugIndex := i;
2985      if DrugIndex > -1 then FoundDrugs.Delete(DrugIndex);
2986    end;
2987  
2988  begin
2989    Result := '';
2990   if FInptDlg then                                // inpatient dialog
2991    begin
2992      DrugOK := True;
2993      for i := 0 to Pred(DoseList.Count) do
2994      begin
2995        ADrug := Piece(DoseList[i], U, 2);
2996        if ADrug = '' then DrugOK := False;
2997        if Result = '' then Result := ADrug;
2998        if not AnsiSameText(ADrug, Result) then DrugOK := False;
2999        if not DrugOK then Break;
3000      end;
3001      if not DrugOK then Result :='';
3002    end else                                        // outpatient dialog
3003    begin
3004      // check the dose combinations for each dispense drug
3005      FoundDrugs := TStringList.Create;
3006      try
3007        if FAllDoses.Count > 0
3008          then PossibleDoses := Length(Piece(Piece(FAllDoses[0], U, 3), '&', 1)) > 0
3009          else PossibleDoses := False;
3010        for i := 0 to Pred(FAllDrugs.Count) do
3011        begin
3012          ADrug := Piece(FAllDrugs[i], U, 1);
3013          DrugOK := True;
3014          DrugStrength := StrToFloatDef(Piece(FAllDrugs[i], U, 2), 0);
3015          DrugUnits := Piece(FAllDrugs[i], U, 3);
3016          SplitTab := Piece(FAllDrugs[i], U, 5) = '1';
3017          for j := 0 to Pred(DoseList.Count) do
3018          begin
3019            ADose:= Piece(DoseList[j], U, 1);
3020            DoseFields := FindDoseFields(ADrug, ADose);  // get the idnode for the dose/drug combination
3021            if not PossibleDoses then
3022            begin
3023              if DoseFields = '' then DrugOK := False else SaveDrug(ADrug, 0);
3024            end else
3025            begin
3026              DoseValue := StrToFloatDef(Piece(DoseFields, '&', 1), 0);
3027              if DoseValue = 0 then DoseValue := ExtractFloat(ADose);
3028              UnitsPerDose := DoseValue / DrugStrength;
3029              if (Frac(UnitsPerDose) = 0) or (SplitTab and (Frac(UnitsPerDose) = 0.5))
3030                then SaveDrug(ADrug, UnitsPerDose)
3031                else DrugOK := False;
3032              // make sure this dose is using the same units as the drug
3033              if DoseFields = '' then
3034              begin
3035                for UnitIndex := 1 to Length(ADose) do
3036                  if not (ADose[UnitIndex] in ['0'..'9','.']) then Break;
3037                DoseUnits := Copy(ADose, UnitIndex, Length(ADose));
3038              end
3039              else DoseUnits := Piece(DoseFields, '&', 2);
3040              if (not AnsiSameText(DoseUnits, DrugUnits)) then DrugOK := False;
3041            end;
3042            if not DrugOK then
3043            begin
3044              KillDrug(ADrug);
3045              Break;
3046            end; {if not DrugOK}
3047          end; {with..for j}
3048        end; {for i}
3049        if FoundDrugs.Count > 0 then
3050        begin
3051          if not PossibleDoses then Result := Piece(FoundDrugs[0], U, 1) else
3052          begin
3053            UnitsPerDose := 99999999;
3054            for i := 0 to Pred(FoundDrugs.Count) do
3055            begin
3056              if (StrToFloatDef(Piece(FoundDrugs[i], U, 2), 99999999) = 1) or (StrToFloatDef(Piece(FoundDrugs[i], U, 2), 99999999) < UnitsPerDose) then
3057              begin
3058                Result := Piece(FoundDrugs[i], U, 1);
3059                UnitsPerDose := StrToFloatDef(Piece(FoundDrugs[i], U, 2), 99999999);
3060                if UnitsPerDose = 1 then Break;
3061              end; {if StrToFloatDef}
3062            end; {for i..FoundDrugs}
3063          end; {if not..else PossibleDoses}
3064        end; {if FoundDrugs}
3065      finally
3066        FoundDrugs.Free;
3067      end; {try}
3068    end; {if..else FInptDlg}
3069  end; {FindCommonDrug}