unit ZJJLFme;

interface

uses
  ZJJLDm,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, DBCtrls, Mask, JimLabels, DB, ComCtrls,
  ToolWin, ActnList, ZjGridDBA, ZJGrid, dxBar, sdGridDBA, sdDB;

type
  TRowIndex = (riCurGatherMeasure, riBGLCode, riPegName, riBeginPeg, riEndPeg, riFBFXName, riUnitName, riDrawingCode,
    riFormulaMemoTitle, riFormulaMemoValue, riRelaFileTitle, riRelaFileValue);
const
  RowFields: array [TRowIndex] of string = ('', 'BGLCode', 'PegName', 'BeginPeg', 'EndPeg', 'FBFXName', 'UnitName', 'DrawingCode',
    '', 'FormulaMemo', '', 'RelaFile');
  RowFormats: array [TRowIndex] of string = ('本期计量%s：%s', '变更令号：', '部位：', '起始桩号：', '终止桩号：', '分部分项工程：', '计量单元：', '图号：',
    '计算式说明：', '', '计算草图几何尺寸：', '');
type
  TZJJLFrame = class(TFrame)
    labTitle: TJimGradLabel;
    alBGL: TActionList;
    actnGenerate: TAction;
    pnlTop: TPanel;
    pnlButton: TPanel;
    tobaTop: TToolBar;
    tobtnGenerate: TToolButton;
    lePreText: TLabeledEdit;
    pnlZJJL: TPanel;
    zgZJJL: TZJGrid;
    pnlListTitle: TPanel;
    pnlListBar: TPanel;
    lblListTitle: TLabel;
    pnlZJJLProperty: TPanel;
    pnlDataTitle: TPanel;
    lblDataTiel: TLabel;
    pnlDataBar: TPanel;
    dxpmZJJL: TdxBarPopupMenu;
    actnLocateBills: TAction;
    dxpmAutoGenerate: TdxBarPopupMenu;
    actnGenerateFxZJJL: TAction;
    actnGenerateGclZJJL: TAction;
    saZJJL: TsdGridDBA;
    zgDetailInfo: TZJGrid;
    actnGenerateGclGatherZJJL: TAction;
    procedure actnGenerateExecute(Sender: TObject);
    procedure lePreTextExit(Sender: TObject);
    procedure zgZJJLMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure dxpmZJJLPopup(Sender: TObject);
    procedure actnLocateBillsExecute(Sender: TObject);
    procedure actnLocateBillsUpdate(Sender: TObject);
    procedure dxpmAutoGeneratePopup(Sender: TObject);
    procedure actnGenerateFxZJJLExecute(Sender: TObject);
    procedure actnGenerateGclZJJLExecute(Sender: TObject);
    procedure actnGenerateUpdate(Sender: TObject);
    procedure zgDetailInfoGetCellText(Sender: TObject;
      const ACoord: TPoint; var Value: String; DisplayText: Boolean);
    procedure zgDetailInfoSetCellText(Sender: TObject;
      const ACoord: TPoint; var Value: String; DisplayText: Boolean);
    procedure zgDetailInfoCellCanEdit(Sender: TObject;
      const ACoord: TPoint; var Allow: Boolean);
    procedure actnGenerateGclGatherZJJLExecute(Sender: TObject);
  private
    FZJJLData: TZJJLData;
    FDataReadOnly: Boolean;
    procedure SetDataReadOnly(const Value: Boolean);

    procedure RefreshTitle;
    procedure RefreshDetailGrid;
    procedure GenerateZJJL;

    procedure InitDetailGrid;
  public
    constructor Create(AParent: TFrame; AZJJLData: TZJJLData);
    destructor Destroy; override;

    procedure ResetFrameLink(AZJJLData: TZJJLData);

    property DataReadOnly: Boolean read FDataReadOnly write SetDataReadOnly;
  end;

implementation

uses
  MergeTextFrm, PhaseData, MainFrm, ProjectData, UtilMethods,
  PhaseProperty, Math;

{$R *.dfm}

{ TZJJLFrame }

constructor TZJJLFrame.Create(AParent: TFrame; AZJJLData: TZJJLData);
begin
  inherited Create(AParent);
  zgDetailInfo.RowCount := Integer(riRelaFileValue) + 1;
  ResetFrameLink(AZJJLData);
  if TPhaseData(FZJJLData.PhaseData).Active then
  begin
    with TPhaseData(FZJJLData.PhaseData).PhaseProperty do
      lePreText.Text := ZJJLPreText;
  end;
  RefreshTitle;
  FZJJLData.RefreshDetailGrid := RefreshDetailGrid;
