DealPaymentDm.pas 19 KB

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