unit BillsMeasureFme;

interface

uses
  BillsMeasureDm, UtilMethods, BillsClipboard, sdIDTreeCells,
  ColVisibleManager,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZJGrid, ComCtrls, ToolWin, StdCtrls, ExtCtrls, sdGridDBA,
  sdGridTreeDBA, sdDB, ActnList, dxBar, sdIDTree, JimLabels;

type
  TBillsMeasureFrame = class(TFrame)
    pnlExprs: TPanel;
    laEdtExprs: TLabeledEdit;
    pnlBills: TPanel;
    zgBillsMeasure: TZJGrid;
    stdBillsMeasure: TsdGridTreeDBA;
    dxpmBills: TdxBarPopupMenu;
    alBills: TActionList;
    actnCalculateAll: TAction;
    actnExportGridToExcel: TAction;
    actnSetStageBookmark: TAction;
    pnlNodeDetail: TPanel;
    pnlDealProperty: TPanel;
    labDealPropertyTitle: TJimGradLabel;
    zgDealProperty: TZJGrid;
    dxpmBillsCol: TdxBarPopupMenu;
    actnHiddenCol: TAction;
    actnCancelHiddenCol: TAction;
    actnLocateZJJL: TAction;
    procedure dxpmBillsPopup(Sender: TObject);
    procedure zgBillsMeasureMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure laEdtExprsKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure laEdtExprsExit(Sender: TObject);
    procedure actnCalculateAllExecute(Sender: TObject);
    procedure zgBillsMeasureCellGetColor(Sender: TObject; ACoord: TPoint;
      var AColor: TColor);
    procedure actnExportGridToExcelExecute(Sender: TObject);
    procedure zgBillsMeasureCellButtonClick(Sender: TObject; Col,
      Row: Integer);
    procedure zgBillsMeasureCustomPaste(Sender: TObject; ABounds: TRect;
      ASourSheet: TZjSheet);
    procedure zgBillsMeasureKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure actnSetStageBookmarkExecute(Sender: TObject);
    procedure zgBillsMeasureCurrentChanged(Sender: TObject; Col,
      Row: Integer);
    procedure zgDealPropertyCellGetFont(Sender: TObject; ACoord: TPoint;
      AFont: TFont);
    procedure zgDealPropertyCellTextChanged(Sender: TObject; Col,
      Row: Integer);
    procedure zgDealPropertyCellCanEdit(Sender: TObject;
      const ACoord: TPoint; var Allow: Boolean);
    procedure actnSetStageBookmarkUpdate(Sender: TObject);
    procedure dxpmBillsColPopup(Sender: TObject);
    procedure actnHiddenColExecute(Sender: TObject);
    procedure actnCancelHiddenColExecute(Sender: TObject);
    procedure actnLocateZJJLExecute(Sender: TObject);
    procedure actnLocateZJJLUpdate(Sender: TObject);
    procedure actnCalculateAllUpdate(Sender: TObject);
  private
    FBillsMeasureData: TBillsMeasureData;
    FShowPriceChange: Boolean;
    FShowBGLCode: Boolean;
    FShowDesignQuantity: Boolean;
    FShowAddField: Boolean; // For Inner Test
    FShowPMField: Boolean; // For Inner Test
    FOnAfterSetBookmark: TBookmarkRefreshEvent;
    FShowAlias: Boolean;
    FShowApprovalCode: Boolean;
    FShowIsGather: Boolean;

    FColVisibleManager: TBM_ColVisibleManager;
    FOnLocateZJJL: TLocateZJJLEvent;

    procedure ExpandMouseDown(AGridCell: TzjCell);

    function GridColToDBACol(AGridCol: Integer): Integer;
    procedure ShowGridCols(AShow: Boolean; ALeft, ARight: Integer);

    function CheckExprsColumn: Boolean;
    function CheckMemoStrColumn: Boolean;

    procedure SetColumnVisible(const AColumn: string; AVisible: Boolean);
    procedure SetAddFieldVisible(AValue: Boolean);
    procedure SetPMFieldVisible(AValue: Boolean);

    procedure LoadDealProperty(ARec: TsdDataRecord);

    procedure BeginExpandNode;
    procedure EndExpandNode;

    procedure ResetPhaseDataReadOnly(AReadOnly: Boolean);
    procedure ResetBaseDataReadOnly(AReadOnly: Boolean);
    procedure ResetAllowInsert(AAllow: Boolean);

    procedure SetShowPriceChange(const Value: Boolean);
    procedure SetShowBGLCode(const Value: Boolean);
    procedure SetShowDesignQuantity(const Value: Boolean);
    procedure SetShowAlias(const Value: Boolean);
    procedure SetShowApprovalCode(const Value: Boolean);
    procedure SetShowIsGather(const Value: Boolean);
  public
    constructor Create(AProjectFrame: TFrame; ABillsMeasureData: TBillsMeasureData);
    destructor Destroy; override;

    procedure ExpandNodeTo(ALevel: Integer);
    procedure ExpandXmjNode;
    procedure ExpandCurPhase;

    procedure RefreshPhase_Stage;

    property ShowPriceChange: Boolean read FShowPriceChange write SetShowPriceChange;
    property ShowBGLCode: Boolean read FShowBGLCode write SetShowBGLCode;
    property ShowDesignQuantity: Boolean read FShowDesignQuantity write SetShowDesignQuantity;
    property ShowAlias: Boolean read FShowAlias write SetShowAlias;
    property ShowApprovalCode: Boolean read FShowApprovalCode write SetShowApprovalCode;
    property ShowIsGather: Boolean read FShowIsGather write SetShowIsGather;

    property OnAfterSetBookmark: TBookmarkRefreshEvent read FOnAfterSetBookmark write FOnAfterSetBookmark;
    property OnLocateZJJL: TLocateZJJLEvent read FOnLocateZJJL write FOnLocateZJJL;
    property BillsMeasureData: TBillsMeasureData read FBillsMeasureData;
  end;

