unit rdpBillsGatherDm;
// Report Data Prepare -- Gcl_Bills_Gather

interface

uses
  SysUtils, Classes, sdProvider, sdDB, ADODB, GclBillsGatherModel,
  ProjectData;

type
  TrdpBillsGatherData = class(TDataModule)
    sdpGclBills: TsdADOProvider;
    sddGclBills: TsdDataSet;
    sdpDetailGclBills: TsdADOProvider;
    sddDetailGclBills: TsdDataSet;
    sdpDetailBGLBills: TsdADOProvider;
    sddDetailBGLBills: TsdDataSet;
    sdpDetailDealBills: TsdADOProvider;
    sddDetailDealBills: TsdDataSet;
    sdpLeafXmjs: TsdADOProvider;
    sddLeafXmjs: TsdDataSet;
  private
    FProjectData: TProjectData;

    procedure BeforeWrite;
    procedure AfterWrite;

    procedure WriteDetailDealNode(AGclNode: TGclNode);
    procedure WriteDetailBGLNode(AGclNode: TGclNode);
    procedure WriteDetailGclNode(AGclNode: TGclNode);
    procedure WriteGclNode(AGclNode: TGclNode);
    procedure WriteLeafXmjNode(ALeafXmj: TLeafXmjNode);
    procedure WriteGatherRecord(AGcls, AXmjs: TList);
  public
    constructor Create(AProjectData: TProjectData);
    destructor Destroy; override;

    procedure LoadGclBillsGatherData;
  end;

implementation

uses
  mPegFilter;

{$R *.dfm}

{ TrdpBillsGatherData }

procedure TrdpBillsGatherData.AfterWrite;
begin
  sddGclBills.EndUpdate;
  sddLeafXmjs.EndUpdate;
  sddDetailGclBills.EndUpdate;
  sddDetailBGLBills.EndUpdate;
  sddDetailDealBills.EndUpdate;
end;

procedure TrdpBillsGatherData.BeforeWrite;
begin
  sddGclBills.BeginUpdate;
  sddLeafXmjs.BeginUpdate;
  sddDetailGclBills.BeginUpdate;
  sddDetailBGLBills.BeginUpdate;
  sddDetailDealBills.BeginUpdate;
end;

constructor TrdpBillsGatherData.Create(AProjectData: TProjectData);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
  sdpLeafXmjs.Connection := FProjectData.ADOConnection;
  sddLeafXmjs.Open;
  sdpGclBills.Connection := FProjectData.ADOConnection;
  sddGclBills.Open;
  sdpDetailGclBills.Connection := FProjectData.ADOConnection;
  sddDetailGclBills.Open;
  sdpDetailBGLBills.Connection := FProjectData.ADOConnection;
  sddDetailBGLBills.Open;
  sdpDetailDealBills.Connection := FProjectData.ADOConnection;
  sddDetailDealBills.Open;
end;

destructor TrdpBillsGatherData.Destroy;
begin
  sddLeafXmjs.Save;
  sddGclBills.Save;
  sddDetailGclBills.Save;
  sddDetailBGLBills.Save;
  sddDetailDealBills.Save;
  inherited;
end;

procedure TrdpBillsGatherData.LoadGclBillsGatherData;
var
  Gather: TGclGatherModel;
begin
  Gather := TGclGatherModel.Create(FProjectData);
  try
    Gather.WriteGatherData := WriteGatherRecord;
    Gather.Execute;
  finally
    Gather.Free;
  end;
end;

procedure TrdpBillsGatherData.WriteDetailBGLNode(AGclNode: TGclNode);
var
  iBGL: Integer;
  vBGL: TDetailBGLNode;
  Rec: TsdDataRecord;
