unit AddLeafBillsFrm; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, ZJGrid, DataBase, ZjIDTree, ConstMethodUnit, dxBar; type TAddLeafBillsForm = class(TForm) pnlPosition: TPanel; zgPosition: TZJGrid; pnlPositionSpr: TPanel; lblPostion: TLabel; pnlBills: TPanel; lblBills: TLabel; zgBills: TZJGrid; pnlBillsSpr: TPanel; pnlOther: TPanel; leBeginCode: TLabeledEdit; btnOK: TButton; btnCancel: TButton; leDrawingCode: TLabeledEdit; dxpmAddLeafBills: TdxBarPopupMenu; procedure zgPositionCustomPaste(Sender: TObject; ABounds: TRect; ASourSheet: TZjSheet); procedure zgBillsCustomPaste(Sender: TObject; ABounds: TRect; ASourSheet: TZjSheet); procedure btnOKClick(Sender: TObject); procedure zgPositionMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private FBillsData: TDMDataBase; procedure ResetPositionGridHead; procedure ResetBillsGridHead; procedure PasteData(AGrid: TZJGrid; ABounds: TRect; ASourSheet: TZjSheet); procedure SetRowAndColumnCount(AGrid: TZJGrid; ASourSheet: TZjSheet); function ReplaceLastNum(const ACode: string; ARow: Integer): string; procedure SetXmjNodeData(ARow: Integer; ANode: TZjIDTreeNode); procedure AddXmjNode(ARow, AParentID, ANextSiblingID: Integer); procedure AddBillsNode(AQtyRow, ARow: Integer; AParent: TZjIDTreeNode); procedure AddBillsNodes(AQtyRow: Integer; AParent: TZjIDTreeNode); procedure BatchAddBillsNodes(AParentID, ANextSiblingID: Integer); public procedure Init; procedure Execute(ABillsData: TDMDataBase); end; procedure AddLeafBills(ABillsData: TDMDataBase); implementation uses ScBillsTree; {$R *.dfm} procedure AddLeafBills(ABillsData: TDMDataBase); var AddLeafBillsForm: TAddLeafBillsForm; begin AddLeafBillsForm := TAddLeafBillsForm.Create(nil); try AddLeafBillsForm.Init; if AddLeafBillsForm.ShowModal = mrOK then AddLeafBillsForm.Execute(ABillsData); finally AddLeafBillsForm.Free; end; end; { TAddLeafBillsForm } procedure TAddLeafBillsForm.AddXmjNode(ARow, AParentID, ANextSiblingID: Integer); var ztnNode: TZjIDTreeNode; begin if zgPosition.Cells[1, ARow].Text = '' then Exit; ztnNode := FBillsData.BillsTree.Add(AParentID, ANextSiblingID); SetXmjNodeData(ARow, ztnNode); AddBillsNodes(ARow, ztnNode); end; procedure TAddLeafBillsForm.BatchAddBillsNodes(AParentID, ANextSiblingID: Integer); var iRow: Integer; begin for iRow := 1 to zgPosition.RowCount - 1 do AddXmjNode(iRow, AParentID, ANextSiblingID); end; procedure TAddLeafBillsForm.Execute(ABillsData: TDMDataBase); begin FBillsData := ABillsData; with FBillsData.BillsTree do if Assigned(Selected) and Assigned(Selected.Parent) then BatchAddBillsNodes(Selected.ParentID, Selected.NextSiblingID); end; procedure TAddLeafBillsForm.Init; begin ResetPositionGridHead; ResetBillsGridHead; end; procedure TAddLeafBillsForm.PasteData(AGrid: TZJGrid; ABounds: TRect; ASourSheet: TZjSheet); var iCol, iRow: Integer; begin for iRow := 0 to ASourSheet.RowCount - 1 do for iCol := 0 to ASourSheet.ColCount - 1 do begin with AGrid.Cells[iCol + ABounds.Left , iRow + ABounds.Top] do if CanEdit then Text := ASourSheet.Values[iCol, iRow]; end; end; function TAddLeafBillsForm.ReplaceLastNum(const ACode: string; ARow: Integer): string; var sgs: TStringList; begin sgs := TStringList.Create; try sgs.Delimiter := '-'; sgs.DelimitedText := ACode; sgs[sgs.Count - 1] := IntToStr(StrToInt(sgs[sgs.Count - 1]) + ARow - 1); Result := sgs.DelimitedText; finally sgs.Free; end; end; procedure TAddLeafBillsForm.ResetPositionGridHead; var iCol: Integer; begin zgPosition.Cells[1, 0].Text := '部位'; zgPosition.ColWidths[1] := 120; for iCol := 2 to zgPosition.ColCount - 1 do begin zgPosition.Cells[iCol, 0].Text := '清单' + IntToStr(iCol - 1); zgPosition.ColWidths[iCol] := 50; end; end; procedure TAddLeafBillsForm.SetRowAndColumnCount(AGrid: TZJGrid; ASourSheet: TZjSheet); begin if AGrid.ColCount < ASourSheet.ColCount + AGrid.CurCol then AGrid.ColCount := ASourSheet.ColCount + AGrid.CurCol; if AGrid.RowCount < ASourSheet.RowCount + AGrid.CurRow + 1 then AGrid.RowCount := ASourSheet.RowCount + AGrid.CurRow + 1; end; procedure TAddLeafBillsForm.SetXmjNodeData(ARow: Integer; ANode: TZjIDTreeNode); begin with FBillsData do if cdsBills.FindKey([ANode.ID]) then begin cdsBills.Edit; cdsBillsCode.AsString := ReplaceLastNum(leBeginCode.Text, ARow); cdsBillsName.AsString := zgPosition.Cells[1, ARow].Text; cdsBillsDrawingCode.AsString := leDrawingCode.Text; cdsBills.Post; end; end; procedure TAddLeafBillsForm.zgPositionCustomPaste(Sender: TObject; ABounds: TRect; ASourSheet: TZjSheet); begin SetRowAndColumnCount(TZJGrid(Sender), ASourSheet); ResetPositionGridHead; PasteData(TZJGrid(Sender), ABounds, ASourSheet); zgBills.RowCount := zgPosition.ColCount - 1; ResetBillsGridHead; end; procedure TAddLeafBillsForm.zgBillsCustomPaste(Sender: TObject; ABounds: TRect; ASourSheet: TZjSheet); begin SetRowAndColumnCount(TZJGrid(Sender), ASourSheet); PasteData(TZJGrid(Sender), ABounds, ASourSheet); end; procedure TAddLeafBillsForm.AddBillsNodes(AQtyRow: Integer; AParent: TZjIDTreeNode); var iRow: Integer; begin for iRow := 1 to zgBills.RowCount - 1 do AddBillsNode(AQtyRow, iRow, AParent); end; procedure TAddLeafBillsForm.AddBillsNode(AQtyRow, ARow: Integer; AParent: TZjIDTreeNode); var ztnNode: TZjIDTreeNode; fQuantity: Double; begin if (zgBills.Cells[1, ARow].Text = '') or (zgPosition.Cells[ARow + 1, AQtyRow].Text = '') or not TryStrToFloat(zgPosition.Cells[ARow + 1, AQtyRow].Text, fQuantity) then Exit; ztnNode := FBillsData.BillsTree.Add(AParent.ID, -1); with FBillsData do if cdsBills.FindKey([ztnNode.ID]) then begin cdsBills.Edit; cdsBillsB_Code.AsString := zgBills.Cells[1, ARow].Text; cdsBillsName.AsString := zgBills.Cells[2, ARow].Text; cdsBillsUnits.AsString := zgBills.Cells[3, ARow].Text; cdsBillsQuantity.AsString := zgPosition.Cells[ARow + 1, AQtyRow].Text; cdsBills.Post; end; end; procedure TAddLeafBillsForm.ResetBillsGridHead; var iRow: Integer; begin zgBills.Cells[1, 0].Text := '编号'; zgBills.ColWidths[1] := 80; zgBills.Cells[2, 0].Text := '名称'; zgBills.ColWidths[2] := 120; zgBills.Cells[3, 0].Text := '单位'; zgBills.ColWidths[3] := 60; for iRow := 1 to zgBills.RowCount - 1 do zgBills.Cells[0, iRow].Text := '清单' + IntToStr(iRow); end; procedure TAddLeafBillsForm.btnOKClick(Sender: TObject); function CheckGridHasData(AGrid: TZJGrid): Boolean; var iRow: Integer; begin Result := False; for iRow := 1 to AGrid.RowCount - 1 do if AGrid.Cells[1, iRow].Text <> '' then begin Result := True; Break; end; end; function CheckBeginCodeAvailable(const ACode: string): Boolean; var sgsCode: TStrings; iCode: Integer; begin sgsCode := TStringList.Create; try sgsCode.Delimiter := '-'; sgsCode.DelimitedText := ACode; Result := TryStrToInt(sgsCode[sgsCode.Count - 1], iCode); finally sgsCode.Free; end; end; begin if not CheckGridHasData(zgPosition) then MessageError(Handle, '请输入部位数量复核数据!') else if not CheckGridHasData(zgBills) then MessageError(Handle, '请输入清单编号等数据!') else if leBeginCode.Text = '' then MessageError(Handle, '请输入起始编号!') else if not CheckBeginCodeAvailable(leBeginCode.Text) then MessageError(Handle, '请输入规范的起始部位编号,如1或1-1等。') else ModalResult := mrOK; end; procedure TAddLeafBillsForm.zgPositionMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button = mbRight then dxpmAddLeafBills.PopupFromCursorPos; end; end.