unit StandardBillsDm;

interface

uses
  SysUtils, Classes, ADODB, DB, DBClient, Provider, sdIDTree,
  UtilMethods, sdDB, sdProvider;

type
  TStandardBillsData = class(TDataModule)
    sdpStdBills: TsdADOProvider;
    sddStdBills: TsdDataSet;
    sdvStdBills: TsdDataView;
  private
    FBillsTree: TsdIDTree;

    procedure CreateBillsTree;
    procedure OpenConnetion(AConnection: TADOConnection);
    procedure ExpandFirstNode;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure Open(AConnection: TADOConnection);

    property BillsTree: TsdIDTree read FBillsTree;
  end;

implementation

{$R *.dfm}

{ TStandardBillsData }

constructor TStandardBillsData.Create(AOwner: TComponent);
begin
  inherited;
  CreateBillsTree;
end;

procedure TStandardBillsData.CreateBillsTree;
begin
  FBillsTree := TsdIDTree.Create;
  FBillsTree.KeyFieldName := 'ID';
  FBillsTree.ParentFieldName := 'ParentID';
  FBillsTree.NextSiblingFieldName := 'NextID';
  FBillsTree.AutoExpand := False;
end;

destructor TStandardBillsData.Destroy;
begin
  FBillsTree.Free;
  inherited;
end;

procedure TStandardBillsData.ExpandFirstNode;
begin
  if FBillsTree.FirstNode <> nil then
    FBillsTree.FirstNode.Expand;
end;

procedure TStandardBillsData.Open(AConnection: TADOConnection);
begin
  OpenConnetion(AConnection);
  FBillsTree.DataView := sdvStdBills;
  ExpandFirstNode;
end;

procedure TStandardBillsData.OpenConnetion(AConnection: TADOConnection);
begin
  sdpStdBills.Connection := AConnection;
  sdpStdBills.TableName := 'BillsTree';
  sddStdBills.Open;
  sdvStdBills.Open;
  sddStdBills.AddIndex('idxID', 'ID');
  sddStdBills.AddIndex('sdxCode', 'Code');
  sddStdBills.AddIndex('sddBCode', 'B_Code');
end;

end.