end;

destructor TZJJLFrame.Destroy;
begin
  inherited;
end;

procedure TZJJLFrame.ResetFrameLink(AZJJLData: TZJJLData);
begin
  FZJJLData := AZJJLData;
  saZJJL.DataView := FZJJLData.sdvZJJL;
  InitDetailGrid;
  if (saZJJL.DataView.RecordCount > 0) then
    saZJJL.DataView.LocateInControl(saZJJL.DataView.Records[0]);
  FZJJLData.RefreshDetailGrid := RefreshDetailGrid;
  zgDetailInfo.Invalidate;
end;

procedure TZJJLFrame.actnGenerateExecute(Sender: TObject);
var
  bIsRefer: Boolean; 
  P: TPoint;
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    bIsRefer := ProjProperties.AuditStatus = 0;
  if bIsRefer then
  begin
    P := pnlButton.ClientToScreen(Point(tobtnGenerate.Left, tobtnGenerate.Top + tobtnGenerate.Height));
    dxpmAutoGenerate.Popup(P.X, P.Y);
  end
  else
    GenerateZJJL;
end;

procedure TZJJLFrame.lePreTextExit(Sender: TObject);
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
  begin
    ProjProperties.ZJJLPreText := lePreText.Text;
    PhaseData.PhaseProperty.ZJJLPreText := lePreText.Text;
  end;
end;

procedure TZJJLFrame.SetDataReadOnly(const Value: Boolean);
begin
  FDataReadOnly := Value;
  actnGenerate.Enabled := not FDataReadOnly;
  lePreText.Enabled := not FDataReadOnly;
  saZJJL.Columns.ColumnByName('Code').ReadOnly := FDataReadOnly;
  saZJJL.Columns.ColumnByName('CertificateCode').ReadOnly := FDataReadOnly;
  lePreText.Enabled := not Value;
end;

procedure TZJJLFrame.zgZJJLMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmZJJL.PopupFromCursorPos
  else if (ssDouble in Shift) and (zgZJJL.CurCol = 1) and (zgZJJL.CurRow > 0) then
  begin
    if saZJJL.DataView.RecordCount > 0 then
      FZJJLData.LocateBills;
  end;
end;

procedure TZJJLFrame.dxpmZJJLPopup(Sender: TObject);
begin
  SetDxBtnAction(actnLocateBills, MainForm.dxbtnLocateBills);
end;

procedure TZJJLFrame.actnLocateBillsExecute(Sender: TObject);
begin
  FZJJLData.LocateBills;
end;

procedure TZJJLFrame.actnLocateBillsUpdate(Sender: TObject);
begin
  TAction(Sender).Enabled := Assigned(FZJJLData.sdvZJJL.Current);
end;

procedure TZJJLFrame.RefreshTitle;
var
  iType: Integer;
begin
  if not TPhaseData(FZJJLData.PhaseData).Active then Exit;
  iType := TPhaseData(FZJJLData.PhaseData).PhaseProperty.ZJJLType;
  case iType of
    0: labTitle.Caption := '中间计量(0号台账)';
    1: labTitle.Caption := '中间计量(总量控制)';
    2: labTitle.Caption := '中间计量(计量汇总)';
  end;
end;

procedure TZJJLFrame.dxpmAutoGeneratePopup(Sender: TObject);
begin
  SetDxBtnAction(actnGenerateFxZJJL, MainForm.dxbtnFxZJJL);
  SetDxBtnAction(actnGenerateGclZJJL, MainForm.dxbtnGclZJJL);
  SetDxBtnAction(actnGenerateGclGatherZJJL, MainForm.dxbtnGclGatherZJJL);
end;

procedure TZJJLFrame.actnGenerateFxZJJLExecute(Sender: TObject);
begin
  with TPhaseData(FZJJLData.PhaseData).PhaseProperty do
  begin
    ZJJLType := 0;
    GenerateZJJL;
  end;
end;

procedure TZJJLFrame.GenerateZJJL;
var
  iType: Integer;
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    ProjProperties.ZJJLPreText := lePreText.Text;

  iType := TPhaseData(FZJJLData.PhaseData).PhaseProperty.ZJJLType;
  case iType of
    0: FZJJLData.GenerateAll;
    1: FZJJLData.GenerateAllByB_Code;
    2: FZJJLData.GenerateAllByB_CodeGather;
  end;
  RefreshTitle;
  if (saZJJL.DataView.RecordCount > 0) then
    saZJJL.DataView.LocateInControl(saZJJL.DataView.Records[0]);
  zgDetailInfo.Invalidate;
