MCacheTree.pas 31 KB

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