unit rmGcl_XmjBillsDm;

interface

uses
  SysUtils, Classes, ProjectData, DB, DBClient, sdIDTree, sdDB,
  GclBillsGatherModel;

type
  {-----------------------------
    gxtTopGcl: 汇总方式默认，写入方式：工程量清单1&其所属全部项目节, 工程量清单2&其所属全部项目节, ...;
      示例: 202-1       456
                   1-1  123
                   1-2  333
            203-1       222
                   1-1  120
                   1-2  102
    gxtFlowGcl: 汇总方式默认，写入方式：工程量清单1所属全部项目节, 工程量清单2所属全部项目节, ...;
      示例: 202-1  1-1  123
            202-1  1-2  333
            203-1  1-1  120
            203-1  1-1  102
    gxtWithoutXmj: 同gxtTopGcl，完成后过滤所有清单编号为空的节点
      示例: 202-1       456
            203-1       222
  -----------------------------}
  TGXType = (gxtTopGcl, gxtFlowGcl, gxtWithoutXmj);

  TrmGcl_XmjBillsData = class(TDataModule)
    cdsGcl: TClientDataSet;
    cdsGclIndexCode: TStringField;
    cdsGclIndexID: TIntegerField;
    cdsGclB_Code: TStringField;
    cdsGclName: TWideStringField;
    cdsGclCode: TStringField;
    cdsGclPeg: TWideStringField;
    cdsGclNameDanWei: TWideStringField;
    cdsGclNameFenBu: TWideStringField;
    cdsGclNameFenXiang: TWideStringField;
    cdsGclNameUnit: TWideStringField;
    cdsGclDrawingCode: TWideStringField;
    cdsGclPrice: TFloatField;
    cdsGclQuantity: TFloatField;
    cdsGclTotalPrice: TFloatField;
    cdsGclDealQuantity: TFloatField;
    cdsGclDealTotalPrice: TFloatField;
    cdsGclDifferQuantity: TFloatField;
    cdsGclDifferTotalPrice: TFloatField;
    cdsGclUnits: TWideStringField;
    cdsGclNewPrice: TFloatField;
    cdsGclPosition: TWideStringField;
    cdsGclOrgQuantity: TFloatField;
    cdsGclOrgTotalPrice: TFloatField;
    cdsGclMisQuantity: TFloatField;
    cdsGclMisTotalPrice: TFloatField;
    cdsGclOthQuantity: TFloatField;
    cdsGclOthTotalPrice: TFloatField;
  private
    FProjectData: TProjectData;
    FIndex: Integer;
    FGXType: TGXType;

    procedure BeforeOperation;
    procedure AfterOperation;

    procedure WriteXmjNode(AGclNode: TGclNode);
    procedure WriteGclNode(AGclNode: TGclNode);
    procedure WriteTopGclTypeData(AGcls: TList);

    procedure WriteFlowGclNode(AGclNode: TGclNode);
    procedure WriteFlowGclTypeData(AGcls: TList);

    procedure WriteData(AGcls, AXmjs: TList);

    procedure GatherData;
  public
    function AssignData(AProjectData: TProjectData; AGXType: TGXType): TDataSet;
  end;

implementation

uses
  ZhAPI, UtilMethods;

{$R *.dfm}

{ TrmGclBillsData }