implementation

{$R *.dfm}

uses
  MainFrm, ProjectFme, ProjectData, ExportExcel, BGLDm, BGLSelectFrm,
  Types, ZhAPI, BillsTree, mDataRecord, ConditionalDefines;

{ TBillsFrame }

constructor TBillsMeasureFrame.Create(AProjectFrame: TFrame;
  ABillsMeasureData: TBillsMeasureData);
begin
  inherited Create(AProjectFrame);
  FBillsMeasureData := ABillsMeasureData;
  stdBillsMeasure.IDTree := FBillsMeasureData.BillsMeasureTree;
  zgBillsMeasure.OnExpandMouseDown := ExpandMouseDown;

  FColVisibleManager := TBM_ColVisibleManager.Create(stdBillsMeasure);
  if not _IsDebugView then
    zgBillsMeasure.OnKeyDown := nil;
end;

destructor TBillsMeasureFrame.Destroy;
begin
  FColVisibleManager.Free;
  inherited;
end;

procedure TBillsMeasureFrame.dxpmBillsPopup(Sender: TObject);
begin
  SetDxBtnAction(actnCalculateAll, MainForm.dxbtnCalculateAll);
  SetDxBtnAction(actnExportGridToExcel, MainForm.dxbtnExportGridToExcel);
  SetDxBtnAction(actnSetStageBookmark, MainForm.dxbtnSetBookmark);
  SetDxBtnAction(actnLocateZJJL, MainForm.dxbtnLocateZJJL);
end;

procedure TBillsMeasureFrame.zgBillsMeasureMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  vCol: TsdGridColumn;
  vViewCol: TsdViewColumn;
begin
  if Button = mbRight then
  begin
    if _IsDebugView and (zgBillsMeasure.Selection.SelectType = stCol) and (Y < (zgBillsMeasure.RowHeights[0] + zgBillsMeasure.RowHeights[1])) then
      dxpmBillsCol.PopupFromCursorPos
    else
      dxpmBills.PopupFromCursorPos;
  end
  else
  begin
    if CheckExprsColumn or CheckMemoStrColumn then
      laEdtExprs.Text := zgBillsMeasure.CurCell.EditText
    else
      laEdtExprs.Text := '';
    stdBillsMeasure.FindColumn(zgBillsMeasure.CurCol, vCol);
    laEdtExprs.ReadOnly := vCol.ReadOnly;
  end;
end;

procedure TBillsMeasureFrame.SetShowPriceChange(const Value: Boolean);
begin
  FShowPriceChange := Value;
  FColVisibleManager.ShowPriceChange(FShowPriceChange);
end;

