Function

TfrmODMedNVA.FindCommonDrug

Module

fODMedNVA

Last Modified

7/15/2014 3:26:42 PM

Comments

DoseList[n] = DoseText ^ Dispense Drug Pointer

Visibility

Private

Owner

TfrmODMedNVA

Declaration

function FindCommonDrug(DoseList: TStringList): string;

Calls Hierarchy


TfrmODMedNVA.FindCommonDrug
 ├TfrmODMedNVA.FindDoseFields
 ├SaveDrug
 └KillDrug

Called-By Hierarchy


            TfrmODMedNVA.FindCommonDrug
           TfrmODMedNVA.ControlChange┘ 
           TfrmODMedNVA.SetupDialog┤   
              TfrmODMedNVA.Validate┤   
        TfrmODMedNVA.btnSelectClick┤   
        TfrmODMedNVA.UMDelayClick┘ │   
         TfrmODMedNVA.tabDoseChange┤   
        TfrmODMedNVA.cboRouteChange┤   
      TfrmODMedNVA.SetOnMedSelect┘ │   
       TfrmODMedNVA.SetupDialog┤   │   
 TfrmODMedNVA.btnSelectClick...┤   │   
  TfrmODMedNVA.CheckFormAltDose┘   │   
  TfrmODMedNVA.cboDosageClick┘     │   
 TfrmODMedNVA.cboDosageExit┘       │   
  TfrmODMedNVA.timCheckChangesTimer┤   
       TfrmODMedNVA.UpdateRelated┘ │   
       TfrmODMedNVA.SetupDialog┤   │   
 TfrmODMedNVA.btnSelectClick...┤   │   
 TfrmODMedNVA.cboDosageClick...┤   │   
   TfrmODMedNVA.cboDosageChange┤   │   
  TfrmODMedNVA.cboScheduleClick┤   │   
 TfrmODMedNVA.cboScheduleChange┤   │   
      TfrmODMedNVA.grdDosesExit┘   │   
           TfrmODMedNVA.chkPRNClick┤   
TfrmODMedNVA.lbStatementsClickCheck┘   

Calls

Name Declaration Comments
TfrmODMedNVA.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
TfrmODMedNVA.ControlChange procedure ControlChange(Sender: TObject); -


Source

