BillsTree.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. unit BillsTree;
  2. interface
  3. uses
  4. sdIDTree, sdDB, mDataRecord, ZhAPI;
  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. procedure RecodeChildrenCode(const AParentCode: string);
  34. procedure RecodeChildrenB_Code(const AParentCode: string);
  35. property Rec: TBillsRecord read GetRec;
  36. property ChapterParent: TBillsIDTreeNode read GetChapterParent;
  37. property ChapterParentID: Integer read GetChapterParentID;
  38. property DealQuantity: Double read FDealQuantity write FDealQuantity;
  39. property DealTotalPrice: Double read FDealTotalPrice write FDealTotalPrice;
  40. property QcQuantity: Double read FQcQuantity write FQcQuantity;
  41. property QcTotalPrice: Double read FQcTotalPrice write FQcTotalPrice;
  42. property PcQuantity: Double read FPcQuantity write FPcQuantity;
  43. property PcTotalPrice: Double read FPcTotalPrice write FPcTotalPrice;
  44. property GatherQuantity: Double read FGatherQuantity write FGatherQuantity;
  45. property GatherTotalPrice: Double read FGatherTotalPrice write FGatherTotalPrice;
  46. // Cache Data
  47. property StageRec: TStageRecord read FStageRec write FStageRec;
  48. end;
  49. TReCalculateNode = procedure(AID: Integer) of object;
  50. TBillsIDTree = class(TsdIDTree)
  51. private
  52. function GetNewID(AID: Integer = -1): Integer;
  53. protected
  54. function CreateItem: TsdIDTreeNode; override;
  55. public
  56. function CanDelete(ANode: TsdIDTreeNode): Boolean; override;
  57. function DeleteNode(ANode: TsdIDTreeNode): Boolean; override;
  58. function Add(AParentID, ANextSiblingID: TsdTreeNodeID): TsdIDTreeNode; override;
  59. function AddNode(AParentID, ANextSiblingID: TsdTreeNodeID; AID: Integer = -1): TsdIDTreeNode;
  60. procedure RecodeChildrenCode(ANode: TBillsIDTreeNode; AOrgCode, ANewCode: string);
  61. procedure RecodeChildrenB_Code(ANode: TBillsIDTreeNode; AOrgCode, ANewCode: string);
  62. procedure DoOnReCalcNode(AID: Integer); overload; virtual; abstract;
  63. procedure DoOnReCalcNode(ANode: TsdIDTreeNode); overload; virtual; abstract;
  64. end;
  65. TCompileBillsIDTree = class(TBillsIDTree)
  66. private
  67. FOnReCalcNode: TReCalculateNode;
  68. public
  69. procedure DoOnReCalcNode(AID: Integer); overload; override;
  70. procedure DoOnReCalcNode(ANode: TsdIDTreeNode); overload; override;
  71. property OnReCalcNode: TReCalculateNode read FOnReCalcNode write FOnReCalcNode;
  72. end;
  73. TMeasureBillsIDTreeNode = class(TBillsIDTreeNode)
  74. private
  75. FStageRec: TStageRecord;
  76. public
  77. // Cache Data
  78. property StageRec: TStageRecord read FStageRec write FStageRec;
  79. end;
  80. TMeasureBillsIDTree = class(TBillsIDTree)
  81. private
  82. FCompileTree: TCompileBillsIDTree;
  83. protected
  84. function CreateItem: TsdIDTreeNode; override;
  85. public
  86. procedure DoOnReCalcNode(AID: Integer); overload; override;
  87. procedure DoOnReCalcNode(ANode: TsdIDTreeNode); overload; override;
  88. property CompileTree: TCompileBillsIDTree read FCompileTree write FCompileTree;
  89. end;
  90. TEstimateIDTreeNode = class(TsdIDTreeNode)
  91. public
  92. function CanExpand: Boolean; override;
  93. end;
  94. TEstimateIDTree = class(TsdIDTree)
  95. public
  96. function CreateItem: TsdIDTreeNode; override;
  97. end;
  98. implementation
  99. uses SysUtils, UtilMethods;
  100. { TBillsIDTree }
  101. function TBillsIDTree.Add(AParentID,
  102. ANextSiblingID: TsdTreeNodeID): TsdIDTreeNode;
  103. begin
  104. // 不允许插入首层节点
  105. if (Selected <> nil) and (Selected.Level = 0) then
  106. Result := inherited Add(Selected.ID, -1)
  107. else
  108. Result := inherited Add(AParentID, ANextSiblingID);
  109. end;
  110. function TBillsIDTree.AddNode(AParentID, ANextSiblingID: TsdTreeNodeID;
  111. AID: Integer): TsdIDTreeNode;
  112. var
  113. iID: Integer;
  114. Rec: TsdDataRecord;
  115. begin
  116. if not (Assigned(DataView) and DataView.Active){Active} then
  117. begin
  118. WarningMessage('无法在一个关闭的数据集上执行该操作');
  119. Exit;
  120. end;
  121. Result := nil;
  122. if CanAdd(AParentID, ANextSiblingID) then
  123. begin
  124. Rec := DataView.Append(True);
  125. try
  126. Rec.AddValue(ParentField, AParentID);
  127. Rec.AddValue(NextSiblingField, ANextSiblingID);
  128. iID := GetNewID(AID);
  129. if AutoCreateKeyID then
  130. Rec.AddValue(KeyField, iID);
  131. InitDBRecord(Rec);
  132. finally
  133. Rec.EndUpdate;
  134. end;
  135. Result := Add(iID, AParentID, ANextSiblingID, Rec);
  136. end;
  137. end;
  138. function TBillsIDTree.CanDelete(ANode: TsdIDTreeNode): Boolean;
  139. begin
  140. Result := Inherited CanDelete(ANode)
  141. and ((ANode.ID >= 100) or (ANode.Level > 0))
  142. and (not ANode.Rec.ValueByName('LockedLevel').AsBoolean)
  143. and (ANode.Rec.ValueByName('AddDealQuantity').AsFloat = 0)
  144. and (ANode.Rec.ValueByName('AddDealTotalPrice').AsFloat = 0)
  145. and (ANode.Rec.ValueByName('AddQcQuantity').AsFloat = 0)
  146. and (ANode.Rec.ValueByName('AddQcTotalPrice').AsFloat = 0)
  147. and (ANode.Rec.ValueByName('AddPcQuantity').AsFloat = 0)
  148. and (ANode.Rec.ValueByName('AddPcTotalPrice').AsFloat = 0);
  149. end;
  150. function TBillsIDTree.CreateItem: TsdIDTreeNode;
  151. begin
  152. Result := TBillsIDTreeNode.Create(Self);
  153. end;
  154. function TBillsIDTree.DeleteNode(ANode: TsdIDTreeNode): Boolean;
  155. var
  156. vParent: TsdIDTreeNode;
  157. begin
  158. vParent := ANode.Parent;
  159. Result := inherited DeleteNode(ANode);
  160. DoOnReCalcNode(vParent);
  161. end;
  162. function TBillsIDTree.GetNewID(AID: Integer): Integer;
  163. var
  164. vNode: TsdIDTreeNode;
  165. begin
  166. vNode := FindNode(AID);
  167. if Assigned(vNode) or (AID <= 0) or (AID >= 100) then
  168. Result := NextNewID
  169. else
  170. Result := AID;
  171. end;
  172. procedure TBillsIDTree.RecodeChildrenB_Code(ANode: TBillsIDTreeNode;
  173. AOrgCode, ANewCode: string);
  174. var
  175. iCount, iTotal: Integer;
  176. vChild: TBillsIDTreeNode;
  177. begin
  178. if (ANewCode = '') or (AOrgCode = '') or (AOrgCode = ANewCode) then Exit;
  179. iCount := 0;
  180. iTotal := ANode.PosterityCount;
  181. vChild := TBillsIDTreeNode(ANode.NextNode);
  182. while (iCount < iTotal) and Assigned(vChild) do
  183. begin
  184. if vChild.Rec.B_Code.AsString <> '' then
  185. begin
  186. if Pos(AOrgCode+'-', vChild.Rec.B_Code.AsString) = 1 then
  187. begin
  188. vChild.Rec.B_Code.AsString := StringReplace(vChild.Rec.B_Code.AsString,
  189. AOrgCode+'-', ANewCode+'-', []);
  190. vChild.Rec.SetBoolValue(vChild.Rec.LockedInfo, False);
  191. end;
  192. end;
  193. vChild := TBillsIDTreeNode(vChild.NextNode);
  194. Inc(iCount);
  195. end;
  196. end;
  197. procedure TBillsIDTree.RecodeChildrenCode(ANode: TBillsIDTreeNode;
  198. AOrgCode, ANewCode: string);
  199. var
  200. iCount, iTotal: Integer;
  201. vChild: TBillsIDTreeNode;
  202. begin
  203. if (ANewCode = '') or (AOrgCode = '') or (AOrgCode = ANewCode) then Exit;
  204. iCount := 0;
  205. iTotal := ANode.PosterityCount;
  206. vChild := TBillsIDTreeNode(ANode.NextNode);
  207. while (iCount < iTotal) and Assigned(vChild) do
  208. begin
  209. if vChild.Rec.Code.AsString <> '' then
  210. begin
  211. if Pos(AOrgCode+'-', vChild.Rec.Code.AsString) = 1 then
  212. begin
  213. vChild.Rec.Code.AsString := StringReplace(vChild.Rec.Code.AsString,
  214. AOrgCode+'-', ANewCode+'-', []);
  215. vChild.Rec.SetBoolValue(vChild.Rec.LockedInfo, False);
  216. end;
  217. end;
  218. vChild := TBillsIDTreeNode(vChild.NextNode);
  219. Inc(iCount);
  220. end;
  221. end;
  222. { TBillsIDTreeNode }
  223. function TBillsIDTreeNode.CanDownLevel: Boolean;
  224. begin
  225. Result := Inherited CanDownLevel
  226. and (Level > 0)
  227. and (not Rec.ValueByName('LockedLevel').AsBoolean)
  228. and not HasMeasure;
  229. if Assigned(PrevSibling) then
  230. begin
  231. Result := Result
  232. and (PrevSibling.HasChildren or not TBillsIDTreeNode(PrevSibling).HasMeasure);
  233. end;
  234. end;
  235. function TBillsIDTreeNode.CanDownMove: Boolean;
  236. begin
  237. Result := Inherited CanDownMove
  238. and (not Rec.ValueByName('LockedLevel').AsBoolean);
  239. end;
  240. function TBillsIDTreeNode.CanUpLevel: Boolean;
  241. var
  242. vNextSibling: TsdIDTreeNode;
  243. begin
  244. Result := Inherited CanUpLevel
  245. and (Level > 1)
  246. and (not Rec.ValueByName('LockedLevel').AsBoolean)
  247. and not HasMeasure;
  248. vNextSibling := NextSibling;
  249. while Assigned(vNextSibling) and Result do
  250. begin
  251. Result := Result
  252. and not TBillsIDTreeNode(NextSibling).HasMeasure;
  253. vNextSibling := vNextSibling.NextSibling;
  254. end;
  255. end;
  256. function TBillsIDTreeNode.CanUpMove: Boolean;
  257. begin
  258. Result := Inherited CanUpMove
  259. and (not Rec.ValueByName('LockedLevel').AsBoolean);
  260. end;
  261. function TBillsIDTreeNode.CountPriceEnable: Boolean;
  262. begin
  263. Result := HasCountPrice or (not HasTotalPrice);
  264. end;
  265. function TBillsIDTreeNode.DownLevel: Boolean;
  266. var
  267. iOrgParentID: Integer;
  268. begin
  269. iOrgParentID := ParentID;
  270. Result := inherited DownLevel;
  271. if not Result then Exit;
  272. // 如升级后变为父项,则清空数量、单价
  273. if Assigned(Parent) then
  274. begin
  275. Parent.Rec.ValueByName('OrgQuantity').AsFloat := 0;
  276. Parent.Rec.ValueByName('MisQuantity').AsFloat := 0;
  277. Parent.Rec.ValueByName('OthQuantity').AsFloat := 0;
  278. Parent.Rec.ValueByName('Quantity').AsFloat := 0;
  279. Parent.Rec.ValueByName('Price').AsFloat := 0;
  280. end;
  281. TBillsIDTree(Owner).DoOnReCalcNode(ParentID);
  282. TBillsIDTree(Owner).DoOnReCalcNode(iOrgParentID);
  283. end;
  284. function TBillsIDTreeNode.GetChapterParent: TBillsIDTreeNode;
  285. begin
  286. Result := nil;
  287. if Self.Level <= 1 then Exit;
  288. Result := TBillsIDTreeNode(Self.Parent);
  289. while Result.Level > 1 do
  290. Result := TBillsIDTreeNode(Result.Parent);
  291. end;
  292. function TBillsIDTreeNode.GetChapterParentID: Integer;
  293. var
  294. vNode: TBillsIDTreeNode;
  295. begin
  296. vNode := GetChapterParent;
  297. if Assigned(vNode) then
  298. Result := vNode.ID
  299. else
  300. Result := -1;
  301. end;
  302. function TBillsIDTreeNode.GetRec: TBillsRecord;
  303. begin
  304. Result := TBillsRecord(TsdIDTreeNode(Self).Rec);
  305. end;
  306. function TBillsIDTreeNode.HasCountPrice: Boolean;
  307. begin
  308. Result := False;
  309. if not Assigned(Rec) then Exit;
  310. Result := (Rec.Price.AsFloat <> 0)
  311. or (Rec.OrgQuantity.AsFloat <> 0)
  312. or (Rec.MisQuantity.AsFloat <> 0)
  313. or (Rec.OthQuantity.AsFloat <> 0)
  314. or (Rec.AddDealQuantity.AsFloat <> 0)
  315. or (Rec.AddQcQuantity.AsFloat <> 0)
  316. or (Rec.AddPcQuantity.AsFloat <> 0);
  317. end;
  318. function TBillsIDTreeNode.HasLedger: Boolean;
  319. begin
  320. Result := False;
  321. if not Assigned(Rec) then Exit;
  322. Result := (Rec.Price.AsFloat <> 0)
  323. or (Rec.Quantity.AsFloat <> 0);
  324. end;
  325. function TBillsIDTreeNode.HasMeasure: Boolean;
  326. begin
  327. Result := False;
  328. if not Assigned(Rec) then Exit;
  329. Result := (Rec.AddDealQuantity.AsFloat <> 0)
  330. or (Rec.AddDealTotalPrice.AsFloat <> 0)
  331. or (Rec.AddQcQuantity.AsFloat <> 0)
  332. or (Rec.AddQcTotalPrice.AsFloat <> 0)
  333. or (Rec.AddPcQuantity.AsFloat <> 0)
  334. or (Rec.AddPcTotalPrice.AsFloat <> 0);
  335. end;
  336. function TBillsIDTreeNode.HasTotalPrice: Boolean;
  337. begin
  338. Result := False;
  339. if not Assigned(Rec) then Exit;
  340. Result := (Rec.OrgTotalPrice.AsFloat <> 0)
  341. or (Rec.MisTotalPrice.AsFloat <> 0)
  342. or (Rec.OthTotalPrice.AsFloat <> 0)
  343. or (Rec.AddDealTotalPrice.AsFloat <> 0)
  344. or (Rec.AddQcTotalPrice.AsFloat <> 0)
  345. or (Rec.AddPcTotalPrice.AsFloat <> 0);
  346. end;
  347. procedure TBillsIDTreeNode.RecodeChildrenB_Code(const AParentCode: string);
  348. var
  349. iChild: Integer;
  350. vChild: TBillsIDTreeNode;
  351. begin
  352. if (AParentCode = '') then Exit;
  353. for iChild := 0 to Self.ChildCount - 1 do
  354. begin
  355. vChild := TBillsIDTreeNode(Self.ChildNodes[iChild]);
  356. if vChild.Rec.B_Code.AsString <> '' then
  357. begin
  358. vChild.Rec.SetStrValue(vChild.Rec.B_Code, AParentCode + '-' + GetLastSetmentOfCode(AParentCode));
  359. vChild.Rec.SetBoolValue(vChild.Rec.LockedInfo, False);
  360. end;
  361. end;
  362. end;
  363. procedure TBillsIDTreeNode.RecodeChildrenCode(const AParentCode: string);
  364. var
  365. iChild: Integer;
  366. vChild: TBillsIDTreeNode;
  367. begin
  368. if (AParentCode = '') then Exit;
  369. for iChild := 0 to Self.ChildCount - 1 do
  370. begin
  371. vChild := TBillsIDTreeNode(Self.ChildNodes[iChild]);
  372. if vChild.Rec.Code.AsString <> '' then
  373. begin
  374. vChild.Rec.SetStrValue(vChild.Rec.Code, AParentCode + '-' + GetLastSetmentOfCode(AParentCode));
  375. vChild.Rec.SetBoolValue(vChild.Rec.LockedInfo, False);
  376. end;
  377. end;
  378. end;
  379. function TBillsIDTreeNode.TotalPriceEnable: Boolean;
  380. begin
  381. Result := not HasCountPrice;
  382. end;
  383. function TBillsIDTreeNode.UpLevel: Boolean;
  384. var
  385. iOrgParentID: Integer;
  386. begin
  387. iOrgParentID := ParentID;
  388. Result := inherited UpLevel;
  389. if not Result then Exit;
  390. // 如升级后变为父项,则清空数量、单价
  391. if HasChildren then
  392. begin
  393. Rec.ValueByName('OrgQuantity').AsFloat := 0;
  394. Rec.ValueByName('MisQuantity').AsFloat := 0;
  395. Rec.ValueByName('OthQuantity').AsFloat := 0;
  396. Rec.ValueByName('Quantity').AsFloat := 0;
  397. Rec.ValueByName('Price').AsFloat := 0;
  398. end;
  399. TBillsIDTree(Owner).DoOnReCalcNode(iOrgParentID);
  400. TBillsIDTree(Owner).DoOnReCalcNode(ParentID);
  401. end;
  402. { TEstimateIDTreeNode }
  403. function TEstimateIDTreeNode.CanExpand: Boolean;
  404. var
  405. iChild: Integer;
  406. vChild: TsdIDTreeNode;
  407. begin
  408. Result := True;
  409. if HasChildren then
  410. for iChild := 0 to ChildCount - 1 do
  411. begin
  412. vChild := ChildNodes[iChild];
  413. if vChild.Rec.ValueByName('B_Code').AsString <> '' then
  414. begin
  415. Result := False;
  416. Break;
  417. end;
  418. end;
  419. end;
  420. { TEstimateIDTree }
  421. function TEstimateIDTree.CreateItem: TsdIDTreeNode;
  422. begin
  423. Result := TEstimateIDTreeNode.Create(Self);
  424. end;
  425. { TCompileBillsIDTree }
  426. procedure TCompileBillsIDTree.DoOnReCalcNode(AID: Integer);
  427. begin
  428. if (AID <> -1) and Assigned(FOnReCalcNode) then
  429. FOnReCalcNode(AID);
  430. end;
  431. procedure TCompileBillsIDTree.DoOnReCalcNode(ANode: TsdIDTreeNode);
  432. begin
  433. if Assigned(ANode) then
  434. DoOnReCalcNode(ANode.ID);
  435. end;
  436. { TMeasureBillsIDTree }
  437. procedure TMeasureBillsIDTree.DoOnReCalcNode(AID: Integer);
  438. begin
  439. if Assigned(FCompileTree) then
  440. FCompileTree.DoOnReCalcNode(AID);
  441. end;
  442. function TMeasureBillsIDTree.CreateItem: TsdIDTreeNode;
  443. begin
  444. Result := TMeasureBillsIDTreeNode.Create(Self);
  445. end;
  446. procedure TMeasureBillsIDTree.DoOnReCalcNode(ANode: TsdIDTreeNode);
  447. begin
  448. if Assigned(FCompileTree) then
  449. FCompileTree.DoOnReCalcNode(ANode);
  450. end;
  451. end.