BatchInsertBillsFrm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. unit BatchInsertBillsFrm;
  2. interface
  3. uses
  4. BillsCompileDm,
  5. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6. Dialogs, StdCtrls, ExtCtrls, ZJGrid, sdIDTree, dxBar, sdGridDBA, ComCtrls,
  7. ActnList;
  8. type
  9. TInsertType = (itChild, itNextSibling);
  10. TBatchInsertBillsForm = class(TForm)
  11. pnlButton: TPanel;
  12. btnOK: TButton;
  13. btnCancel: TButton;
  14. dxpmInsertBills: TdxBarPopupMenu;
  15. pnlAllGrid: TPanel;
  16. pnlPositon_Bills: TPanel;
  17. pnlPosition: TPanel;
  18. lblPostion: TLabel;
  19. zgPosition: TZJGrid;
  20. pnlPositionSpr: TPanel;
  21. pnlBills: TPanel;
  22. lblBills: TLabel;
  23. zgBills: TZJGrid;
  24. pnlBillsSpr: TPanel;
  25. pnlDealBills: TPanel;
  26. pnlOther: TPanel;
  27. leBeginCode: TLabeledEdit;
  28. leDrawingCode: TLabeledEdit;
  29. zgDealBills: TZJGrid;
  30. pnlDealBillsSpr: TPanel;
  31. lblDealBills: TLabel;
  32. lblHint: TLabel;
  33. sgdDealBills: TsdGridDBA;
  34. udBeginCode: TUpDown;
  35. alBatchInsertBills: TActionList;
  36. actnInsertCol: TAction;
  37. dxpmBills: TdxBarPopupMenu;
  38. actnDeleteRow: TAction;
  39. procedure zgPositionCustomPaste(Sender: TObject; ABounds: TRect;
  40. ASourSheet: TZjSheet);
  41. procedure zgBillsCustomPaste(Sender: TObject; ABounds: TRect;
  42. ASourSheet: TZjSheet);
  43. procedure btnOKClick(Sender: TObject);
  44. procedure zgPositionMouseDown(Sender: TObject; Button: TMouseButton;
  45. Shift: TShiftState; X, Y: Integer);
  46. procedure zgBillsCellTextChanged(Sender: TObject; Col, Row: Integer);
  47. procedure zgDealBillsMouseDown(Sender: TObject; Button: TMouseButton;
  48. Shift: TShiftState; X, Y: Integer);
  49. procedure leBeginCodeKeyPress(Sender: TObject; var Key: Char);
  50. procedure zgPositionCellTextChanged(Sender: TObject; Col,
  51. Row: Integer);
  52. procedure dxpmInsertBillsPopup(Sender: TObject);
  53. procedure actnInsertColExecute(Sender: TObject);
  54. procedure zgBillsMouseDown(Sender: TObject; Button: TMouseButton;
  55. Shift: TShiftState; X, Y: Integer);
  56. procedure actnInsertColUpdate(Sender: TObject);
  57. procedure FormResize(Sender: TObject);
  58. procedure actnDeleteRowExecute(Sender: TObject);
  59. procedure actnDeleteRowUpdate(Sender: TObject);
  60. private
  61. FInsertType: TInsertType;
  62. FBillsCompileData: TBillsCompileData;
  63. FParentCode: string;
  64. procedure ResetPositionGridHead;
  65. procedure ResetBillsGridHead;
  66. procedure PasteData(AGrid: TZJGrid; ABounds: TRect; ASourSheet: TZjSheet);
  67. procedure SetRowAndColumnCount(AGrid: TZJGrid; ASourSheet: TZjSheet);
  68. procedure LoadParentCode;
  69. function ReplaceLastNum(const ACode: string; ARow: Integer): string;
  70. function GetXmjCode(ARow: Integer): string;
  71. procedure AddXmjNode(ARow, AParentID, ANextSiblingID: Integer);
  72. procedure AddBillsNode(AQtyRow, ARow: Integer; AParent: TsdIDTreeNode);
  73. procedure AddBillsNodes(AQtyRow: Integer; AParent: TsdIDTreeNode);
  74. procedure BatchAddBillsNodes(AParentID, ANextSiblingID: Integer);
  75. public
  76. procedure Init(ABillsCompileData: TBillsCompileData; AInsertType: TInsertType);
  77. procedure Execute;
  78. end;
  79. procedure AddLeafBills(ABillsCompileData: TBillsCompileData; AInsertType: TInsertType);
  80. implementation
  81. uses
  82. sdDB, UtilMethods, ProjectData, MainFrm, Globals, DealBillsDm;
  83. {$R *.dfm}
  84. const
  85. iBwNameCol = 1;
  86. iBwDrawingCodeCol = 2;
  87. iBwBillsQtyCol = 3;
  88. iGclCodeCol = 1;
  89. iGclNameCol = 2;
  90. iGclUnitsCol = 3;
  91. iGclPriceCol = 4;
  92. iGclDrawingCodeCol = 5;
  93. procedure AddLeafBills(ABillsCompileData: TBillsCompileData; AInsertType: TInsertType);
  94. var
  95. InsertBillsForm: TBatchInsertBillsForm;
  96. begin
  97. InsertBillsForm := TBatchInsertBillsForm.Create(nil);
  98. try
  99. InsertBillsForm.Init(ABillsCompileData, AInsertType);
  100. if InsertBillsForm.ShowModal = mrOK then
  101. InsertBillsForm.Execute;
  102. finally
  103. InsertBillsForm.Free;
  104. end;
  105. end;
  106. { TAddLeafBillsForm }
  107. procedure TBatchInsertBillsForm.AddXmjNode(ARow, AParentID, ANextSiblingID: Integer);
  108. var
  109. stnNode: TsdIDTreeNode;
  110. begin
  111. if zgPosition.Cells[1, ARow].Text = '' then Exit;
  112. stnNode := FBillsCompileData.BillsCompileTree.Add(AParentID, ANextSiblingID);
  113. with stnNode.Rec do
  114. begin
  115. ValueByName('Code').AsString := GetXmjCode(ARow);{ReplaceLastNum(leBeginCode.Text, ARow);}
  116. ValueByName('Name').AsString := zgPosition.Cells[iBwNameCol, ARow].Text;
  117. ValueByName('DrawingCode').AsString := zgPosition.Cells[iBwDrawingCodeCol, ARow].Text;
  118. end;
  119. AddBillsNodes(ARow, stnNode);
  120. end;
  121. procedure TBatchInsertBillsForm.BatchAddBillsNodes(AParentID, ANextSiblingID: Integer);
  122. var
  123. iRow: Integer;
  124. begin
  125. for iRow := 1 to zgPosition.RowCount - 1 do
  126. AddXmjNode(iRow, AParentID, ANextSiblingID);
  127. end;
  128. procedure TBatchInsertBillsForm.Execute;
  129. begin
  130. with FBillsCompileData.BillsCompileTree do
  131. begin
  132. if FInsertType = itChild then
  133. begin
  134. if not Assigned(Selected) then Exit;
  135. BatchAddBillsNodes(Selected.ID, -1);
  136. FBillsCompileData.Calculate(Selected.ID);
  137. end
  138. else if FInsertType = itNextSibling then
  139. begin
  140. if not Assigned(Selected.Parent) then Exit;
  141. BatchAddBillsNodes(Selected.ParentID, Selected.NextSiblingID);
  142. FBillsCompileData.Calculate(Selected.ParentID);
  143. end;
  144. end;
  145. end;
  146. procedure TBatchInsertBillsForm.Init(ABillsCompileData: TBillsCompileData;
  147. AInsertType: TInsertType);
  148. begin
  149. ClientHeight := SupportManager.ConfigInfo.BatchInsertFrmHeight;
  150. ClientWidth := SupportManager.ConfigInfo.BatchInsertFrmWidth;
  151. pnlBills.Height := Trunc((pnlPositon_Bills.Height - pnlOther.Height)/22*9);
  152. OnResize := FormResize;
  153. FBillsCompileData := ABillsCompileData;
  154. sgdDealBills.DataView := TProjectData(ABillsCompileData.ProjectData).DealBillsData.sdvDealBills;
  155. FInsertType := AInsertType;
  156. if FInsertType = itChild then
  157. Caption := '批量插入子项'
  158. else if FInsertType = itNextSibling then
  159. Caption := '批量插入后项';
  160. ResetPositionGridHead;
  161. ResetBillsGridHead;
  162. LoadParentCode;
  163. end;
  164. procedure TBatchInsertBillsForm.PasteData(AGrid: TZJGrid; ABounds: TRect;
  165. ASourSheet: TZjSheet);
  166. var
  167. iCol, iRow: Integer;
  168. begin
  169. for iRow := 0 to ASourSheet.RowCount - 1 do
  170. for iCol := 0 to ASourSheet.ColCount - 1 do
  171. begin
  172. with AGrid.Cells[iCol + ABounds.Left , iRow + ABounds.Top] do
  173. if CanEdit then Text := ASourSheet.Values[iCol, iRow];
  174. end;
  175. end;
  176. function TBatchInsertBillsForm.ReplaceLastNum(const ACode: string;
  177. ARow: Integer): string;
  178. var
  179. sgs: TStringList;
  180. begin
  181. sgs := TStringList.Create;
  182. try
  183. sgs.Delimiter := '-';
  184. sgs.DelimitedText := ACode;
  185. sgs[sgs.Count - 1] := IntToStr(StrToInt(sgs[sgs.Count - 1]) + ARow - 1);
  186. Result := sgs.DelimitedText;
  187. finally
  188. sgs.Free;
  189. end;
  190. end;
  191. procedure TBatchInsertBillsForm.ResetPositionGridHead;
  192. var
  193. iCol, iColDiff: Integer;
  194. begin
  195. zgPosition.Cells[iBwNameCol, 0].Text := '部位';
  196. zgPosition.ColWidths[iBwNameCol] := 100;
  197. zgPosition.Cells[iBwDrawingCodeCol, 0].Text := '图(册)号';
  198. zgPosition.ColWidths[iBwDrawingCodeCol] := 100;
  199. iColDiff := iBwBillsQtyCol - 1;
  200. for iCol := iBwBillsQtyCol to zgPosition.ColCount - 1 do
  201. begin
  202. zgPosition.Cells[iCol, 0].Text := '清单' + IntToStr(iCol - iColDiff);
  203. zgPosition.ColWidths[iCol] := 50;
  204. end;
  205. end;
  206. procedure TBatchInsertBillsForm.SetRowAndColumnCount(AGrid: TZJGrid;
  207. ASourSheet: TZjSheet);
  208. begin
  209. if AGrid.ColCount < ASourSheet.ColCount + AGrid.CurCol + 1 then
  210. AGrid.ColCount := ASourSheet.ColCount + AGrid.CurCol + 1;
  211. if AGrid.RowCount < ASourSheet.RowCount + AGrid.CurRow + 1 then
  212. AGrid.RowCount := ASourSheet.RowCount + AGrid.CurRow + 1;
  213. end;
  214. procedure TBatchInsertBillsForm.zgPositionCustomPaste(Sender: TObject;
  215. ABounds: TRect; ASourSheet: TZjSheet);
  216. begin
  217. SetRowAndColumnCount(TZJGrid(Sender), ASourSheet);
  218. ResetPositionGridHead;
  219. PasteData(TZJGrid(Sender), ABounds, ASourSheet);
  220. zgBills.RowCount := zgPosition.ColCount - 1;
  221. ResetBillsGridHead;
  222. end;
  223. procedure TBatchInsertBillsForm.zgBillsCustomPaste(Sender: TObject;
  224. ABounds: TRect; ASourSheet: TZjSheet);
  225. begin
  226. SetRowAndColumnCount(TZJGrid(Sender), ASourSheet);
  227. PasteData(TZJGrid(Sender), ABounds, ASourSheet);
  228. end;
  229. procedure TBatchInsertBillsForm.AddBillsNodes(AQtyRow: Integer;
  230. AParent: TsdIDTreeNode);
  231. var
  232. iRow: Integer;
  233. begin
  234. for iRow := 1 to zgBills.RowCount - 1 do
  235. AddBillsNode(AQtyRow, iRow, AParent);
  236. end;
  237. procedure TBatchInsertBillsForm.AddBillsNode(AQtyRow, ARow: Integer;
  238. AParent: TsdIDTreeNode);
  239. var
  240. stnNode: TsdIDTreeNode;
  241. fQuantity: Double;
  242. begin
  243. if (zgBills.Cells[1, ARow].Text = '') or
  244. (zgPosition.Cells[ARow + iBwBillsQtyCol - 1, AQtyRow].Text = '') or
  245. not TryStrToFloat(zgPosition.Cells[ARow + iBwBillsQtyCol - 1, AQtyRow].Text, fQuantity) then Exit;
  246. stnNode := FBillsCompileData.BillsCompileTree.Add(AParent.ID, -1);
  247. with stnNode.Rec do
  248. begin
  249. ValueByName('B_Code').AsString := zgBills.Cells[iGclCodeCol, ARow].Text;
  250. ValueByName('Name').AsString := zgBills.Cells[iGclNameCol, ARow].Text;
  251. ValueByName('Units').AsString := zgBills.Cells[iGclUnitsCol, ARow].Text;
  252. ValueByName('Price').AsFloat := StrToFloatDef(zgBills.Cells[iGclPriceCol, ARow].Text, 0);
  253. ValueByName('OrgQuantity').AsFloat := StrToFloatDef(zgPosition.Cells[ARow + iBwBillsQtyCol - 1, AQtyRow].Text, 0);
  254. ValueByName('DrawingCode').AsString := zgBills.Cells[iGclDrawingCodeCol, ARow].Text;
  255. end;
  256. end;
  257. procedure TBatchInsertBillsForm.ResetBillsGridHead;
  258. var
  259. iRow: Integer;
  260. begin
  261. zgBills.Cells[iGclCodeCol, 0].Text := '编号';
  262. zgBills.ColWidths[1] := 80;
  263. zgBills.Cells[iGclNameCol, 0].Text := '名称';
  264. zgBills.ColWidths[2] := 120;
  265. zgBills.Cells[iGclUnitsCol, 0].Text := '单位';
  266. zgBills.ColWidths[3] := 60;
  267. zgBills.Cells[iGclPriceCol, 0].Text := '单价';
  268. zgBills.ColWidths[4] := 60;
  269. zgBills.Cells[iGclDrawingCodeCol, 0].Text := '图(册)号';
  270. zgBills.ColWidths[5] := 60;
  271. for iRow := 1 to zgBills.RowCount - 1 do
  272. zgBills.Cells[0, iRow].Text := '清单' + IntToStr(iRow);
  273. end;
  274. procedure TBatchInsertBillsForm.btnOKClick(Sender: TObject);
  275. function CheckGridHasData(AGrid: TZJGrid): Boolean;
  276. var
  277. iRow: Integer;
  278. begin
  279. Result := False;
  280. for iRow := 1 to AGrid.RowCount - 1 do
  281. if AGrid.Cells[1, iRow].Text <> '' then
  282. begin
  283. Result := True;
  284. Break;
  285. end;
  286. end;
  287. function CheckBeginCodeAvailable(const ACode: string): Boolean;
  288. var
  289. sgsCode: TStrings;
  290. iCode: Integer;
  291. begin
  292. sgsCode := TStringList.Create;
  293. try
  294. sgsCode.Delimiter := '-';
  295. sgsCode.DelimitedText := ACode;
  296. Result := TryStrToInt(sgsCode[sgsCode.Count - 1], iCode);
  297. finally
  298. sgsCode.Free;
  299. end;
  300. end;
  301. begin
  302. if not CheckGridHasData(zgPosition) then
  303. ErrorMessage('请输入部位数量复核数据!')
  304. else if not CheckGridHasData(zgBills) then
  305. ErrorMessage('请输入清单编号等数据!')
  306. else if leBeginCode.Text = '' then
  307. ErrorMessage('请输入起始编号!')
  308. else if not CheckBeginCodeAvailable(leBeginCode.Text) then
  309. ErrorMessage('请输入规范的起始部位编号,如1或1-1等。')
  310. else
  311. ModalResult := mrOK;
  312. end;
  313. procedure TBatchInsertBillsForm.zgPositionMouseDown(Sender: TObject;
  314. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  315. begin
  316. if Button = mbRight then
  317. begin
  318. dxpmInsertBills.Tag := 0;
  319. dxpmInsertBills.PopupFromCursorPos;
  320. end;
  321. end;
  322. procedure TBatchInsertBillsForm.zgBillsCellTextChanged(Sender: TObject;
  323. Col, Row: Integer);
  324. var
  325. sB_Code: string;
  326. Rec: TsdDataRecord;
  327. begin
  328. if (Col = 1) and (Row > 0) then
  329. begin
  330. zgBills.Cells[Col, Row].Align := gaCenterLeft;
  331. sB_Code := zgBills.Cells[Col, Row].Text;
  332. if sB_Code = '' then Exit;
  333. with TProjectData(FBillsCompileData.ProjectData).DealBillsData do
  334. Rec := sddDealBills.Locate('B_Code', sB_Code);
  335. if not Assigned(Rec) then
  336. begin
  337. with FBillsCompileData.BillsData do
  338. Rec := sddBills.Locate('B_Code', sB_Code);
  339. end;
  340. if Assigned(Rec) then
  341. begin
  342. zgBills.Cells[iGclNameCol, Row].Text := Rec.ValueByName('Name').AsString;
  343. zgBills.Cells[iGclNameCol, Row].Align := gaCenterLeft;
  344. zgBills.Cells[iGclUnitsCol, Row].Text := Rec.ValueByName('Units').AsString;
  345. zgBills.Cells[iGclUnitsCol, Row].Align := gaCenterCenter;
  346. zgBills.Cells[iGclPriceCol, Row].Font.Name := 'smartSimSun';
  347. zgBills.Cells[iGclPriceCol, Row].Text := Rec.ValueByName('Price').AsString;
  348. zgBills.Cells[iGclPriceCol, Row].Align := gaCenterRight;
  349. zgBills.Cells[iGclDrawingCodeCol, Row].Align := gaCenterLeft;
  350. end;
  351. end
  352. else if (Col = iGclNameCol) and (Row > 0) then
  353. zgBills.Cells[iGclNameCol, Row].Align := gaCenterLeft
  354. else if (Col = iGclPriceCol) and (Row > 0) then
  355. begin
  356. zgBills.Cells[Col, Row].Value := PriceRoundTo(
  357. StrToFloatDef(zgBills.Cells[Col, Row].Text, 0));
  358. zgBills.Cells[Col, Row].Align := gaCenterRight;
  359. end
  360. else if (Col = iGclDrawingCodeCol) and (Row > 0) then
  361. zgBills.Cells[Col, Row].Align := gaCenterLeft;
  362. end;
  363. procedure TBatchInsertBillsForm.zgDealBillsMouseDown(Sender: TObject;
  364. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  365. var
  366. i: Integer;
  367. begin
  368. if (Button = mbLeft) and (ssDouble in Shift) then
  369. for i := 1 to 4 do
  370. zgBills.Cells[i, zgBills.CurRow].Text := zgDealBills.Cells[i, zgDealBills.CurRow].Text;
  371. end;
  372. procedure TBatchInsertBillsForm.LoadParentCode;
  373. var
  374. Parent: TsdIDTreeNode;
  375. begin
  376. if FInsertType = itChild then
  377. Parent := FBillsCompileData.BillsCompileTree.Selected
  378. else if FInsertType = itNextSibling then
  379. Parent := FBillsCompileData.BillsCompileTree.Selected.Parent;
  380. FParentCode := Parent.Rec.ValueByName('Code').AsString;
  381. end;
  382. function TBatchInsertBillsForm.GetXmjCode(ARow: Integer): string;
  383. var
  384. iBeginCode: Integer;
  385. begin
  386. iBeginCode := StrToIntDef(leBeginCode.Text, 1);
  387. Result := Format('%s-%d', [FParentCode, iBeginCode + ARow - 1]);
  388. end;
  389. procedure TBatchInsertBillsForm.leBeginCodeKeyPress(Sender: TObject;
  390. var Key: Char);
  391. begin
  392. ValidInteger(Key);
  393. end;
  394. procedure TBatchInsertBillsForm.zgPositionCellTextChanged(Sender: TObject;
  395. Col, Row: Integer);
  396. var
  397. value: Double;
  398. begin
  399. if (Row > 0) then
  400. begin
  401. if (Col >= iBwBillsQtyCol) then
  402. begin
  403. if (TryStrToFloat(zgPosition.Cells[Col, Row].Text, value)) then
  404. begin
  405. zgPosition.Cells[Col, Row].Value := QuantityRoundTo(Value);
  406. zgPosition.Cells[Col, Row].Align := gaCenterRight;
  407. end
  408. else
  409. zgPosition.Cells[Col, Row].Value := '';
  410. end
  411. else if (Col > zgPosition.FixedColCount) then
  412. zgPosition.Cells[Col, Row].Align := gaCenterLeft;
  413. end;
  414. end;
  415. procedure TBatchInsertBillsForm.dxpmInsertBillsPopup(Sender: TObject);
  416. begin
  417. dxpmInsertBills.ItemLinks[6].Visible := (dxpmInsertBills.Tag = 1);
  418. SetDxBtnAction(actnInsertCol, MainForm.dxbtnInsertCol);
  419. SetDxBtnAction(actnDeleteRow, MainForm.dxbtnDeleteRow);
  420. end;
  421. procedure TBatchInsertBillsForm.actnInsertColExecute(Sender: TObject);
  422. begin
  423. zgPosition.ColCount := zgPosition.ColCount + 1;
  424. zgPosition.Cells[zgPosition.ColCount-1, 0].Text := '清单' + IntToStr(zgPosition.ColCount - iBwBillsQtyCol);
  425. zgPosition.ColWidths[zgPosition.ColCount-1] := 50;
  426. zgBills.RowCount := zgPosition.ColCount - (iBwBillsQtyCol - 1);
  427. zgBills.Cells[0, zgBills.RowCount-1].Text := '清单' + IntToStr(zgBills.RowCount-1);
  428. end;
  429. procedure TBatchInsertBillsForm.zgBillsMouseDown(Sender: TObject;
  430. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  431. begin
  432. if Button = mbRight then
  433. begin
  434. dxpmInsertBills.Tag := 1;
  435. dxpmInsertBills.PopupFromCursorPos;
  436. end;
  437. end;
  438. procedure TBatchInsertBillsForm.actnInsertColUpdate(Sender: TObject);
  439. begin
  440. if dxpmInsertBills.Tag = 0 then
  441. TAction(Sender).Caption := '插入一列'
  442. else
  443. TAction(Sender).Caption := '插入一行';
  444. end;
  445. procedure TBatchInsertBillsForm.FormResize(Sender: TObject);
  446. begin
  447. SupportManager.ConfigInfo.BatchInsertFrmHeight := ClientHeight;
  448. SupportManager.ConfigInfo.BatchInsertFrmWidth := ClientWidth;
  449. pnlBills.Height := Trunc((pnlPositon_Bills.Height - pnlOther.Height)/22*9);
  450. end;
  451. procedure TBatchInsertBillsForm.actnDeleteRowExecute(Sender: TObject);
  452. begin
  453. zgPosition.DeleteRow(zgPosition.CurRow);
  454. end;
  455. procedure TBatchInsertBillsForm.actnDeleteRowUpdate(Sender: TObject);
  456. begin
  457. TAction(Sender).Visible := dxpmInsertBills.Tag = 0;
  458. end;
  459. end.