unit tpPeg_GclDm;

interface

uses
  SysUtils, Classes, sdDB, sdProvider, tpPeg;

type
  TtpPeg_GclData = class(TDataModule)
    smpPeg: TsdMemoryProvider;
    sddPeg: TsdDataSet;
    sdvPeg: TsdDataView;
    smpGclBills: TsdMemoryProvider;
    sddGclBills: TsdDataSet;
    sdvGclBills: TsdDataView;
    procedure sdvGclBillsFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvPegCurrentChanged(ARecord: TsdDataRecord);
  private
  public
    procedure LoadData(APegs: TList);
  end;

implementation

uses
  tpGatherTree;

{$R *.dfm}

{ TtpPeg_GclData }

procedure TtpPeg_GclData.LoadData(APegs: TList);

  procedure BeforeLoadData;
  begin
    sddPeg.Active := True;
    sddPeg.DisableControls;
    sddPeg.BeginUpdate;
    sddPeg.DeleteAll;

    sddGclBills.Active := True;
    sddGclBills.DisableControls;
    sddGclBills.BeginUpdate;
    sddGclBills.DeleteAll;

    sdvGclBills.Filtered := False;
  end;

  procedure AfterLoadData;
  begin
    sddPeg.EndUpdate;
    sddPeg.EnableControls;
    sddGclBills.EndUpdate;
    sddGclBills.EnableControls;

    sdvPeg.LocateInControl(sdvPeg.Records[0]);
    sdvGclBills.Filtered := True;
  end;

  procedure LoadGclBills(APeg: TtpPegNode);
  var
    i: Integer;
    Rec: TsdDataRecord;
    vGclNode: TtpGatherTreeNode;
  begin
    for i := 0 to APeg.RelaGclNodes.Count - 1 do
    begin
      vGclNode := TtpGatherTreeNode(APeg.RelaGclNodes.Items[i]);
      Rec := sddGclBills.Add;
      Rec.ValueByName('ID').AsInteger := vGclNode.ID;
      Rec.ValueByName('PegID').AsInteger := APeg.RelaNode.ID;
      Rec.ValueByName('B_Code').AsString := vGclNode.B_Code;
      Rec.ValueByName('Name').AsString := vGclNode.Name;
      Rec.ValueByName('Units').AsString := vGclNode.Units;
      Rec.ValueByName('Price').AsFloat := vGclNode.Price;
      Rec.ValueByName('Quantity').AsFloat := vGclNode.Quantity;
      Rec.ValueByName('TotalPrice').AsFloat := vGclNode.TotalPrice;
    end;
  end;

  procedure LoadPeg(APeg: TtpPegNode);
  var
    i: Integer;
    Rec: TsdDataRecord;
  begin
    Rec := sddPeg.Add;
    Rec.ValueByName('ID').AsInteger := APeg.RelaNode.ID;
    Rec.ValueByName('Code').AsString := APeg.RelaNode.Code;
    Rec.ValueByName('Name').AsString := APeg.RelaNode.Name;
    Rec.ValueByName('Units').AsString := APeg.RelaNode.Units;
    Rec.ValueByName('TotalPrice').AsFloat := APeg.RelaNode.TotalPrice;

    Rec.ValueByName('BeginPeg').AsString := APeg.BeginPeg;
    Rec.ValueByName('BeginPegNum').AsFloat := APeg.BeginPegNum;
    Rec.ValueByName('EndPeg').AsString := APeg.EndPeg;
    Rec.ValueByName('EndPegNum').AsFloat := APeg.EndPegNum;

    Rec.ValueByName('Peg').AsString := APeg.Peg;

    Rec.ValueByName('DrawingCode').AsString := APeg.RelaNode.DrawingCode;
    LoadGclBills(APeg);
  end;

var
  i: Integer;
  vPeg: TtpPegNode;
begin
  BeforeLoadData;
  try
    for i := 0 to APegs.Count - 1 do
    begin
      vPeg := TtpPegNode(APegs.Items[i]);
      LoadPeg(vPeg);
    end;
  finally
    AfterLoadData;
  end;
end;

procedure TtpPeg_GclData.sdvGclBillsFilterRecord(ARecord: TsdDataRecord;
  var Allow: Boolean);
begin
  if Assigned(sdvPeg.Current) then
    Allow := ARecord.ValueByName('PegID').AsInteger = sdvPeg.Current.ValueByName('ID').AsInteger
  else
    Allow := False;
end;

procedure TtpPeg_GclData.sdvPegCurrentChanged(ARecord: TsdDataRecord);
begin
  sdvGclBills.RefreshFilter;
end;

end.
