unit PriceMarginBillsFme;

interface

uses
  SelectDetailGLsFrm,
  PriceMarginBillsDm, ProjectGLDm, DetailGLDm, sdDB,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, sdGridDBA, ZJGrid, ExtCtrls, StdCtrls, JimLabels, dxBar,
  JimPages, ComCtrls, ToolWin, ActnList, XPMenu;

type
  TPriceMarginBillsFrame = class(TFrame)
    pnlGclBills: TPanel;
    pnlDetail: TPanel;
    sprDetail: TSplitter;
    zgGclBills: TZJGrid;
    saGclBills: TsdGridDBA;
    pnlRelaData: TPanel;
    sprRela: TSplitter;
    pnlDetailBills: TPanel;
    labTitle: TJimGradLabel;
    zgDetailGclBills: TZJGrid;
    saDetailGclBills: TsdGridDBA;
    pnlRelaTop: TPanel;
    tbRelaTop: TToolBar;
    tobtnDetailGL: TToolButton;
    tobtnParent: TToolButton;
    jpsRela: TJimPages;
    jpsRelaDetailGL: TJimPage;
    jpsRelaBillsParent: TJimPage;
    zgDetailGL: TZJGrid;
    saDetailGL: TsdGridDBA;
    dxpmDetailGL: TdxBarPopupMenu;
    pnlBills: TPanel;
    actnPriceMarginBills: TActionList;
    actnAddDetailGLs: TAction;
    dxpmDetailGclBills: TdxBarPopupMenu;
    actnCopyDetailGLs: TAction;
    xpm: TXPMenu;
    actnApplyToSameBills: TAction;
    procedure zgDetailGLMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure dxpmDetailGLPopup(Sender: TObject);
    procedure actnAddDetailGLsExecute(Sender: TObject);
    procedure dxpmDetailGclBillsPopup(Sender: TObject);
    procedure actnApplyToSameBillsExecute(Sender: TObject);
    procedure zgDetailGclBillsMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure actnAddDetailGLsUpdate(Sender: TObject);
  private
    FPriceMarginBillsData: TPriceMarginBillsData;
    FProjectGLData: TProjectGLData;
    FDetailGLData: TDetailGLData;

    procedure AddDetailGLs;
  public
    constructor Create(APriceMarginBillsData: TPriceMarginBillsData);
    destructor Destroy; override;

    procedure ResetViewControl;
  end;

implementation

uses
  UtilMethods, MainFrm, BillsTree, ProjectData;

{$R *.dfm}

{ TPriceMarginBillsFrame }

constructor TPriceMarginBillsFrame.Create(
  APriceMarginBillsData: TPriceMarginBillsData);
begin
  inherited Create(nil);
  FPriceMarginBillsData := APriceMarginBillsData;
  FProjectGLData := TProjectData(FPriceMarginBillsData.ProjectData).ProjectGLData;
  FDetailGLData := TProjectData(FPriceMarginBillsData.ProjectData).DetailGLData;

  saGclBills.DataView := APriceMarginBillsData.sdvGclBills;
  saDetailGclBills.DataView := APriceMarginBillsData.sdvDetailGclBills;
  saDetailGL.DataView := APriceMarginBillsData.sdvDetailGL;
end;

destructor TPriceMarginBillsFrame.Destroy;
begin
  inherited;
end;

procedure TPriceMarginBillsFrame.zgDetailGLMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmDetailGL.PopupFromCursorPos;
end;

procedure TPriceMarginBillsFrame.dxpmDetailGLPopup(Sender: TObject);
begin
  SetDxBtnAction(actnAddDetailGLs, MainForm.dxbtnAddDetailGLs);
end;

procedure TPriceMarginBillsFrame.actnAddDetailGLsExecute(Sender: TObject);
var
  iID: Integer;
  vNode: TBillsIDTreeNode;
begin
  iID := saDetailGclBills.DataView.Current.ValueByName('RelaBillsID').AsInteger;
  vNode := TBillsIDTreeNode(FPriceMarginBillsData.MainBillsTree.FindNode(iID));
  AddDetailGLs;
  {  ErrorMessage('当前清单已进行材料调差，不可再添加调差工料。');}
end;

procedure TPriceMarginBillsFrame.AddDetailGLs;
var
  DetailGLs: TList;
begin
  DetailGLs := TList.Create;
  try
    if SelectDetailGLs(FProjectGLData, DetailGLs) then
      FPriceMarginBillsData.AddDetailGLs(DetailGLs);
  finally
    DetailGLs.Free;
  end;
end;

procedure TPriceMarginBillsFrame.dxpmDetailGclBillsPopup(Sender: TObject);
begin
  SetDxBtnAction(actnApplyToSameBills, MainForm.dxbtnApplyToSameBills);
  SetDxBtnAction(actnCopyDetailGLs, MainForm.dxbtnCopyDetailGls);
end;

procedure TPriceMarginBillsFrame.actnApplyToSameBillsExecute(
  Sender: TObject);
var
  CurRec, Rec: TsdDataRecord;
  vGLs: TList;
  iRec: Integer;
begin
  if saDetailGclBills.DataView.RecordCount = 1 then Exit;

  vGLs := TList.Create;
  try
    CurRec := saDetailGclBills.DataView.Current;
    FDetailGLData.LoadDetailGLs(CurRec.ValueByName('RelaBillsID').AsInteger, vGLs);
    for iRec := 0 to saDetailGclBills.DataView.RecordCount - 1 do
    begin
      Rec := saDetailGclBills.DataView.Records[iRec];
      if Rec <> CurRec then
      begin
        FDetailGLData.ResetDetailGLs(Rec.ValueByName('RelaBillsID').AsInteger, vGLs);
        // 应重新计算清单
        with TProjectData(FPriceMarginBillsData.ProjectData) do
          PhaseData.StageData.CalculatePriceMargin(Rec.ValueByName('RelaBillsID').AsInteger);
        FPriceMarginBillsData.RefreshDetailBills(Rec.ValueByName('RelaBillsID').AsInteger);
      end;
      // 应重新计算使用到的工料
      FProjectGLData.CalculateGLs_PM(vGLs);
    end;
  finally
    vGLs.Free;
  end;
end;

procedure TPriceMarginBillsFrame.zgDetailGclBillsMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmDetailGclBills.PopupFromCursorPos;
end;

procedure TPriceMarginBillsFrame.actnAddDetailGLsUpdate(Sender: TObject);
begin
  with TProjectData(FPriceMarginBillsData.ProjectData) do
    TAction(Sender).Enabled := (not PriceMarginReadOnly) and Assigned(saDetailGclBills.DataView.Current);
end;

procedure TPriceMarginBillsFrame.ResetViewControl;
begin
  with TProjectData(FPriceMarginBillsData.ProjectData) do
  begin
    saDetailGL.Columns.ColumnByName('Quantity').ReadOnly := PriceMarginReadOnly;
    if PriceMarginReadOnly then
      saDetailGL.Options := saDetailGL.Options - [aoAllowDelete]
    else
      saDetailGL.Options := saDetailGL.Options + [aoAllowDelete];
  end;
end;

end.
