BatchInsertBillsFrm.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. type
  8. TInsertType = (itChild, itNextSibling);
  9. TBatchInsertBillsForm = class(TForm)
  10. pnlButton: TPanel;
  11. btnOK: TButton;
  12. btnCancel: TButton;
  13. dxpmInsertBills: TdxBarPopupMenu;
  14. pnlAllGrid: TPanel;
  15. pnlPositon_Bills: TPanel;
  16. pnlPosition: TPanel;
  17. lblPostion: TLabel;
  18. zgPosition: TZJGrid;
  19. pnlPositionSpr: TPanel;
  20. pnlBills: TPanel;
  21. lblBills: TLabel;
  22. zgBills: TZJGrid;
  23. pnlBillsSpr: TPanel;
  24. pnlDealBills: TPanel;
  25. pnlOther: TPanel;
  26. leBeginCode: TLabeledEdit;
  27. leDrawingCode: TLabeledEdit;
  28. zgDealBills: TZJGrid;
  29. pnlDealBillsSpr: TPanel;
  30. lblDealBills: TLabel;
  31. lblHint: TLabel;
  32. sgdDealBills: TsdGridDBA;
  33. udBeginCode: TUpDown;
  34. procedure zgPositionCustomPaste(Sender: TObject; ABounds: TRect;
  35. ASourSheet: TZjSheet);
  36. procedure zgBillsCustomPaste(Sender: TObject; ABounds: TRect;
  37. ASourSheet: TZjSheet);
  38. procedure btnOKClick(Sender: TObject);
  39. procedure zgPositionMouseDown(Sender: TObject; Button: TMouseButton;
  40. Shift: TShiftState; X, Y: Integer);
  41. procedure zgBillsCellTextChanged(Sender: TObject; Col, Row: Integer);
  42. procedure zgDealBillsMouseDown(Sender: TObject; Button: TMouseButton;
  43. Shift: TShiftState; X, Y: Integer);
  44. procedure leBeginCodeKeyPress(Sender: TObject; var Key: Char);
  45. procedure zgPositionCellTextChanged(Sender: TObject; Col,
  46. Row: Integer);
  47. private
  48. FInsertType: TInsertType;
  49. FBillsCompileData: TBillsCompileData;
  50. FParentCode: string;
  51. procedure ResetPositionGridHead;
  52. procedure ResetBillsGridHead;
  53. procedure PasteData(AGrid: TZJGrid; ABounds: TRect; ASourSheet: TZjSheet);
  54. procedure SetRowAndColumnCount(AGrid: TZJGrid; ASourSheet: TZjSheet);
  55. procedure LoadParentCode;
  56. function ReplaceLastNum(const ACode: string; ARow: Integer): string;
  57. function GetXmjCode(ARow: Integer): string;
  58. procedure AddXmjNode(ARow, AParentID, ANextSiblingID: Integer);
  59. procedure AddBillsNode(AQtyRow, ARow: Integer; AParent: TsdIDTreeNode);
  60. procedure AddBillsNodes(AQtyRow: Integer; AParent: TsdIDTreeNode);
  61. procedure BatchAddBillsNodes(AParentID, ANextSiblingID: Integer);
  62. public
  63. procedure Init(ABillsCompileData: TBillsCompileData; AInsertType: TInsertType);
  64. procedure Execute;
  65. end;
  66. procedure AddLeafBills(ABillsCompileData: TBillsCompileData; AInsertType: TInsertType);
  67. implementation
  68. uses
  69. sdDB, UtilMethods, ProjectData;
  70. {$R *.dfm}
  71. procedure AddLeafBills(ABillsCompileData: TBillsCompileData; AInsertType: TInsertType);
  72. var
  73. InsertBillsForm: TBatchInsertBillsForm;
  74. begin
  75. InsertBillsForm := TBatchInsertBillsForm.Create(nil);
  76. try
  77. InsertBillsForm.Init(ABillsCompileData, AInsertType);
  78. if InsertBillsForm.ShowModal = mrOK then
  79. InsertBillsForm.Execute;
  80. finally
  81. InsertBillsForm.Free;
  82. end;
  83. end;
  84. { TAddLeafBillsForm }
  85. procedure TBatchInsertBillsForm.AddXmjNode(ARow, AParentID, ANextSiblingID: Integer);
  86. var
  87. stnNode: TsdIDTreeNode;
  88. begin
  89. if zgPosition.Cells[1, ARow].Text = '' then Exit;
  90. stnNode := FBillsCompileData.BillsCompileTree.Add(AParentID, ANextSiblingID);
  91. with stnNode.Rec do
  92. begin
  93. ValueByName('Code').AsString := GetXmjCode(ARow);{ReplaceLastNum(leBeginCode.Text, ARow);}
  94. ValueByName('Name').AsString := zgPosition.Cells[1, ARow].Text;
  95. ValueByName('DrawingCode').AsString := leDrawingCode.Text;
  96. end;
  97. AddBillsNodes(ARow, stnNode);
  98. end;
  99. procedure TBatchInsertBillsForm.BatchAddBillsNodes(AParentID, ANextSiblingID: Integer);
  100. var
  101. iRow: Integer;
  102. begin
  103. for iRow := 1 to zgPosition.RowCount - 1 do
  104. AddXmjNode(iRow, AParentID, ANextSiblingID);
  105. end;
  106. procedure TBatchInsertBillsForm.Execute;
  107. begin
  108. with FBillsCompileData.BillsCompileTree do
  109. begin
  110. if FInsertType = itChild then
  111. begin
  112. if not Assigned(Selected) then Exit;
  113. BatchAddBillsNodes(Selected.ID, -1);
  114. FBillsCompileData.Calculate(Selected.ID);
  115. end
  116. else if FInsertType = itNextSibling then
  117. begin
  118. if not Assigned(Selected.Parent) then Exit;
  119. BatchAddBillsNodes(Selected.ParentID, Selected.NextSiblingID);
  120. FBillsCompileData.Calculate(Selected.ParentID);
  121. end;
  122. end;
  123. end;
  124. procedure TBatchInsertBillsForm.Init(ABillsCompileData: TBillsCompileData;
  125. AInsertType: TInsertType);
  126. begin
  127. FBillsCompileData := ABillsCompileData;
  128. sgdDealBills.DataView := TProjectData(ABillsCompileData.ProjectData).DealBillsData.sdvDealBills;
  129. FInsertType := AInsertType;
  130. if FInsertType = itChild then
  131. Caption := '批量插入子项'
  132. else if FInsertType = itNextSibling then
  133. Caption := '批量插入后项';
  134. ResetPositionGridHead;
  135. ResetBillsGridHead;
  136. LoadParentCode;
  137. end;
  138. procedure TBatchInsertBillsForm.PasteData(AGrid: TZJGrid; ABounds: TRect;
  139. ASourSheet: TZjSheet);
  140. var
  141. iCol, iRow: Integer;
  142. begin
  143. for iRow := 0 to ASourSheet.RowCount - 1 do
  144. for iCol := 0 to ASourSheet.ColCount - 1 do
  145. begin
  146. with AGrid.Cells[iCol + ABounds.Left , iRow + ABounds.Top] do
  147. if CanEdit then Text := ASourSheet.Values[iCol, iRow];
  148. end;
  149. end;
  150. function TBatchInsertBillsForm.ReplaceLastNum(const ACode: string;
  151. ARow: Integer): string;
  152. var
  153. sgs: TStringList;
  154. begin
  155. sgs := TStringList.Create;
  156. try
  157. sgs.Delimiter := '-';
  158. sgs.DelimitedText := ACode;
  159. sgs[sgs.Count - 1] := IntToStr(StrToInt(sgs[sgs.Count - 1]) + ARow - 1);
  160. Result := sgs.DelimitedText;
  161. finally
  162. sgs.Free;
  163. end;
  164. end;
  165. procedure TBatchInsertBillsForm.ResetPositionGridHead;
  166. var
  167. iCol: Integer;
  168. begin
  169. zgPosition.Cells[1, 0].Text := '部位';
  170. zgPosition.ColWidths[1] := 120;
  171. for iCol := 2 to zgPosition.ColCount - 1 do
  172. begin
  173. zgPosition.Cells[iCol, 0].Text := '清单' + IntToStr(iCol - 1);
  174. zgPosition.ColWidths[iCol] := 50;
  175. end;
  176. end;
  177. procedure TBatchInsertBillsForm.SetRowAndColumnCount(AGrid: TZJGrid;
  178. ASourSheet: TZjSheet);
  179. begin
  180. if AGrid.ColCount < ASourSheet.ColCount + AGrid.CurCol then
  181. AGrid.ColCount := ASourSheet.ColCount + AGrid.CurCol;
  182. if AGrid.RowCount < ASourSheet.RowCount + AGrid.CurRow + 1 then
  183. AGrid.RowCount := ASourSheet.RowCount + AGrid.CurRow + 1;
  184. end;
  185. procedure TBatchInsertBillsForm.zgPositionCustomPaste(Sender: TObject;
  186. ABounds: TRect; ASourSheet: TZjSheet);
  187. begin
  188. SetRowAndColumnCount(TZJGrid(Sender), ASourSheet);
  189. ResetPositionGridHead;
  190. PasteData(TZJGrid(Sender), ABounds, ASourSheet);
  191. zgBills.RowCount := zgPosition.ColCount - 1;
  192. ResetBillsGridHead;
  193. end;
  194. procedure TBatchInsertBillsForm.zgBillsCustomPaste(Sender: TObject;
  195. ABounds: TRect; ASourSheet: TZjSheet);
  196. begin
  197. SetRowAndColumnCount(TZJGrid(Sender), ASourSheet);
  198. PasteData(TZJGrid(Sender), ABounds, ASourSheet);
  199. end;
  200. procedure TBatchInsertBillsForm.AddBillsNodes(AQtyRow: Integer;
  201. AParent: TsdIDTreeNode);
  202. var
  203. iRow: Integer;
  204. begin
  205. for iRow := 1 to zgBills.RowCount - 1 do
  206. AddBillsNode(AQtyRow, iRow, AParent);
  207. end;
  208. procedure TBatchInsertBillsForm.AddBillsNode(AQtyRow, ARow: Integer;
  209. AParent: TsdIDTreeNode);
  210. var
  211. stnNode: TsdIDTreeNode;
  212. fQuantity: Double;
  213. begin
  214. if (zgBills.Cells[1, ARow].Text = '') or
  215. (zgPosition.Cells[ARow + 1, AQtyRow].Text = '') or
  216. not TryStrToFloat(zgPosition.Cells[ARow + 1, AQtyRow].Text, fQuantity) then Exit;
  217. stnNode := FBillsCompileData.BillsCompileTree.Add(AParent.ID, -1);
  218. with stnNode.Rec do
  219. begin
  220. ValueByName('B_Code').AsString := zgBills.Cells[1, ARow].Text;
  221. ValueByName('Name').AsString := zgBills.Cells[2, ARow].Text;
  222. ValueByName('Units').AsString := zgBills.Cells[3, ARow].Text;
  223. ValueByName('Price').AsFloat := StrToFloatDef(zgBills.Cells[4, ARow].Text, 0);
  224. ValueByName('OrgQuantity').AsFloat := StrToFloatDef(zgPosition.Cells[ARow + 1, AQtyRow].Text, 0);
  225. end;
  226. end;
  227. procedure TBatchInsertBillsForm.ResetBillsGridHead;
  228. var
  229. iRow: Integer;
  230. begin
  231. zgBills.Cells[1, 0].Text := '编号';
  232. zgBills.ColWidths[1] := 80;
  233. zgBills.Cells[2, 0].Text := '名称';
  234. zgBills.ColWidths[2] := 120;
  235. zgBills.Cells[3, 0].Text := '单位';
  236. zgBills.ColWidths[3] := 60;
  237. zgBills.Cells[4, 0].Text := '单价';
  238. zgBills.ColWidths[4] := 60;
  239. for iRow := 1 to zgBills.RowCount - 1 do
  240. zgBills.Cells[0, iRow].Text := '清单' + IntToStr(iRow);
  241. end;
  242. procedure TBatchInsertBillsForm.btnOKClick(Sender: TObject);
  243. function CheckGridHasData(AGrid: TZJGrid): Boolean;
  244. var
  245. iRow: Integer;
  246. begin
  247. Result := False;
  248. for iRow := 1 to AGrid.RowCount - 1 do
  249. if AGrid.Cells[1, iRow].Text <> '' then
  250. begin
  251. Result := True;
  252. Break;
  253. end;
  254. end;
  255. function CheckBeginCodeAvailable(const ACode: string): Boolean;
  256. var
  257. sgsCode: TStrings;
  258. iCode: Integer;
  259. begin
  260. sgsCode := TStringList.Create;
  261. try
  262. sgsCode.Delimiter := '-';
  263. sgsCode.DelimitedText := ACode;
  264. Result := TryStrToInt(sgsCode[sgsCode.Count - 1], iCode);
  265. finally
  266. sgsCode.Free;
  267. end;
  268. end;
  269. begin
  270. if not CheckGridHasData(zgPosition) then
  271. ErrorMessage('请输入部位数量复核数据!')
  272. else if not CheckGridHasData(zgBills) then
  273. ErrorMessage('请输入清单编号等数据!')
  274. else if leBeginCode.Text = '' then
  275. ErrorMessage('请输入起始编号!')
  276. else if not CheckBeginCodeAvailable(leBeginCode.Text) then
  277. ErrorMessage('请输入规范的起始部位编号,如1或1-1等。')
  278. else
  279. ModalResult := mrOK;
  280. end;
  281. procedure TBatchInsertBillsForm.zgPositionMouseDown(Sender: TObject;
  282. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  283. begin
  284. if Button = mbRight then
  285. dxpmInsertBills.PopupFromCursorPos;
  286. end;
  287. procedure TBatchInsertBillsForm.zgBillsCellTextChanged(Sender: TObject;
  288. Col, Row: Integer);
  289. var
  290. sB_Code: string;
  291. Rec: TsdDataRecord;
  292. begin
  293. if (Col = 1) and (Row > 0) then
  294. begin
  295. zgBills.Cells[Col, Row].Align := gaCenterLeft;
  296. sB_Code := zgBills.Cells[Col, Row].Text;
  297. if sB_Code = '' then Exit;
  298. with FBillsCompileData.BillsData do
  299. Rec := sddBills.Locate('B_Code', sB_Code);
  300. if Assigned(Rec) then
  301. begin
  302. zgBills.Cells[2, Row].Text := Rec.ValueByName('Name').AsString;
  303. zgBills.Cells[2, Row].Align := gaCenterLeft;
  304. zgBills.Cells[3, Row].Text := Rec.ValueByName('Units').AsString;
  305. zgBills.Cells[3, Row].Align := gaCenterLeft;
  306. zgBills.Cells[3, Row].Font.Name := 'smartSimSun';
  307. zgBills.Cells[4, Row].Text := Rec.ValueByName('Price').AsString;
  308. zgBills.Cells[4, Row].Align := gaCenterRight;
  309. end;
  310. end
  311. else if (Col = 4) and (Row > 0) then
  312. begin
  313. zgBills.Cells[Col, Row].Value := PriceRoundTo(
  314. StrToFloatDef(zgBills.Cells[Col, Row].Text, 0));
  315. zgBills.Cells[Col, Row].Align := gaCenterRight;
  316. end;
  317. end;
  318. procedure TBatchInsertBillsForm.zgDealBillsMouseDown(Sender: TObject;
  319. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  320. var
  321. i: Integer;
  322. begin
  323. if (Button = mbLeft) and (ssDouble in Shift) then
  324. for i := 1 to 4 do
  325. zgBills.Cells[i, zgBills.CurRow].Text := zgDealBills.Cells[i, zgDealBills.CurRow].Text;
  326. end;
  327. procedure TBatchInsertBillsForm.LoadParentCode;
  328. var
  329. Parent: TsdIDTreeNode;
  330. begin
  331. if FInsertType = itChild then
  332. Parent := FBillsCompileData.BillsCompileTree.Selected
  333. else if FInsertType = itNextSibling then
  334. Parent := FBillsCompileData.BillsCompileTree.Selected.Parent;
  335. FParentCode := Parent.Rec.ValueByName('Code').AsString;
  336. end;
  337. function TBatchInsertBillsForm.GetXmjCode(ARow: Integer): string;
  338. var
  339. iBeginCode: Integer;
  340. begin
  341. iBeginCode := StrToIntDef(leBeginCode.Text, 1);
  342. Result := Format('%s-%d', [FParentCode, iBeginCode + ARow - 1]);
  343. end;
  344. procedure TBatchInsertBillsForm.leBeginCodeKeyPress(Sender: TObject;
  345. var Key: Char);
  346. begin
  347. ValidInteger(Key);
  348. end;
  349. procedure TBatchInsertBillsForm.zgPositionCellTextChanged(Sender: TObject;
  350. Col, Row: Integer);
  351. begin
  352. if (Col > 1) and (Row > 0) then
  353. begin
  354. zgPosition.Cells[Col, Row].Value := QuantityRoundTo(
  355. StrToFloatDef(zgPosition.Cells[Col, Row].Text, 0));
  356. zgPosition.Cells[Col, Row].Align := gaCenterRight;
  357. end;
  358. end;
  359. end.