procedure TrmGcl_XmjBillsData.AfterOperation;
begin
  if FGXType = gxtWithoutXmj then
  begin
    cdsGcl.Filter := 'B_Code <> ''''';
    cdsGcl.Filtered := True;
  end;
  cdsGcl.IndexFieldNames := 'IndexCode;IndexID';
end;

function TrmGcl_XmjBillsData.AssignData(
  AProjectData: TProjectData; AGXType: TGXType): TDataSet;
begin
  FProjectData := AProjectData;
  FGXType := AGXType;
  BeforeOperation;
  try
    GatherData;
  finally
    AfterOperation;
    Result := cdsGcl;
  end;
end;

procedure TrmGcl_XmjBillsData.BeforeOperation;
begin
  cdsGcl.Active := True;
  cdsGcl.Filtered := False;
  cdsGcl.EmptyDataSet;
end;

procedure TrmGcl_XmjBillsData.GatherData;
var
  vGather: TGclGatherModel;
begin
  vGather := TGclGatherModel.Create(FProjectData);
  try
    vGather.GatherDeal := True;
    vGather.WriteGatherData := WriteData;
    vGather.Execute;
  finally
    vGather.Free;
  end;
end;

procedure TrmGcl_XmjBillsData.WriteData(AGcls, AXmjs: TList);
begin
  case FGXType of
    gxtTopGcl, gxtWithoutXmj: WriteTopGclTypeData(AGcls);
    gxtFlowGcl: WriteFlowGclTypeData(AGcls);
  end;
end;

procedure TrmGcl_XmjBillsData.WriteFlowGclNode(AGclNode: TGclNode);
var
  i: Integer;
  DetailGcl: TDetailGclNode;
begin
  for i := 0 to AGclNode.DetailGclCount - 1 do
  begin
    DetailGcl := AGclNode.DetailGcl[i];
    cdsGcl.Append;
    cdsGclIndexCode.AsString := AGclNode.IndexCode;
    cdsGclIndexID.AsInteger := i+1;

    cdsGclB_Code.AsString := AGclNode.B_Code;
    cdsGclName.AsString := AGclNode.Name;
    cdsGclUnits.AsString := AGclNode.Units;
    cdsGclPrice.AsFloat := AGclNode.Price;

    if Assigned(DetailGcl.LeafXmj) then
    begin
      cdsGclCode.AsString := DetailGcl.LeafXmj.XmjCode;
      cdsGclPeg.AsString := DetailGcl.LeafXmj.Peg;
      cdsGclNameDanWei.AsString := DetailGcl.LeafXmj.NameDanWei;
      cdsGclNameFenBu.AsString := DetailGcl.LeafXmj.NameFenBu;
      cdsGclNameFenXiang.AsString := DetailGcl.LeafXmj.NameFenXiang;
      cdsGclNameUnit.AsString := DetailGcl.LeafXmj.NameUnit;
      cdsGclDrawingCode.AsString := DetailGcl.LeafXmj.DrawingCode;
      cdsGclPosition.AsString := DetailGcl.LeafXmj.Position;
    end;

    cdsGclOrgQuantity.AsFloat := DetailGcl.OrgQuantity;
    cdsGclOrgTotalPrice.AsFloat := DetailGcl.OrgTotalPrice;
    cdsGclMisQuantity.AsFloat := DetailGcl.MisQuantity;
    cdsGclMisTotalPrice.AsFloat := DetailGcl.MisTotalPrice;
    cdsGclOthQuantity.AsFloat := DetailGcl.OthQuantity;
    cdsGclOthTotalPrice.AsFloat := DetailGcl.OthTotalPrice;

    cdsGclQuantity.AsFloat := DetailGcl.Quantity;
    cdsGclTotalPrice.AsFloat := DetailGcl.TotalPrice;
    cdsGcl.Post;
  end;
end;

procedure TrmGcl_XmjBillsData.WriteFlowGclTypeData(AGcls: TList);
var
  i: Integer;
begin
  FIndex := 0;
  for i := 0 to AGcls.Count - 1 do
    WriteFlowGclNode(TGclNode(AGcls.Items[i]));
end;

procedure TrmGcl_XmjBillsData.WriteGclNode(
  AGclNode: TGclNode);
var
  i: Integer;
begin
  cdsGcl.Append;
  cdsGclIndexCode.AsString := AGclNode.IndexCode;
  cdsGclIndexID.AsInteger := 0;

  cdsGclB_Code.AsString := AGclNode.B_Code;
  cdsGclName.AsString := AGclNode.Name;
  cdsGclUnits.AsString := AGclNode.Units;
  cdsGclPrice.AsFloat := AGclNode.Price;

  cdsGclQuantity.AsFloat := AGclNode.Quantity;
  cdsGclTotalPrice.AsFloat := AGclNode.TotalPrice;
  cdsGclDealQuantity.AsFloat := AGclNode.DealQuantity;
  cdsGclDealTotalPrice.AsFloat := AGclNode.DealTotalPrice;
  cdsGclDifferQuantity.AsFloat := AGclNode.DealQuantity - AGclNode.Quantity;
  cdsGclDifferTotalPrice.AsFloat := AGclNode.DealTotalPrice - AGclNode.TotalPrice;

  cdsGclOrgQuantity.AsFloat := AGclNode.OrgQuantity;
  cdsGclOrgTotalPrice.AsFloat := AGclNode.OrgTotalPrice;
  cdsGclMisQuantity.AsFloat := AGclNode.MisQuantity;
  cdsGclMisTotalPrice.AsFloat := AGclNode.MisTotalPrice;
  cdsGclOthQuantity.AsFloat := AGclNode.OthQuantity;
  cdsGclOthTotalPrice.AsFloat := AGclNode.OthTotalPrice;
  cdsGcl.Post;
  WriteXmjNode(AGclNode);
end;

procedure TrmGcl_XmjBillsData.WriteTopGclTypeData(AGcls: TList);
var
  i: Integer;
begin
  FIndex := 0;
  for i := 0 to AGcls.Count - 1 do
    WriteGclNode(TGclNode(AGcls.Items[i]));
end;

procedure TrmGcl_XmjBillsData.WriteXmjNode(
  AGclNode: TGclNode);
var
  i: Integer;
  DetailGcl: TDetailGclNode;
begin
  for i := 0 to AGclNode.DetailGclCount - 1 do
  begin
    DetailGcl := AGclNode.DetailGcl[i];
    cdsGcl.Append;
    cdsGclIndexCode.AsString := AGclNode.IndexCode;
    cdsGclIndexID.AsInteger := FIndex;

    if Assigned(DetailGcl.LeafXmj) then
    begin
      cdsGclCode.AsString := DetailGcl.LeafXmj.XmjCode;
      cdsGclPeg.AsString := DetailGcl.LeafXmj.Peg;
      cdsGclNameDanWei.AsString := DetailGcl.LeafXmj.NameDanWei;
      cdsGclNameFenBu.AsString := DetailGcl.LeafXmj.NameFenBu;
      cdsGclNameFenXiang.AsString := DetailGcl.LeafXmj.NameFenXiang;
      cdsGclNameUnit.AsString := DetailGcl.LeafXmj.NameUnit;
      cdsGclPosition.AsString := DetailGcl.LeafXmj.Position;
      cdsGclDrawingCode.AsString := DetailGcl.LeafXmj.DrawingCode;
    end;

    cdsGclOrgQuantity.AsFloat := DetailGcl.OrgQuantity;
    cdsGclOrgTotalPrice.AsFloat := DetailGcl.OrgTotalPrice;
    cdsGclMisQuantity.AsFloat := DetailGcl.MisQuantity;
    cdsGclMisTotalPrice.AsFloat := DetailGcl.MisTotalPrice;
    cdsGclOthQuantity.AsFloat := DetailGcl.OthQuantity;
    cdsGclOthTotalPrice.AsFloat := DetailGcl.OthTotalPrice;

    cdsGclQuantity.AsFloat := DetailGcl.Quantity;
    cdsGclTotalPrice.AsFloat := DetailGcl.TotalPrice;
    cdsGcl.Post;
    Inc(FIndex);
  end;
end;

end.
