unit tpPegGclGatherDm;

interface

uses
  SysUtils, Classes, tpGatherGcl, sdDB, sdProvider;

type
  TtpPegGclGatherData = class(TDataModule)
    smpGclGather: TsdMemoryProvider;
    sddGclGather: TsdDataSet;
    sdvGclGather: TsdDataView;
    smpGclRela: TsdMemoryProvider;
    sddGclRela: TsdDataSet;
    sdvGclRela: TsdDataView;
    procedure sdvGclGatherCurrentChanged(ARecord: TsdDataRecord);
    procedure sdvGclRelaFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
  private
  public
    procedure LoadGclGatherData(AGcls: TtpGatherGclList);
  end;

implementation

uses tpGatherTree;

{$R *.dfm}

{ TtpPegGclGatherData }

procedure TtpPegGclGatherData.LoadGclGatherData(AGcls: TtpGatherGclList);

  procedure BeforeLoad;
  begin
    sddGclGather.Active := True;
    sddGclGather.DisableControls;
    sddGclGather.BeginUpdate;
    sddGclGather.DeleteAll;

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

    sdvGclRela.Filtered := False;
  end;

  procedure AfterLoad;
  begin
    sddGclGather.EndUpdate;
    sddGclGather.EnableControls;
    sddGclRela.EndUpdate;
    sddGclRela.EnableControls;

    sdvGclGather.LocateInControl(sdvGclGather.Records[0]);
    sdvGclRela.Filtered := True;
  end;

  procedure LoadGclRela(AGcl: TtpGatherGcl);
  var
    iRela: Integer;
    vRela: TtpGatherGclRela;
    Rec: TsdDataRecord;
  begin
    for iRela := 0 to AGcl.RelaCount - 1 do
    begin
      vRela := AGcl.Rela[iRela];
      Rec := sddGclRela.Add;
      Rec.ValueByName('DetailGclID').AsInteger := vRela.RelaGcl.ID;
      Rec.ValueByName('GclID').AsInteger := AGcl.ID;
      Rec.ValueByName('PegXmjID').AsInteger := vRela.RelaPegXmj.ID;
      Rec.ValueByName('PegXmjCode').AsString := vRela.RelaPegXmj.Code;
      Rec.ValueByName('PegXmjName').AsString := vRela.RelaPegXmj.Name;
      Rec.ValueByName('PegXmjUnits').AsString := vRela.RelaPegXmj.Units;
      Rec.ValueByName('Quantity').AsFloat := vRela.RelaGcl.Quantity;
      Rec.ValueByName('TotalPrice').AsFloat := vRela.RelaPegXmj.TotalPrice;
      Rec.ValueByName('DrawingCode').AsString := vRela.RelaPegXmj.DrawingCode;
    end;
  end;

  procedure LoadGclNode(AGcl: TtpGatherGcl);
  var
    Rec: TsdDataRecord;
  begin
    Rec := sddGclGather.Add;
    Rec.ValueByName('ID').AsInteger := AGcl.ID;
    Rec.ValueByname('IndexCode').AsString := AGcl.IndexCode;
    Rec.ValueByname('B_Code').AsString := AGcl.B_Code;
    Rec.ValueByName('Name').AsString := AGcl.Name;
    Rec.ValueByName('Units').AsString := AGcl.Units;
    Rec.ValueByName('Price').AsFloat := AGcl.Price;
    Rec.ValueByName('Quantity').AsFloat := AGcl.Quantity;
    Rec.ValueByName('TotalPrice').AsFloat := AGcl.TotalPrice;
    LoadGclRela(AGcl);
  end;

var
  iGcl: Integer;
begin
  BeforeLoad;
  try
    for iGcl := 0 to AGcls.GclCount - 1 do
      LoadGclNode(AGcls.Gcl[iGcl]);
  finally
    AfterLoad;
  end;
end;

procedure TtpPegGclGatherData.sdvGclGatherCurrentChanged(
  ARecord: TsdDataRecord);
begin
  sdvGclRela.RefreshFilter;
end;

procedure TtpPegGclGatherData.sdvGclRelaFilterRecord(
  ARecord: TsdDataRecord; var Allow: Boolean);
begin
  if Assigned(sdvGclGather.Current) then
    Allow := ARecord.ValueByName('GclID').AsInteger = sdvGclGather.Current.ValueByName('ID').AsInteger
  else
    Allow := False;
end;

end.
