unit BillsBookmarkDm;

interface

uses
  SysUtils, Classes, sdDB;

type
  TBillsBookmarkData = class(TDataModule)
    sdvBillsBookmark: TsdDataView;
    sdvStageBookmark: TsdDataView;
    procedure sdvBillsBookmarkFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvStageBookmarkFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
  private
    FProjectData: TObject;
  public
    constructor Create(AProjectData: TObject);
    destructor Destroy; override;

    procedure Open;
    procedure Close;

    procedure ResetPhaseStageLink;

    procedure RefreshBillsBookmark;
    procedure RefreshStageBookmark;
  end;

implementation

uses
  ProjectData, BillsDm, BillsTree;

{$R *.dfm}

{ TBillsBookmarkData }

procedure TBillsBookmarkData.Close;
begin
  sdvBillsBookmark.Close;
  sdvStageBookmark.Close;
end;

constructor TBillsBookmarkData.Create(AProjectData: TObject);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
end;

destructor TBillsBookmarkData.Destroy;
begin

  inherited;
end;

procedure TBillsBookmarkData.Open;
begin
  sdvBillsBookmark.DataSet := TProjectData(FProjectData).BillsData.sddBills;
  sdvBillsBookmark.IndexName := 'idxSerialNo';
  sdvBillsBookmark.Open;
  sdvStageBookmark.DataSet := TProjectData(FProjectData).BillsData.sddBills;
  sdvStageBookmark.IndexName := 'idxSerialNo';
  sdvStageBookmark.Open;
end;

procedure TBillsBookmarkData.RefreshBillsBookmark;
begin
  sdvBillsBookmark.RefreshFilter;
end;

procedure TBillsBookmarkData.RefreshStageBookmark;
begin
  sdvStageBookmark.RefreshFilter;
end;

procedure TBillsBookmarkData.ResetPhaseStageLink;
begin
  with TProjectData(FProjectData).PhaseData do
  begin
    sdvStageBookmark.Columns.FindColumn('HasBookmark').LookupDataSet := StageData.sddStage;
    sdvStageBookmark.Columns.FindColumn('MarkMemo').LookupDataSet := StageData.sddStage;
  end;
  sdvStageBookmark.RefreshFilter;
end;

procedure TBillsBookmarkData.sdvBillsBookmarkFilterRecord(
  ARecord: TsdDataRecord; var Allow: Boolean);
begin
  Allow := ARecord.ValueByName('HasBookmark').AsBoolean;
end;

procedure TBillsBookmarkData.sdvStageBookmarkFilterRecord(
  ARecord: TsdDataRecord; var Allow: Boolean);
var
  vNode: TMeasureBillsIDTreeNode;
  StageRec: TsdDataRecord;
begin
  if (TProjectData(FProjectData).ProjProperties.PhaseCount > 0) and
     (TProjectData(FProjectData).PhaseData.Active) then
  begin
    with TProjectData(FProjectData).BillsMeasureData do
      vNode := TMeasureBillsIDTreeNode(BillsMeasureTree.FindNode(ARecord.ValueByName('ID').AsInteger));
    if Assigned(vNode) then
    begin
      StageRec := vNode.StageRec;
      if Assigned(StageRec) then
        Allow := StageRec.ValueByName('HasBookmark').AsBoolean
      else
        Allow := False;
    end
    else
      Allow := False;
  end
  else
    Allow := False;
end;

end.
