BatchInsertBillsFrm.pas 15 KB

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