procedure TBillsMeasureFrame.laEdtExprsKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_Return then
  begin
    zgBillsMeasure.SetFocus;
    if not TLabeledEdit(Sender).ReadOnly then
      if CheckExprsColumn or CheckMemoStrColumn then
        zgBillsMeasure.CurCell.Text := laEdtExprs.Text;
  end;
end;

procedure TBillsMeasureFrame.laEdtExprsExit(Sender: TObject);
begin
  if not TLabeledEdit(Sender).ReadOnly then
    if CheckExprsColumn or CheckMemoStrColumn then
      zgBillsMeasure.CurCell.Text := laEdtExprs.Text;
end;

procedure TBillsMeasureFrame.actnCalculateAllExecute(Sender: TObject);
begin
  TProjectData(FBillsMeasureData.ProjectData).CalculateAll;
end;

procedure TBillsMeasureFrame.zgBillsMeasureCellGetColor(Sender: TObject; ACoord: TPoint;
  var AColor: TColor);
var
  stnNode: TMeasureBillsIDTreeNode;
  iCreatePhaseID: Integer;
  StageRec: TStageRecord;
  fDiffer, fCompare: Double;
begin
  AColor := clWindow;
  if ACoord.Y > stdBillsMeasure.IDTree.Count + 1 then Exit;
  stnNode := TMeasureBillsIDTreeNode(stdBillsMeasure.IDTree.Items[ACoord.Y - 2]);
  if not Assigned(stnNode) then Exit;
  iCreatePhaseID := stnNode.Rec.CreatePhaseID.AsInteger;
  // 根据层次底色不同
  if stnNode.ParentID = 1 then
    AColor := $00FBCAC4
  else if (stnNode.Rec.B_Code.AsString = '') and (stnNode.Level > 0) then
    AColor := $00F9E8DF;
  // 书签
  if TProjectData(FBillsMeasureData.ProjectData).ProjProperties.PhaseCount > 0 then
  begin
    StageRec := stnNode.StageRec;
    if Assigned(StageRec) and StageRec.HasBookmark.AsBoolean then
      AColor := $00CFE2F9;
  end;
  // 根据节点创建期数底色不同
  if iCreatePhaseID > 0 then
  begin
    // 当前期不存在节点，底色为灰色提示用户
    if iCreatePhaseID > TProjectData(FBillsMeasureData.ProjectData).PhaseIndex then
      AColor := $00D5D5D5
    // 当前期新增节点，底色为黄色提示用户
    else if iCreatePhaseID = TProjectData(FBillsMeasureData.ProjectData).PhaseIndex then
      AColor := $00A7FDFD;
  end;
  // 叶子节点，累计合同计量超过0号台账，整行数据的底色变为暗红提示用户
  if TProjectData(FBillsMeasureData.ProjectData).ProjProperties.ShowOverRange and not stnNode.HasChildren then
  begin
    with stnNode.Rec do
    begin
      if CalcType.AsInteger = 0 then
      begin
        fDiffer := QuantityRoundTo(AddDealQuantity.AsFloat - Quantity.AsFloat);
        fCompare := TProjectData(FBillsMeasureData.ProjectData).ProjProperties.DecimalManager.Common.Quantity.CompareValue;
      end
      else
      begin
        fDiffer := TotalPriceRoundTo(AddDealTotalPrice.AsFloat - TotalPrice.AsFloat);
        fCompare := TProjectData(FBillsMeasureData.ProjectData).ProjProperties.DecimalManager.Common.TotalPrice.CompareValue;
      end;
      if fDiffer > fCompare then
        AColor := $00646AFE;
    end;
  end;
end;

procedure TBillsMeasureFrame.SetColumnVisible(const AColumn: string;
  AVisible: Boolean);
begin
  if AVisible then
    stdBillsMeasure.Column(AColumn).Width := 60
  else
    stdBillsMeasure.Column(AColumn).Width := 0;
end;

procedure TBillsMeasureFrame.SetShowBGLCode(const Value: Boolean);
begin
  FShowBGLCode := Value;
  FColVisibleManager.ShowBGLCode(FShowBGLCode);
end;

procedure TBillsMeasureFrame.actnExportGridToExcelExecute(Sender: TObject);
var
  sFileName: string;
  ExcelExportor: TExcelExportor;
