unit stgGclGatherDm;
// 结果展示，内存表

interface

uses
  SysUtils, Classes, stgGatherGclCacheData, sdDB, sdProvider, stgGatherUtils;

type
  TstgGclGatherData = class(TDataModule)
    smpGatherGcl: TsdMemoryProvider;
    sddGatherGcl: TsdDataSet;
    sdvGatherGcl: TsdDataView;
    smpSubTenders: TsdMemoryProvider;
    sddSubTenders: TsdDataSet;
    sdvSubTenders: TsdDataView;
    smpBillsDetail: TsdMemoryProvider;
    sddBillsDetail: TsdDataSet;
    sdvBillsDetail: TsdDataView;
    sdmErrorDetail: TsdMemoryProvider;
    sddErrorDetail: TsdDataSet;
    sdvErrorDetail: TsdDataView;
    procedure sdvBillsDetailGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
    procedure sdvBillsDetailFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvGatherGclCurrentChanged(ARecord: TsdDataRecord);
    procedure sdvGatherGclGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
    procedure sdvErrorDetailFilterRecord(ARecord: TsdDataRecord;
      var Allow: Boolean);
    procedure sdvSubTendersCurrentChanged(ARecord: TsdDataRecord);
    procedure sdvErrorDetailGetText(var Text: String;
      ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
      DisplayText: Boolean);
  private
    procedure LoadSubTenders(ACacheData: TstgGatherGclCacheData);

    procedure LoadGatherGclDetail(AGcl: TstgGatherGcl);
    procedure LoadGatherGcl(AGcl: TstgGatherGcl);
    procedure LoadGatherGcls(ACacheData: TstgGatherGclCacheData);
  public
    procedure LoadGatherData(ACacheData: TstgGatherGclCacheData);
  end;

implementation

uses
  Variants, DB, ConditionalDefines;

{$R *.dfm}

{ TstgGatherData }

procedure TstgGclGatherData.LoadGatherData(ACacheData: TstgGatherGclCacheData);
begin
  LoadSubTenders(ACacheData);
  LoadGatherGcls(ACacheData);
  
  if not Assigned(sddGatherGcl.FindIndex('idxIndexCode')) then
    sddGatherGcl.AddIndex('idxIndexCode', 'IndexCode');
  sdvGatherGcl.Active := True;
  sdvGatherGcl.IndexName := 'idxIndexCode';

  sdvBillsDetail.Active := True;
  sdvBillsDetail.RefreshFilter;

  sdvSubTenders.Active := True;
end;

procedure TstgGclGatherData.LoadGatherGcls(ACacheData: TstgGatherGclCacheData);
var
  i: Integer;
begin
  for i := 0 to ACacheData.GatherGclCount - 1 do
    LoadGatherGcl(ACacheData.GatherGcl[i]);
end;

procedure TstgGclGatherData.LoadGatherGcl(AGcl: TstgGatherGcl);
var
  Rec: TsdDataRecord;
begin
  Rec := sddGatherGcl.Add;
  Rec.ValueByName('ID').AsInteger := AGcl.ID;
  Rec.ValueByName('B_Code').AsString := AGcl.B_Code;
  Rec.ValueByName('IndexCode').AsString := AGcl.IndexCode;
  Rec.ValueByName('Name').AsString := AGcl.Name;
  Rec.ValueByName('Units').AsString := AGcl.Units;
  Rec.ValueByName('Price').AsFloat := AGcl.Price;

  Rec.ValueByName('DealQuantity').AsFloat := AGcl.Gather.DealQuantity;
  Rec.ValueByName('DealTotalPrice').AsFloat := AGcl.Gather.DealTotalPrice;
  Rec.ValueByName('QcQuantity').AsFloat := AGcl.Gather.QcQuantity;
  Rec.ValueByName('QcTotalPrice').AsFloat := AGcl.Gather.QcTotalPrice;
  Rec.ValueByName('QcBGLCode').AsString := AGcl.Gather.QcBGLCode;
  Rec.ValueByName('QcBGLNum').AsString := AGcl.Gather.QcBGLNum;
  
  LoadGatherGclDetail(AGcl);
end;

procedure TstgGclGatherData.LoadGatherGclDetail(
  AGcl: TstgGatherGcl);
var
  iSub, iDetail: Integer;
  vSub: TstgGclSubTenderStageData;
  vDetail: TstgGclSubTenderDetailData;
  Rec: TsdDataRecord;
  sSubTenderName: string;
