unit rpgDealPayDm;

interface

uses
  SysUtils, Classes, sdProvider, sdDB, ProjGatherDealPay, ADODB;

type
  TrgpDealPayData = class(TDataModule)
    sdpDealPay: TsdADOProvider;
    sddDealPay: TsdDataSet;
  private
    FProjIndex: Integer;
    FProjType: Integer;

    procedure SaveDealPayNodeCalc(ADealPay: TProjGatherDealPayNode; ACalc: TDealPayCalcData);
    procedure SaveDealPayCalc(ADealPays: TProjGatherDealPayList);
    procedure SaveDealPayGather(ADealPays: TProjGatherDealPayList);
  public
    constructor Create(AConnection: TADOConnection);

    procedure SaveSpecialProjDataTo(ADealPays: TProjGatherDealPayList; AProjType: Integer; const ATableName: string);
    procedure SaveProjDataTo(ADealPays: TProjGatherDealPayList; AProjIndex: Integer; const ATableName: string);
    procedure SaveGatherDataTo(ADealPays: TProjGatherDealPayList; const ATableName: string);
  end;

implementation

{$R *.dfm}

{ TrgpDealPayData }

constructor TrgpDealPayData.Create(AConnection: TADOConnection);
begin
  inherited Create(nil);
  sdpDealPay.Connection := AConnection;
end;

procedure TrgpDealPayData.SaveDealPayCalc(
  ADealPays: TProjGatherDealPayList);
var
  iDeal: Integer;
  vDealPay: TProjGatherDealPayNode;
begin
  for iDeal := 0 to ADealPays.Count - 1 do
  begin
    vDealPay := ADealPays.DealPay[iDeal];
    if FProjType = 0 then
      SaveDealPayNodeCalc(vDealPay, vDealPay.Proj[FProjIndex])
    else if FProjType > 0 then
      SaveDealPayNodeCalc(vDealPay, vDealPay.SpecialProj[FProjType-1]);
  end;
end;

procedure TrgpDealPayData.SaveDealPayGather(
  ADealPays: TProjGatherDealPayList);
var
  iDeal: Integer;
  vDealPay: TProjGatherDealPayNode;
begin
  for iDeal := 0 to ADealPays.Count - 1 do
  begin
    vDealPay := ADealPays.DealPay[iDeal];
    SaveDealPayNodeCalc(vDealPay, vDealPay.GatherCalc);
  end;
end;

procedure TrgpDealPayData.SaveDealPayNodeCalc(
  ADealPay: TProjGatherDealPayNode; ACalc: TDealPayCalcData);
var
  Rec: TsdDataRecord;
begin
  Rec := sddDealPay.Add;
  Rec.ValueByName('ID').AsInteger := ADealPay.ID;
  Rec.ValueByName('ProjID').AsInteger := FProjIndex;
  Rec.ValueByName('ProjType').AsInteger := FProjType;

  Rec.ValueByName('Name').AsString := ADealPay.Name;
  Rec.ValueByName('CalcType').AsInteger := ADealPay.CalcType;
  Rec.ValueByName('IsMinus').AsBoolean := ADealPay.IsMinus;

  Rec.ValueByName('SerialNo').AsInteger := ADealPay.SerialNo;
  Rec.ValueByName('LinkSerialNo').AsInteger := ADealPay.LinkSerialNo;

  Rec.ValueByName('CurTotalPrice').AsFloat := ACalc.CurTotalPrice;
  Rec.ValueByName('PreTotalPrice').AsFloat := ACalc.PreTotalPrice;
  Rec.ValueByName('EndTotalPrice').AsFloat := ACalc.EndTotalPrice;
  Rec.ValueByName('AddTotalPrice').AsFloat := ACalc.AddTotalPrice;

  Rec.ValueByName('ZoneTotalPrice').AsFloat := ACalc.ZoneTotalPrice;
end;

procedure TrgpDealPayData.SaveGatherDataTo(
  ADealPays: TProjGatherDealPayList; const ATableName: string);
begin
  FProjIndex := -2;
  FProjType := 0;

  sdpDealPay.TableName := ATableName;
  sddDealPay.Open;
  sddDealPay.BeginUpdate;
  try
    SaveDealPayGather(ADealPays);
  finally
    sddDealPay.EndUpdate;
    sddDealPay.Save;
  end;
end;

procedure TrgpDealPayData.SaveProjDataTo(ADealPays: TProjGatherDealPayList;
  AProjIndex: Integer; const ATableName: string);
begin
  FProjIndex := AProjIndex;
  FProjType := 0;
  
  sdpDealPay.TableName := ATableName;
  sddDealPay.Open;
  sddDealPay.BeginUpdate;
  try
    SaveDealPayCalc(ADealPays);
  finally
    sddDealPay.EndUpdate;
    sddDealPay.Save;
  end;
end;

procedure TrgpDealPayData.SaveSpecialProjDataTo(
  ADealPays: TProjGatherDealPayList; AProjType: Integer;
  const ATableName: string);
begin
  FProjIndex := -3;
  FProjType := AProjType;

  sdpDealPay.TableName := ATableName;
  sddDealPay.Open;
  sddDealPay.BeginUpdate;
  try
    SaveDealPayCalc(ADealPays);
  finally
    sddDealPay.EndUpdate;
    sddDealPay.Save;
  end;
end;

end.