begin
  if SaveExcelFile(sFileName) then
  begin
    ExcelExportor := TExcelExportor.Create;
    try
      ExcelExportor.ExportToFile(zgBillsMeasure, sFileName);
    finally
      ExcelExportor.Free;
    end;
  end;
end;

function TBillsMeasureFrame.CheckExprsColumn: Boolean;
var
  iCol: Integer;
begin
  iCol := zgBillsMeasure.CurCol - zgBillsMeasure.FixedColCount;
  Result := (iCol = stdBillsMeasure.VisibleCol('CurDealQuantity'))
         or (iCol = stdBillsMeasure.VisibleCol('CurDealTotalPrice'))
         or (iCol = stdBillsMeasure.VisibleCol('CurQcQuantity'))
         or (iCol = stdBillsMeasure.VisibleCol('CurQcTotalPrice'))
         or (iCol = stdBillsMeasure.VisibleCol('CurPcQuantity'))
         or (iCol = stdBillsMeasure.VisibleCol('CurPcTotalPrice'));
end;

procedure TBillsMeasureFrame.zgBillsMeasureCellButtonClick(Sender: TObject;
  Col, Row: Integer);

  procedure SelectAndUpdateBGL(const AType, AField: string; ANode: TsdIDTreeNode);
  var
    vOrgBGL, vNewBGL: TBGLSelectInfo;
    StageRec: TStageRecord;
  begin
    if not Assigned(ANode) then Exit;
    StageRec := TMeasureBillsIDTreeNode(ANode).StageRec;
    try
      if Assigned(StageRec) then
      begin
        vOrgBGL := TBGLSelectInfo.Create(ANode.Rec,
          StageRec.ValueByName(AType + AField).AsFloat, True);
        vOrgBGL.MergedCode := StageRec.ValueByName(AType + 'BGLCode').AsString;
        vOrgBGL.MergedNum := StageRec.ValueByName(AType + 'BGLNum').AsString;
      end
      else
        vOrgBGL := TBGLSelectInfo.Create(ANode.Rec, 0, True);
      vNewBGL := TBGLSelectInfo.Create(ANode.Rec, 0, False);
      if SelectBGLAndBGNum(vOrgBGL, vNewBGL, FBillsMeasureData.ProjectData) then
      begin
        if not Assigned(StageRec) then
        begin
          StageRec := FBillsMeasureData.StageData.AddStageRecord(ANode.ID);
          TMeasureBillsIDTreeNode(ANode).StageRec := StageRec;
        end;
        StageRec.ValueByName(AType + AField).AsFloat := vNewBGL.TotalNum;
        StageRec.ValueByName(AType + 'BGLCode').AsString := vNewBGL.MergedCode;
        StageRec.ValueByName(AType + 'BGLNum').AsString := vNewBGL.MergedNum;
        FBillsMeasureData.StageData.UpdateBGLInfo(StageRec, AType);
        FBillsMeasureData.UpdateBGLInfo(ANode.ID, StageRec, AType);
        TProjectData(FBillsMeasureData.ProjectData).BGLData.ApplyBGL(vOrgBGL, vNewBGL);
        // 重新计算合同支付所有数据
        TProjectData(FBillsMeasureData.ProjectData).PhaseData.PhasePayData.CalculateAll;
      end;
    finally
      vOrgBGL.Free;
      vNewBGL.Free;
    end;
  end;

  procedure GetTypeAndField(var AType, AField: string);
  begin
    if (Col = stdBillsMeasure.VisibleCol('CurQcQuantity') + 1) then
    begin
      AType := 'Qc';
      AField := 'Quantity';
    end
    else if (Col = stdBillsMeasure.VisibleCol('CurPcQuantity') + 1) then
    begin
      AType := 'Pc';
      AField := 'Quantity';
    end;
  end;

var
  stnCurNode: TBillsIDTreeNode;
  sType, sField: string;
begin
  stnCurNode := TBillsIDTreeNode(stdBillsMeasure.IDTree.Selected);
  if stnCurNode.HasChildren then Exit;
  with TProjectData(FBillsMeasureData.ProjectData) do
    if PhaseData.StageDataReadOnly then Exit;
  GetTypeAndField(sType, sField);
  if (sType = '') or (sField = '') then Exit;
  if stnCurNode.Rec.CalcType.AsInteger = 1 then
  begin
    WarningMessage('该清单不可输入数量单价，如需使用数量×单价计算，请先清空所有直接输入的金额！');
    Exit;
  end;
  SelectAndUpdateBGL(sType, sField, stnCurNode);
