BillsTree.pas 9.8 KB

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