BillsTree.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. unit BillsTree;
  2. interface
  3. uses
  4. sdIDTree, sdDB, mDataRecord;
  5. type
  6. TBillsIDTreeNode = class(TsdIDTreeNode)
  7. private
  8. FStageRec: TStageRecord;
  9. FDealQuantity: Double;
  10. FDealTotalPrice: Double;
  11. FQcQuantity: Double;
  12. FQcTotalPrice: Double;
  13. FPcQuantity: Double;
  14. FPcTotalPrice: Double;
  15. FGatherTotalPrice: Double;
  16. FGatherQuantity: Double;
  17. function HasCountPrice: Boolean;
  18. function HasTotalPrice: Boolean;
  19. function GetRec: TBillsRecord;
  20. function GetChapterParentID: Integer;
  21. function GetChapterParent: TBillsIDTreeNode;
  22. public
  23. function CanUpLevel: Boolean; override;
  24. function CanDownLevel: Boolean; override;
  25. function CanUpMove: Boolean; override;
  26. function CanDownMove: Boolean; override;
  27. function UpLevel: Boolean; override;
  28. function DownLevel: Boolean; override;
  29. function HasMeasure: Boolean;
  30. function HasLedger: Boolean;
  31. function CountPriceEnable: Boolean;
  32. function TotalPriceEnable: Boolean;
  33. property Rec: TBillsRecord read GetRec;
  34. property ChapterParent: TBillsIDTreeNode read GetChapterParent;
  35. property ChapterParentID: Integer read GetChapterParentID;
  36. property DealQuantity: Double read FDealQuantity write FDealQuantity;
  37. property DealTotalPrice: Double read FDealTotalPrice write FDealTotalPrice;
  38. property QcQuantity: Double read FQcQuantity write FQcQuantity;
  39. property QcTotalPrice: Double read FQcTotalPrice write FQcTotalPrice;
  40. property PcQuantity: Double read FPcQuantity write FPcQuantity;
  41. property PcTotalPrice: Double read FPcTotalPrice write FPcTotalPrice;
  42. property GatherQuantity: Double read FGatherQuantity write FGatherQuantity;
  43. property GatherTotalPrice: Double read FGatherTotalPrice write FGatherTotalPrice;
  44. // Cache Data
  45. property StageRec: TStageRecord read FStageRec write FStageRec;
  46. end;
  47. TReCalculateNode = procedure(AID: Integer) of object;
  48. TBillsIDTree = class(TsdIDTree)
  49. protected
  50. function CreateItem: TsdIDTreeNode; override;
  51. public
  52. function CanDelete(ANode: TsdIDTreeNode): Boolean; override;
  53. function DeleteNode(ANode: TsdIDTreeNode): Boolean; override;
  54. function Add(AParentID, ANextSiblingID: TsdTreeNodeID): TsdIDTreeNode; override;
  55. procedure RecodeChildrenCode(ANode: TBillsIDTreeNode; AOrgCode, ANewCode: string);
  56. procedure RecodeChildrenB_Code(ANode: TBillsIDTreeNode; AOrgCode, ANewCode: string);
  57. procedure DoOnReCalcNode(AID: Integer); overload; virtual; abstract;
  58. procedure DoOnReCalcNode(ANode: TsdIDTreeNode); overload; virtual; abstract;
  59. end;
  60. TCompileBillsIDTree = class(TBillsIDTree)
  61. private
  62. FOnReCalcNode: TReCalculateNode;
  63. public
  64. procedure DoOnReCalcNode(AID: Integer); overload; override;
  65. procedure DoOnReCalcNode(ANode: TsdIDTreeNode); overload; override;
  66. property OnReCalcNode: TReCalculateNode read FOnReCalcNode write FOnReCalcNode;
  67. end;
  68. TMeasureBillsIDTreeNode = class(TBillsIDTreeNode)
  69. private
  70. FStageRec: TStageRecord;
  71. public
  72. // Cache Data
  73. property StageRec: TStageRecord read FStageRec write FStageRec;
  74. end;
  75. TMeasureBillsIDTree = class(TBillsIDTree)
  76. private
  77. FCompileTree: TCompileBillsIDTree;
  78. protected
  79. function CreateItem: TsdIDTreeNode; override;
  80. public
  81. procedure DoOnReCalcNode(AID: Integer); overload; override;
  82. procedure DoOnReCalcNode(ANode: TsdIDTreeNode); overload; override;
  83. property CompileTree: TCompileBillsIDTree read FCompileTree write FCompileTree;
  84. end;
  85. TEstimateIDTreeNode = class(TsdIDTreeNode)
  86. public
  87. function CanExpand: Boolean; override;
  88. end;
  89. TEstimateIDTree = class(TsdIDTree)
  90. public
  91. function CreateItem: TsdIDTreeNode; override;
  92. end;
  93. implementation
  94. uses SysUtils;
  95. { TBillsIDTree }
  96. function TBillsIDTree.Add(AParentID,
  97. ANextSiblingID: TsdTreeNodeID): TsdIDTreeNode;
  98. begin
  99. // 不允许插入首层节点
  100. if (Selected <> nil) and (Selected.Level = 0) then
  101. Result := inherited Add(Selected.ID, -1)
  102. else
  103. Result := inherited Add(AParentID, ANextSiblingID);
  104. end;
  105. function TBillsIDTree.CanDelete(ANode: TsdIDTreeNode): Boolean;
  106. begin
  107. Result := Inherited CanDelete(ANode)
  108. and (ANode.ID >= 100)
  109. and (not ANode.Rec.ValueByName('LockedLevel').AsBoolean)
  110. and (ANode.Rec.ValueByName('AddDealQuantity').AsFloat = 0)
  111. and (ANode.Rec.ValueByName('AddDealTotalPrice').AsFloat = 0)
  112. and (ANode.Rec.ValueByName('AddQcQuantity').AsFloat = 0)
  113. and (ANode.Rec.ValueByName('AddQcTotalPrice').AsFloat = 0)
  114. and (ANode.Rec.ValueByName('AddPcQuantity').AsFloat = 0)
  115. and (ANode.Rec.ValueByName('AddPcTotalPrice').AsFloat = 0);
  116. end;
  117. function TBillsIDTree.CreateItem: TsdIDTreeNode;
  118. begin
  119. Result := TBillsIDTreeNode.Create(Self);
  120. end;
  121. function TBillsIDTree.DeleteNode(ANode: TsdIDTreeNode): Boolean;
  122. var
  123. vParent: TsdIDTreeNode;
  124. begin
  125. vParent := ANode.Parent;
  126. Result := inherited DeleteNode(ANode);
  127. DoOnReCalcNode(vParent);
  128. end;
  129. procedure TBillsIDTree.RecodeChildrenB_Code(ANode: TBillsIDTreeNode;
  130. AOrgCode, ANewCode: string);
  131. var
  132. iCount, iTotal: Integer;
  133. vChild: TBillsIDTreeNode;
  134. begin
  135. if (ANewCode = '') or (AOrgCode = '') or (AOrgCode = ANewCode) then Exit;
  136. iCount := 0;
  137. iTotal := ANode.PosterityCount;
  138. vChild := TBillsIDTreeNode(ANode.NextNode);
  139. while (iCount < iTotal) and Assigned(vChild) do
  140. begin
  141. if vChild.Rec.B_Code.AsString <> '' then
  142. begin
  143. if Pos(AOrgCode+'-', vChild.Rec.B_Code.AsString) = 1 then
  144. begin
  145. vChild.Rec.B_Code.AsString := StringReplace(vChild.Rec.B_Code.AsString,
  146. AOrgCode+'-', ANewCode+'-', []);
  147. vChild.Rec.SetBoolValue(vChild.Rec.LockedInfo, False);
  148. end;
  149. end;
  150. vChild := TBillsIDTreeNode(vChild.NextNode);
  151. Inc(iCount);
  152. end;
  153. end;
  154. procedure TBillsIDTree.RecodeChildrenCode(ANode: TBillsIDTreeNode;
  155. AOrgCode, ANewCode: string);
  156. var
  157. iCount, iTotal: Integer;
  158. vChild: TBillsIDTreeNode;
  159. begin
  160. if (ANewCode = '') or (AOrgCode = '') or (AOrgCode = ANewCode) then Exit;
  161. iCount := 0;
  162. iTotal := ANode.PosterityCount;
  163. vChild := TBillsIDTreeNode(ANode.NextNode);
  164. while (iCount < iTotal) and Assigned(vChild) do
  165. begin
  166. if vChild.Rec.Code.AsString <> '' then
  167. begin
  168. if Pos(AOrgCode+'-', vChild.Rec.Code.AsString) = 1 then
  169. begin
  170. vChild.Rec.Code.AsString := StringReplace(vChild.Rec.Code.AsString,
  171. AOrgCode+'-', ANewCode+'-', []);
  172. vChild.Rec.SetBoolValue(vChild.Rec.LockedInfo, False);
  173. end;
  174. end;
  175. vChild := TBillsIDTreeNode(vChild.NextNode);
  176. Inc(iCount);
  177. end;
  178. end;
  179. { TBillsIDTreeNode }
  180. function TBillsIDTreeNode.CanDownLevel: Boolean;
  181. begin
  182. Result := Inherited CanDownLevel
  183. and (Level > 0)
  184. and (not Rec.ValueByName('LockedLevel').AsBoolean)
  185. and not HasMeasure;
  186. if Assigned(PrevSibling) then
  187. begin
  188. Result := Result
  189. and (PrevSibling.HasChildren or not TBillsIDTreeNode(PrevSibling).HasMeasure);
  190. end;
  191. end;
  192. function TBillsIDTreeNode.CanDownMove: Boolean;
  193. begin
  194. Result := Inherited CanDownMove
  195. and (not Rec.ValueByName('LockedLevel').AsBoolean);
  196. end;
  197. function TBillsIDTreeNode.CanUpLevel: Boolean;
  198. var
  199. vNextSibling: TsdIDTreeNode;
  200. begin
  201. Result := Inherited CanUpLevel
  202. and (Level > 1)
  203. and (not Rec.ValueByName('LockedLevel').AsBoolean)
  204. and not HasMeasure;
  205. vNextSibling := NextSibling;
  206. while Assigned(vNextSibling) and Result do
  207. begin
  208. Result := Result
  209. and not TBillsIDTreeNode(NextSibling).HasMeasure;
  210. vNextSibling := vNextSibling.NextSibling;
  211. end;
  212. end;
  213. function TBillsIDTreeNode.CanUpMove: Boolean;
  214. begin
  215. Result := Inherited CanUpMove
  216. and (not Rec.ValueByName('LockedLevel').AsBoolean);
  217. end;
  218. function TBillsIDTreeNode.CountPriceEnable: Boolean;
  219. begin
  220. Result := HasCountPrice or (not HasTotalPrice);
  221. end;
  222. function TBillsIDTreeNode.DownLevel: Boolean;
  223. var
  224. iOrgParentID: Integer;
  225. begin
  226. iOrgParentID := ParentID;
  227. Result := inherited DownLevel;
  228. if not Result then Exit;
  229. // 如升级后变为父项,则清空数量、单价
  230. if Assigned(Parent) then
  231. begin
  232. Parent.Rec.ValueByName('OrgQuantity').AsFloat := 0;
  233. Parent.Rec.ValueByName('MisQuantity').AsFloat := 0;
  234. Parent.Rec.ValueByName('OthQuantity').AsFloat := 0;
  235. Parent.Rec.ValueByName('Quantity').AsFloat := 0;
  236. Parent.Rec.ValueByName('Price').AsFloat := 0;
  237. end;
  238. TBillsIDTree(Owner).DoOnReCalcNode(ParentID);
  239. TBillsIDTree(Owner).DoOnReCalcNode(iOrgParentID);
  240. end;
  241. function TBillsIDTreeNode.GetChapterParent: TBillsIDTreeNode;
  242. begin
  243. Result := nil;
  244. if Self.Level <= 1 then Exit;
  245. Result := TBillsIDTreeNode(Self.Parent);
  246. while Result.Level > 1 do
  247. Result := TBillsIDTreeNode(Result.Parent);
  248. end;
  249. function TBillsIDTreeNode.GetChapterParentID: Integer;
  250. var
  251. vNode: TBillsIDTreeNode;
  252. begin
  253. vNode := GetChapterParent;
  254. if Assigned(vNode) then
  255. Result := vNode.ID
  256. else
  257. Result := -1;
  258. end;
  259. function TBillsIDTreeNode.GetRec: TBillsRecord;
  260. begin
  261. Result := TBillsRecord(TsdIDTreeNode(Self).Rec);
  262. end;
  263. function TBillsIDTreeNode.HasCountPrice: Boolean;
  264. begin
  265. Result := False;
  266. if not Assigned(Rec) then Exit;
  267. Result := (Rec.Price.AsFloat <> 0)
  268. or (Rec.OrgQuantity.AsFloat <> 0)
  269. or (Rec.MisQuantity.AsFloat <> 0)
  270. or (Rec.OthQuantity.AsFloat <> 0)
  271. or (Rec.AddDealQuantity.AsFloat <> 0)
  272. or (Rec.AddQcQuantity.AsFloat <> 0)
  273. or (Rec.AddPcQuantity.AsFloat <> 0);
  274. end;
  275. function TBillsIDTreeNode.HasLedger: Boolean;
  276. begin
  277. Result := False;
  278. if not Assigned(Rec) then Exit;
  279. Result := (Rec.Price.AsFloat <> 0)
  280. or (Rec.Quantity.AsFloat <> 0);
  281. end;
  282. function TBillsIDTreeNode.HasMeasure: Boolean;
  283. begin
  284. Result := False;
  285. if not Assigned(Rec) then Exit;
  286. Result := (Rec.AddDealQuantity.AsFloat <> 0)
  287. or (Rec.AddDealTotalPrice.AsFloat <> 0)
  288. or (Rec.AddQcQuantity.AsFloat <> 0)
  289. or (Rec.AddQcTotalPrice.AsFloat <> 0)
  290. or (Rec.AddPcQuantity.AsFloat <> 0)
  291. or (Rec.AddPcTotalPrice.AsFloat <> 0);
  292. end;
  293. function TBillsIDTreeNode.HasTotalPrice: Boolean;
  294. begin
  295. Result := False;
  296. if not Assigned(Rec) then Exit;
  297. Result := (Rec.OrgTotalPrice.AsFloat <> 0)
  298. or (Rec.MisTotalPrice.AsFloat <> 0)
  299. or (Rec.OthTotalPrice.AsFloat <> 0)
  300. or (Rec.AddDealTotalPrice.AsFloat <> 0)
  301. or (Rec.AddQcTotalPrice.AsFloat <> 0)
  302. or (Rec.AddPcTotalPrice.AsFloat <> 0);
  303. end;
  304. function TBillsIDTreeNode.TotalPriceEnable: Boolean;
  305. begin
  306. Result := not HasCountPrice;
  307. end;
  308. function TBillsIDTreeNode.UpLevel: Boolean;
  309. var
  310. iOrgParentID: Integer;
  311. begin
  312. iOrgParentID := ParentID;
  313. Result := inherited UpLevel;
  314. if not Result then Exit;
  315. // 如升级后变为父项,则清空数量、单价
  316. if HasChildren then
  317. begin
  318. Rec.ValueByName('OrgQuantity').AsFloat := 0;
  319. Rec.ValueByName('MisQuantity').AsFloat := 0;
  320. Rec.ValueByName('OthQuantity').AsFloat := 0;
  321. Rec.ValueByName('Quantity').AsFloat := 0;
  322. Rec.ValueByName('Price').AsFloat := 0;
  323. end;
  324. TBillsIDTree(Owner).DoOnReCalcNode(iOrgParentID);
  325. TBillsIDTree(Owner).DoOnReCalcNode(ParentID);
  326. end;
  327. { TEstimateIDTreeNode }
  328. function TEstimateIDTreeNode.CanExpand: Boolean;
  329. var
  330. iChild: Integer;
  331. vChild: TsdIDTreeNode;
  332. begin
  333. Result := True;
  334. if HasChildren then
  335. for iChild := 0 to ChildCount - 1 do
  336. begin
  337. vChild := ChildNodes[iChild];
  338. if vChild.Rec.ValueByName('B_Code').AsString <> '' then
  339. begin
  340. Result := False;
  341. Break;
  342. end;
  343. end;
  344. end;
  345. { TEstimateIDTree }
  346. function TEstimateIDTree.CreateItem: TsdIDTreeNode;
  347. begin
  348. Result := TEstimateIDTreeNode.Create(Self);
  349. end;
  350. { TCompileBillsIDTree }
  351. procedure TCompileBillsIDTree.DoOnReCalcNode(AID: Integer);
  352. begin
  353. if (AID <> -1) and Assigned(FOnReCalcNode) then
  354. FOnReCalcNode(AID);
  355. end;
  356. procedure TCompileBillsIDTree.DoOnReCalcNode(ANode: TsdIDTreeNode);
  357. begin
  358. if Assigned(ANode) then
  359. DoOnReCalcNode(ANode.ID);
  360. end;
  361. { TMeasureBillsIDTree }
  362. procedure TMeasureBillsIDTree.DoOnReCalcNode(AID: Integer);
  363. begin
  364. if Assigned(FCompileTree) then
  365. FCompileTree.DoOnReCalcNode(AID);
  366. end;
  367. function TMeasureBillsIDTree.CreateItem: TsdIDTreeNode;
  368. begin
  369. Result := TMeasureBillsIDTreeNode.Create(Self);
  370. end;
  371. procedure TMeasureBillsIDTree.DoOnReCalcNode(ANode: TsdIDTreeNode);
  372. begin
  373. if Assigned(FCompileTree) then
  374. FCompileTree.DoOnReCalcNode(ANode);
  375. end;
  376. end.