end;

procedure TBillsMeasureFrame.zgBillsMeasureCustomPaste(Sender: TObject;
  ABounds: TRect; ASourSheet: TZjSheet);
var
  iRow, iCol: Integer;
begin
  for iRow := ABounds.Top to ABounds.Bottom - 1 do
  begin
    if not zgBillsMeasure.RowVisible[iRow] then Continue;
    for iCol := ABounds.Left to ABounds.Right - 1 do
      with TZJGrid(Sender).Cells[iCol, iRow] do
        if CanEdit then Text := ASourSheet.Values[iCol - ABounds.Left, iRow - ABounds.Top];
  end;
end;

procedure TBillsMeasureFrame.SetShowDesignQuantity(const Value: Boolean);
begin
  FShowDesignQuantity := Value;
  FColVisibleManager.ShowDesign(FShowDesignQuantity);
end;

procedure TBillsMeasureFrame.SetAddFieldVisible(AValue: Boolean);
begin
  FShowAddField := AValue;
  stdBillsMeasure.Column('AddDealQuantity').Visible := AValue;
  stdBillsMeasure.Column('AddDealTotalPrice').Visible := AValue;
  stdBillsMeasure.Column('AddQcQuantity').Visible := AValue;
  stdBillsMeasure.Column('AddQcTotalPrice').Visible := AValue;
  stdBillsMeasure.Column('AddPcQuantity').Visible := AValue;
  stdBillsMeasure.Column('AddPcTotalPrice').Visible := AValue;
  stdBillsMeasure.Column('AddGatherQuantity').Visible := AValue;
  stdBillsMeasure.Column('AddGatherTotalPrice').Visible := AValue;
end;

procedure TBillsMeasureFrame.zgBillsMeasureKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  // For Inner Test
  if (ssCtrl in Shift) and (ssShift in Shift) and (ssAlt in Shift)
      and (Key in [65, 97]) // 'a', 'A'
      and (zgBillsMeasure.CurCol = 4) then
    SetAddFieldVisible(not FShowAddField);

  if (ssCtrl in Shift) and (ssShift in Shift) and (ssAlt in Shift)
      and (Key in [80, 112]) // 'p', 'P' - Parent
      and (zgBillsMeasure.CurCol = 5) then
  begin
    FBillsMeasureData.ShowParentData := not FBillsMeasureData.ShowParentData;
    zgBillsMeasure.Invalidate;
  end;

  if (ssCtrl in Shift) and (ssShift in Shift) and (ssAlt in Shift)
      and (Key in [77, 109]) // 'm', 'M' - PriceMargin
      and (zgBillsMeasure.CurCol = 5) then
  begin
    SetPMFieldVisible(not FShowPMField);
  end;

  if (ssCtrl in Shift) and (ssShift in Shift) and (ssAlt in Shift)
      and (Key in [67, 99]) then // 'c', 'C'
    actnCalculateAll.Execute;

  if (ssCtrl in Shift) and (ssShift in Shift) and (ssAlt in Shift)
      and (Key in [66, 98]) then
    ShowIsGather := not ShowIsGather;
end;

procedure TBillsMeasureFrame.actnSetStageBookmarkExecute(Sender: TObject);
var
  vNode: TMeasureBillsIDTreeNode;
  StageRec: TStageRecord;
begin
  vNode := TMeasureBillsIDTreeNode(FBillsMeasureData.BillsMeasureTree.Selected);
  StageRec := vNode.StageRec;
  if not Assigned(StageRec) then
  begin
    StageRec := FBillsMeasureData.StageData.AddStageRecord(stdBillsMeasure.IDTree.Selected.ID);
    vNode.StageRec := StageRec;
  end;

  if StageRec.HasBookMark.AsBoolean then
  begin
    StageRec.HasBookMark.AsBoolean := False;
    StageRec.MarkMemo.AsString := '';
  end
  else
    StageRec.HasBookMark.AsBoolean := True;
  TProjectData(FBillsMeasureData.ProjectData).BillsBookmarkData.RefreshStageBookmark;
  if Assigned(FOnAfterSetBookmark) then
    FOnAfterSetBookmark(StageRec.HasBookMark.AsBoolean);
  zgBillsMeasure.InvalidateRow(zgBillsMeasure.CurRow);
