MCacheTree.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. unit MCacheTree;
  2. // CacheTree For Measure, Inherit From CacheTree
  3. interface
  4. uses
  5. Classes, CacheTree, Math, ZhAPI, sdIDTree;
  6. type
  7. // For Import Temp Excel
  8. TBillsCacheNode = class(TCacheNode)
  9. private
  10. FLevelCode: string;
  11. FCode: string;
  12. FB_Code: string;
  13. FName: string;
  14. FUnits: string;
  15. FCanDelete: Boolean;
  16. FQuantity: Double;
  17. FDgnQuantity1: Double;
  18. FDgnQuantity2: Double;
  19. FMemoStr: string;
  20. FPrice: Double;
  21. FDrawingCode: string;
  22. public
  23. property LevelCode: string read FLevelCode write FLevelCode;
  24. property Code: string read FCode write FCode;
  25. property B_Code: string read FB_Code write FB_Code;
  26. property Name: string read FName write FName;
  27. property Units: string read FUnits write FUnits;
  28. property CanDelete: Boolean read FCanDelete write FCanDelete;
  29. property Price: Double read FPrice write FPrice;
  30. property Quantity: Double read FQuantity write FQuantity;
  31. property DgnQuantity1: Double read FDgnQuantity1 write FDgnQuantity1;
  32. property DgnQuantity2: Double read FDgnQuantity2 write FDgnQuantity2;
  33. property DrawingCode: string read FDrawingCode write FDrawingCode;
  34. property MemoStr: string read FMemoStr write FMemoStr;
  35. end;
  36. TBillsCacheTree = class(TCacheTree)
  37. private
  38. FLastNode: TCacheNode;
  39. FSeparateChar: Char;
  40. FAutoSort: Boolean;
  41. FFixedIDNodes: TList;
  42. function GetNewNode(AID: Integer = -1): TBillsCacheNode; overload;
  43. function FindNode(const ACode: string): TBillsCacheNode; overload;
  44. function FindNode(AParent: TBillsCacheNode; const ACode: string): TBillsCacheNode; overload;
  45. function FindParent(const ACode: string): TBillsCacheNode;
  46. function FindNextSibling(const ACode: string): TBillsCacheNode;
  47. procedure SetSeparateChar(const Value: Char);
  48. public
  49. constructor Create; override;
  50. destructor Destroy; override;
  51. function AddNode(AParent: TCacheNode; ANextSibling: TCacheNode = nil; AFixedID: Integer = -1): TBillsCacheNode;
  52. function AddNodeByCode(const ACode: string; AFixedID: Integer = -1): TBillsCacheNode;
  53. function AddLeafBillsNode(const AB_Code: string): TBillsCacheNode;
  54. function FindXmjChild(AParent: TBillsCacheNode; const ACode, AName: string): TBillsCacheNode;
  55. function FindGclChild(AParent: TBillsCacheNode; const AB_Code, AName, AUnits: string; APrice: Double): TBillsCacheNode;
  56. function FindFixedIDNode(AID: Integer): TBillsCacheNode;
  57. // Only for Debugging lot of Data
  58. procedure SaveTreeToFile(const AFileName: string);
  59. property SeparateChar: Char read FSeparateChar write SetSeparateChar;
  60. property AutoSort: Boolean read FAutoSort write FAutoSort;
  61. property FixedIDNodes: TList read FFixedIDNodes;
  62. end;
  63. // 此树仅用于导入工程量清单,禁止作为它用
  64. // 如须使用应遵守以下两点:1.完全清楚相关的导入方法类及需求 2.派生子类。
  65. TGclCacheNode = class(TCacheNode)
  66. private
  67. FB_Code: string;
  68. FName: string;
  69. FUnits: string;
  70. FPrice: Double;
  71. FQuantity: Double;
  72. public
  73. property B_Code: string read FB_Code write FB_Code;
  74. property Name: string read FName write FName;
  75. property Units: string read FUnits write FUnits;
  76. property Price: Double read FPrice write FPrice;
  77. property Quantity: Double read FQuantity write FQuantity;
  78. end;
  79. TGclCacheTree = class(TCacheTree)
  80. private
  81. FLastBlank1: TGclCacheNode;
  82. FLastNode: TGclCacheNode;
  83. function AddNodeByName(const AName: string): TGclCacheNode;
  84. function AddNodeByB_Code(const AB_Code: string): TGclCacheNode;
  85. protected
  86. function GetNewNode: TCacheNode; override;
  87. public
  88. function AddNodeByData(const AB_Code, AName: string): TGclCacheNode;
  89. procedure SaveTreeToFile(const AFileName: string);
  90. end;
  91. {For Report Memory(Gather or Compare Projects)}
  92. TDoubleArray = array of Double;
  93. TReportCacheNode = class(TCacheNode)
  94. private
  95. FCode: string;
  96. FB_Code: string;
  97. FName: string;
  98. FUnits: string;
  99. FMemoStr: string;
  100. FXiangCode: string;
  101. FMuCode: string;
  102. FJieCode: string;
  103. FXiMuCode: string;
  104. FPrice: Double;
  105. FQuantity: Double;
  106. FTotalPrice: Double;
  107. FRatioPercent: Double; // 0号台账 - 各项费用所占比例
  108. FDesignQuantity1: Double;
  109. FDesignQuantity2: Double;
  110. FAddQcQuantity: Double;
  111. FAddPcTotalPrice: Double;
  112. FAddQcTotalPrice: Double;
  113. FAddDealQuantity: Double;
  114. FAddDealTotalPrice: Double;
  115. FAddPcQuantity: Double;
  116. FAddRatioPercent: Double; // 决算 - 各项费用所占比例
  117. FDealDesignQuantity1: Double;
  118. FDealDesignQuantity2: Double;
  119. FCDesignQuantity1: Double;
  120. FCDesignQuantity2: Double;
  121. FPDQuantity: Double;
  122. FPDTotalPrice: Double;
  123. FPDDesignQuantity1: Double;
  124. FPDDesignQuantity2: Double;
  125. FPDDesignPrice: Double;
  126. FCDDQuantity: Double;
  127. FCDDTotalPrice: Double;
  128. FCDDDesignQuantity1: Double;
  129. FCDDDesignQuantity2: Double;
  130. FCDDDesignPrice: Double;
  131. FABTotalPrice: Double;
  132. FABQuantity: Double;
  133. FABDesignQuantity1: Double;
  134. FABDesignQuantity2: Double;
  135. FABDesignPrice: Double;
  136. FProjectCount: Integer;
  137. FP_TotalPrice: TDoubleArray;
  138. FP_Quantity: TDoubleArray;
  139. FP_Price: TDoubleArray;
  140. FP_DgnQuantity1: TDoubleArray;
  141. FP_DgnQuantity2: TDoubleArray;
  142. procedure ResolveCode;
  143. function GetDoubleArrayTotal(ADoubleArray: TDoubleArray): Double;
  144. procedure SetCode(const Value: string);
  145. function GetGatherP_TotalPrice: Double;
  146. function GetAddGatherQuantity: Double;
  147. function GetAddGatherTotalPrice: Double;
  148. public
  149. constructor Create(ACacheTree: TCacheTree; AID, AProjectCount: Integer);
  150. property Code: string read FCode write SetCode;
  151. property B_Code: string read FB_Code write FB_Code;
  152. property Name: string read FName write FName;
  153. property Units: string read FUnits write FUnits;
  154. property MemoStr: string read FMemoStr write FMemoStr;
  155. property XiangCode: string read FXiangCode;
  156. property MuCode: string read FMuCode;
  157. property JieCode: string read FJieCode;
  158. property XiMuCode: string read FXiMuCode;
  159. // 用于汇总多个项目的合同、变更(数量、金额)
  160. property Price: Double read FPrice write FPrice;
  161. // 0号台账合同
  162. property Quantity: Double read FQuantity write FQuantity;
  163. property TotalPrice: Double read FTotalPrice write FTotalPrice;
  164. property RatioPercent: Double read FRatioPercent write FRatioPercent;
  165. property DesignQuantity1: Double read FDesignQuantity1 write FDesignQuantity1;
  166. property DesignQuantity2: Double read FDesignQuantity2 write FDesignQuantity2;
  167. // 累计各值
  168. property AddDealQuantity: Double read FAddDealQuantity write FAddDealQuantity;
  169. property AddDealTotalPrice: Double read FAddDealTotalPrice write FAddDealTotalPrice;
  170. property AddQcQuantity: Double read FAddQcQuantity write FAddQcQuantity;
  171. property AddQcTotalPrice: Double read FAddQcTotalPrice write FAddQcTotalPrice;
  172. property AddPcQuantity: Double read FAddPcQuantity write FAddPcQuantity;
  173. property AddPcTotalPrice: Double read FAddPcTotalPrice write FAddPcTotalPrice;
  174. property AddGatherQuantity: Double read GetAddGatherQuantity;
  175. property AddGatherTotalPrice: Double read GetAddGatherTotalPrice;
  176. property AddRatioPercent: Double read FAddRatioPercent write FAddRatioPercent;
  177. // 合同&变更 设计数量
  178. property DealDesignQuantity1: Double read FDealDesignQuantity1 write FDealDesignQuantity1;
  179. property DealDesignQuantity2: Double read FDealDesignQuantity2 write FDealDesignQuantity2;
  180. property CDesignQuantity1: Double read FCDesignQuantity1 write FCDesignQuantity1;
  181. property CDesignQuantity2: Double read FCDesignQuantity2 write FCDesignQuantity2;
  182. // ----仅用于汇总生成决算02表----
  183. // 初步设计 Preliminary Design
  184. property PDQuantity: Double read FPDQuantity write FPDQuantity;
  185. property PDTotalPrice: Double read FPDTotalPrice write FPDTotalPrice;
  186. property PDDesignQuantity1: Double read FPDDesignQuantity1 write FPDDesignQuantity1;
  187. property PDDesignQuantity2: Double read FPDDesignQuantity2 write FPDDesignQuantity2;
  188. property PDDesignPrice: Double read FPDDesignPrice write FPDDesignPrice;
  189. // 施工图设计 Construction Drawing Design
  190. property CDDQuantity: Double read FCDDQuantity write FCDDQuantity;
  191. property CDDTotalPrice: Double read FCDDTotalPrice write FCDDTotalPrice;
  192. property CDDDesignQuantity1: Double read FCDDDesignQuantity1 write FCDDDesignQuantity1;
  193. property CDDDesignQuantity2: Double read FCDDDesignQuantity2 write FCDDDesignQuantity2;
  194. property CDDDesignPrice: Double read FCDDDesignPrice write FCDDDesignPrice;
  195. // ------------------------------
  196. // ----仅用于汇总生成决算02表(部颁)----
  197. // 批准概(预算)算 Approved Budget
  198. property ABQuantity: Double read FABQuantity write FABQuantity;
  199. property ABTotalPrice: Double read FABTotalPrice write FABTotalPrice;
  200. property ABDesignQuantity1: Double read FABDesignQuantity1 write FABDesignQuantity1;
  201. property ABDesignQuantity2: Double read FABDesignQuantity2 write FABDesignQuantity2;
  202. property ABDesignPrice: Double read FABDesignPrice write FABDesignPrice;
  203. // ------------------------------------
  204. // 用于记录多个项目的数量、单价、金额、设计数量
  205. property P_Price: TDoubleArray read FP_Price write FP_Price;
  206. property P_Quantity: TDoubleArray read FP_Quantity write FP_Quantity;
  207. property P_TotalPrice: TDoubleArray read FP_TotalPrice write FP_TotalPrice;
  208. property P_DgnQuantity1: TDoubleArray read FP_DgnQuantity1 write FP_DgnQuantity1;
  209. property P_DgnQuantity2: TDoubleArray read FP_DgnQuantity2 write FP_DgnQuantity2;
  210. property GatherP_TotalPrice: Double read GetGatherP_TotalPrice;
  211. property ProjectCount: Integer read FProjectCount;
  212. end;
  213. TStringArray = array of string;
  214. TReportCacheTree = class(TCacheTree)
  215. private
  216. FProjectCount: Integer;
  217. FGatherCacheNode: TReportCacheNode;
  218. FProjectName: TStringArray;
  219. function GetNewNode(AProjectCount: Integer): TReportCacheNode; overload;
  220. public
  221. constructor Create(AProjectCount: Integer);
  222. destructor Destroy; override;
  223. function AddNode(AParent: TCacheNode; ANextSibling: TCacheNode = nil): TReportCacheNode;
  224. function FindNextSibling(AParent: TCacheNode; ACode, AB_Code: string): TReportCacheNode;
  225. function FindNode(AParent: TCacheNode; ACode, AB_Code: string): TReportCacheNode; overload;
  226. function FindNode(AParent: TCacheNode; AName: string): TReportCacheNode; overload;
  227. function FindNode(AParent: TCacheNode; ACode, AB_Code, AName: string): TReportCacheNode; overload;
  228. procedure ReCalcGatherData;
  229. // 调用此方法先须先调用ReCalcGatherData
  230. // RatioPercent = 金额/总金额,这里的总金额取GatherCacheNode的金额,故须先汇总计算GatherCacheNode。
  231. procedure ReCalcRatioPercent;
  232. // Only for Debugging lot of Data
  233. procedure SaveTreeToFile(const AFileName: string);
  234. property ProjectCount: Integer read FProjectCount;
  235. property GatherCacheNode: TReportCacheNode read FGatherCacheNode;
  236. property ProjectName: TStringArray read FProjectName write FProjectName;
  237. end;
  238. TapDoubleArray = array [1..50] of Double;
  239. TAllPhaseCacheNode = class(TCacheNode)
  240. private
  241. FCode: string;
  242. FB_Code: string;
  243. FName: string;
  244. FUnits: string;
  245. FPrice: Double;
  246. FQuantity: Double;
  247. FTotalPrice: Double;
  248. FMemoStr: string;
  249. public
  250. FP_Quantity: TapDoubleArray;
  251. FP_TotalPrice: TapDoubleArray;
  252. property Code: string read FCode write FCode;
  253. property B_Code: string read FB_Code write FB_Code;
  254. property Name: string read FName write FName;
  255. property Units: string read FUnits write FUnits;
  256. property Price: Double read FPrice write FPrice;
  257. property Quantity: Double read FQuantity write FQuantity;
  258. property TotalPrice: Double read FTotalPrice write FTotalPrice;
  259. property MemoStr: string read FMemoStr write FMemoStr;
  260. end;
  261. // 仅用于汇总同一项目的不同期数据
  262. TAllPhaseCacheTree = class(TCacheTree)
  263. private
  264. function GetNewNode(AID: Integer): TAllPhaseCacheNode;
  265. public
  266. function AddNode(AID: Integer; AParent: TCacheNode; ANextSibling: TCacheNode = nil): TAllPhaseCacheNode;
  267. function FindNode(AID: Integer): TAllPhaseCacheNode;
  268. // Only for Debugging lot of Data
  269. procedure SaveTreeToFile(const AFileName: string);
  270. end;
  271. implementation
  272. uses
  273. SysUtils, UtilMethods;
  274. { TBillsCacheTree }
  275. function TBillsCacheTree.AddNodeByCode(const ACode: string;
  276. AFixedID: Integer): TBillsCacheNode;
  277. var
  278. Parent, NextSibling: TBillsCacheNode;
  279. begin
  280. Result := FindNode(ACode);
  281. FLastNode := Result;
  282. if Assigned(Result) then Exit;
  283. Parent := FindParent(ACode);
  284. if AutoSort then
  285. NextSibling := FindNextSibling(ACode)
  286. else
  287. NextSibling := nil;
  288. Result := AddNode(Parent, NextSibling, AFixedID);
  289. Result.FLevelCode := ACode;
  290. FLastNode := Result;
  291. end;
  292. function TBillsCacheTree.FindNode(const ACode: string): TBillsCacheNode;
  293. begin
  294. Result := FindNode(TBillsCacheNode(Root), ACode);
  295. end;
  296. function TBillsCacheTree.FindNextSibling(
  297. const ACode: string): TBillsCacheNode;
  298. var
  299. Parent, Node: TBillsCacheNode;
  300. sCodeID, sCodeID2: string;
  301. begin
  302. Parent := FindParent(ACode);
  303. if Assigned(Parent) then
  304. Node := TBillsCacheNode(Parent.FirstChild)
  305. else
  306. Node := TBillsCacheNode(Root.FirstChild);
  307. Result := nil;
  308. sCodeID := ConvertDigitCode(ACode, 3, '-');
  309. while Assigned(Node) do
  310. begin
  311. sCodeID2 := ConvertDigitCode(Node.LevelCode, 3, SeparateChar);
  312. if sCodeID < sCodeID2 then
  313. begin
  314. Result := Node;
  315. Break;
  316. end;
  317. Node := TBillsCacheNode(Node.NextSibling);
  318. end;
  319. end;
  320. function TBillsCacheTree.FindNode(AParent: TBillsCacheNode;
  321. const ACode: string): TBillsCacheNode;
  322. begin
  323. Result := TBillsCacheNode(AParent.FirstChild);
  324. while Assigned(Result) do
  325. begin
  326. if Result.LevelCode = ACode then
  327. Break
  328. else if Pos(Result.LevelCode + SeparateChar, ACode) = 1 then
  329. begin
  330. Result := FindNode(Result, ACode);
  331. Break;
  332. end
  333. else
  334. Result := TBillsCacheNode(Result.NextSibling);
  335. end;
  336. end;
  337. function TBillsCacheTree.FindParent(const ACode: string): TBillsCacheNode;
  338. var
  339. sCode: string;
  340. begin
  341. Result := nil;
  342. sCode := GetPrefixOfCode(ACode, SeparateChar);
  343. while (Result = nil) and (sCode <> '') do
  344. begin
  345. Result := FindNode(sCode);
  346. sCode := GetPrefixOfCode(sCode, SeparateChar);
  347. end;
  348. end;
  349. function TBillsCacheTree.GetNewNode(AID: Integer): TBillsCacheNode;
  350. begin
  351. if AID = -1 then
  352. Result := TBillsCacheNode.Create(Self, GetNewNodeID)
  353. else
  354. Result := TBillsCacheNode.Create(Self, AID);
  355. CacheNodes.Add(Result);
  356. if Result.ID < 100 then
  357. FFixedIDNodes.Add(Result);
  358. end;
  359. function TBillsCacheTree.AddNode(AParent, ANextSibling: TCacheNode;
  360. AFixedID: Integer): TBillsCacheNode;
  361. begin
  362. Result := GetNewNode(AFixedID);
  363. if Assigned(ANextSibling) then
  364. ANextSibling.InsertPreSibling(Result)
  365. else if Assigned(AParent) then
  366. AParent.InsertChild(Result)
  367. else
  368. Root.InsertChild(Result);
  369. end;
  370. function TBillsCacheTree.AddLeafBillsNode(const AB_Code: string): TBillsCacheNode;
  371. function GetLastXmjParent: TBillsCacheNode;
  372. begin
  373. Result := TBillsCacheNode(FLastNode);
  374. while Assigned(Result) and (Result.B_Code <> '') do
  375. Result := TBillsCacheNode(Result.Parent);
  376. end;
  377. var
  378. Parent: TBillsCacheNode;
  379. begin
  380. Parent := GetLastXmjParent;
  381. Result := AddNodeByCode(Parent.Code + '-' + AB_Code, -1);
  382. end;
  383. procedure TBillsCacheTree.SetSeparateChar(const Value: Char);
  384. var
  385. I: Integer;
  386. Node: TBillsCacheNode;
  387. begin
  388. for I := 0 to CacheNodes.Count - 1 do
  389. begin
  390. Node := TBillsCacheNode(CacheNodes.Items[I]);
  391. Node.FLevelCode := StringReplace(Node.FLevelCode, FSeparateChar, Value, [rfReplaceAll]);
  392. end;
  393. FSeparateChar := Value;
  394. end;
  395. procedure TBillsCacheTree.SaveTreeToFile(const AFileName: string);
  396. var
  397. sgs: TStringList;
  398. I: Integer;
  399. Node: TBillsCacheNode;
  400. begin
  401. sgs := TStringList.Create;
  402. try
  403. for I := 0 to CacheNodes.Count - 1 do
  404. begin
  405. Node := TBillsCacheNode(CacheNodes.Items[I]);
  406. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; Code: %s; B_Code: %s; Name: %s;',
  407. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.Code, Node.B_Code, Node.Name]));
  408. end;
  409. sgs.SaveToFile(AFileName);
  410. finally
  411. sgs.Free;
  412. end;
  413. end;
  414. function TBillsCacheTree.FindGclChild(AParent: TBillsCacheNode;
  415. const AB_Code, AName, AUnits: string; APrice: Double): TBillsCacheNode;
  416. var
  417. vChild: TBillsCacheNode;
  418. begin
  419. Result := nil;
  420. if Assigned(AParent) then
  421. vChild := TBillsCacheNode(AParent.FirstChild)
  422. else
  423. vChild := TBillsCacheNode(Root.FirstChild);
  424. while Assigned(vChild) and not Assigned(Result) do
  425. begin
  426. if SameText(AB_Code, vChild.B_Code) and
  427. SameText(AName, vChild.Name) and
  428. SameText(AUnits, vChild.Units) and
  429. (APrice = vChild.Price) then
  430. Result := vChild;
  431. vChild := TBillsCacheNode(vChild.NextSibling);
  432. end;
  433. end;
  434. function TBillsCacheTree.FindXmjChild(AParent: TBillsCacheNode;
  435. const ACode, AName: string): TBillsCacheNode;
  436. var
  437. vChild: TBillsCacheNode;
  438. begin
  439. Result := nil;
  440. if Assigned(AParent) then
  441. vChild := TBillsCacheNode(AParent.FirstChild)
  442. else
  443. vChild := TBillsCacheNode(Root.FirstChild);
  444. while Assigned(vChild) and not Assigned(Result) do
  445. begin
  446. if SameText(ACode, vChild.Code) and SameText(AName, vChild.Name) then
  447. Result := vChild;
  448. vChild := TBillsCacheNode(vChild.NextSibling);
  449. end;
  450. end;
  451. constructor TBillsCacheTree.Create;
  452. begin
  453. inherited;
  454. FFixedIDNodes := TList.Create;
  455. end;
  456. destructor TBillsCacheTree.Destroy;
  457. begin
  458. FFixedIDNodes.Free;
  459. inherited;
  460. end;
  461. function TBillsCacheTree.FindFixedIDNode(AID: Integer): TBillsCacheNode;
  462. var
  463. iNode: Integer;
  464. vNode: TCacheNode;
  465. begin
  466. Result := nil;
  467. for iNode := 0 to FFixedIDNodes.Count - 1 do
  468. begin
  469. vNode := TCacheNode(FFixedIDNodes.Items[iNode]);
  470. if vNode.ID = AID then
  471. begin
  472. Result := TBillsCacheNode(vNode);
  473. Break;
  474. end;
  475. end;
  476. end;
  477. { TReportCacheNode }
  478. constructor TReportCacheNode.Create(ACacheTree: TCacheTree; AID,
  479. AProjectCount: Integer);
  480. begin
  481. inherited Create(ACacheTree, AID);
  482. FProjectCount := AProjectCount;
  483. SetLength(FP_Quantity, AProjectCount);
  484. SetLength(FP_Price, AProjectCount);
  485. SetLength(FP_TotalPrice, AProjectCount);
  486. SetLength(FP_DgnQuantity1, AProjectCount);
  487. SetLength(FP_DgnQuantity2, AProjectCount);
  488. end;
  489. function TReportCacheNode.GetAddGatherQuantity: Double;
  490. begin
  491. Result := AddDealQuantity + AddQcQuantity;
  492. end;
  493. function TReportCacheNode.GetAddGatherTotalPrice: Double;
  494. begin
  495. Result := AddDealTotalPrice + AddQcTotalPrice + AddPcTotalPrice;
  496. end;
  497. function TReportCacheNode.GetDoubleArrayTotal(
  498. ADoubleArray: TDoubleArray): Double;
  499. var
  500. i: Integer;
  501. begin
  502. Result := 0;
  503. for i := Low(ADoubleArray) to High(ADoubleArray) do
  504. Result := Result + ADoubleArray[i];
  505. end;
  506. function TReportCacheNode.GetGatherP_TotalPrice: Double;
  507. begin
  508. Result := GetDoubleArrayTotal(FP_TotalPrice);
  509. end;
  510. procedure TReportCacheNode.ResolveCode;
  511. var
  512. sgs: TStrings;
  513. i: Integer;
  514. begin
  515. sgs := TStringList.Create;
  516. try
  517. sgs.Delimiter := '-';
  518. sgs.DelimitedText := FCode;
  519. FXiangCode := '';
  520. FMuCode := '';
  521. FJieCode := '';
  522. FXiMuCode := '';
  523. case sgs.Count of
  524. 1: FXiangCode := '';
  525. 2: FXiangCode := ChinessNum(StrToIntDef(sgs[1], 0));
  526. 3: FMuCode := sgs[2];
  527. 4: FJieCode := sgs[3];
  528. else
  529. begin
  530. for i := 4 to sgs.Count - 1 do
  531. if FXiMuCode = '' then
  532. FXiMuCode := sgs[i]
  533. else
  534. FXiMuCode := FXiMuCode + '-' + sgs[i];
  535. end;
  536. end;
  537. finally
  538. sgs.Free;
  539. end;
  540. end;
  541. procedure TReportCacheNode.SetCode(const Value: string);
  542. begin
  543. FCode := Value;
  544. ResolveCode;
  545. end;
  546. { TReportCacheTree }
  547. function TReportCacheTree.AddNode(AParent,
  548. ANextSibling: TCacheNode): TReportCacheNode;
  549. begin
  550. Result := GetNewNode(FProjectCount);
  551. if Assigned(ANextSibling) then
  552. ANextSibling.InsertPreSibling(Result)
  553. else if Assigned(AParent) then
  554. AParent.InsertChild(Result)
  555. else
  556. Root.InsertChild(Result);
  557. end;
  558. constructor TReportCacheTree.Create(AProjectCount: Integer);
  559. begin
  560. inherited Create;
  561. FProjectCount := AProjectCount;
  562. FGatherCacheNode := TReportCacheNode.Create(nil, -2, AProjectCount);
  563. SetLength(FProjectName, AProjectCount);
  564. end;
  565. destructor TReportCacheTree.Destroy;
  566. begin
  567. FGatherCacheNode.Free;
  568. inherited;
  569. end;
  570. function TReportCacheTree.FindNextSibling(AParent: TCacheNode; ACode,
  571. AB_Code: string): TReportCacheNode;
  572. var
  573. Node: TReportCacheNode;
  574. sCodeID, sCodeID2, sB_CodeID, sB_CodeID2: string;
  575. begin
  576. if Assigned(AParent) then
  577. Node := TReportCacheNode(AParent.FirstChild)
  578. else
  579. Node := TReportCacheNode(Root.FirstChild);
  580. Result := nil;
  581. if (ACode = '') and (AB_Code = '') then Exit;
  582. sCodeID := ConvertDigitCode(ACode, 3, '-');
  583. sB_CodeID := ConvertDigitCode(AB_Code, 4, '-');
  584. while Assigned(Node) do
  585. begin
  586. sCodeID2 := ConvertDigitCode(Node.Code, 3, '-');
  587. sB_CodeID2 := ConvertDigitCode(Node.B_Code, 4, '-');
  588. if sCodeID < sCodeID2 then
  589. begin
  590. Result := Node;
  591. Break;
  592. end
  593. else if sB_CodeID < sB_CodeID2 then
  594. begin
  595. Result := Node;
  596. Break;
  597. end;
  598. Node := TReportCacheNode(Node.NextSibling);
  599. end;
  600. end;
  601. function TReportCacheTree.FindNode(AParent: TCacheNode; ACode,
  602. AB_Code: string): TReportCacheNode;
  603. var
  604. Node: TReportCacheNode;
  605. begin
  606. if Assigned(AParent) then
  607. Node := TReportCacheNode(AParent.FirstChild)
  608. else
  609. Node := TReportCacheNode(Root.FirstChild);
  610. Result := nil;
  611. while Assigned(Node) do
  612. begin
  613. if (Node.Code = ACode) and (Node.B_Code = AB_Code) then
  614. begin
  615. Result := Node;
  616. Break;
  617. end;
  618. Node := TReportCacheNode(Node.NextSibling);
  619. end;
  620. end;
  621. function TReportCacheTree.FindNode(AParent: TCacheNode;
  622. AName: string): TReportCacheNode;
  623. var
  624. Node: TReportCacheNode;
  625. begin
  626. if Assigned(AParent) then
  627. Node := TReportCacheNode(AParent.FirstChild)
  628. else
  629. Node := TReportCacheNode(Root.FirstChild);
  630. Result := nil;
  631. while Assigned(Node) do
  632. begin
  633. if SameText(Node.Name, AName) then
  634. begin
  635. Result := Node;
  636. Break;
  637. end;
  638. Node := TReportCacheNode(Node.NextSibling);
  639. end;
  640. end;
  641. function TReportCacheTree.FindNode(AParent: TCacheNode; ACode, AB_Code,
  642. AName: string): TReportCacheNode;
  643. var
  644. Node: TReportCacheNode;
  645. begin
  646. if Assigned(AParent) then
  647. Node := TReportCacheNode(AParent.FirstChild)
  648. else
  649. Node := TReportCacheNode(Root.FirstChild);
  650. Result := nil;
  651. while Assigned(Node) do
  652. begin
  653. if SameText(Node.Code, ACode) and SameText(Node.B_Code, AB_Code)
  654. and SameText(Node.Name, AName) then
  655. begin
  656. Result := Node;
  657. Break;
  658. end;
  659. Node := TReportCacheNode(Node.NextSibling);
  660. end;
  661. end;
  662. function TReportCacheTree.GetNewNode(
  663. AProjectCount: Integer): TReportCacheNode;
  664. begin
  665. Result := TReportCacheNode.Create(Self, GetNewNodeID, AProjectCount);
  666. CacheNodes.Add(Result);
  667. end;
  668. procedure TReportCacheTree.ReCalcGatherData;
  669. var
  670. i: Integer;
  671. CacheNode: TReportCacheNode;
  672. begin
  673. FGatherCacheNode.Free;
  674. FGatherCacheNode := TReportCacheNode.Create(nil, -2, FProjectCount);
  675. CacheNode := TReportCacheNode(FirstNode);
  676. while Assigned(CacheNode) do
  677. begin
  678. FGatherCacheNode.TotalPrice := FGatherCacheNode.TotalPrice + CacheNode.TotalPrice;
  679. FGatherCacheNode.AddDealTotalPrice := FGatherCacheNode.AddDealTotalPrice + CacheNode.AddDealTotalPrice;
  680. FGatherCacheNode.AddQcTotalPrice := FGatherCacheNode.AddQcTotalPrice + CacheNode.AddQcTotalPrice;
  681. FGatherCacheNode.AddPcTotalPrice := FGatherCacheNode.AddPcTotalPrice + CacheNode.AddPcTotalPrice;
  682. FGatherCacheNode.PDTotalPrice := FGatherCacheNode.PDTotalPrice + CacheNode.PDTotalPrice;
  683. FGatherCacheNode.CDDTotalPrice := FGatherCacheNode.CDDTotalPrice + CacheNode.CDDTotalPrice;
  684. FGatherCacheNode.ABTotalPrice := FGatherCacheNode.ABTotalPrice + CacheNode.ABTotalPrice;
  685. for i := 0 to FProjectCount - 1 do
  686. FGatherCacheNode.P_TotalPrice[i] := FGatherCacheNode.P_TotalPrice[i] + CacheNode.P_TotalPrice[i];
  687. CacheNode := TReportCacheNode(CacheNode.NextSibling);
  688. end;
  689. end;
  690. procedure TReportCacheTree.ReCalcRatioPercent;
  691. var
  692. i: Integer;
  693. CacheNode: TReportCacheNode;
  694. begin
  695. for i := 0 to CacheNodes.Count - 1 do
  696. begin
  697. CacheNode := TReportCacheNode(CacheNodes.Items[i]);
  698. if GatherCacheNode.TotalPrice <> 0 then
  699. CacheNode.RatioPercent := AdvRoundTo(CacheNode.TotalPrice/GatherCacheNode.TotalPrice*100);
  700. if GatherCacheNode.AddGatherTotalPrice <> 0 then
  701. CacheNode.AddRatioPercent := AdvRoundTo(CacheNode.AddGatherTotalPrice/GatherCacheNode.AddGatherTotalPrice*100);
  702. end;
  703. end;
  704. procedure TReportCacheTree.SaveTreeToFile(const AFileName: string);
  705. var
  706. sgs: TStringList;
  707. I: Integer;
  708. Node: TReportCacheNode;
  709. begin
  710. sgs := TStringList.Create;
  711. try
  712. for I := 0 to CacheNodes.Count - 1 do
  713. begin
  714. Node := TReportCacheNode(CacheNodes.Items[I]);
  715. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; Code: %s; B_Code: %s; Name: %s;',
  716. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.Code, Node.B_Code, Node.Name]));
  717. end;
  718. sgs.SaveToFile(AFileName);
  719. finally
  720. sgs.Free;
  721. end;
  722. end;
  723. { TAllPhaseCacheTree }
  724. function TAllPhaseCacheTree.AddNode(AID: Integer; AParent,
  725. ANextSibling: TCacheNode): TAllPhaseCacheNode;
  726. begin
  727. Result := GetNewNode(AID);
  728. if Assigned(ANextSibling) then
  729. ANextSibling.InsertPreSibling(Result)
  730. else if Assigned(AParent) then
  731. AParent.InsertChild(Result)
  732. else
  733. Root.InsertChild(Result);
  734. end;
  735. function TAllPhaseCacheTree.FindNode(AID: Integer): TAllPhaseCacheNode;
  736. var
  737. i: Integer;
  738. Node: TAllPhaseCacheNode;
  739. begin
  740. Result := nil;
  741. for i := 0 to CacheNodes.Count - 1 do
  742. begin
  743. Node := TAllPhaseCacheNode(CacheNodes.Items[i]);
  744. if Node.ID = AID then
  745. begin
  746. Result := Node;
  747. Break;
  748. end;
  749. end;
  750. end;
  751. function TAllPhaseCacheTree.GetNewNode(
  752. AID: Integer): TAllPhaseCacheNode;
  753. begin
  754. Result := TAllPhaseCacheNode.Create(Self, AID);
  755. CacheNodes.Add(Result);
  756. end;
  757. procedure TAllPhaseCacheTree.SaveTreeToFile(const AFileName: string);
  758. var
  759. sgs: TStringList;
  760. I: Integer;
  761. Node: TAllPhaseCacheNode;
  762. begin
  763. sgs := TStringList.Create;
  764. try
  765. for I := 0 to CacheNodes.Count - 1 do
  766. begin
  767. Node := TAllPhaseCacheNode(CacheNodes.Items[I]);
  768. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; Code: %s; B_Code: %s; Name: %s;',
  769. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.Code, Node.B_Code, Node.Name]));
  770. end;
  771. sgs.SaveToFile(AFileName);
  772. finally
  773. sgs.Free;
  774. end;
  775. end;
  776. { TGclCacheTree }
  777. function TGclCacheTree.AddNodeByB_Code(
  778. const AB_Code: string): TGclCacheNode;
  779. function FindParent: TGclCacheNode;
  780. begin
  781. Result := FLastNode;
  782. while Assigned(Result) and (Result <> Root) and (Result.B_Code <> '') and (Pos(Result.B_Code + '-', AB_Code) <> 1) do
  783. Result := TGclCacheNode(Result.Parent);
  784. end;
  785. var
  786. vParent: TGclCacheNode;
  787. begin
  788. vParent := FindParent;
  789. Result := TGclCacheNode(AddNode(vParent));
  790. FLastNode := Result;
  791. end;
  792. function TGclCacheTree.AddNodeByData(const AB_Code,
  793. AName: string): TGclCacheNode;
  794. begin
  795. if AB_Code = '' then
  796. Result := AddNodeByName(AName)
  797. else
  798. Result := AddNodeByB_Code(AB_Code);
  799. end;
  800. function TGclCacheTree.AddNodeByName(const AName: string): TGclCacheNode;
  801. begin
  802. if Pos('第100章至', AName) <> 0 then
  803. begin
  804. Result := TGclCacheNode(AddNode(nil));
  805. FLastBlank1 := Result;
  806. end
  807. else
  808. Result := TGclCacheNode(AddNode(FLastBlank1));
  809. FLastNode := Result;
  810. end;
  811. function TGclCacheTree.GetNewNode: TCacheNode;
  812. begin
  813. Result := TGclCacheNode.Create(Self, GetNewNodeID);
  814. CacheNodes.Add(Result);
  815. end;
  816. procedure TGclCacheTree.SaveTreeToFile(const AFileName: string);
  817. var
  818. sgs: TStringList;
  819. I: Integer;
  820. Node: TGclCacheNode;
  821. begin
  822. sgs := TStringList.Create;
  823. try
  824. for I := 0 to CacheNodes.Count - 1 do
  825. begin
  826. Node := TGclCacheNode(CacheNodes.Items[I]);
  827. sgs.Add(Format('ID: %3d; ParentID: %3d; NextID: %3d; B_Code: %s; Name: %s;',
  828. [Node.ID, Node.ParentID, Node.NextSiblingID, Node.B_Code, Node.Name]));
  829. end;
  830. sgs.SaveToFile(AFileName);
  831. finally
  832. sgs.Free;
  833. end;
  834. end;
  835. end.