AddLeafBillsFrm.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. unit AddLeafBillsFrm;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, ExtCtrls, ZJGrid, DataBase, ZjIDTree, ConstMethodUnit,
  6. dxBar;
  7. type
  8. TAddLeafBillsForm = class(TForm)
  9. pnlPosition: TPanel;
  10. zgPosition: TZJGrid;
  11. pnlPositionSpr: TPanel;
  12. lblPostion: TLabel;
  13. pnlBills: TPanel;
  14. lblBills: TLabel;
  15. zgBills: TZJGrid;
  16. pnlBillsSpr: TPanel;
  17. pnlOther: TPanel;
  18. leBeginCode: TLabeledEdit;
  19. btnOK: TButton;
  20. btnCancel: TButton;
  21. leDrawingCode: TLabeledEdit;
  22. dxpmAddLeafBills: TdxBarPopupMenu;
  23. procedure zgPositionCustomPaste(Sender: TObject; ABounds: TRect;
  24. ASourSheet: TZjSheet);
  25. procedure zgBillsCustomPaste(Sender: TObject; ABounds: TRect;
  26. ASourSheet: TZjSheet);
  27. procedure btnOKClick(Sender: TObject);
  28. procedure zgPositionMouseDown(Sender: TObject; Button: TMouseButton;
  29. Shift: TShiftState; X, Y: Integer);
  30. private
  31. FBillsData: TDMDataBase;
  32. procedure ResetPositionGridHead;
  33. procedure ResetBillsGridHead;
  34. procedure PasteData(AGrid: TZJGrid; ABounds: TRect; ASourSheet: TZjSheet);
  35. procedure SetRowAndColumnCount(AGrid: TZJGrid; ASourSheet: TZjSheet);
  36. function ReplaceLastNum(const ACode: string; ARow: Integer): string;
  37. procedure SetXmjNodeData(ARow: Integer; ANode: TZjIDTreeNode);
  38. procedure AddXmjNode(ARow, AParentID, ANextSiblingID: Integer);
  39. procedure AddBillsNode(AQtyRow, ARow: Integer; AParent: TZjIDTreeNode);
  40. procedure AddBillsNodes(AQtyRow: Integer; AParent: TZjIDTreeNode);
  41. procedure BatchAddBillsNodes(AParentID, ANextSiblingID: Integer);
  42. public
  43. procedure Init;
  44. procedure Execute(ABillsData: TDMDataBase);
  45. end;
  46. procedure AddLeafBills(ABillsData: TDMDataBase);
  47. implementation
  48. uses ScBillsTree;
  49. {$R *.dfm}
  50. procedure AddLeafBills(ABillsData: TDMDataBase);
  51. var
  52. AddLeafBillsForm: TAddLeafBillsForm;
  53. begin
  54. AddLeafBillsForm := TAddLeafBillsForm.Create(nil);
  55. try
  56. AddLeafBillsForm.Init;
  57. if AddLeafBillsForm.ShowModal = mrOK then
  58. AddLeafBillsForm.Execute(ABillsData);
  59. finally
  60. AddLeafBillsForm.Free;
  61. end;
  62. end;
  63. { TAddLeafBillsForm }
  64. procedure TAddLeafBillsForm.AddXmjNode(ARow, AParentID, ANextSiblingID: Integer);
  65. var
  66. ztnNode: TZjIDTreeNode;
  67. begin
  68. if zgPosition.Cells[1, ARow].Text = '' then Exit;
  69. ztnNode := FBillsData.BillsTree.Add(AParentID, ANextSiblingID);
  70. SetXmjNodeData(ARow, ztnNode);
  71. AddBillsNodes(ARow, ztnNode);
  72. end;
  73. procedure TAddLeafBillsForm.BatchAddBillsNodes(AParentID, ANextSiblingID: Integer);
  74. var
  75. iRow: Integer;
  76. begin
  77. for iRow := 1 to zgPosition.RowCount - 1 do
  78. AddXmjNode(iRow, AParentID, ANextSiblingID);
  79. end;
  80. procedure TAddLeafBillsForm.Execute(ABillsData: TDMDataBase);
  81. begin
  82. FBillsData := ABillsData;
  83. with FBillsData.BillsTree do
  84. if Assigned(Selected) and Assigned(Selected.Parent) then
  85. BatchAddBillsNodes(Selected.ParentID, Selected.NextSiblingID);
  86. end;
  87. procedure TAddLeafBillsForm.Init;
  88. begin
  89. ResetPositionGridHead;
  90. ResetBillsGridHead;
  91. end;
  92. procedure TAddLeafBillsForm.PasteData(AGrid: TZJGrid; ABounds: TRect;
  93. ASourSheet: TZjSheet);
  94. var
  95. iCol, iRow: Integer;
  96. begin
  97. for iRow := 0 to ASourSheet.RowCount - 1 do
  98. for iCol := 0 to ASourSheet.ColCount - 1 do
  99. begin
  100. with AGrid.Cells[iCol + ABounds.Left , iRow + ABounds.Top] do
  101. if CanEdit then Text := ASourSheet.Values[iCol, iRow];
  102. end;
  103. end;
  104. function TAddLeafBillsForm.ReplaceLastNum(const ACode: string;
  105. ARow: Integer): string;
  106. var
  107. sgs: TStringList;
  108. begin
  109. sgs := TStringList.Create;
  110. try
  111. sgs.Delimiter := '-';
  112. sgs.DelimitedText := ACode;
  113. sgs[sgs.Count - 1] := IntToStr(StrToInt(sgs[sgs.Count - 1]) + ARow - 1);
  114. Result := sgs.DelimitedText;
  115. finally
  116. sgs.Free;
  117. end;
  118. end;
  119. procedure TAddLeafBillsForm.ResetPositionGridHead;
  120. var
  121. iCol: Integer;
  122. begin
  123. zgPosition.Cells[1, 0].Text := '部位';
  124. zgPosition.ColWidths[1] := 120;
  125. for iCol := 2 to zgPosition.ColCount - 1 do
  126. begin
  127. zgPosition.Cells[iCol, 0].Text := '清单' + IntToStr(iCol - 1);
  128. zgPosition.ColWidths[iCol] := 50;
  129. end;
  130. end;
  131. procedure TAddLeafBillsForm.SetRowAndColumnCount(AGrid: TZJGrid;
  132. ASourSheet: TZjSheet);
  133. begin
  134. if AGrid.ColCount < ASourSheet.ColCount + AGrid.CurCol then
  135. AGrid.ColCount := ASourSheet.ColCount + AGrid.CurCol;
  136. if AGrid.RowCount < ASourSheet.RowCount + AGrid.CurRow + 1 then
  137. AGrid.RowCount := ASourSheet.RowCount + AGrid.CurRow + 1;
  138. end;
  139. procedure TAddLeafBillsForm.SetXmjNodeData(ARow: Integer;
  140. ANode: TZjIDTreeNode);
  141. begin
  142. with FBillsData do
  143. if cdsBills.FindKey([ANode.ID]) then
  144. begin
  145. cdsBills.Edit;
  146. cdsBillsCode.AsString := ReplaceLastNum(leBeginCode.Text, ARow);
  147. cdsBillsName.AsString := zgPosition.Cells[1, ARow].Text;
  148. cdsBillsDrawingCode.AsString := leDrawingCode.Text;
  149. cdsBills.Post;
  150. end;
  151. end;
  152. procedure TAddLeafBillsForm.zgPositionCustomPaste(Sender: TObject;
  153. ABounds: TRect; ASourSheet: TZjSheet);
  154. begin
  155. SetRowAndColumnCount(TZJGrid(Sender), ASourSheet);
  156. ResetPositionGridHead;
  157. PasteData(TZJGrid(Sender), ABounds, ASourSheet);
  158. zgBills.RowCount := zgPosition.ColCount - 1;
  159. ResetBillsGridHead;
  160. end;
  161. procedure TAddLeafBillsForm.zgBillsCustomPaste(Sender: TObject;
  162. ABounds: TRect; ASourSheet: TZjSheet);
  163. begin
  164. SetRowAndColumnCount(TZJGrid(Sender), ASourSheet);
  165. PasteData(TZJGrid(Sender), ABounds, ASourSheet);
  166. end;
  167. procedure TAddLeafBillsForm.AddBillsNodes(AQtyRow: Integer;
  168. AParent: TZjIDTreeNode);
  169. var
  170. iRow: Integer;
  171. begin
  172. for iRow := 1 to zgBills.RowCount - 1 do
  173. AddBillsNode(AQtyRow, iRow, AParent);
  174. end;
  175. procedure TAddLeafBillsForm.AddBillsNode(AQtyRow, ARow: Integer;
  176. AParent: TZjIDTreeNode);
  177. var
  178. ztnNode: TZjIDTreeNode;
  179. fQuantity: Double;
  180. begin
  181. if (zgBills.Cells[1, ARow].Text = '') or
  182. (zgPosition.Cells[ARow + 1, AQtyRow].Text = '') or
  183. not TryStrToFloat(zgPosition.Cells[ARow + 1, AQtyRow].Text, fQuantity) then Exit;
  184. ztnNode := FBillsData.BillsTree.Add(AParent.ID, -1);
  185. with FBillsData do
  186. if cdsBills.FindKey([ztnNode.ID]) then
  187. begin
  188. cdsBills.Edit;
  189. cdsBillsB_Code.AsString := zgBills.Cells[1, ARow].Text;
  190. cdsBillsName.AsString := zgBills.Cells[2, ARow].Text;
  191. cdsBillsUnits.AsString := zgBills.Cells[3, ARow].Text;
  192. cdsBillsQuantity.AsString := zgPosition.Cells[ARow + 1, AQtyRow].Text;
  193. cdsBills.Post;
  194. end;
  195. end;
  196. procedure TAddLeafBillsForm.ResetBillsGridHead;
  197. var
  198. iRow: Integer;
  199. begin
  200. zgBills.Cells[1, 0].Text := '编号';
  201. zgBills.ColWidths[1] := 80;
  202. zgBills.Cells[2, 0].Text := '名称';
  203. zgBills.ColWidths[2] := 120;
  204. zgBills.Cells[3, 0].Text := '单位';
  205. zgBills.ColWidths[3] := 60;
  206. for iRow := 1 to zgBills.RowCount - 1 do
  207. zgBills.Cells[0, iRow].Text := '清单' + IntToStr(iRow);
  208. end;
  209. procedure TAddLeafBillsForm.btnOKClick(Sender: TObject);
  210. function CheckGridHasData(AGrid: TZJGrid): Boolean;
  211. var
  212. iRow: Integer;
  213. begin
  214. Result := False;
  215. for iRow := 1 to AGrid.RowCount - 1 do
  216. if AGrid.Cells[1, iRow].Text <> '' then
  217. begin
  218. Result := True;
  219. Break;
  220. end;
  221. end;
  222. function CheckBeginCodeAvailable(const ACode: string): Boolean;
  223. var
  224. sgsCode: TStrings;
  225. iCode: Integer;
  226. begin
  227. sgsCode := TStringList.Create;
  228. try
  229. sgsCode.Delimiter := '-';
  230. sgsCode.DelimitedText := ACode;
  231. Result := TryStrToInt(sgsCode[sgsCode.Count - 1], iCode);
  232. finally
  233. sgsCode.Free;
  234. end;
  235. end;
  236. begin
  237. if not CheckGridHasData(zgPosition) then
  238. MessageError(Handle, '请输入部位数量复核数据!')
  239. else if not CheckGridHasData(zgBills) then
  240. MessageError(Handle, '请输入清单编号等数据!')
  241. else if leBeginCode.Text = '' then
  242. MessageError(Handle, '请输入起始编号!')
  243. else if not CheckBeginCodeAvailable(leBeginCode.Text) then
  244. MessageError(Handle, '请输入规范的起始部位编号,如1或1-1等。')
  245. else
  246. ModalResult := mrOK;
  247. end;
  248. procedure TAddLeafBillsForm.zgPositionMouseDown(Sender: TObject;
  249. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  250. begin
  251. if Button = mbRight then
  252. dxpmAddLeafBills.PopupFromCursorPos;
  253. end;
  254. end.