end;

procedure TBillsMeasureFrame.zgBillsMeasureCurrentChanged(Sender: TObject;
  Col, Row: Integer);
var
  stnNode: TsdIDTreeNode;
  bShowDealProperty: Boolean;
begin
  stnNode := stdBillsMeasure.IDTree.Selected;
  bShowDealProperty := False;
  if Assigned(stnNode) then
  begin
    bShowDealProperty := stnNode.Rec.ValueByName('DealCode').AsString <> '';
    pnlNodeDetail.Visible := bShowDealProperty;
    LoadDealProperty(stnNode.Rec);
  end;
end;

procedure TBillsMeasureFrame.LoadDealProperty(ARec: TsdDataRecord);
var
  fHasPay, fNotPay: Double;
begin
  zgDealProperty.OnCellTextChanged := nil;
  zgDealProperty.Cells[0, 0].Text := '合同类别';
  zgDealProperty.Cells[0, 1].Text := ARec.ValueByName('DealType').AsString;
  zgDealProperty.Cells[0, 1].Align := gaCenterLeft;
  zgDealProperty.ColWidths[0] := 80;

  zgDealProperty.Cells[1, 0].Text := '累计应扣款';
  zgDealProperty.Cells[1, 1].Text := ARec.ValueByName('AddCutTotalPrice').AsString;
  zgDealProperty.Cells[1, 1].Align := gaCenterRight;
  zgDealProperty.ColWidths[1] := 80;

  zgDealProperty.Cells[2, 0].Text := '累计应支付';
  fHasPay := TotalPriceRoundTo(
     ARec.ValueByName('AddGatherTotalPrice').AsFloat - ARec.ValueByName('AddCutTotalPrice').AsFloat);
  zgDealProperty.Cells[2, 1].Text := FloatToStr(fHasPay);
  zgDealProperty.Cells[2, 1].Align := gaCenterRight;
  zgDealProperty.ColWidths[2] := 80;

  zgDealProperty.Cells[3, 0].Text := '累计已支付';
  zgDealProperty.Cells[3, 1].Text := ARec.ValueByName('AddPayTotalPrice').AsString;
  zgDealProperty.Cells[3, 1].Align := gaCenterRight;
  zgDealProperty.ColWidths[3] := 80;

  zgDealProperty.Cells[4, 0].Text := '待支付';
  fNotPay := TotalPriceRoundTo(fHasPay - ARec.ValueByName('AddPayTotalPrice').AsFloat);
  zgDealProperty.Cells[4, 1].Text := FloatToStr(fNotPay);
  zgDealProperty.Cells[4, 1].Align := gaCenterRight;
  zgDealProperty.ColWidths[4] := 80;
  zgDealProperty.OnCellTextChanged := zgDealPropertyCellTextChanged;
end;

procedure TBillsMeasureFrame.zgDealPropertyCellGetFont(Sender: TObject;
  ACoord: TPoint; AFont: TFont);
begin
  if ((ACoord.X = 2) or (ACoord.X = 4)) and (ACoord.Y = 1) then
    AFont.Color := clGrayText;
end;

procedure TBillsMeasureFrame.zgDealPropertyCellTextChanged(Sender: TObject;
  Col, Row: Integer);

  procedure ModifyField(ARec: TsdDataRecord; const AField: string; ACell: TzjCell);
  var
    fValue, fHasPay, fNotPay: Double;
  begin
    if (ACell.Text = '') or TryStrToFloat(ACell.Text, fValue) then
    begin
      ARec.ValueByName(AField).AsString := ACell.Text;
      // 更新显示应支付&待支付数据
      fHasPay := TotalPriceRoundTo(
          ARec.ValueByName('AddGatherTotalPrice').AsFloat - ARec.ValueByName('AddCutTotalPrice').AsFloat);
      zgDealProperty.Cells[2, 1].Text := FloatToStr(fHasPay);
      fNotPay := TotalPriceRoundTo(fHasPay - ARec.ValueByName('AddPayTotalPrice').AsFloat);
      zgDealProperty.Cells[4, 1].Text := FloatToStr(fNotPay);
    end
    else
    begin
      ErrorMessage('只允许输入数字！！');
      ACell.Text := ARec.ValueByName(AField).AsString;
    end;
  end;

