unit OtherMeasureOnceDm;

interface

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

type
  TOtherMeasureOnceData = class(TDataModule)
    sdpOnce: TsdADOProvider;
    sddOnce: TsdDataSet;
    sdvOnce: TsdDataView;
    procedure sddOnceAfterAddRecord(ARecord: TsdDataRecord);
    procedure sdvOnceSetText(var Text: String; ARecord: TsdDataRecord;
      AValue: TsdValue; AColumn: TsdViewColumn; var Allow: Boolean);
    procedure sdvOnceGetText(var Text: String; ARecord: TsdDataRecord;
      AValue: TsdValue; AColumn: TsdViewColumn; DisplayText: Boolean);
  private
    FProjectData: TObject;

    function GetNewID: Integer;
    function GetNewSerialNo: Integer;
    function GetDecimal: TCalcDecimal;
  public
    constructor Create(AProjectData: TObject);
    destructor Destroy; override;

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

    property Decimal: TCalcDecimal read GetDecimal;
  end;

implementation

uses
  ProjectData, UtilMethods, Variants, DB;

{$R *.dfm}

{ TOtherMeasureOnceData }

procedure TOtherMeasureOnceData.Close;
begin
  sddOnce.Close;
end;

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

destructor TOtherMeasureOnceData.Destroy;
begin
  inherited;
end;

function TOtherMeasureOnceData.GetNewID: Integer;
var
  idx: TsdIndex;
begin
  idx := sddOnce.FindIndex('idxID');
  if idx.RecordCount > 0 then
    Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  else
    Result := 1;
end;

function TOtherMeasureOnceData.GetNewSerialNo: Integer;
var
  idx: TsdIndex;
begin
  idx := sddOnce.FindIndex('idxSerial');
  if idx.RecordCount > 0 then
    Result := idx.Records[idx.RecordCount - 1].ValueByName('SerialNo').AsInteger + 1
  else
    Result := 1;
end;

procedure TOtherMeasureOnceData.Open(AConnection: TADOConnection);
begin
  sdpOnce.Connection := AConnection;
  sddOnce.Open;
  if not Assigned(sddOnce.FindIndex('idxID')) then
    sddOnce.AddIndex('idxID', 'ID');
  if not Assigned(sddOnce.FindIndex('idxSerial')) then
    sddOnce.AddIndex('idxSerial', 'SerialNo');
  sdvOnce.Open;
  sdvOnce.IndexName := 'idxSerial';
end;

procedure TOtherMeasureOnceData.Save;
begin
  sddOnce.Save;
end;

procedure TOtherMeasureOnceData.sddOnceAfterAddRecord(
  ARecord: TsdDataRecord);
begin
  ARecord.ValueByName('ID').AsInteger := GetNewID;
  ARecord.ValueByName('SerialNo').AsInteger := GetNewSerialNo;
  ARecord.ValueByName('CreatePhaseID').AsInteger := TProjectData(FProjectData).PhaseIndex;
  ARecord.ValueByName('CreateStageID').AsInteger := TProjectData(FProjectData).StageIndex;
end;

procedure TOtherMeasureOnceData.sdvOnceSetText(var Text: String;
  ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  var Allow: Boolean);
begin
  if SameText(AColumn.FieldName, 'Code') then
  begin
    if (Text = '') then
    begin
      if (ARecord.ValueByName('Name').AsString = '') then
        DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
      if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
        sddOnce.Remove(ARecord);
    end;
  end
  else if SameText(AColumn.FieldName, 'Name') then
  begin
    if (Text = '') then
    begin
      if (ARecord.ValueByName('Code').AsString = '') then
        DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
      if(ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
        sddOnce.Remove(ARecord);
    end;
  end
  else
  begin
    if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
    begin
      DataSetErrorMessage(Allow, '编号、名称不可同时为空，请先填写编号或名称，再填写其他信息。');
      sddOnce.Remove(AValue.Owner);
    end
    else if Pos('TotalPrice', AColumn.FieldName) > 0 then
      Text := FloatToStr(Decimal.TotalPrice.RoundTo(StrToFloatDef(Text, 0)));
  end;
end;

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

  procedure GetDisplayText;
  begin
    if AValue.DataType = ftFloat then
      if AValue.AsFloat = 0 then
        Text := '';
  end;  

begin
  if DisplayText then
    GetDisplayText;
end;

function TOtherMeasureOnceData.GetDecimal: TCalcDecimal;
begin
  Result := TProjectData(FProjectData).ProjProperties.DecimalManager.Common;
end;

end.
