unit BatchReplaceBillsFrm;

interface

uses
  sdDB, BillsDm, ZhAPI,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ZJGrid, ExtCtrls;

type
  TBatchReplaceBillsForm = class(TForm)
    pnlBills: TPanel;
    lblBills: TLabel;
    zgBills: TZJGrid;
    pnlBillsSpr: TPanel;
    pnlOther: TPanel;
    btnOk: TButton;
    btnCancel: TButton;
    pnlOrgBills: TPanel;
    lblOrgBills: TLabel;
    zgOrgBills: TZJGrid;
    pnlOrgBillsSpr: TPanel;
    procedure zgOrgBillsCellCanEdit(Sender: TObject; const ACoord: TPoint;
      var Allow: Boolean);
    procedure btnOkClick(Sender: TObject);
    procedure zgBillsCellTextChanged(Sender: TObject; Col, Row: Integer);
  private
    FBillsData: TBillsData;
    procedure InitGrid(AGrid: TZJGrid; ARec: TsdDataRecord);
  public
    procedure Init(ARec: TsdDataRecord);
    procedure Execute;

    property BillsData: TBillsData read FBillsData write FBillsData;
  end;

procedure BatchReplaceBillsInfo(ARec: TsdDataRecord; ABillsData: TBillsData);

implementation

uses
  UtilMethods, ProjectData;

procedure BatchReplaceBillsInfo(ARec: TsdDataRecord; ABillsData: TBillsData);
var
  ReplaceBillsFrm: TBatchReplaceBillsForm;
begin
  ReplaceBillsFrm := TBatchReplaceBillsForm.Create(nil);
  try
    ReplaceBillsFrm.Init(ARec);
    ReplaceBillsFrm.BillsData := ABillsData;
    if ReplaceBillsFrm.ShowModal = mrOk then
      ReplaceBillsFrm.Execute;
  finally
    ReplaceBillsFrm.Free;
  end;
end;

{$R *.dfm}

{ TBatchReplaceBillsForm }

procedure TBatchReplaceBillsForm.Execute;
var
  iIndex: Integer;
  Rec: TsdDataRecord;
  bHasChange: Boolean;
begin
  FBillsData.sddBills.BeginUpdate;
  try
    bHasChange := False;
    for iIndex := 0 to FBillsData.sddBills.RecordCount - 1 do
    begin
      Rec := FBillsData.sddBills.Records[iIndex];
      if SameText(Rec.ValueByName('B_Code').AsString, zgOrgBills.Cells[1, 1].Text) and
          SameText(Rec.ValueByName('Name').AsString, zgOrgBills.Cells[2, 1].Text) and
          SameText(Rec.ValueByName('Units').AsString, zgOrgBills.Cells[3, 1].Text) then
      begin
        Rec.ValueByName('B_Code').AsString := zgBills.Cells[1, 1].Text;
        Rec.ValueByName('Name').AsString := zgBills.Cells[2, 1].Text;
        Rec.ValueByName('Units').AsString := zgBills.Cells[3, 1].Text;
        if Rec.ValueByName('Price').AsString <> zgBills.Cells[4, 1].Text then
        begin
          bHasChange := True;
          Rec.ValueByName('Price').AsString := zgBills.Cells[4, 1].Text;
        end;
      end;
    end;
    if bHasChange then
      TProjectData(FBillsData.ProjectData).BillsCompileData.CalculateAll;
  finally
    FBillsData.sddBills.EndUpdate;
  end;
end;

procedure TBatchReplaceBillsForm.Init(ARec: TsdDataRecord);
begin
  ClientHeight := 218;
  ClientWidth := 375;

  InitGrid(zgOrgBills, ARec);
  InitGrid(zgBills, ARec);
end;

procedure TBatchReplaceBillsForm.InitGrid(AGrid: TZJGrid; ARec: TsdDataRecord);

  procedure InitGridHead;
  begin
    AGrid.Cells[1, 0].Text := '清单编号';
    AGrid.ColWidths[1] := 80;
    AGrid.Cells[2, 0].Text := '名称';
    AGrid.ColWidths[2] := 120;
    AGrid.Cells[3, 0].Text := '单位';
    AGrid.ColWidths[3] := 50;
    AGrid.Cells[4, 0].Text := '单价';
    AGrid.ColWidths[4] := 60;
  end;

  procedure InitGridInfo;
  begin
    AGrid.Cells[1, 1].Text := ARec.ValueByName('B_Code').AsString;
    AGrid.Cells[1, 1].TextAlign := gaCenterLeft;
    AGrid.Cells[2, 1].Text := ARec.ValueByName('Name').AsString;
    AGrid.Cells[2, 1].TextAlign := gaCenterLeft;
    AGrid.Cells[3, 1].Text := ARec.ValueByName('Units').AsString;
    AGrid.Cells[3, 1].TextAlign := gaCenterLeft;
    AGrid.Cells[4, 1].Text := ARec.ValueByName('Price').AsString;
    AGrid.Cells[4, 1].TextAlign := gaCenterRight;
  end;

begin
  InitGridHead;
  InitGridInfo;
end;

procedure TBatchReplaceBillsForm.zgOrgBillsCellCanEdit(Sender: TObject;
  const ACoord: TPoint; var Allow: Boolean);
begin
  Allow := False;
end;

procedure TBatchReplaceBillsForm.btnOkClick(Sender: TObject);
begin
  if zgBills.Cells[1, 1].Text = '' then
    ErrorMessage('请输入清单编号！')
  else
    ModalResult := mrOK;
end;

procedure TBatchReplaceBillsForm.zgBillsCellTextChanged(Sender: TObject;
  Col, Row: Integer);
var
  fPrice: Double;
begin
  if (Col = 4) and (Row = 1) then
  begin
    if (zgBills.Cells[Col, Row].Text <> '') and not TryStrToFloat(zgBills.Cells[Col, Row].Text, fPrice) then
    begin
      ErrorMessage('单价只允许输入数字');
      zgBills.Cells[Col, Row].Text := '';
    end
    else
      zgBills.Cells[Col, Row].Value := PriceRoundTo(
          StrToFloatDef(zgBills.Cells[Col, Row].Text, 0));
  end;
end;

end.
