CalcDecimal.pas 12 KB

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