rmBGLExecutionDm.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. unit rmBGLExecutionDm;
  2. interface
  3. uses
  4. SysUtils, Classes, ProjectData, DB, DBClient, sdIDTree, sdDB;
  5. type
  6. TGclNode = class
  7. private
  8. FQuantity: Double;
  9. FTotalPrice: Double;
  10. FPrice: Double;
  11. FUnits: string;
  12. FName: string;
  13. FB_Code: string;
  14. public
  15. property B_Code: string read FB_Code write FB_Code;
  16. property Name: string read FName write FName;
  17. property Units: string read FUnits write FUnits;
  18. property Price: Double read FPrice write FPrice;
  19. property Quantity: Double read FQuantity write FQuantity;
  20. property TotalPrice: Double read FTotalPrice write FTotalPrice;
  21. end;
  22. TXmjNode = class
  23. private
  24. FCode: string;
  25. FName: string;
  26. FUnits: string;
  27. FGclList: TList;
  28. public
  29. constructor Create;
  30. destructor Destroy; override;
  31. function AddGclNode(AB_Code: string): TGclNode;
  32. property Code: string read FCode write FCode;
  33. property Name: string read FName write FName;
  34. property Units: string read FUnits write FUnits;
  35. property GclList: TList read FGclList;
  36. end;
  37. TBGLNode = class
  38. private
  39. FBGLCode: string;
  40. FXmjList: TList;
  41. public
  42. constructor Create;
  43. destructor Destroy; override;
  44. function FindXmjNode(ACode: string): TXmjNode;
  45. function AddXmjNode(ACode: string): TXmjNode;
  46. property BGLCode: string read FBGLCode write FBGLCode;
  47. property XmjList: TList read FXmjList;
  48. end;
  49. TrmBGLExecutionData = class(TDataModule)
  50. cdsBGLExecution: TClientDataSet;
  51. cdsBGLExecutionBGLCode: TWideStringField;
  52. cdsBGLExecutionCode: TStringField;
  53. cdsBGLExecutionB_Code: TStringField;
  54. cdsBGLExecutionName: TWideStringField;
  55. cdsBGLExecutionUnits: TWideStringField;
  56. cdsBGLExecutionPrice: TFloatField;
  57. cdsBGLExecutionQuantity: TFloatField;
  58. cdsBGLExecutionTotalPrice: TFloatField;
  59. private
  60. FProjectData: TProjectData;
  61. FBGLList: TList;
  62. function FindBGLNode(ABGLCode: string): TBGLNode;
  63. function AddBGLNode(ABGLCode: string): TBGLNode;
  64. procedure FilterGclBills(ANode: TsdIDTreeNode);
  65. procedure FilterBills(ANode: TsdIDTreeNode);
  66. procedure WriteData;
  67. public
  68. function AssignData(AProjectData: TProjectData): TDataSet;
  69. end;
  70. implementation
  71. uses
  72. ZhAPI, UtilMethods, PhaseData, BillsCompileDm;
  73. {$R *.dfm}
  74. { TrmBGLExecutionData }
  75. function TrmBGLExecutionData.AddBGLNode(ABGLCode: string): TBGLNode;
  76. var
  77. Node: TBGLNode;
  78. i, iIndex: Integer;
  79. begin
  80. // 新增
  81. Result := TBGLNode.Create;
  82. Result.BGLCode := ABGLCode;
  83. // List中根据顺序新增
  84. iIndex := FBGLList.Count;
  85. for i := 0 to FBGLList.Count - 1 do
  86. begin
  87. Node := TBGLNode(FBGLList.Items[i]);
  88. if ABGLCode < Node.BGLCode then
  89. begin
  90. iIndex := i;
  91. Break;
  92. end;
  93. end;
  94. FBGLList.Insert(iIndex, Result);
  95. end;
  96. function TrmBGLExecutionData.AssignData(
  97. AProjectData: TProjectData): TDataSet;
  98. begin
  99. cdsBGLExecution.DisableControls;
  100. cdsBGLExecution.Active := True;
  101. cdsBGLExecution.EmptyDataSet;
  102. FBGLList := TList.Create;
  103. try
  104. FProjectData := AProjectData;
  105. FilterBills(AProjectData.BillsMeasureData.BillsMeasureTree.FirstNode);
  106. WriteData;
  107. Result := cdsBGLExecution;
  108. finally
  109. ClearObjects(FBGLList);
  110. FBGLList.Free;
  111. cdsBGLExecution.EnableControls;
  112. end;
  113. end;
  114. procedure TrmBGLExecutionData.FilterBills(ANode: TsdIDTreeNode);
  115. begin
  116. if not Assigned(ANode) then Exit;
  117. if not ANode.HasChildren then
  118. FilterGclBills(ANode);
  119. FilterBills(ANode.FirstChild);
  120. FilterBills(ANode.NextSibling);
  121. end;
  122. procedure TrmBGLExecutionData.FilterGclBills(ANode: TsdIDTreeNode);
  123. procedure AddBGLData(ABGLCode, ABGLNum: string);
  124. var
  125. BGLNode: TBGLNode;
  126. XmjNode: TXmjNode;
  127. GclNode: TGclNode;
  128. LeafXmjParent: TsdIDTreeNode;
  129. begin
  130. // BGL
  131. BGLNode := FindBGLNode(ABGLCode);
  132. if not Assigned(BGLNode) then
  133. BGLNode := AddBGLNode(ABGLCode);
  134. // Xmj
  135. with FProjectData.BillsCompileData do
  136. LeafXmjParent := BillsCompileTree.FindNode(GetLeafXmjParentID(ANode.ID));
  137. XmjNode := BGLNode.FindXmjNode(LeafXmjParent.Rec.ValueByName('Code').AsString);
  138. if not Assigned(XmjNode) then
  139. XmjNode := BGLNode.AddXmjNode(LeafXmjParent.Rec.ValueByName('Code').AsString);
  140. XmjNode.Name := LeafXmjParent.Rec.ValueByName('Name').AsString;
  141. XmjNode.Units := LeafXmjParent.Rec.ValueByName('Units').AsString;
  142. // Gcl
  143. GclNode := XmjNode.AddGclNode(ANode.Rec.ValueByName('B_Code').AsString);
  144. GclNode.Name := ANode.Rec.ValueByName('Name').AsString;
  145. GclNode.FUnits := ANode.Rec.ValueByName('Units').AsString;
  146. GclNode.Price := ANode.Rec.ValueByName('Price').AsFloat;
  147. GclNode.Quantity := StrToFloatDef(ABGLNum, 0);
  148. GclNode.TotalPrice := GclNode.Quantity * GclNode.Price;
  149. end;
  150. var
  151. StageRec: TsdDataRecord;
  152. sgsCode, sgsNum: TStringList;
  153. i: Integer;
  154. begin
  155. StageRec := FProjectData.PhaseData.StageData.StageRecord(ANode.ID);
  156. if (ANode.Rec.ValueByName('B_Code').AsString = '') or
  157. (not Assigned(StageRec)) or
  158. (StageRec.ValueByName('QcBGLCode').AsString = '') then Exit;
  159. sgsCode := TStringList.Create;
  160. sgsNum := TStringList.Create;
  161. try
  162. sgsCode.Delimiter := ';';
  163. sgsCode.DelimitedText := StageRec.ValueByName('QcBGLCode').AsString;
  164. sgsNum.Delimiter := ';';
  165. sgsNum.DelimitedText := StageRec.ValueByName('QcBGLNum').AsString;
  166. for i := 0 to sgsCode.Count - 1 do
  167. AddBGLData(sgsCode[i], sgsNum[i]);
  168. finally
  169. sgsNum.Free;
  170. sgsCode.Free;
  171. end;
  172. end;
  173. function TrmBGLExecutionData.FindBGLNode(ABGLCode: string): TBGLNode;
  174. var
  175. iBGL: Integer;
  176. BGLNode: TBGLNode;
  177. begin
  178. Result := nil;
  179. for iBGL := 0 to FBGLList.Count - 1 do
  180. begin
  181. BGLNode := TBGLNode(FBGLList.Items[iBGL]);
  182. if SameText(BGLNode.BGLCode, ABGLCode) then
  183. begin
  184. Result := BGLNode;
  185. Break;
  186. end;
  187. end;
  188. end;
  189. procedure TrmBGLExecutionData.WriteData;
  190. procedure WriteGclData(AXmjNode: TXmjNode);
  191. var
  192. iGcl: Integer;
  193. GclNode: TGclNode;
  194. begin
  195. for iGcl := 0 to AXmjNode.GclList.Count - 1 do
  196. begin
  197. GclNode := TGclNode(AXmjNode.GclList.Items[iGcl]);
  198. cdsBGLExecution.Append;
  199. cdsBGLExecutionB_Code.AsString := GclNode.B_Code;
  200. cdsBGLExecutionName.AsString := GclNode.Name;
  201. cdsBGLExecutionUnits.AsString := GclNode.Units;
  202. cdsBGLExecutionPrice.AsFloat := GclNode.Price;
  203. cdsBGLExecutionQuantity.AsFloat := GclNode.Quantity;
  204. cdsBGLExecutionTotalPrice.AsFloat := GclNode.TotalPrice;
  205. cdsBGLExecution.Post;
  206. end;
  207. end;
  208. procedure WriteXmjData(ABGLNode: TBGLNode);
  209. var
  210. iXmj: Integer;
  211. XmjNode: TXmjNode;
  212. begin
  213. for iXmj := 0 to ABGLNode.XmjList.Count - 1 do
  214. begin
  215. XmjNode := TXmjNode(ABGLNode.XmjList.Items[iXmj]);
  216. cdsBGLExecution.Append;
  217. cdsBGLExecutionCode.AsString := XmjNode.Code;
  218. cdsBGLExecutionName.AsString := XmjNode.Name;
  219. cdsBGLExecutionUnits.AsString := XmjNode.Units;
  220. cdsBGLExecution.Post;
  221. WriteGclData(XmjNode);
  222. end;
  223. end;
  224. var
  225. iBGL: Integer;
  226. BGLNode: TBGLNode;
  227. begin
  228. for iBGL := 0 to FBGLList.Count - 1 do
  229. begin
  230. BGLNode := TBGLNode(FBGLList.Items[iBGL]);
  231. cdsBGLExecution.Append;
  232. cdsBGLExecutionBGLCode.AsString := BGLNode.BGLCode;
  233. cdsBGLExecution.Post;
  234. WriteXmjData(BGLNode);
  235. end;
  236. end;
  237. { TXmjNode }
  238. function TXmjNode.AddGclNode(AB_Code: string): TGclNode;
  239. var
  240. Node: TGclNode;
  241. i, iIndex: Integer;
  242. sB_Code1, sB_Code2: string;
  243. begin
  244. // 新增
  245. Result := TGclNode.Create;
  246. Result.B_Code := AB_Code;
  247. // List中根据顺序新增
  248. iIndex := FGclList.Count;
  249. sB_Code1 := B_CodeToIndexCode(AB_Code);
  250. for i := 0 to FGclList.Count - 1 do
  251. begin
  252. Node := TGclNode(FGclList.Items[i]);
  253. sB_Code2 := B_CodeToIndexCode(Node.B_Code);
  254. if sB_Code1 < sB_Code2 then
  255. begin
  256. iIndex := i;
  257. Break;
  258. end;
  259. end;
  260. FGclList.Insert(iIndex, Result);
  261. end;
  262. constructor TXmjNode.Create;
  263. begin
  264. FGclList := TList.Create;
  265. end;
  266. destructor TXmjNode.Destroy;
  267. begin
  268. ClearObjects(FGclList);
  269. FGclList.Free;
  270. inherited;
  271. end;
  272. { TBGLNode }
  273. function TBGLNode.AddXmjNode(ACode: string): TXmjNode;
  274. var
  275. Node: TXmjNode;
  276. i, iIndex: Integer;
  277. sCode1, sCode2: string;
  278. begin
  279. // 新增
  280. Result := TXmjNode.Create;
  281. Result.Code := ACode;
  282. // List中根据顺序新增
  283. iIndex := FXmjList.Count;
  284. sCode1 := ConvertDigitCode(ACode, 3);
  285. for i := 0 to FXmjList.Count - 1 do
  286. begin
  287. Node := TXmjNode(FXmjList.Items[i]);
  288. sCode2 := ConvertDigitCode(Node.Code, 3);
  289. if sCode1 < sCode2 then
  290. begin
  291. iIndex := i;
  292. Break;
  293. end;
  294. end;
  295. FXmjList.Insert(iIndex, Result);
  296. end;
  297. constructor TBGLNode.Create;
  298. begin
  299. FXmjList := TList.Create;
  300. end;
  301. destructor TBGLNode.Destroy;
  302. begin
  303. ClearObjects(FXmjList);
  304. FXmjList.Free;
  305. inherited;
  306. end;
  307. function TBGLNode.FindXmjNode(ACode: string): TXmjNode;
  308. var
  309. iXmj: Integer;
  310. XmjNode: TXmjNode;
  311. begin
  312. Result := nil;
  313. for iXmj := 0 to FXmjList.Count - 1 do
  314. begin
  315. XmjNode := TXmjNode(FXmjList.Items[iXmj]);
  316. if SameText(XmjNode.Code, ACode) then
  317. begin
  318. Result := XmjNode;
  319. Break;
  320. end;
  321. end;
  322. end;
  323. end.