BillsTree.pas 9.6 KB

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