end;

procedure TZJJLFrame.actnGenerateGclZJJLExecute(Sender: TObject);
begin
  with TPhaseData(FZJJLData.PhaseData).PhaseProperty do
  begin
    ZJJLType := 1;
    GenerateZJJL;
  end;
end;

procedure TZJJLFrame.actnGenerateUpdate(Sender: TObject);
begin
  with TProjectData(TPhaseData(FZJJLData.PhaseData).ProjectData) do
    TAction(Sender).Enabled := not StageDataReadOnly;
end;

procedure TZJJLFrame.zgDetailInfoGetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);

  function GetDefaultValue(ARec: TsdDataRecord; AFieldName: string): string;
  begin
    if Assigned(ARec) and (AFieldName <> '') then
      Result := ARec.ValueByName(AFieldName).AsString
    else
      Result := '';
  end;

  function GetText(ARow: TRowIndex): string;
  var
    Rec: TsdDataRecord;
  begin
    Rec := saZJJL.DataView.Current;
    if DisplayText then
      Result := RowFormats[ARow] + GetDefaultValue(Rec, RowFields[ARow])
    else
      Result := GetDefaultValue(Rec, RowFields[ARow]);
  end;

  function GetFloatStr(ANum: Double): string;
  begin
    if ANum = 0 then
      Result := ''
    else
      Result := FloatToStr(ANum);
  end;

  function GetCurGatherMeasure: string;
  var
    Rec: TsdDataRecord;
  begin
    Rec := saZJJL.DataView.Current;
    if not Assigned(Rec) then
      Result := Format(RowFormats[riCurGatherMeasure], ['', ''])
    else if Rec.ValueByName('Type').AsInteger = Integer(ztFx) then
      Result := Format(RowFormats[riCurGatherMeasure], ['金额', GetFloatStr(FZJJLData.GetZJJLCalcData(Rec, 'GatherTotalPrice'))])
    else
      Result := Format(RowFormats[riCurGatherMeasure], ['数量', GetFloatStr(FZJJLData.GetZJJLCalcData(Rec, 'GatherQuantity'))]);
  end;

begin
  if (ACoord.Y = Integer(riCurGatherMeasure)) then
    Value := GetCurGatherMeasure
  else if (ACoord.Y > Integer(riCurGatherMeasure)) and (ACoord.Y <= Integer(riRelaFileValue)) then
    Value := GetText(TRowIndex(ACoord.Y));
end;

procedure TZJJLFrame.InitDetailGrid;
var
  iRowIndex: Integer;
begin
  zgDetailInfo.DefaultColWidth := zgDetailInfo.Width - 25;
  for iRowIndex := 0 to zgDetailInfo.RowCount - 1 do
    zgDetailInfo[0, iRowIndex].Align := gaTopLeft;
  zgDetailInfo.RowHeights[Integer(riFormulaMemoValue)] := 57;
  zgDetailInfo.Cells[0, Integer(riFormulaMemoValue)].Align := gaTopLeft;
  zgDetailInfo.RowHeights[Integer(riRelaFileValue)] := 57;
  zgDetailInfo.Cells[0, Integer(riRelaFileValue)].Align := gaTopLeft;
end;

procedure TZJJLFrame.zgDetailInfoSetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String; DisplayText: Boolean);
var
  Rec: TsdDataRecord;
  sFieldName: string;
begin
  Rec := saZJJL.DataView.Current;
  if Assigned(Rec) then
  begin
    sFieldName := RowFields[TRowIndex(ACoord.Y)];
    if (sFieldName <> '') then
    begin
      saZJJL.DataView.Text[saZJJL.DataView.IndexOf(Rec), saZJJL.DataView.FindColumn(sFieldName).Index] := Value;
    end;
  end;
end;

procedure TZJJLFrame.zgDetailInfoCellCanEdit(Sender: TObject;
  const ACoord: TPoint; var Allow: Boolean);
var
  sFieldName: string;
begin
  sFieldName := RowFields[TRowIndex(ACoord.Y)];
  Allow := (not FDataReadOnly) and (sFieldName <> '') and Assigned(saZJJL.DataView.Current);
end;

procedure TZJJLFrame.actnGenerateGclGatherZJJLExecute(Sender: TObject);
begin
  with TPhaseData(FZJJLData.PhaseData).PhaseProperty do
  begin
    ZJJLType := Integer(ztGclGather);
    GenerateZJJL;
  end;
end;

procedure TZJJLFrame.RefreshDetailGrid;
begin
  zgDetailInfo.Invalidate;
end;

end.