var
  Rec: TsdDataRecord;
begin
  Rec := stdBillsMeasure.IDTree.Selected.Rec;
  case Col of
    0: Rec.ValueByName('DealType').AsString := zgDealProperty.Cells[Col, Row].Text;
    1: ModifyField(Rec, 'AddCutTotalPrice', zgDealProperty.Cells[Col, Row]);
    3: ModifyField(Rec, 'AddPayTotalPrice', zgDealProperty.Cells[Col, Row]);
  end;
end;

procedure TBillsMeasureFrame.zgDealPropertyCellCanEdit(Sender: TObject;
  const ACoord: TPoint; var Allow: Boolean);
begin
  Allow := (ACoord.X = 0) or (ACoord.X = 1) or (ACoord.X = 3);
end;

procedure TBillsMeasureFrame.SetShowAlias(const Value: Boolean);
begin
  FShowAlias := Value;
  FColVisibleManager.ShowAlias(FShowAlias);
end;

procedure TBillsMeasureFrame.actnSetStageBookmarkUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := TProjectData(FBillsMeasureData.ProjectData).ProjProperties.PhaseCount > 0;
end;

procedure TBillsMeasureFrame.ExpandNodeTo(ALevel: Integer);
begin
  BeginExpandNode;
  try
    FBillsMeasureData.ExpandNodeTo(ALevel);
  finally
    EndExpandNode;
  end;
end;

procedure TBillsMeasureFrame.ExpandXmjNode;
begin
  BeginExpandNode;
  try
    FBillsMeasureData.ExpandXmjNode;
  finally
    EndExpandNode;
  end;
end;

procedure TBillsMeasureFrame.BeginExpandNode;
begin
  zgBillsMeasure.BeginUpdate;
  zgBillsMeasure.OnCellGetColor := nil;
  BeginUpdateWindow(zgBillsMeasure.Handle);
  stdBillsMeasure.DisableControl;
end;

procedure TBillsMeasureFrame.EndExpandNode;
begin
  stdBillsMeasure.EnableControl;
  EndUpdateWindow(zgBillsMeasure.Handle);
  zgBillsMeasure.OnCellGetColor := zgBillsMeasureCellGetColor;
  zgBillsMeasure.EndUpdate;
end;

procedure TBillsMeasureFrame.ExpandCurPhase;
begin
  BeginExpandNode;
  try
    FBillsMeasureData.ExpandCurPhase;
  finally
    EndExpandNode;
  end;
end;

procedure TBillsMeasureFrame.RefreshPhase_Stage;
begin
  with TProjectData(FBillsMeasureData.ProjectData) do
  begin
    ResetPhaseDataReadOnly(StageDataReadOnly);
    ResetBaseDataReadOnly(BaseDataReadOnly);
    ResetAllowInsert(AllowInsert);
  end;
  zgBillsMeasure.Invalidate;
end;

procedure TBillsMeasureFrame.ResetPhaseDataReadOnly(AReadOnly: Boolean);
begin
  stdBillsMeasure.Column('CurDealQuantity').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('CurDealTotalPrice').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('CurQcQuantity').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('CurPcQuantity').ReadOnly := AReadOnly;
end;

procedure TBillsMeasureFrame.ResetBaseDataReadOnly(AReadOnly: Boolean);
begin
  stdBillsMeasure.Column('Code').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('B_Code').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('Name').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('Units').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('Price').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('NewPrice').ReadOnly := AReadOnly;
  stdBillsMeasure.Column('DrawingCode').ReadOnly := AReadOnly;
end;

procedure TBillsMeasureFrame.ResetAllowInsert(AAllow: Boolean);
begin
  if AAllow then
    stdBillsMeasure.Options := stdBillsMeasure.Options + [aoAllowInsert]
  else
    stdBillsMeasure.Options := stdBillsMeasure.Options - [aoAllowInsert];
end;

procedure TBillsMeasureFrame.SetPMFieldVisible(AValue: Boolean);
begin
  FShowAddField := AValue;
  stdBillsMeasure.Column('PM_PreTotalPrice').Visible := AValue;
  stdBillsMeasure.Column('PM_TotalPrice').Visible := AValue;
  stdBillsMeasure.Column('PM_AddTotalPrice').Visible := AValue;
end;

