BatchInsertBillsFrm.pas 14 KB

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