DealPaymentDm.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. unit DealPaymentDm;
  2. interface
  3. uses
  4. SysUtils, Classes, sdDB, sdProvider, ADODB, FormulaCalc, UtilMethods;
  5. type
  6. TDealPaymentData = class(TDataModule)
  7. sdpDealPayment: TsdADOProvider;
  8. sddDealPayment: TsdDataSet;
  9. sdvDealPayment: TsdDataView;
  10. procedure sddDealPaymentAfterAddRecord(ARecord: TsdDataRecord);
  11. procedure sdvDealPaymentGetText(var Text: String;
  12. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  13. DisplayText: Boolean);
  14. procedure sdvDealPaymentNeedLookupRecord(ARecord: TsdDataRecord;
  15. AColumn: TsdViewColumn; ANewText: String);
  16. procedure sdvDealPaymentSetText(var Text: string;
  17. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  18. var Allow: Boolean);
  19. procedure sddDealPaymentBeforeAddRecord(ARecord: TsdDataRecord;
  20. var Allow: Boolean);
  21. procedure sddDealPaymentAfterValueChanged(AValue: TsdValue);
  22. procedure sddDealPaymentBeforeDeleteRecord(ARecord: TsdDataRecord;
  23. var Allow: Boolean);
  24. procedure sdvDealPaymentBeforeAddRecord(ARecord: TsdDataRecord;
  25. var Allow: Boolean);
  26. private
  27. FProjectData: TObject;
  28. FPayFormula: TPayFormula;
  29. procedure InitPredefinedPayItems;
  30. procedure RepairSerialNo;
  31. procedure RepairLockedFormula;
  32. function GetNewID: Integer;
  33. function GetNewSerialNo: Integer;
  34. function GetAddTotalPrice: Double;
  35. function GetCutTotalPrice: Double;
  36. function GetPaidTotalPrice: Double;
  37. public
  38. constructor Create(AProjectData: TObject);
  39. destructor Destroy; override;
  40. procedure Open(AConnection: TADOConnection);
  41. procedure Close;
  42. procedure Save;
  43. procedure Init;
  44. // 扣款项是否已达起扣金额
  45. function CheckStartedPrice(AID: Integer): Boolean;
  46. function CheckReachPlan(ARec: TsdDataRecord): Boolean;
  47. function GetAllowTotalPrice(AID: Integer; ATotalPrice: Double): Double;
  48. procedure UpdateTotalPrice(AID: Integer; ATotalPrice: Double);
  49. function DealPayRecord(const AName: string): TsdDataRecord;
  50. function PlanStr(ARec: TsdDataRecord): string;
  51. function GetStartedPrice(AID: Integer): Double;
  52. procedure CalcStarted_RangePrice;
  53. procedure ResetPhaseLink;
  54. procedure LockedData;
  55. procedure UpdateLinkSerialNo;
  56. property ProjectData: TObject read FProjectData;
  57. // 扣款项,累计金额合计
  58. property CutTotalPrice: Double read GetCutTotalPrice;
  59. // 非扣款项,累计金额合计
  60. property AddTotalPrice: Double read GetAddTotalPrice;
  61. // 实付
  62. property PaidTotalPrice: Double read GetPaidTotalPrice;
  63. property PayFormula: TPayFormula read FPayFormula;
  64. end;
  65. implementation
  66. uses
  67. ProjectData, PhasePayDm, PhaseData, ZhAPI, BillsDm, Math;
  68. {$R *.dfm}
  69. { TDealPaymentData }
  70. function TDealPaymentData.CheckStartedPrice(AID: Integer): Boolean;
  71. var
  72. Rec: TsdDataRecord;
  73. begin
  74. Rec := sddDealPayment.FindKey('idxID', AID);
  75. with TProjectData(FProjectData).BillsData do
  76. Result := Settlement[AddGatherIndex] >= Rec.ValueByName('StartedPrice').AsFloat;
  77. end;
  78. constructor TDealPaymentData.Create(AProjectData: TObject);
  79. begin
  80. inherited Create(nil);
  81. FProjectData := AProjectData;
  82. FPayFormula := TPayFormula.Create(FProjectData);
  83. end;
  84. destructor TDealPaymentData.Destroy;
  85. begin
  86. FPayFormula.Free;
  87. inherited;
  88. end;
  89. function TDealPaymentData.GetAllowTotalPrice(AID: Integer;
  90. ATotalPrice: Double): Double;
  91. var
  92. Rec: TsdDataRecord;
  93. fAllowPrice: Double;
  94. begin
  95. Result := ATotalPrice;
  96. Rec := sddDealPayment.FindKey('idxID', AID);
  97. if Rec.ValueByName('RangePrice').AsFloat = 0 then Exit;
  98. fAllowPrice := Rec.ValueByName('RangePrice').AsFloat - Rec.ValueByName('TotalPrice').AsFloat;
  99. if not CheckReachPlan(Rec) then
  100. Result := Min(fAllowPrice, ATotalPrice)
  101. else
  102. Result := fAllowPrice;
  103. end;
  104. function TDealPaymentData.GetNewID: Integer;
  105. var
  106. idx: TsdIndex;
  107. begin
  108. idx := sddDealPayment.FindIndex('idxID');
  109. if idx.RecordCount > 0 then
  110. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  111. else
  112. Result := 1;
  113. end;
  114. procedure TDealPaymentData.Init;
  115. begin
  116. if sddDealPayment.RecordCount > 0 then Exit;
  117. InitPredefinedPayItems;
  118. end;
  119. procedure TDealPaymentData.InitPredefinedPayItems;
  120. procedure AddPredefinedPayItem(const AItem: string);
  121. var
  122. sgsItem: TStrings;
  123. f: Double;
  124. Rec: TsdDataRecord;
  125. begin
  126. sgsItem := TStringList.Create;
  127. try
  128. sgsItem.Delimiter := ';';
  129. sgsItem.DelimitedText := AItem;
  130. if sgsItem.Count < 7 then Exit;
  131. Rec := sddDealPayment.Add;
  132. Rec.ValueByName('Name').AsString := sgsItem[0];
  133. Rec.ValueByName('CalcType').AsInteger := StrToIntDef(sgsItem[1], 0);
  134. Rec.ValueByName('IsMinus').AsBoolean := sgsItem[2] = '1';
  135. if TryStrToFloat(sgsItem[3], f) then
  136. Rec.ValueByName('StartedPrice').AsFloat := f
  137. else
  138. Rec.ValueByName('SFormula').AsString := sgsItem[3];
  139. if TryStrToFloat(sgsItem[4], f) then
  140. Rec.ValueByName('RangePrice').AsFloat := f
  141. else
  142. Rec.ValueByName('RFormula').AsString := sgsItem[4];
  143. Rec.ValueByName('Formula').AsString := sgsItem[5];
  144. if SameText(sgsItem[5], 'bqwc') then
  145. Rec.ValueByName('LockedFormula').AsBoolean := True;
  146. Rec.ValueByName('PreDefined').AsBoolean := StrToBoolDef(sgsItem[6], False);
  147. finally
  148. sgsItem.Free;
  149. end;
  150. end;
  151. var
  152. sgsItems: TStrings;
  153. I: Integer;
  154. begin
  155. sgsItems := TStringList.Create;
  156. try
  157. sgsItems.LoadFromFile(GetAppFilePath + 'DealPayment.txt');
  158. for I := 0 to sgsItems.Count - 1 do
  159. if sgsItems[I] <> '' then
  160. AddPredefinedPayItem(sgsItems[I]);
  161. finally
  162. sgsItems.Free;
  163. end;
  164. end;
  165. procedure TDealPaymentData.LockedData;
  166. var
  167. iIndex: Integer;
  168. Rec: TsdDataRecord;
  169. begin
  170. for iIndex := 0 to sddDealPayment.RecordCount - 1 do
  171. begin
  172. Rec := sddDealPayment.Records[iIndex];
  173. Rec.ValueByName('Locked').AsBoolean := True;
  174. if Rec.ValueByName('Formula').AsString <> '' then
  175. Rec.ValueByName('LockedFormula').AsBoolean := True;
  176. end;
  177. end;
  178. procedure TDealPaymentData.Open(AConnection: TADOConnection);
  179. begin
  180. sdpDealPayment.Connection := AConnection;
  181. sddDealPayment.Open;
  182. RepairSerialNo;
  183. RepairLockedFormula;
  184. if not Assigned(sddDealPayment.IndexList.FindByName('idxID')) then
  185. sddDealPayment.AddIndex('idxID', 'ID');
  186. if not Assigned(sddDealPayment.IndexList.FindByName('idxView')) then
  187. sddDealPayment.AddIndex('idxView', 'SerialNo');
  188. // 开始计量前,CutTotalPrice链接至TotalPrice,已解决无计量数据时无法输入问题
  189. sddDealPayment.FieldByName('TotalPrice').ValidChars := sddDealPayment.FieldByName('TotalPrice').ValidChars + ArithmeticCharSet + ExprsBaseCharSet;
  190. sddDealPayment.FieldByName('StartedPrice').ValidChars := sddDealPayment.FieldByName('StartedPrice').ValidChars + ArithmeticCharSet + ExprsExceptCharSet;
  191. sddDealPayment.FieldByName('RangePrice').ValidChars := sddDealPayment.FieldByName('RangePrice').ValidChars + ArithmeticCharSet + ExprsExceptCharSet;
  192. sdvDealPayment.Open;
  193. sdvDealPayment.IndexName := 'idxView';
  194. end;
  195. procedure TDealPaymentData.ResetPhaseLink;
  196. begin
  197. with TProjectData(FProjectData).PhaseData do
  198. begin
  199. sdvDealPayment.Columns.FindColumn('CurTotalPrice').LookupDataSet := PhasePayData.sddPhasePay;
  200. sdvDealPayment.Columns.FindColumn('CurTotalPrice').LookupResultField := 'TotalPrice' + IntToStr(StageIndex);
  201. end;
  202. end;
  203. procedure TDealPaymentData.Save;
  204. begin
  205. sddDealPayment.Save;
  206. end;
  207. procedure TDealPaymentData.sddDealPaymentAfterAddRecord(
  208. ARecord: TsdDataRecord);
  209. var
  210. iSerialNo: Integer;
  211. begin
  212. iSerialNo := GetNewSerialNo;
  213. ARecord.ValueByName('ID').AsInteger := GetNewID;
  214. ARecord.ValueByName('SerialNo').AsInteger := iSerialNo;
  215. ARecord.ValueByName('CreatePhaseID').AsInteger := TProjectData(FProjectData).PhaseIndex;
  216. end;
  217. procedure TDealPaymentData.sdvDealPaymentGetText(var Text: String;
  218. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  219. DisplayText: Boolean);
  220. procedure GetDisplayText;
  221. begin
  222. if not Assigned(AValue) or
  223. ((Pos('Price', AValue.FieldName) > 0) and (AValue.AsFloat = 0)) then
  224. Text := '';
  225. end;
  226. function GetFormulaField(const APriceField: string): string;
  227. begin
  228. if SameText(APriceField, 'StartedPrice') then
  229. Result := 'SFormula'
  230. else if SameText(APriceField, 'RangePrice') then
  231. Result := 'RFormula'
  232. else if SameText(APriceField, 'CurTotalPrice') then
  233. Result := 'Formula'
  234. else if Pos('TotalPrice', APriceField) = 1 then
  235. Result := StringReplace(AValue.FieldName, 'TotalPrice', 'Formula', []);
  236. end;
  237. procedure GetStageEditText;
  238. var
  239. sField, sFormula: string;
  240. begin
  241. if Assigned(AValue) then
  242. begin
  243. Text := AValue.AsString;
  244. sField := GetFormulaField(AValue.FieldName);
  245. sFormula := AValue.Owner.ValueByName(sField).AsString;
  246. if (sField <> '') and (sFormula <> '') then
  247. Text:= sFormula;
  248. end
  249. else
  250. Text := '';
  251. end;
  252. procedure GetMainEditText;
  253. var
  254. sField, sFormula: string;
  255. begin
  256. sField := GetFormulaField(AColumn.FieldName);
  257. if (sField <> '') then
  258. begin
  259. sFormula := ARecord.ValueByName(sField).AsString;
  260. if (sFormula <> '') then
  261. Text := sFormula;
  262. end
  263. else
  264. Text := '';
  265. end;
  266. begin
  267. if Pos('Price', AColumn.FieldName)> 0 then
  268. begin
  269. if DisplayText then
  270. GetDisplayText
  271. else if TProjectData(FProjectData).PhaseIndex > 0 then
  272. GetStageEditText
  273. else
  274. GetMainEditText;
  275. end;
  276. end;
  277. procedure TDealPaymentData.sdvDealPaymentNeedLookupRecord(
  278. ARecord: TsdDataRecord; AColumn: TsdViewColumn; ANewText: String);
  279. procedure SetNewRecData(APayRec: TsdDataRecord);
  280. var
  281. AID: Integer;
  282. sTPField, sFField: string;
  283. fTotalPrice: Double;
  284. begin
  285. sTPField := 'TotalPrice' + IntToStr(TProjectData(FProjectData).PhaseData.StageIndex);
  286. sFField := 'Formula' + IntToStr(TProjectData(FProjectData).PhaseData.StageIndex);
  287. if CheckNumeric(ANewText) then
  288. APayRec.ValueByName(sTPField).AsString := ANewText
  289. else
  290. begin
  291. AID := ARecord.ValueByName('ID').AsInteger;
  292. APayRec.ValueByName(sFField).AsString := ANewText;
  293. ARecord.ValueByName('Formula').AsString := ANewText;
  294. fTotalPrice := FPayFormula.Calculate(ANewText, ARecord.ValueByName('StartedPrice').AsFloat);
  295. if CheckStartedPrice(AID) then
  296. APayRec.ValueByName(sTPField).AsFloat := GetAllowTotalPrice(AID, fTotalPrice);
  297. end;
  298. end;
  299. var
  300. NewRec: TsdDataRecord;
  301. begin
  302. if SameText(AColumn.FieldName, 'CurTotalPrice') then
  303. begin
  304. with TProjectData(FProjectData).PhaseData.PhasePayData do
  305. NewRec := AddPayRecord(ARecord.ValueByName('ID').AsInteger);
  306. SetNewRecData(NewRec);
  307. end;
  308. end;
  309. procedure TDealPaymentData.sdvDealPaymentSetText(var Text: string;
  310. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  311. var Allow: Boolean);
  312. procedure CheckLockedData;
  313. var
  314. Rec: TsdDataRecord;
  315. sFormula: string;
  316. begin
  317. Rec := sddDealPayment.FindKey('idxID', ARecord.ValueByName('ID').AsInteger);
  318. if Rec.ValueByName('CalcType').AsInteger = 1 then
  319. begin
  320. if SameText(AValue.FieldName, 'Name') or
  321. SameText(AValue.FieldName, 'IsMinus') or
  322. (Pos('TotalPrice', AValue.FieldName) > 0) or
  323. SameText(AValue.FieldName, 'StartedPrice') or
  324. SameText(AValue.FieldName, 'RangePrice') then
  325. DataSetErrorMessage(Allow, '固定项不可修改!');
  326. end;
  327. if not Allow then Exit;
  328. if Rec.ValueByName('LockedFormula').AsBoolean then
  329. begin
  330. if (Pos('TotalPrice', AValue.FieldName) = 1) and
  331. (Rec.ValueByName('Formula').AsString <> '') then
  332. begin
  333. // 如果有公式计算,如果是纯数学计算式,则可设置,基数计算,则不可
  334. sFormula := Rec.ValueByName('Formula').AsString;
  335. if (Pos('bqwc', sFormula)>0) or (Pos('htj', sFormula)>0) or
  336. (Pos('kgyfk', sFormula)>0) or (Pos('clyfk', sFormula)>0) then
  337. DataSetErrorMessage(Allow, '该支付(扣款)项已设置基数计算公式且被锁定,不可修改!');
  338. end;
  339. end;
  340. if not Allow then Exit;
  341. if Rec.ValueByName('Locked').AsBoolean then
  342. begin
  343. if SameText(AValue.FieldName, 'StartedPrice') then
  344. DataSetErrorMessage(Allow, '该支付(扣款)项的起扣金额被锁定,不可修改!')
  345. else if SameText(AValue.FieldName, 'RangePrice') then
  346. DataSetErrorMessage(Allow, '该支付(扣款)项的付(扣)款限额被锁定,不可修改!')
  347. else if SameText(AValue.FieldName, 'Name') or SameText(AValue.FieldName, 'IsMinus') then
  348. DataSetErrorMessage(Allow, '该项已被锁定,不可修改!');
  349. end;
  350. end;
  351. procedure DoStartedPriceChanged;
  352. begin
  353. if AValue.Owner.ValueByName('TotalPrice').AsFloat <> 0 then
  354. DataSetErrorMessage(Allow, '该付(扣)款金额已经计量,不可修改起扣金额!');
  355. if not Allow then Exit;
  356. if CheckStringNull(Text) or CheckNumeric(Text) then
  357. AValue.Owner.ValueByName('SFormula').AsString := ''
  358. else if Pos('bqwc', Text) = 0 then
  359. begin
  360. AValue.Owner.ValueByName('SFormula').AsString := Text;
  361. Text := FloatToStr(FPayFormula.Calculate(Text));
  362. end
  363. else
  364. DataSetErrorMessage(Allow, '起扣金额不可引用“本期计算价”进行计算!');
  365. end;
  366. procedure DoRangePriceChanged;
  367. begin
  368. if AValue.Owner.ValueByName('TotalPrice').AsFloat <> 0 then
  369. DataSetErrorMessage(Allow, '该付(扣)款金额已经计量,不可修改付(扣)款限额!');
  370. if not Allow then Exit;
  371. if CheckStringNull(Text) or CheckNumeric(Text) then
  372. AValue.Owner.ValueByName('RFormula').AsString := ''
  373. else if Pos('bqwc', Text) = 0 then
  374. begin
  375. AValue.Owner.ValueByName('RFormula').AsString := Text;
  376. Text := FloatToStr(FPayFormula.Calculate(Text));
  377. end
  378. else
  379. DataSetErrorMessage(Allow, '付(扣)款限额不可引用“本期计算价”进行计算!');
  380. end;
  381. procedure DoCurTotalPriceChanged;
  382. var
  383. AID: Integer;
  384. sFField: string;
  385. fTotalPrice: Double;
  386. Rec: TsdDataRecord;
  387. begin
  388. Rec := sddDealPayment.FindKey('idxID', ARecord.ValueByName('ID').AsInteger);
  389. sFField := StringReplace(AValue.FieldName, 'TotalPrice', 'Formula', []);
  390. if CheckStringNull(Text) or CheckNumeric(Text) then
  391. begin
  392. Rec.ValueByName('Formula').AsString := '';
  393. AValue.Owner.ValueByName(sFField).AsString := '';
  394. {AID := ARecord.ValueByName('ID').AsInteger;
  395. fTotalPrice := StrToFloatDef(Text, 0) - AValue.AsFloat;
  396. if CheckStartedPrice(AID) then
  397. Text := FloatToStr(AValue.AsFloat + GetAllowTotalPrice(AID, fTotalPrice))
  398. else
  399. Text := '';}
  400. end
  401. else
  402. begin
  403. Rec.ValueByName('Formula').AsString := Text;
  404. AValue.Owner.ValueByName(sFField).AsString := Text;
  405. AID := ARecord.ValueByName('ID').AsInteger;
  406. if ARecord.ValueByName('Pre' + AValue.FieldName).AsFloat = 0 then
  407. fTotalPrice := FPayFormula.Calculate(Text, Rec.ValueByName('StartedPrice').AsFloat) - AValue.AsFloat
  408. else
  409. fTotalPrice := FPayFormula.Calculate(Text) - AValue.AsFloat;
  410. if CheckStartedPrice(AID) then
  411. Text := FloatToStr(AValue.AsFloat + GetAllowTotalPrice(AID, fTotalPrice))
  412. else
  413. Text := '';
  414. end;
  415. end;
  416. procedure DoLedgerFormulaChanged;
  417. begin
  418. if CheckStringNull(Text) or CheckNumeric(Text) then
  419. ARecord.ValueByName('Formula').AsString := ''
  420. else
  421. ARecord.ValueByName('Formula').AsString := Text;
  422. Text := '';
  423. end;
  424. begin
  425. if not Assigned(AValue) then Exit;
  426. Text := Trim(Text);
  427. if SameText('Name', AValue.FieldName) and (Text = '') then
  428. begin
  429. ErrorMessage('合同支付项名称不允许为空,如需删除,请点击右键进行删除');
  430. Allow := False;
  431. Exit;
  432. end;
  433. CheckLockedData;
  434. if not Allow then Exit;
  435. if SameText('StartedPrice', AValue.FieldName) then
  436. DoStartedPriceChanged;
  437. if SameText('RangePrice', AValue.FieldName) then
  438. DoRangePriceChanged;
  439. if Pos('TotalPrice', AValue.FieldName) = 1 then
  440. if TProjectData(FProjectData).ProjProperties.PhaseCount > 0 then
  441. DoCurTotalPriceChanged
  442. else
  443. DoLedgerFormulaChanged;
  444. end;
  445. procedure TDealPaymentData.UpdateTotalPrice(AID: Integer;
  446. ATotalPrice: Double);
  447. var
  448. Rec: TsdDataRecord;
  449. begin
  450. Rec := sddDealPayment.FindKey('idxID', AID);
  451. Rec.ValueByName('TotalPrice').AsFloat := Rec.ValueByName('TotalPrice').AsFloat + ATotalPrice;
  452. end;
  453. procedure TDealPaymentData.sddDealPaymentBeforeAddRecord(
  454. ARecord: TsdDataRecord; var Allow: Boolean);
  455. begin
  456. { if ARecord.ValueByName('Name').AsString = '' then
  457. Allow := False;}
  458. end;
  459. procedure TDealPaymentData.CalcStarted_RangePrice;
  460. var
  461. I: Integer;
  462. Rec: TsdDataRecord;
  463. begin
  464. if TProjectData(FProjectData).ProjProperties.PhaseCount > 0 then Exit;
  465. for I := 0 to sddDealPayment.RecordCount - 1 do
  466. begin
  467. Rec := sddDealPayment.Records[I];
  468. if Rec.ValueByName('CalcType').AsInteger <> 0 then Continue;
  469. Rec.ValueByName('StartedPrice').AsFloat := FPayFormula.Calculate(Rec.ValueByName('SFormula').AsString);
  470. Rec.ValueByName('RangePrice').AsFloat := FPayFormula.Calculate(Rec.ValueByName('RFormula').AsString);
  471. end;
  472. end;
  473. procedure TDealPaymentData.sddDealPaymentAfterValueChanged(
  474. AValue: TsdValue);
  475. begin
  476. if SameText('IsMinus', AValue.FieldName) then
  477. TProjectData(FProjectData).PhaseData.PhasePayData.CalculateCurPay;
  478. end;
  479. function TDealPaymentData.GetAddTotalPrice: Double;
  480. var
  481. iIndex: Integer;
  482. Rec: TsdDataRecord;
  483. begin
  484. Result := 0;
  485. for iIndex := 0 to sddDealPayment.RecordCount - 1 do
  486. begin
  487. Rec := sddDealPayment.Records[iIndex];
  488. if (Rec.ValueByName('CalcType').AsInteger = 0) and not Rec.ValueByName('IsMinus').AsBoolean then
  489. Result := Result + Rec.ValueByName('TotalPrice').AsFloat;
  490. end;
  491. end;
  492. function TDealPaymentData.GetCutTotalPrice: Double;
  493. var
  494. iIndex: Integer;
  495. Rec: TsdDataRecord;
  496. begin
  497. Result := 0;
  498. for iIndex := 0 to sddDealPayment.RecordCount - 1 do
  499. begin
  500. Rec := sddDealPayment.Records[iIndex];
  501. if (Rec.ValueByName('CalcType').AsInteger = 0) and Rec.ValueByName('IsMinus').AsBoolean then
  502. Result := Result + Rec.ValueByName('TotalPrice').AsFloat;
  503. end;
  504. end;
  505. function TDealPaymentData.GetPaidTotalPrice: Double;
  506. var
  507. Rec: TsdDataRecord;
  508. begin
  509. Rec := sddDealPayment.Locate('CalcType', 2);
  510. if Assigned(Rec) then
  511. Result := Rec.ValueByName('TotalPrice').AsFloat
  512. else
  513. Result := 0;
  514. end;
  515. procedure TDealPaymentData.sddDealPaymentBeforeDeleteRecord(
  516. ARecord: TsdDataRecord; var Allow: Boolean);
  517. begin
  518. if ARecord.ValueByName('Locked').AsBoolean then
  519. DataSetErrorMessage(Allow, '该支付(扣款)项已锁定,不可删除!')
  520. else if ARecord.ValueByName('PreDefined').AsBoolean then
  521. DataSetErrorMessage(Allow, '此项为预定义项,不允许删除!')
  522. else if ARecord.ValueByName('TotalPrice').AsFloat <> 0 then
  523. DataSetErrorMessage(Allow, '该支付(扣款)项存在数据,如需删除请先清除本期金额!');
  524. if Allow and TProjectData(FProjectData).PhaseData.Active then
  525. TProjectData(FProjectData).PhaseData.PhasePayData.Delete(ARecord.ValueByName('ID').AsInteger);
  526. end;
  527. procedure TDealPaymentData.Close;
  528. begin
  529. sddDealPayment.Close;
  530. end;
  531. procedure TDealPaymentData.RepairSerialNo;
  532. var
  533. Rec: TsdDataRecord;
  534. iRec: Integer;
  535. begin
  536. if sddDealPayment.RecordCount = 0 then Exit;
  537. Rec := sddDealPayment.Records[0];
  538. if Rec.ValueByName('SerialNo').AsString = '' then
  539. begin
  540. for iRec := 0 to sddDealPayment.RecordCount - 1 do
  541. begin
  542. Rec := sddDealPayment.Records[iRec];
  543. Rec.ValueByName('SerialNo').AsInteger := iRec;
  544. end;
  545. end;
  546. end;
  547. function TDealPaymentData.GetNewSerialNo: Integer;
  548. var
  549. idx: TsdIndex;
  550. begin
  551. idx := sddDealPayment.FindIndex('idxView');
  552. if idx.RecordCount > 0 then
  553. Result := idx.Records[idx.RecordCount - 1].ValueByName('SerialNo').AsInteger + 1
  554. else
  555. Result := 1;
  556. end;
  557. procedure TDealPaymentData.RepairLockedFormula;
  558. function CheckHasRepair: Boolean;
  559. var
  560. iRec: Integer;
  561. Rec: TsdDataRecord;
  562. begin
  563. Result := True;
  564. for iRec := 0 to sddDealPayment.RecordCount - 1 do
  565. begin
  566. Rec := sddDealPayment.Records[iRec];
  567. if Rec.ValueByName('Locked').AsBoolean and
  568. (Rec.ValueByName('Formula').AsString <> '') and (Rec.ValueByName('LockedFormula').AsBoolean) then
  569. begin
  570. Result := False;
  571. Break;
  572. end;
  573. end;
  574. end;
  575. var
  576. iRec: Integer;
  577. Rec: TsdDataRecord;
  578. begin
  579. if sddDealPayment.RecordCount = 0 then Exit;
  580. if CheckHasRepair then
  581. begin
  582. for iRec := 0 to sddDealPayment.RecordCount - 1 do
  583. begin
  584. Rec := sddDealPayment.Records[iRec];
  585. if Rec.ValueByName('Locked').AsBoolean and (Rec.ValueByName('Formula').AsString <> '') then
  586. Rec.ValueByName('LockedFormula').AsBoolean := True;
  587. end;
  588. end;
  589. end;
  590. function TDealPaymentData.GetStartedPrice(AID: Integer): Double;
  591. var
  592. Rec: TsdDataRecord;
  593. begin
  594. Rec := sddDealPayment.FindKey('idxID', AID);
  595. Result := Rec.ValueByName('StartedPrice').AsFloat;
  596. end;
  597. function TDealPaymentData.CheckReachPlan(ARec: TsdDataRecord): Boolean;
  598. var
  599. fCurValue, fDeadlineValue: Double;
  600. begin
  601. Result := False;
  602. if ARec.ValueByName('PlanType').AsInteger <> 0 then
  603. begin
  604. if ARec.ValueByName('PlanType').AsInteger = 1 then
  605. fCurValue := TProjectData(FProjectData).ProjProperties.PhaseCount
  606. else if ARec.ValueByName('PlanSubType').AsInteger = 0 then
  607. fCurValue := TProjectData(FProjectData).BillsData.Settlement[4]
  608. else if ARec.ValueByName('PlanSubType').AsInteger = 1 then
  609. fCurValue := TProjectData(FProjectData).BillsData.Settlement[1]
  610. else if ARec.ValueByName('PlanSubType').AsInteger = 2 then
  611. fCurValue := TProjectData(FProjectData).BillsData.Settlement[2];
  612. fDeadlineValue := ARec.ValueByName('PlanDeadline').AsFloat;
  613. Result := fCurValue >= fDeadlineValue;
  614. end;
  615. end;
  616. function TDealPaymentData.PlanStr(ARec: TsdDataRecord): string;
  617. begin
  618. if ARec.ValueByName('PlanType').AsInteger = 0 then
  619. Result := '无'
  620. else if ARec.ValueByName('PlanType').AsInteger = 1 then
  621. Result := Format('计量期数 >= %d', [ARec.ValueByName('PlanDeadline').AsInteger])
  622. else if ARec.ValueByName('PlanSubType').AsInteger = 0 then
  623. Result := Format('累计完成计量金额 >= %f', [ARec.ValueByName('PlanDeadline').AsFloat])
  624. else if ARec.ValueByName('PlanSubType').AsInteger = 1 then
  625. Result := Format('累计合同计量金额 >= %f', [ARec.ValueByName('PlanDeadline').AsFloat])
  626. else if ARec.ValueByName('PlanSubType').AsInteger = 2 then
  627. Result := Format('累计变更计量金额 >= %f', [ARec.ValueByName('PlanDeadline').AsFloat]);
  628. end;
  629. procedure TDealPaymentData.UpdateLinkSerialNo;
  630. var
  631. iPay, iCut, iIndex: Integer;
  632. Rec: TsdDataRecord;
  633. begin
  634. iPay := 1;
  635. iCut := 1;
  636. for iIndex := 0 to sdvDealPayment.RecordCount - 1 do
  637. begin
  638. Rec := sdvDealPayment.Records[iIndex];
  639. if Rec.ValueByName('CalcType').AsInteger = 0 then
  640. begin
  641. if Rec.ValueByName('IsMinus').AsBoolean then
  642. begin
  643. Rec.ValueByName('LinkSerialNo').AsInteger := iCut;
  644. Inc(iCut);
  645. end
  646. else
  647. begin
  648. Rec.ValueByName('LinkSerialNo').AsInteger := iPay;
  649. Inc(iPay);
  650. end;
  651. end;
  652. end;
  653. end;
  654. function TDealPaymentData.DealPayRecord(
  655. const AName: string): TsdDataRecord;
  656. var
  657. iRec: Integer;
  658. Rec: TsdDataRecord;
  659. begin
  660. Result := nil;
  661. for iRec := 0 to sddDealPayment.RecordCount - 1 do
  662. begin
  663. Rec := sddDealPayment.Records[iRec];
  664. if SameText(AName, Rec.ValueByName('Name').AsString) then
  665. begin
  666. Result := Rec;
  667. Break;
  668. end;
  669. end;
  670. end;
  671. procedure TDealPaymentData.sdvDealPaymentBeforeAddRecord(
  672. ARecord: TsdDataRecord; var Allow: Boolean);
  673. begin
  674. Allow := ARecord.ValueByName('Name').AsString <> '';
  675. end;
  676. end.