BillsTree.pas 9.6 KB

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