PhasePayDm.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. unit PhasePayDm;
  2. interface
  3. uses
  4. SysUtils, Classes, sdDB, sdProvider, ADODB, FormulaCalc, CalcDecimal;
  5. type
  6. TPhasePayData = class(TDataModule)
  7. sdpPhasePay: TsdADOProvider;
  8. sddPhasePay: TsdDataSet;
  9. procedure sddPhasePayBeforeValueChange(AValue: TsdValue;
  10. const NewValue: Variant; var Allow: Boolean);
  11. procedure sddPhasePayAfterValueChanged(AValue: TsdValue);
  12. private
  13. FPhaseData: TObject;
  14. FPayFormula: TPayFormula;
  15. FBeforeChangeTotalPrice: Double;
  16. FDecimal: TCalcDecimal;
  17. function CheckMinus(AID: Integer): Boolean;
  18. function GetFormula(AID: Integer): string;
  19. function GetCalcType(AID: Integer): Integer;
  20. function GetName(AID: Integer): string;
  21. function GetPayablePrice(AIndex: Integer): Double;
  22. procedure RepairPhaseRecord;
  23. procedure BeforeBatchOperation;
  24. procedure AfterBatchOperation;
  25. function GetCurPayable: Double;
  26. function GetLastestPhasePay(AType: Integer): Double;
  27. procedure SetFieldValidChar;
  28. function GetBqwc(AType: Integer): Double;
  29. function GetTableName: string;
  30. public
  31. constructor Create(APhaseData: TObject);
  32. destructor Destroy; override;
  33. procedure Open(AConnection: TADOConnection);
  34. procedure Save;
  35. function AddPayRecord(AID: Integer): TsdDataRecord;
  36. function PayRecord(AID: Integer): TsdDataRecord;
  37. procedure Delete(AID: Integer);
  38. procedure CalculateAll;
  39. procedure Calculate(AID: Integer);
  40. procedure CalculateCurPay;
  41. procedure UpdateDataForNewAudit;
  42. procedure CopyPrePhasePayData; overload;
  43. procedure CopyPrePhasePayData(const APreFile: string; APreFinalIndex: Integer); overload;
  44. property TableName: string read GetTableName;
  45. property PayablePrice[AIndex: Integer]: Double read GetPayablePrice;
  46. // 本期应付
  47. property CurPayable: Double read GetCurPayable;
  48. // AType表示不同类型,取值如下:
  49. // 1: 本期数据 2: 截止本期数据 3: 截止上期数据
  50. property LastestPhasePay[AType: Integer]: Double read GetLastestPhasePay;
  51. property Bqwc[AType: Integer]: Double read GetBqwc;
  52. property Decimal: TCalcDecimal read FDecimal;
  53. end;
  54. implementation
  55. uses
  56. PhaseData, ProjectData, DealPaymentDm, BillsDm, ConstUnit, UtilMethods;
  57. {$R *.dfm}
  58. { TPhasePayData }
  59. constructor TPhasePayData.Create(APhaseData: TObject);
  60. begin
  61. inherited Create(nil);
  62. FPhaseData := APhaseData;
  63. FPayFormula := TPayFormula.Create(TPhaseData(FPhaseData).ProjectData);
  64. with TProjectData(TPhaseData(FPhaseData).ProjectData) do
  65. begin
  66. FPayFormula.Decimal := ProjProperties.DecimalManager.DealPay;
  67. FDecimal := ProjProperties.DecimalManager.DealPay;
  68. end;
  69. end;
  70. procedure TPhasePayData.Delete(AID: Integer);
  71. begin
  72. sddPhasePay.Remove(sddPhasePay.FindKey('idxID', AID));
  73. end;
  74. destructor TPhasePayData.Destroy;
  75. begin
  76. FPayFormula.Free;
  77. inherited;
  78. end;
  79. procedure TPhasePayData.UpdateDataForNewAudit;
  80. var
  81. iNewAudit, iIndex: Integer;
  82. Rec: TsdDataRecord;
  83. begin
  84. BeforeBatchOperation;
  85. try
  86. iNewAudit := TPhaseData(FPhaseData).PhaseProperty.AuditCount;
  87. for iIndex := 0 to sddPhasePay.RecordCount - 1 do
  88. begin
  89. Rec := sddPhasePay.Records[iIndex];
  90. Rec.ValueByName('TotalPrice' + IntToStr(iNewAudit)).AsString :=
  91. Rec.ValueByName('TotalPrice' + IntToStr(iNewAudit - 1)).AsString;
  92. Rec.ValueByName('Formula' + IntToStr(iNewAudit)).AsString :=
  93. Rec.ValueByName('Formula' + IntToStr(iNewAudit - 1)).AsString;
  94. Rec.ValueByName('EndTotalPrice' + IntToStr(iNewAudit)).AsString :=
  95. Rec.ValueByName('EndTotalPrice' + IntToStr(iNewAudit - 1)).AsString;
  96. Rec.ValueByName('PreTotalPrice' + IntToStr(iNewAudit)).AsString :=
  97. Rec.ValueByName('PreTotalPrice' + IntToStr(iNewAudit - 1)).AsString;
  98. end;
  99. finally
  100. AfterBatchOperation;
  101. end;
  102. end;
  103. procedure TPhasePayData.Open(AConnection: TADOConnection);
  104. begin
  105. sdpPhasePay.Connection := AConnection;
  106. sddPhasePay.Open;
  107. if not Assigned(sddPhasePay.IndexList.FindByName('idxID')) then
  108. sddPhasePay.AddIndex('idxID', 'ID');
  109. SetFieldValidChar;
  110. // 为适应旧项目处不计算合同支付值所做修改。
  111. if sddPhasePay.RecordCount = 0 then
  112. begin
  113. CopyPrePhasePayData;
  114. CalculateAll;
  115. end;
  116. end;
  117. procedure TPhasePayData.Save;
  118. begin
  119. sddPhasePay.Save;
  120. end;
  121. function TPhasePayData.AddPayRecord(AID: Integer): TsdDataRecord;
  122. begin
  123. Result := sddPhasePay.Add;
  124. Result.ValueByName('ID').AsInteger := AID;
  125. end;
  126. procedure TPhasePayData.sddPhasePayBeforeValueChange(AValue: TsdValue;
  127. const NewValue: Variant; var Allow: Boolean);
  128. begin
  129. if not Assigned(AValue) then Exit;
  130. if Pos('TotalPrice', AValue.FieldName) = 1 then
  131. FBeforeChangeTotalPrice := AValue.AsFloat;
  132. end;
  133. procedure TPhasePayData.sddPhasePayAfterValueChanged(AValue: TsdValue);
  134. begin
  135. if Pos('TotalPrice', AValue.FieldName) = 1 then
  136. begin
  137. AValue.Owner.ValueByName('End' + AValue.FieldName).AsFloat := AValue.AsFloat
  138. + AValue.Owner.ValueByName('Pre' + AValue.FieldName).AsFloat;
  139. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  140. UpdateTotalPrice(AValue.Owner.ValueByName('ID').AsInteger, AValue.AsFloat - FBeforeChangeTotalPrice);
  141. end;
  142. CalculateCurPay;
  143. end;
  144. procedure TPhasePayData.CalculateAll;
  145. var
  146. iIndex: Integer;
  147. Rec: TsdDataRecord;
  148. begin
  149. if TPhaseData(FPhaseData).StageDataReadOnly then Exit;
  150. RepairPhaseRecord;
  151. for iIndex := 0 to sddPhasePay.RecordCount - 1 do
  152. begin
  153. Rec := sddPhasePay.Records[iIndex];
  154. if GetCalcType(Rec.ValueByName('ID').AsInteger) in [0, 3] then
  155. Calculate(Rec.ValueByName('ID').AsInteger);
  156. end; CalculateCurPay;
  157. end;
  158. procedure TPhasePayData.Calculate(AID: Integer);
  159. var
  160. Rec: TsdDataRecord;
  161. // 金额列名, 公式列名
  162. sTPField, sFField, sPreField: string;
  163. iID: Integer;
  164. fTotalPrice, fStartedPrice: Double;
  165. begin
  166. Rec := sddPhasePay.FindKey('idxID', AID);
  167. sTPField := 'TotalPrice' + IntToStr(TPhaseData(FPhaseData).StageIndex);
  168. sFField := 'Formula' + IntToStr(TPhaseData(FPhaseData).StageIndex);
  169. sPreField := 'PreTotalPrice' + IntToStr(TPhaseData(FPhaseData).StageIndex);
  170. if not Rec.ValueByName('StopCalc').AsBoolean then
  171. begin
  172. iID := Rec.ValueByName('ID').AsInteger;
  173. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  174. begin
  175. // 获取起扣金额
  176. fStartedPrice := GetStartedPrice(iID);
  177. if Rec.ValueByName(sFField).AsString <> '' then
  178. begin
  179. // 初次达到起扣金额时,bqwc基数值取值为累计完成计量-起扣金额
  180. if FDecimal.TotalPrice.RoundTo(Rec.ValueByName('Pre'+sTPField).AsFloat) = 0 then
  181. fTotalPrice := FPayFormula.Calculate(Rec.ValueByName(sFField).AsString, fStartedPrice)
  182. else
  183. fTotalPrice := FPayFormula.Calculate(Rec.ValueByName(sFField).AsString);
  184. end
  185. else
  186. fTotalPrice := 0;
  187. if CheckStartedPrice(iID) then
  188. Rec.ValueByName(sTPField).AsFloat := GetAllowTotalPrice(iID, fTotalPrice, Rec.ValueByName(sPreField).AsFloat)
  189. else
  190. Rec.ValueByName(sTPField).AsFloat := 0;
  191. end;
  192. end
  193. else
  194. Rec.ValueByName(sTPField).AsFloat := 0;
  195. {if Rec.ValueByName(sFField).AsString <> '' then
  196. begin
  197. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  198. begin
  199. iID := Rec.ValueByName('ID').AsInteger;
  200. // 获取起扣金额
  201. fStartedPrice := GetStartedPrice(iID);
  202. // 初次达到起扣金额时,bqwc基数值取值为累计完成计量-起扣金额
  203. if Rec.ValueByName('Pre'+sTPField).AsFloat = 0 then
  204. fTotalPrice := FPayFormula.Calculate(Rec.ValueByName(sFField).AsString, fStartedPrice)
  205. else
  206. fTotalPrice := FPayFormula.Calculate(Rec.ValueByName(sFField).AsString);
  207. if CheckStartedPrice(iID) then
  208. Rec.ValueByName(sTPField).AsFloat := GetAllowTotalPrice(iID, fTotalPrice, Rec.ValueByName(sPreField).AsFloat)
  209. else
  210. Rec.ValueByName(sTPField).AsFloat := 0;
  211. end;
  212. end;}
  213. // 计算截止数据
  214. Rec.ValueByName('End' + sTPField).AsFloat := FDecimal.TotalPrice.RoundTo(Rec.ValueByName(sTPField).AsFloat
  215. + Rec.ValueByName('Pre' + sTPField).AsFloat);
  216. with TProjectData(TPhaseData(FPhaseData).ProjectData) do
  217. if StageDataReadOnly then
  218. DealPaymentData.SetTotalPrice(AID, Rec.ValueByName('End' + sTPField).AsFloat);
  219. end;
  220. function TPhasePayData.GetPayablePrice(AIndex: Integer): Double;
  221. var
  222. iIndex: Integer;
  223. sTotalPriceField: string;
  224. Rec: TsdDataRecord;
  225. begin
  226. Result := 0;
  227. // 阶段在0--14之间
  228. if (AIndex < 0) or (AIndex > iMaxStageCount-1) then Exit;
  229. sTotalPriceField := 'TotalPrice' + IntToStr(AIndex);
  230. for iIndex := 0 to sddPhasePay.RecordCount - 1 do
  231. begin
  232. Rec := sddPhasePay.Records[iIndex];
  233. if CheckMinus(Rec.ValueByName('ID').AsInteger) then
  234. Result := Result - Rec.ValueByName(sTotalPriceField).AsFloat
  235. else
  236. Result := Result + Rec.ValueByName(sTotalPriceField).AsFloat;
  237. end;
  238. end;
  239. function TPhasePayData.CheckMinus(AID: Integer): Boolean;
  240. begin
  241. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  242. Result := sddDealPayment.FindKey('idxID', AID).ValueByName('IsMinus').AsBoolean;
  243. end;
  244. function TPhasePayData.GetFormula(AID: Integer): string;
  245. begin
  246. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  247. Result := sddDealPayment.FindKey('idxID', AID).ValueByName('Formula').AsString;
  248. end;
  249. procedure TPhasePayData.AfterBatchOperation;
  250. begin
  251. sddPhasePay.BeforeValueChange := sddPhasePayBeforeValueChange;
  252. sddPhasePay.AfterValueChanged := sddPhasePayAfterValueChanged;
  253. sddPhasePay.EndUpdate;
  254. end;
  255. procedure TPhasePayData.BeforeBatchOperation;
  256. begin
  257. sddPhasePay.BeginUpdate;
  258. sddPhasePay.BeforeValueChange := nil;
  259. sddPhasePay.AfterValueChanged := nil;
  260. end;
  261. procedure TPhasePayData.CalculateCurPay;
  262. var
  263. Rec: TsdDataRecord;
  264. sTPField: string;
  265. I: Integer;
  266. begin
  267. sTPField := 'TotalPrice' + IntToStr(TPhaseData(FPhaseData).StageIndex);
  268. for I := 0 to sddPhasePay.RecordCount - 1 do
  269. begin
  270. Rec := sddPhasePay.Records[I];
  271. if GetCalcType(Rec.ValueByName('ID').AsInteger) = 1 then
  272. begin
  273. Rec.ValueByName(sTPField).AsFloat := CurPayable;
  274. Rec.ValueByName('End' + sTPField).AsFloat := Rec.ValueByName(sTPField).AsFloat
  275. + Rec.ValueByName('Pre' + sTPField).AsFloat;
  276. Break;
  277. end;
  278. end;
  279. end;
  280. function TPhasePayData.GetCurPayable: Double;
  281. var
  282. Rec: TsdDataRecord;
  283. sTPField: string;
  284. i: Integer;
  285. begin
  286. sTPField := 'TotalPrice' + IntToStr(TPhaseData(FPhaseData).StageIndex);
  287. Result := 0;
  288. for I := 0 to sddPhasePay.RecordCount - 1 do
  289. begin
  290. Rec := sddPhasePay.Records[I];
  291. // 检查是否为一般项
  292. if GetCalcType(Rec.ValueByName('ID').AsInteger) = 0 then
  293. if CheckMinus(Rec.ValueByName('ID').AsInteger) then
  294. Result := Result - Rec.ValueByName(sTPField).AsFloat
  295. else
  296. Result := Result + Rec.ValueByName(sTPField).AsFloat;
  297. end;
  298. end;
  299. function TPhasePayData.GetCalcType(AID: Integer): Integer;
  300. var
  301. Rec: TsdDataRecord;
  302. begin
  303. Result := 0;
  304. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  305. Rec := sddDealPayment.FindKey('idxID', AID);
  306. if Assigned(Rec) then
  307. Result := Rec.ValueByName('CalcType').AsInteger;
  308. end;
  309. function TPhasePayData.GetLastestPhasePay(AType: Integer): Double;
  310. var
  311. Rec: TsdDataRecord;
  312. sTPField: string;
  313. i: Integer;
  314. begin
  315. case AType of
  316. 1: sTPField := 'TotalPrice' + IntToStr(TPhaseData(FPhaseData).PhaseProperty.AuditCount);
  317. 2: sTPField := 'EndTotalPrice' + IntToStr(TPhaseData(FPhaseData).PhaseProperty.AuditCount);
  318. 3: sTPField := 'PreTotalPrice' + IntToStr(TPhaseData(FPhaseData).PhaseProperty.AuditCount);
  319. end;
  320. Result := 0;
  321. for i := 0 to sddPhasePay.RecordCount - 1 do
  322. begin
  323. Rec := sddPhasePay.Records[i];
  324. // 检查是否为一般项
  325. if GetCalcType(Rec.ValueByName('ID').AsInteger) = 0 then
  326. if CheckMinus(Rec.ValueByName('ID').AsInteger) then
  327. Result := Result - Rec.ValueByName(sTPField).AsFloat
  328. else
  329. Result := Result + Rec.ValueByName(sTPField).AsFloat;
  330. end;
  331. end;
  332. procedure TPhasePayData.CopyPrePhasePayData;
  333. var
  334. iRecord: Integer;
  335. Rec, NewRec: TsdDataRecord;
  336. begin
  337. BeforeBatchOperation;
  338. try
  339. if sddPhasePay.RecordCount > 0 then Exit;
  340. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  341. for iRecord := 0 to sddDealPayment.RecordCount - 1 do
  342. begin
  343. Rec := sddDealPayment.Records[iRecord];
  344. NewRec := sddPhasePay.Add;
  345. NewRec.ValueByName('ID').AsInteger := Rec.ValueByName('ID').AsInteger;
  346. NewRec.ValueByName('EndTotalPrice0').AsFloat := Rec.ValueByName('TotalPrice').AsFloat;
  347. NewRec.ValueByName('PreTotalPrice0').AsFloat := Rec.ValueByName('TotalPrice').AsFloat;
  348. NewRec.ValueByName('Formula0').AsString := Rec.ValueByName('Formula').AsString;
  349. NewRec.ValueByName('StopCalc').AsBoolean := Rec.ValueByName('StopCalc').AsBoolean;
  350. end;
  351. finally
  352. AfterBatchOperation;
  353. end;
  354. end;
  355. procedure TPhasePayData.SetFieldValidChar;
  356. var
  357. i: Integer;
  358. sField: string;
  359. begin
  360. for i := 0 to iMaxStageCount - 1 do
  361. begin
  362. sField := 'TotalPrice' + IntToStr(i);
  363. sddPhasePay.FieldByName(sField).ValidChars := sddPhasePay.FieldByName(sField).ValidChars + ArithmeticCharSet + ExprsBaseCharSet;
  364. end;
  365. end;
  366. function TPhasePayData.PayRecord(AID: Integer): TsdDataRecord;
  367. begin
  368. Result := sddPhasePay.FindKey('idxID', AID);
  369. end;
  370. procedure TPhasePayData.RepairPhaseRecord;
  371. var
  372. i: Integer;
  373. vDealRec, vPhaseRec: TsdDataRecord;
  374. begin
  375. // 旧设计中,DealPayment中数据多于PhasePay
  376. // 在于用户新增了合同支付项,但未输入金额或公式,以减少数据
  377. // 现,要求存在合同支付项,设置了计提期限,但未输入金额或公式时,达计提期限时,金额需计
  378. // 故在每次计算前,检查一遍合同支付数据,以兼容旧项目,并保证DealPayment与PhasePay数据行相同
  379. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  380. begin
  381. if sddDealPayment.RecordCount > sddPhasePay.RecordCount then
  382. begin
  383. for i := 0 to sddDealPayment.RecordCount - 1 do
  384. begin
  385. vDealRec := sddDealPayment.Records[i];
  386. vPhaseRec := PayRecord(vDealRec.ValueByName('ID').AsInteger);
  387. if not Assigned(vPhaseRec) then
  388. vPhaseRec := AddPayRecord(vDealRec.ValueByName('ID').AsInteger);
  389. end;
  390. end;
  391. end;
  392. end;
  393. function TPhasePayData.GetBqwc(AType: Integer): Double;
  394. var
  395. Rec: TsdDataRecord;
  396. sTPField: string;
  397. i: Integer;
  398. begin
  399. case AType of
  400. 1: sTPField := 'TotalPrice' + IntToStr(TPhaseData(FPhaseData).PhaseProperty.AuditCount);
  401. 2: sTPField := 'EndTotalPrice' + IntToStr(TPhaseData(FPhaseData).PhaseProperty.AuditCount);
  402. 3: sTPField := 'PreTotalPrice' + IntToStr(TPhaseData(FPhaseData).PhaseProperty.AuditCount);
  403. end;
  404. Result := 0;
  405. for i := 0 to sddPhasePay.RecordCount - 1 do
  406. begin
  407. Rec := sddPhasePay.Records[i];
  408. // 检查是否为一般项
  409. if GetName(Rec.ValueByName('ID').AsInteger) = '本期完成计量' then
  410. begin
  411. Result := Rec.ValueByName(sTPField).AsFloat;
  412. Break;
  413. end;
  414. end;
  415. end;
  416. function TPhasePayData.GetName(AID: Integer): string;
  417. var
  418. Rec: TsdDataRecord;
  419. begin
  420. Result := '';
  421. with TProjectData(TPhaseData(FPhaseData).ProjectData).DealPaymentData do
  422. Rec := sddDealPayment.FindKey('idxID', AID);
  423. if Assigned(Rec) then
  424. Result := Rec.ValueByName('Name').AsString;
  425. end;
  426. procedure TPhasePayData.CopyPrePhasePayData(const APreFile: string;
  427. APreFinalIndex: Integer);
  428. const
  429. sCopySql = 'Insert Into %s (' +
  430. ' ID, PreTotalPrice0, EndTotalPrice0, Formula0, StopCalc' +
  431. ' ) Select ID, EndTotalPrice%d, EndTotalPrice%d, Formula%d, StopCalc' +
  432. ' From %s' +
  433. ' In ''%s''';
  434. var
  435. sPre, sSql: string;
  436. i: Integer;
  437. vRec: TsdDataRecord;
  438. fValue: Double;
  439. begin
  440. try
  441. sSql := Format(sCopySql, [TableName, APreFinalIndex, APreFinalIndex,
  442. APreFinalIndex, TableName, APreFile]);
  443. ExecuteSql(TPhaseData(FPhaseData).ADOConnection, sSql);
  444. finally
  445. sddPhasePay.Reload;
  446. for i := 0 to sddPhasePay.RecordCount - 1 do
  447. begin
  448. vRec := sddPhasePay.Records[i];
  449. if TryStrToFloat(vRec.ValueByName('Formula0').AsString, fValue) then
  450. vRec.ValueByName('Formula0').AsString := '';
  451. end;
  452. end;
  453. end;
  454. function TPhasePayData.GetTableName: string;
  455. begin
  456. Result := sdpPhasePay.TableName;
  457. end;
  458. end.