unit DealPayFinalDm;

interface

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

type
  TDealPayFinalData = class(TDataModule)
    sdpDealPayFinal: TsdADOProvider;
    sddDealPayFinal: TsdDataSet;
  private
    FProjectData: TObject;
  public
    constructor Create(AProjectData: TObject);
    destructor Destroy; override;

    procedure Open(AConnection: TADOConnection);
    procedure Close;
    procedure Save;

    procedure LoadLastestDealPayData;   
  end;

implementation

uses
  ProjectData, DealPaymentDm;

{$R *.dfm}

{ TDealPayFinalData }

procedure TDealPayFinalData.Close;
begin
  sddDealPayFinal.Close;
end;

constructor TDealPayFinalData.Create(AProjectData: TObject);
begin
  inherited Create(nil);
  FProjectData := AProjectData;
end;

destructor TDealPayFinalData.Destroy;
begin
  inherited;
end;

procedure TDealPayFinalData.Open(AConnection: TADOConnection);
begin 
  sdpDealPayFinal.Connection := AConnection;
  sddDealPayFinal.Open;
end;

procedure TDealPayFinalData.Save;
begin
  sddDealPayFinal.Save;
end;

procedure TDealPayFinalData.LoadLastestDealPayData;
var
  iPhase, iRec: Integer;
  vDealRec, vRec, vDealPayRec: TsdDataRecord;
  sTpField: string;
begin
  iPhase := TProjectData(FProjectData).PhaseIndex;
  sTpField := 'TotalPrice' + IntToStr(TProjectData(FProjectData).PhaseData.PhaseProperty.AuditCount);
  for iRec := 0 to TProjectData(FProjectData).DealPaymentData.sddDealPayment.RecordCount - 1 do
  begin
    vDealRec := TProjectData(FProjectData).DealPaymentData.sddDealPayment.Records[iRec];
    vRec := sddDealPayFinal.Locate('PhaseID;DealID', VarArrayOf([iPhase, vDealRec.ValueByName('ID').AsInteger]));
    if not Assigned(vRec) then
    begin
      vRec := sddDealPayFinal.Add;
      vRec.ValueByName('PhaseID').AsInteger := iPhase;
      vRec.ValueByName('DealID').AsInteger := vDealRec.ValueByName('ID').AsInteger;
    end;
    vDealPayRec := TProjectData(FProjectData).PhaseData.PhasePayData.PayRecord(vDealRec.ValueByName('ID').AsInteger);
    if Assigned(vDealPayRec) then
    begin
      vRec.ValueByName('TotalPrice').AsFloat := vDealPayRec.ValueByName(sTpField).AsFloat;
      vRec.ValueByName('EndTotalPrice').AsFloat := vDealPayRec.ValueByName('End' + sTpField).AsFloat;
      vRec.ValueByName('PreTotalPrice').AsFloat := vDealPayRec.ValueByName('Pre' + sTpField).AsFloat;
    end;
  end;
end;

end.
