CalcDecimal.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. unit CalcDecimal;
  2. interface
  3. uses
  4. Classes, sdDB;
  5. type
  6. TDecimal = class
  7. private
  8. FDigit: Integer;
  9. FFormat: string;
  10. FLinkViewCols: TList;
  11. FRelaDecimal: TDecimal;
  12. FCompareValue: Double;
  13. procedure SetDigit(const Value: Integer);
  14. function GetDisplayFormat(ADigit: Integer): string;
  15. procedure SetRelaDecimal(const Value: TDecimal);
  16. function GetTrueDigit: Integer;
  17. public
  18. constructor Create;
  19. destructor Destroy; override;
  20. function RoundTo(AValue: Double): Double;
  21. function StrRoundTo(const AValue: string): string;
  22. function CheckSameNum(AValue1, AValue2: Double): Boolean;
  23. procedure ClearLinkViewCols;
  24. procedure AddLinkViewCol(ACol: TObject);
  25. procedure RefreshLinkViewColsFormat;
  26. property Digit: Integer read FDigit write SetDigit;
  27. property Format: string read FFormat;
  28. property CompareValue: Double read FCompareValue;
  29. property TrueDigit: Integer read GetTrueDigit;
  30. property RelaDecimal: TDecimal read FRelaDecimal write SetRelaDecimal;
  31. end;
  32. TCalcDecimal = class
  33. private
  34. FQuantity: TDecimal;
  35. FTotalPrice: TDecimal;
  36. FPrice: TDecimal;
  37. FRelaCalcDecimal: TCalcDecimal;
  38. procedure SetRelaCalcDecimal(const Value: TCalcDecimal);
  39. public
  40. constructor Create;
  41. destructor Destroy; override;
  42. procedure RefreshLinkViewColumnsFormat;
  43. property Quantity: TDecimal read FQuantity;
  44. property TotalPrice: TDecimal read FTotalPrice;
  45. property Price: TDecimal read FPrice;
  46. property RelaCalcDecimal: TCalcDecimal read FRelaCalcDecimal write SetRelaCalcDecimal;
  47. end;
  48. TDecimalManager = class
  49. private
  50. FProjectData: TObject;
  51. FCommon: TCalcDecimal;
  52. FCompile: TCalcDecimal;
  53. FPriceMargin: TCalcDecimal;
  54. FDealPay: TCalcDecimal;
  55. public
  56. constructor Create(AProjectData: TObject);
  57. destructor Destroy; override;
  58. procedure ResetLinkViewColumns;
  59. property Common: TCalcDecimal read FCommon;
  60. property Compile: TCalcDecimal read FCompile;
  61. property PriceMargin: TCalcDecimal read FPriceMargin;
  62. property DealPay: TCalcDecimal read FDealPay;
  63. end;
  64. implementation
  65. uses
  66. ProjectData, UtilMethods, SysUtils, Math;
  67. { TDecimal }
  68. procedure TDecimal.AddLinkViewCol(ACol: TObject);
  69. begin
  70. FLinkViewCols.Add(ACol);
  71. end;
  72. function TDecimal.CheckSameNum(AValue1, AValue2: Double): Boolean;
  73. begin
  74. Result := Abs(RoundTo(AValue1 - AValue2)) < CompareValue;
  75. end;
  76. procedure TDecimal.ClearLinkViewCols;
  77. begin
  78. FLinkViewCols.Clear;
  79. end;
  80. constructor TDecimal.Create;
  81. begin
  82. FLinkViewCols := TList.Create;
  83. FDigit := -1;
  84. end;
  85. destructor TDecimal.Destroy;
  86. begin
  87. FLinkViewCols.Free;
  88. inherited;
  89. end;
  90. function TDecimal.GetDisplayFormat(ADigit: Integer): string;
  91. begin
  92. case ADigit of
  93. 0: Result := '0';
  94. 1: Result := '0.#';
  95. 2: Result := '0.##';
  96. 3: Result := '0.###';
  97. 4: Result := '0.####';
  98. 5: Result := '0.#####';
  99. 6: Result := '0.######';
  100. 7: Result := '0.#######';
  101. 8: Result := '0.########';
  102. 9: Result := '0.#########';
  103. else
  104. Result := '0.##########';
  105. end;
  106. end;
  107. function TDecimal.GetTrueDigit: Integer;
  108. begin
  109. if Assigned(FRelaDecimal) then
  110. Result := FRelaDecimal.TrueDigit
  111. else
  112. Result := FDigit;
  113. end;
  114. procedure TDecimal.RefreshLinkViewColsFormat;
  115. var
  116. i: Integer;
  117. ViewCol: TsdViewColumn;
  118. begin
  119. for i := 0 to FLinkViewCols.Count - 1 do
  120. begin
  121. ViewCol := TsdViewColumn(FLinkViewCols.Items[i]);
  122. if not Assigned(ViewCol) then Continue;
  123. if Assigned(RelaDecimal) then
  124. begin
  125. ViewCol.DisplayFormat := FRelaDecimal.FFormat;
  126. ViewCol.EditFormat := FRelaDecimal.FFormat;
  127. end
  128. else
  129. begin
  130. ViewCol.DisplayFormat := FFormat;
  131. ViewCol.EditFormat := FFormat;
  132. end;
  133. end;
  134. end;
  135. function TDecimal.RoundTo(AValue: Double): Double;
  136. begin
  137. if Assigned(RelaDecimal) then
  138. Result := FRelaDecimal.RoundTo(AValue)
  139. else
  140. Result := CommonRoundTo(AValue, -Digit);
  141. end;
  142. procedure TDecimal.SetDigit(const Value: Integer);
  143. begin
  144. if FDigit <> Value then
  145. begin
  146. FDigit := Value;
  147. FFormat := GetDisplayFormat(FDigit);
  148. FCompareValue := GetCompareDigitValue(FDigit);
  149. RefreshLinkViewColsFormat;
  150. end;
  151. end;
  152. procedure TDecimal.SetRelaDecimal(const Value: TDecimal);
  153. begin
  154. FRelaDecimal := Value;
  155. RefreshLinkViewColsFormat;
  156. end;
  157. function TDecimal.StrRoundTo(const AValue: string): string;
  158. begin
  159. if Assigned(FRelaDecimal) then
  160. Result := RelaDecimal.StrRoundTo(AValue)
  161. else
  162. Result := FloatToStr(RoundTo(StrToFloatDef(AValue, 0)));
  163. end;
  164. { TCalcDecimal }
  165. constructor TCalcDecimal.Create;
  166. begin
  167. FQuantity := TDecimal.Create;
  168. FTotalPrice := TDecimal.Create;
  169. FPrice := TDecimal.Create;
  170. end;
  171. destructor TCalcDecimal.Destroy;
  172. begin
  173. FPrice.Free;
  174. FTotalPrice.Free;
  175. FQuantity.Free;
  176. inherited;
  177. end;
  178. procedure TCalcDecimal.RefreshLinkViewColumnsFormat;
  179. begin
  180. Quantity.RefreshLinkViewColsFormat;
  181. TotalPrice.RefreshLinkViewColsFormat;
  182. Price.RefreshLinkViewColsFormat;
  183. end;
  184. procedure TCalcDecimal.SetRelaCalcDecimal(const Value: TCalcDecimal);
  185. begin
  186. FRelaCalcDecimal := Value;
  187. if Assigned(FRelaCalcDecimal) then
  188. begin
  189. Quantity.RelaDecimal := FRelaCalcDecimal.Quantity;
  190. TotalPrice.RelaDecimal := FRelaCalcDecimal.TotalPrice;
  191. Price.RelaDecimal := FRelaCalcDecimal.Price;
  192. end
  193. else
  194. begin
  195. Quantity.RelaDecimal := nil;
  196. TotalPrice.RelaDecimal := nil;
  197. Price.RelaDecimal := nil;
  198. end;
  199. end;
  200. { TDecimalManager }
  201. constructor TDecimalManager.Create(AProjectData: TObject);
  202. begin
  203. FProjectData := AProjectData;
  204. FCommon := TCalcDecimal.Create;
  205. FCompile := TCalcDecimal.Create;
  206. FPriceMargin := TCalcDecimal.Create;
  207. FDealPay := TCalcDecimal.Create;
  208. end;
  209. destructor TDecimalManager.Destroy;
  210. begin
  211. FDealPay.Free;
  212. FPriceMargin.Free;
  213. FCompile.Free;
  214. FCommon.Free;
  215. inherited;
  216. end;
  217. procedure TDecimalManager.ResetLinkViewColumns;
  218. procedure ResetCommonLink;
  219. begin
  220. FCommon.Quantity.ClearLinkViewCols;
  221. FCommon.TotalPrice.ClearLinkViewCols;
  222. FCommon.Price.ClearLinkViewCols;
  223. with TProjectData(FProjectData).BillsCompileData.sdvBillsCompile do
  224. begin
  225. // Quantity
  226. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('OrgQuantity'));
  227. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('MisQuantity'));
  228. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('OthQuantity'));
  229. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('Quantity'));
  230. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('DgnQuantity1'));
  231. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('DgnQuantity2'));
  232. // TotalPrice
  233. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('OrgTotalPrice'));
  234. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('MisTotalPrice'));
  235. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('OthTotalPrice'));
  236. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('TotalPrice'));
  237. // Price
  238. FCommon.Price.AddLinkViewCol(Columns.FindColumn('Price'));
  239. FCommon.Price.AddLinkViewCol(Columns.FindColumn('NewPrice'));
  240. end;
  241. with TProjectData(FProjectData).BillsMeasureData.sdvBillsMeasure do
  242. begin
  243. // Quantity
  244. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('Quantity'));
  245. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('CurDealQuantity'));
  246. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('CurQcQuantity'));
  247. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('CurPcQuantity'));
  248. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('CurGatherQuantity'));
  249. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('EndDealQuantity'));
  250. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('EndQcQuantity'));
  251. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('EndPcQuantity'));
  252. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('EndGahterQuantity'));
  253. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('AddDealQuantity'));
  254. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('AddQcQuantity'));
  255. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('AddPcQuantity'));
  256. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('AddGahterQuantity'));
  257. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('DealDgnQuantity1'));
  258. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('DealDgnQuantity2'));
  259. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('CDgnQuantity1'));
  260. FCommon.Quantity.AddLinkViewCol(Columns.FindColumn('CDgnQuantity2'));
  261. // TotalPrice
  262. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('TotalPrice'));
  263. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('CurDealTotalPrice'));
  264. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('CurQcTotalPrice'));
  265. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('CurPcTotalPrice'));
  266. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('CurGatherTotalPrice'));
  267. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('EndDealTotalPrice'));
  268. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('EndQcTotalPrice'));
  269. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('EndPcTotalPrice'));
  270. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('EndGahterTotalPrice'));
  271. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('AddDealTotalPrice'));
  272. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('AddQcTotalPrice'));
  273. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('AddPcTotalPrice'));
  274. FCommon.TotalPrice.AddLinkViewCol(Columns.FindColumn('AddGahterTotalPrice'));
  275. // Price
  276. FCommon.Price.AddLinkViewCol(Columns.FindColumn('Price'));
  277. FCommon.Price.AddLinkViewCol(Columns.FindColumn('NewPrice'));
  278. end;
  279. end;
  280. procedure ResetComplieLink;
  281. begin
  282. end;
  283. procedure ResetPriceMarginLink;
  284. begin
  285. FPriceMargin.Price.ClearLinkViewCols;
  286. with TProjectData(FProjectData).ProjectGLData.sdvProjectGL do
  287. begin
  288. FPriceMargin.Price.AddLinkViewCol(Columns.FindColumn('BasePrice'));
  289. FPriceMargin.Price.AddLinkViewCol(Columns.FindColumn('InfoPrice'));
  290. FPriceMargin.Price.AddLinkViewCol(Columns.FindColumn('DeltaPrice'));
  291. FPriceMargin.Price.AddLinkViewCol(Columns.FindColumn('ValidDeltaPrice'));
  292. FPriceMargin.Quantity.AddLinkViewCol(Columns.FindColumn('PM_Quantity'));
  293. FPriceMargin.Quantity.AddLinkViewCol(Columns.FindColumn('UsedQuantity'));
  294. FPriceMargin.Quantity.AddLinkViewCol(Columns.FindColumn('PAL_UsedQuantity'));
  295. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PM_TotalPrice'));
  296. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('UsedTotalPrice'));
  297. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PAL_UsedTotalPrice'));
  298. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PAL_UsedTotalPrice'));
  299. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PAL_DeltaPrice'));
  300. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PAL_Total'));
  301. end;
  302. with TProjectData(FProjectData).ProjectGLData.sdvGather do
  303. begin
  304. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('CurTotalPrice'));
  305. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PreTotalPrice'));
  306. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('EndTotalPrice'));
  307. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('AddTotalPrice'));
  308. end;
  309. with TProjectData(FProjectData).PriceMarginBillsData.sdvDetailGclBills do
  310. begin
  311. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PM_TotalPrice'));
  312. end;
  313. with TProjectData(FProjectData).PriceMarginBillsData.sdvDetailGL do
  314. begin
  315. FPriceMargin.Quantity.AddLinkViewCol(Columns.FindColumn('Quantity'));
  316. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PM_PreTotalPrice'));
  317. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PM_CurTotalPrice'));
  318. FPriceMargin.TotalPrice.AddLinkViewCol(Columns.FindColumn('PM_EndTotalPrice'));
  319. end;
  320. end;
  321. procedure ResetDealPayLink;
  322. begin
  323. with TProjectData(FProjectData).DealPaymentData.sdvDealPayment do
  324. begin
  325. FDealPay.TotalPrice.AddLinkViewCol(Columns.FindColumn('CurTotalPrice'));
  326. FDealPay.TotalPrice.AddLinkViewCol(Columns.FindColumn('TotalPrice'));
  327. FDealPay.TotalPrice.AddLinkViewCol(Columns.FindColumn('StartPrice'));
  328. FDealPay.TotalPrice.AddLinkViewCol(Columns.FindColumn('RangePrice'));
  329. end;
  330. end;
  331. begin
  332. {ResetCommonLink;
  333. FCommon.RefreshLinkViewColumnsFormat;
  334. ResetComplieLink;
  335. FCompile.RefreshLinkViewColumnsFormat;}
  336. ResetPriceMarginLink;
  337. FPriceMargin.RefreshLinkViewColumnsFormat;
  338. ResetDealPayLink;
  339. FDealPay.RefreshLinkViewColumnsFormat;
  340. end;
  341. end.