procedure TBillsMeasureFrame.ExpandMouseDown(AGridCell: TzjCell);

  procedure CommonExpand(vNode: TsdIDTreeNode);
  begin
    AGridCell.Grid.BeginUpdate;
    vNode.Expanded := not vNode.Expanded;
    AGridCell.Grid.EndUpdate;
    AGridCell.Grid.InvalidateView(AGridCell.ViewRect);
  end;

  procedure QuikExpand(vNode: TsdIDTreeNode);
  begin
    Screen.Cursor := crHourGlass;
    BeginExpandNode;
    try
      vNode.Expanded := not vNode.Expanded;
    finally
      EndExpandNode;
      Screen.Cursor := crDefault;
    end;
  end;

var
  stnNode: TsdIDTreeNode;
begin
  stnNode := TsdIDTreeCell(AGridCell).TreeNode;
  if stnNode.PosterityCount > 10000 then
    QuikExpand(stnNode)
  else
    CommonExpand(stnNode);
end;

procedure TBillsMeasureFrame.dxpmBillsColPopup(Sender: TObject);
begin
  SetDxBtnAction(actnHiddenCol, MainForm.dxbtnHidden);
  SetDxBtnAction(actnCancelHiddenCol, MainForm.dxbtnCancelHidden);
end;

procedure TBillsMeasureFrame.actnHiddenColExecute(Sender: TObject);
begin
  ShowGridCols(False, zgBillsMeasure.Selection.Left, zgBillsMeasure.Selection.Right-1);
end;

function TBillsMeasureFrame.GridColToDBACol(AGridCol: Integer): Integer;
var
  iCol, iVisibleCount: Integer;
begin
  Result := -1;
  if AGridCol > stdBillsMeasure.VisibleColCount then Exit;

  iVisibleCount := 0;
  for iCol := 0 to stdBillsMeasure.Columns.Count - 1 do
  begin
    if stdBillsMeasure.Columns.Items[iCol].Visible then
    begin
      Inc(iVisibleCount);
      if iVisibleCount = AGridCol then
      begin
        Result := iCol;
        Break;
      end;
    end;
  end;
end;

procedure TBillsMeasureFrame.actnCancelHiddenColExecute(Sender: TObject);
begin
  ShowGridCols(True, zgBillsMeasure.Selection.Left, zgBillsMeasure.Selection.Right-1);
end;

procedure TBillsMeasureFrame.ShowGridCols(AShow: Boolean; ALeft,
  ARight: Integer);
var
  iCol, iBegin, iEnd: Integer;
begin
  iBegin := GridColToDBACol(ALeft);
  iEnd := GridColToDBACol(ARight);
  FColVisibleManager.ShowGridCol(AShow, iBegin, iEnd);
end;

function TBillsMeasureFrame.CheckMemoStrColumn: Boolean;
var
  iCol: Integer;
begin
  iCol := zgBillsMeasure.CurCol - zgBillsMeasure.FixedColCount;
  Result := (iCol = stdBillsMeasure.VisibleCol('MemoStr'));
end;

procedure TBillsMeasureFrame.actnLocateZJJLExecute(Sender: TObject);
var
  vNode: TsdIDTreeNode;
begin
  vNode := FBillsMeasureData.FindNodeWithZJJL(FBillsMeasureData.BillsMeasureTree.Selected);
  if Assigned(vNode) then
  begin
    if Assigned(OnLocateZJJL) then
      FOnLocateZJJL(vNode.ID);
  end
  else
    WarningMessage('该节点及相关节点，均无中间计量数据');
end;

procedure TBillsMeasureFrame.actnLocateZJJLUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := TProjectData(FBillsMeasureData.ProjectData).PhaseData.Active;
end;

procedure TBillsMeasureFrame.SetShowApprovalCode(const Value: Boolean);
begin
  FShowApprovalCode := Value;
  FColVisibleManager.ShowApprovalCode(FShowApprovalCode);
end;

procedure TBillsMeasureFrame.SetShowIsGather(const Value: Boolean);
begin
  FShowIsGather := Value;
  FColVisibleManager.ShowIsGather(FShowIsGather);
end;

procedure TBillsMeasureFrame.actnCalculateAllUpdate(Sender: TObject);
begin
  with TProjectData(FBillsMeasureData.ProjectData) do
  TAction(Sender).Enabled := PhaseData.Active and not PhaseData.StageDataReadOnly;
end;

end.