1526  function TfrmODMedNVA.FindCommonDrug(DoseList: TStringList): string;
1527  // DoseList[n] = DoseText ^ Dispense Drug Pointer
1528  var
1529    i, j, UnitIndex: Integer;
1530    DrugStrength, DoseValue, UnitsPerDose: Extended;
1531    DrugOK, PossibleDoses, SplitTab: Boolean;
1532    ADrug, ADose, DoseFields, DoseUnits, DrugUnits: string;
1533    FoundDrugs: TStringList;
1534  
1535    procedure SaveDrug(const ADrug: string; UnitsPerDose: Extended);
1536    var
1537      i, DrugIndex: Integer;
1538      CurUnits: Extended;
1539    begin
1540      DrugIndex := -1;
1541      for i := 0 to Pred(FoundDrugs.Count) do
1542        if AnsiSameText(Piece(FoundDrugs[i], U, 1), ADrug) then DrugIndex := i;
1543      if DrugIndex = -1 then FoundDrugs.Add(ADrug + U + FloatToStr(UnitsPerDose)) else
1544      begin
1545        CurUnits := StrToFloatDef(Piece(FoundDrugs[DrugIndex], U, 2), 0);
1546        if UnitsPerDose > CurUnits
1547          then FoundDrugs[DrugIndex] := ADrug + U + FloatToStr(UnitsPerDose);
1548      end;
1549    end;
1550  
1551    procedure KillDrug(const ADrug: string);
1552    var
1553      i, DrugIndex: Integer;
1554    begin
1555      DrugIndex := -1;
1556      for i := 0 to Pred(FoundDrugs.Count) do
1557        if AnsiSameText(Piece(FoundDrugs[i], U, 1), ADrug) then DrugIndex := i;
1558      if DrugIndex > -1 then FoundDrugs.Delete(DrugIndex);
1559    end;
1560  
1561  begin
1562    Result := '';
1563    if FInptDlg then                                // inpatient dialog
1564    begin
1565      DrugOK := True;
1566      for i := 0 to Pred(DoseList.Count) do
1567      begin
1568        ADrug := Piece(DoseList[i], U, 2);
1569        if ADrug = '' then DrugOK := False;
1570        if Result = '' then Result := ADrug;
1571        if not AnsiSameText(ADrug, Result) then DrugOK := False;
1572        if not DrugOK then Break;
1573      end;
1574      
1575      if not DrugOK then Result :='';
1576    end else                                        // outpatient dialog
1577    begin
1578      // check the dose combinations for each dispense drug
1579      FoundDrugs := TStringList.Create;
1580      try
1581        if FAllDoses.Count > 0
1582          then PossibleDoses := Length(Piece(Piece(FAllDoses[0], U, 3), '&', 1)) > 0
1583          else PossibleDoses := False;
1584        for i := 0 to Pred(FAllDrugs.Count) do
1585        begin
1586          ADrug := Piece(FAllDrugs[i], U, 1);
1587          DrugOK := True;
1588          DrugStrength := StrToFloatDef(Piece(FAllDrugs[i], U, 2), 0);
1589          DrugUnits := Piece(FAllDrugs[i], U, 3);
1590          SplitTab := Piece(FAllDrugs[i], U, 5) = '1';
1591          for j := 0 to Pred(DoseList.Count) do
1592          begin
1593            ADose:= Piece(DoseList[j], U, 1);
1594            DoseFields := FindDoseFields(ADrug, ADose);  // get the idnode for the dose/drug combination
1595            if not PossibleDoses then
1596            begin
1597              if DoseFields = '' then DrugOK := False else SaveDrug(ADrug, 0);
1598            end else
1599            begin
1600              DoseValue := StrToFloatDef(Piece(DoseFields, '&', 1), 0);
1601              if DoseValue = 0 then DoseValue := ExtractFloat(ADose);
1602              UnitsPerDose := DoseValue / DrugStrength;
1603              if (Frac(UnitsPerDose) = 0) or (SplitTab and (Frac(UnitsPerDose) = 0.5))
1604                then SaveDrug(ADrug, UnitsPerDose)
1605                else DrugOK := False;
1606              // make sure this dose is using the same units as the drug
1607              if DoseFields = '' then
1608              begin
1609                for UnitIndex := 1 to Length(ADose) do
1610                  if not (ADose[UnitIndex] in ['0'..'9','.']) then Break;
1611                DoseUnits := Copy(ADose, UnitIndex, Length(ADose));
1612              end
1613              else DoseUnits := Piece(DoseFields, '&', 2);
1614              if not AnsiSameText(DoseUnits, DrugUnits) then DrugOK := False;
1615            end;
1616            if not DrugOK then
1617            begin
1618              KillDrug(ADrug);
1619              Break;
1620            end; {if not DrugOK}
1621          end; {with..for j}
1622        end; {for i}
1623        if FoundDrugs.Count > 0 then
1624        begin
1625          if not PossibleDoses then Result := Piece(FoundDrugs[0], U, 1) else
1626          begin
1627            UnitsPerDose := 99999999;
1628            for i := 0 to Pred(FoundDrugs.Count) do
1629            begin
1630              if StrToFloatDef(Piece(FoundDrugs[i], U, 2), 99999999) < UnitsPerDose then
1631              begin
1632                Result := Piece(FoundDrugs[i], U, 1);
1633                UnitsPerDose := StrToFloatDef(Piece(FoundDrugs[i], U, 2), 99999999);
1634              end; {if StrToFloatDef}
1635            end; {for i..FoundDrugs}
1636          end; {if not..else PossibleDoses}
1637        end; {if FoundDrugs}
1638      finally
1639        FoundDrugs.Free;
1640      end; {try}
1641    end; {if..else FInptDlg}
1642  end; {FindCommonDrug}