BillsTree.pas 9.4 KB

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