begin
  for iBGL := 0 to AGclNode.DetailBGLCount - 1 do
  begin
    vBGL := AGclNode.DetailBGL[iBGL];
    Rec := sddDetailBGLBills.Add;

    Rec.ValueByName('ID').AsInteger := vBGL.ID;
    Rec.ValueByName('BillsID').AsInteger := AGclNode.ID;
    Rec.ValueByName('BGBillsID').AsInteger := vBGL.BGBillsID;
    Rec.ValueByName('BGLID').AsInteger := vBGL.BGLID;

    Rec.ValueByName('BGLCode').AsString := vBGL.BGLCode;
    Rec.ValueByName('BGLName').AsString := vBGL.BGLName;
    Rec.ValueByName('BGLApprovalCode').AsString := vBGL.BGLApprovalCode;
    Rec.ValueByName('BGLDrawingCode').AsString := vBGL.BGLDrawingCode;

    Rec.ValueByName('Quantity').AsFloat := vBGL.Quantity;
    Rec.ValueByName('TotalPrice').AsFloat := vBGL.TotalPrice;
  end;
end;

procedure TrdpBillsGatherData.WriteDetailDealNode(AGclNode: TGclNode);
var
  iDeal: Integer;
  vDeal: TDetailDealNode;
  Rec: TsdDataRecord;
begin
  for iDeal := 0 to AGclNode.DetailDealCount - 1 do
  begin
    vDeal := AGclNode.DetailDeal[iDeal];
    Rec := sddDetailDealBills.Add;
    Rec.ValueByName('ID').AsInteger := vDeal.ID;
    Rec.ValueByName('BillsID').AsInteger := AGclNode.ID;
    Rec.ValueByName('DealID').AsInteger := vDeal.DealID;

    Rec.ValueByName('Quantity').AsFloat := vDeal.Quantity;
    Rec.ValueByName('TotalPrice').AsFloat := vDeal.TotalPrice;
  end;
end;

procedure TrdpBillsGatherData.WriteDetailGclNode(AGclNode: TGclNode);
var
  iDetailGcl: Integer;
  vDetailGcl: TDetailGclNode;
  Rec: TsdDataRecord;
begin
  for iDetailGcl := 0 to AGclNode.DetailGclCount -1 do
  begin
    vDetailGcl := AGclNode.DetailGcl[iDetailGcl];

    Rec := sddDetailGclBills.Add;
    Rec.ValueByName('ID').AsInteger := vDetailGcl.ID;
    Rec.ValueByName('BillsID').AsInteger := AGclNode.ID;
    Rec.ValueByName('TreeSerialNo').AsInteger := vDetailGcl.TreeSerialNo;
    Rec.ValueByName('RelaBillsID').AsInteger := vDetailGcl.BillsID;

    if Assigned(vDetailGcl.LeafXmj) then
      Rec.ValueByName('LeafXmjID').AsInteger := vDetailGcl.LeafXmj.ID
    else
      Rec.ValueByName('LeafXmjID').AsInteger := -1;

    Rec.ValueByName('Quantity').AsFloat := vDetailGcl.Quantity;
    Rec.ValueByName('TotalPrice').AsFloat := vDetailGcl.TotalPrice;

    Rec.ValueByName('CurDealQuantity').AsFloat := vDetailGcl.CurDealQuantity;
    Rec.ValueByName('CurDealTotalPrice').AsFloat := vDetailGcl.CurDealTotalPrice;
    Rec.ValueByName('CurQcQuantity').AsFloat := vDetailGcl.CurQcQuantity;
    Rec.ValueByName('CurQcTotalPrice').AsFloat := vDetailGcl.CurQcTotalPrice;
    Rec.ValueByName('CurGatherQuantity').AsFloat := vDetailGcl.CurGatherQuantity;
    Rec.ValueByName('CurGatherTotalPrice').AsFloat := vDetailGcl.CurGatherTotalPrice;

    Rec.ValueByName('PreDealQuantity').AsFloat := vDetailGcl.PreDealQuantity;
    Rec.ValueByName('PreDealTotalPrice').AsFloat := vDetailGcl.PreDealTotalPrice;
    Rec.ValueByName('PreQcQuantity').AsFloat := vDetailGcl.PreQcQuantity;
    Rec.ValueByName('PreQcTotalPrice').AsFloat := vDetailGcl.PreQcTotalPrice;
    Rec.ValueByName('PreGatherQuantity').AsFloat := vDetailGcl.PreGatherQuantity;
    Rec.ValueByName('PreGatherTotalPrice').AsFloat := vDetailGcl.PreGatherTotalPrice;

    Rec.ValueByName('EndDealQuantity').AsFloat := vDetailGcl.EndDealQuantity;
    Rec.ValueByName('EndDealTotalPrice').AsFloat := vDetailGcl.EndDealTotalPrice;
    Rec.ValueByName('EndQcQuantity').AsFloat := vDetailGcl.EndQcQuantity;
    Rec.ValueByName('EndQcTotalPrice').AsFloat := vDetailGcl.EndQcTotalPrice;
    Rec.ValueByName('EndGatherQuantity').AsFloat := vDetailGcl.EndGatherQuantity;
    Rec.ValueByName('EndGatherTotalPrice').AsFloat := vDetailGcl.EndGatherTotalPrice;

    Rec.ValueByName('AddDealQuantity').AsFloat := vDetailGcl.AddDealQuantity;
    Rec.ValueByName('AddDealTotalPrice').AsFloat := vDetailGcl.AddDealTotalPrice;
    Rec.ValueByName('AddQcQuantity').AsFloat := vDetailGcl.AddQcQuantity;
    Rec.ValueByName('AddQcTotalPrice').AsFloat := vDetailGcl.AddQcTotalPrice;
    Rec.ValueByName('AddGatherQuantity').AsFloat := vDetailGcl.AddGatherQuantity;
    Rec.ValueByName('AddGatherTotalPrice').AsFloat := vDetailGcl.AddGatherTotalPrice;
  end;