begin
  for iSub := 0 to AGcl.SubTenderCount - 1 do
  begin
    vSub := AGcl.SubTender[iSub];
    for iDetail := 0 to vSub.DetailCount - 1 do
    begin
      vDetail := vSub.Detail[iDetail];
      Rec := sddBillsDetail.Add;
      Rec.ValueByName('BillsID').AsInteger := AGcl.ID;
      Rec.ValueByName('TenderID').AsInteger := vSub.SubTenderID;
      Rec.ValueByName('TenderName').AsString := '';
      Rec.ValueByName('SerialNo').AsInteger := vDetail.SerialNo;
      Rec.ValueByName('DealQuantity').AsFloat := vDetail.DetailStage.DealQuantity;
      Rec.ValueByName('DealTotalPrice').AsFloat := vDetail.DetailStage.DealTotalPrice;
      Rec.ValueByName('QcQuantity').AsFloat := vDetail.DetailStage.QcQuantity;
      Rec.ValueByName('QcTotalPrice').AsFloat := vDetail.DetailStage.QcTotalPrice;
      Rec.ValueByName('QcBGLCode').AsString := vDetail.DetailStage.QcBGLCode;
      Rec.ValueByName('QcBGLNum').AsString := vDetail.DetailStage.QcBGLNum;
    end;
  end;
end;

procedure TstgGclGatherData.LoadSubTenders(ACacheData: TstgGatherGclCacheData);
var
  i: Integer;
  vSubTender: TstgGatherGclSubTender;
  Rec: TsdDataRecord;
begin
  for i := 0 to ACacheData.SubTenderCount - 1 do
  begin
    vSubTender := ACacheData.SubTender[i];
    Rec := sddSubTenders.Add;
    Rec.ValueByName('ID').AsInteger := vSubTender.ID;
    Rec.ValueByName('Name').AsString := vSubTender.Rec.ValueByName('Name').AsString;
  end;
end;

procedure TstgGclGatherData.sdvBillsDetailGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);

  function GetTenderName(ATenderID: Integer): string;
  begin
    Result := VarToStrDef(sddSubTenders.Lookup('ID', ATenderID, 'Name'), '');
  end;

begin
  if DisplayText and (AColumn.FieldName = 'TenderName') then
    Text := GetTenderName(ARecord.ValueByName('TenderID').asInteger);
end;

procedure TstgGclGatherData.sdvBillsDetailFilterRecord(ARecord: TsdDataRecord;
  var Allow: Boolean);
begin
  if Assigned(sdvGatherGcl.Current) then
    Allow := ARecord.ValueByName('BillsID').AsInteger = sdvGatherGcl.Current.ValueByName('ID').AsInteger
  else
    Allow := False;
end;

procedure TstgGclGatherData.sdvGatherGclCurrentChanged(
  ARecord: TsdDataRecord);
begin
  sdvBillsDetail.RefreshFilter;
end;

procedure TstgGclGatherData.sdvGatherGclGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);
begin
  if (AValue.DataType = ftFloat) and (AValue.AsFloat = 0) then
  begin
    Text := '';
  end;
end;

procedure TstgGclGatherData.sdvErrorDetailFilterRecord(ARecord: TsdDataRecord;
  var Allow: Boolean);
begin
  if Assigned(sdvSubTenders.Current) then
    Allow := ARecord.ValueByName('TenderID').AsInteger = sdvSubTenders.Current.ValueByName('ID').AsInteger
  else
    Allow := False;
end;

procedure TstgGclGatherData.sdvSubTendersCurrentChanged(
  ARecord: TsdDataRecord);
begin
  sdvErrorDetail.RefreshFilter;
end;

procedure TstgGclGatherData.sdvErrorDetailGetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  DisplayText: Boolean);

  function GetTenderName(ATenderID: Integer): string;
  begin
    Result := VarToStrDef(sddSubTenders.Lookup('ID', ATenderID, 'Name'), '');
  end;

begin
  if DisplayText then
  begin
    if (AColumn.FieldName = 'TenderName') then
      Text := GetTenderName(ARecord.ValueByName('TenderID').asInteger)
    else if (AColumn.FieldName = 'ErrorType') then
      Text := GetErrorTypeText(AValue.AsInteger);
  end;
end;

end.