end;

procedure TrdpBillsGatherData.WriteGatherRecord(AGcls, AXmjs: TList);
var
  iGcl, iXmj: Integer;
  vGclNode: TGclNode;
  vLeafXmj: TLeafXmjNode;
begin
  BeforeWrite;
  try
    for iGcl := 0 to AGcls.Count - 1 do
    begin
      vGclNode := TGclNode(AGcls.Items[iGcl]);
      WriteGclNode(vGclNode);
    end;

    for iXmj := 0 to AXmjs.Count - 1 do
    begin
      vLeafXmj := TLeafXmjNode(AXmjs.Items[iXmj]);
      WriteLeafXmjNode(vLeafXmj);
    end;
  finally
    AfterWrite;
  end;
end;

procedure TrdpBillsGatherData.WriteGclNode(AGclNode: TGclNode);
var
  Rec: TsdDataRecord;
begin
  Rec := sddGclBills.Add;
  Rec.ValueByName('ID').AsInteger := AGclNode.ID;

  Rec.ValueByName('IndexCode').AsString := AGclNode.IndexCode;
  Rec.ValueByName('B_Code').AsString := AGclNode.B_Code;
  Rec.ValueByName('Name').AsString := AGclNode.Name;
  Rec.ValueByName('Units').AsString := AGclNode.Units;
  Rec.ValueByName('Price').AsFloat := AGclNode.Price;

  Rec.ValueByName('OrgQuantity').AsFloat := AGclNode.OrgQuantity;
  Rec.ValueByName('OrgTotalPrice').AsFloat := AGclNode.OrgTotalPrice;
  Rec.ValueByName('MisQuantity').AsFloat := AGclNode.MisQuantity;
  Rec.ValueByName('MisTotalPrice').AsFloat := AGclNode.MisTotalPrice;
  Rec.ValueByName('OthQuantity').AsFloat := AGclNode.OthQuantity;
  Rec.ValueByName('OthTotalPrice').AsFloat := AGclNode.OthTotalPrice;

  Rec.ValueByName('Quantity').AsFloat := AGclNode.Quantity;
  Rec.ValueByName('TotalPrice').AsFloat := AGclNode.TotalPrice;

  Rec.ValueByName('CurDealQuantity').AsFloat := AGclNode.CurDealQuantity;
  Rec.ValueByName('CurDealTotalPrice').AsFloat := AGclNode.CurDealTotalPrice;
  Rec.ValueByName('CurQcQuantity').AsFloat := AGclNode.CurQcQuantity;
  Rec.ValueByName('CurQcTotalPrice').AsFloat := AGclNode.CurQcTotalPrice;
  Rec.ValueByName('CurGatherQuantity').AsFloat := AGclNode.CurGatherQuantity;
  Rec.ValueByName('CurGatherTotalPrice').AsFloat := AGclNode.CurGatherTotalPrice;

  Rec.ValueByName('PreDealQuantity').AsFloat := AGclNode.PreDealQuantity;
  Rec.ValueByName('PreDealTotalPrice').AsFloat := AGclNode.PreDealTotalPrice;
  Rec.ValueByName('PreQcQuantity').AsFloat := AGclNode.PreQcQuantity;
  Rec.ValueByName('PreQcTotalPrice').AsFloat := AGclNode.PreQcTotalPrice;
  Rec.ValueByName('PreGatherQuantity').AsFloat := AGclNode.PreGatherQuantity;
  Rec.ValueByName('PreGatherTotalPrice').AsFloat := AGclNode.PreGatherTotalPrice;

  Rec.ValueByName('EndDealQuantity').AsFloat := AGclNode.EndDealQuantity;
  Rec.ValueByName('EndDealTotalPrice').AsFloat := AGclNode.EndDealTotalPrice;
  Rec.ValueByName('EndQcQuantity').AsFloat := AGclNode.EndQcQuantity;
  Rec.ValueByName('EndQcTotalPrice').AsFloat := AGclNode.EndQcTotalPrice;
  Rec.ValueByName('EndGatherQuantity').AsFloat := AGclNode.EndGatherQuantity;
  Rec.ValueByName('EndGatherTotalPrice').AsFloat := AGclNode.EndGatherTotalPrice;

  Rec.ValueByName('AddDealQuantity').AsFloat := AGclNode.AddDealQuantity;
  Rec.ValueByName('AddDealTotalPrice').AsFloat := AGclNode.AddDealTotalPrice;
  Rec.ValueByName('AddQcQuantity').AsFloat := AGclNode.AddQcQuantity;
  Rec.ValueByName('AddQcTotalPrice').AsFloat := AGclNode.AddQcTotalPrice;
  Rec.ValueByName('AddGatherQuantity').AsFloat := AGclNode.AddGatherQuantity;
  Rec.ValueByName('AddGatherTotalPrice').AsFloat := AGclNode.AddGatherTotalPrice;

  Rec.ValueByName('DealQuantity').AsFloat := AGclNode.DealQuantity;
  Rec.ValueByName('DealTotalPrice').AsFloat := AGclNode.DealTotalPrice;

  Rec.ValueByName('BGLQuantity').AsFloat := AGclNode.BGLQuantity;
  Rec.ValueByName('BGLTotalPrice').AsFloat := AGclNode.BGLTotalPrice;

  WriteDetailGclNode(AGclNode);
  WriteDetailDealNode(AGclNode);
  WriteDetailBGLNode(AGclNode);
end;

procedure TrdpBillsGatherData.WriteLeafXmjNode(ALeafXmj: TLeafXmjNode);
var
  Rec: TsdDataRecord;
begin
  Rec := sddLeafXmjs.Add;

  Rec.ValueByName('ID').AsInteger := ALeafXmj.ID;
  Rec.ValueByName('Code').AsString := ALeafXmj.XmjCode;
  Rec.ValueByName('Name').AsString := ALeafXmj.XmjName;
  Rec.ValueByName('Units').AsString := ALeafXmj.XmjUnits;

  Rec.ValueByName('NameDanWei').AsString := ALeafXmj.NameDanWei;
  Rec.ValueByName('NameFenBu').AsString := ALeafXmj.NameFenBu;
  Rec.ValueByName('NameFenXiang').AsString := ALeafXmj.NameFenXiang;
  Rec.ValueByName('NameUnit').AsString := ALeafXmj.NameUnit;

  Rec.ValueByName('Peg').AsString := ALeafXmj.Peg;
  if PegFilter.Filter(ALeafXmj.Peg) then
    Rec.ValueByName('PegLength').AsFloat := PegFilter.EndPegNum - PegFilter.BeginPegNum;

  Rec.ValueByName('RelaPosition').AsString := ALeafXmj.Position;
  Rec.ValueByName('DrawingCode').AsString := ALeafXmj.DrawingCode;
end;

end.
