BGLDm.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. unit BGLDm;
  2. interface
  3. uses
  4. SysUtils, Classes, DB, DBClient, Provider, ADODB, sdIDTree,
  5. sdDB;
  6. type
  7. TBGLSelectInfo = class(TObject)
  8. private
  9. FB_Code: string;
  10. FName: string;
  11. FUnits: string;
  12. FPrice: Double;
  13. FIsOrg: Boolean;
  14. FTotalNum: Double;
  15. FNums: TStrings;
  16. FCodes: TStrings;
  17. procedure SetMergedCode(const Value: string);
  18. procedure SetMergedNum(const Value: string);
  19. function GetCount: Integer;
  20. function GetMergedCode: string;
  21. function GetMergedNum: string;
  22. public
  23. constructor Create(ARec: TsdDataRecord; ATotalNum: Double; AIsOrg: Boolean);
  24. destructor Destroy; override;
  25. procedure Clear;
  26. property MergedCode: string read GetMergedCode write SetMergedCode;
  27. property MergedNum: string read GetMergedNum write SetMergedNum;
  28. property Codes: TStrings read FCodes;
  29. property Nums: TStrings read FNums;
  30. property Count: Integer read GetCount;
  31. property TotalNum: Double read FTotalNum write FTotalNum;
  32. property IsOrg: Boolean read FIsOrg;
  33. property B_Code: string read FB_Code;
  34. property Name: string read FName;
  35. property Units: string read FUnits;
  36. property Price: Double read FPrice;
  37. end;
  38. TBGLData = class(TDataModule)
  39. atBGL: TADOTable;
  40. dspBGL: TDataSetProvider;
  41. cdsBGL: TClientDataSet;
  42. cdsBGLID: TIntegerField;
  43. cdsBGLTotalPrice: TFloatField;
  44. cdsBGLName: TWideStringField;
  45. cdsBGLCode: TWideStringField;
  46. cdsBGLPos_Reason: TWideStringField;
  47. cdsBGLDirection: TWideStringField;
  48. cdsBGLDrawingCode: TWideStringField;
  49. cdsBGLLocked: TBooleanField;
  50. cdsBGLApprovalCode: TWideStringField;
  51. cdsBGLCreatePhaseID: TIntegerField;
  52. cdsBGLView: TClientDataSet;
  53. cdsBGLViewID: TIntegerField;
  54. cdsBGLViewName: TWideStringField;
  55. cdsBGLViewTotalPrice: TFloatField;
  56. cdsBGLViewCode: TWideStringField;
  57. cdsBGLViewPos_Reason: TWideStringField;
  58. cdsBGLViewDirection: TWideStringField;
  59. cdsBGLViewDrawingCode: TWideStringField;
  60. cdsBGLViewLocked: TBooleanField;
  61. cdsBGLViewApprovalCode: TWideStringField;
  62. cdsBGLViewCreatePhaseID: TIntegerField;
  63. dsBGL: TDataSource;
  64. atBGBills: TADOTable;
  65. dspBGBills: TDataSetProvider;
  66. cdsBGBills: TClientDataSet;
  67. cdsBGBillsID: TIntegerField;
  68. cdsBGBillsBGID: TIntegerField;
  69. cdsBGBillsB_Code: TWideStringField;
  70. cdsBGBillsName: TWideStringField;
  71. cdsBGBillsUnits: TWideStringField;
  72. cdsBGBillsPrice: TFloatField;
  73. cdsBGBillsQuantity: TFloatField;
  74. cdsBGBillsTotalPrice: TFloatField;
  75. cdsBGBillsUsedQuantity: TFloatField;
  76. cdsBGBillsLocked: TBooleanField;
  77. cdsBGBillsView: TClientDataSet;
  78. cdsBGBillsViewID: TIntegerField;
  79. cdsBGBillsViewBGID: TIntegerField;
  80. cdsBGBillsViewB_Code: TWideStringField;
  81. cdsBGBillsViewName: TWideStringField;
  82. cdsBGBillsViewUnits: TWideStringField;
  83. cdsBGBillsViewPrice: TFloatField;
  84. cdsBGBillsViewQuantity: TFloatField;
  85. cdsBGBillsViewTotalPrice: TFloatField;
  86. cdsBGBillsViewUsedQuantity: TFloatField;
  87. cdsBGBillsViewLocked: TBooleanField;
  88. cdsBGLExecutionRate: TFloatField;
  89. cdsBGLViewExecutionRate: TFloatField;
  90. procedure cdsBGBillsViewAfterInsert(DataSet: TDataSet);
  91. procedure cdsBGBillsViewAfterPost(DataSet: TDataSet);
  92. procedure cdsBGBillsViewQuantityChange(Sender: TField);
  93. procedure cdsBGBillsViewBeforePost(DataSet: TDataSet);
  94. procedure cdsBGLViewBeforePost(DataSet: TDataSet);
  95. procedure cdsBGLViewBeforeDelete(DataSet: TDataSet);
  96. procedure cdsBGBillsViewBeforeDelete(DataSet: TDataSet);
  97. procedure cdsBGBillsViewAfterDelete(DataSet: TDataSet);
  98. procedure cdsBGLViewNewRecord(DataSet: TDataSet);
  99. procedure cdsBGBillsViewQuantitySetText(Sender: TField;
  100. const Text: String);
  101. procedure cdsBGBillsViewPriceSetText(Sender: TField;
  102. const Text: String);
  103. procedure cdsBGLViewCodeChange(Sender: TField);
  104. private
  105. FProjectData: TObject;
  106. procedure GatherBGLTotalPrice(ABGLID: Integer);
  107. procedure UpdateBGLTotalPrice(ABGLID: Integer; ADiffer: Double);
  108. procedure UpdateBGLExecutionRate(ABGLID: Integer);
  109. procedure ApplyBGL(ABGLInfo: TBGLSelectInfo); overload;
  110. procedure DeleteBGBills(ABGID: Integer);
  111. function CheckSameB_Code(ABGID: Integer; const AB_Code: string): Boolean;
  112. function CheckBGLUsed(ABGID: Integer): Boolean;
  113. function LocateBGL(const ACode: string): Boolean;
  114. procedure LockAllBGL;
  115. procedure LockAllBGBills;
  116. public
  117. constructor Create(AProjectData: TObject);
  118. destructor Destroy; override;
  119. procedure Open(AConnection: TADOConnection);
  120. procedure Close;
  121. procedure Save;
  122. function AllBGLTotalPrice: Double;
  123. procedure AddBGL(const sCode: string);
  124. procedure ApplyBGL(AOrgBGL, ANewBGL: TBGLSelectInfo); overload;
  125. procedure BatchWritePos_Reason;
  126. procedure LockAll;
  127. property ProjectData: TObject read FProjectData;
  128. end;
  129. implementation
  130. uses
  131. ZhAPI, Math, ProjectData, BillsDm, Variants, UtilMethods;
  132. {$R *.dfm}
  133. { TBGLSelectInfo }
  134. procedure TBGLSelectInfo.Clear;
  135. begin
  136. FCodes.Clear;
  137. FNums.Clear;
  138. end;
  139. constructor TBGLSelectInfo.Create(ARec: TsdDataRecord;
  140. ATotalNum: Double; AIsOrg: Boolean);
  141. begin
  142. FB_Code := ARec.ValueByName('B_Code').AsString;
  143. FName := ARec.ValueByName('Name').AsString;
  144. FUnits := ARec.ValueByName('Units').AsString;
  145. FPrice := ARec.ValueByName('Price').AsFloat;
  146. FTotalNum := ATotalNum;
  147. FIsOrg := AIsOrg;
  148. FCodes := TStringList.Create;
  149. FCodes.Delimiter := ';';
  150. FNums := TStringList.Create;
  151. FNums.Delimiter := ';';
  152. end;
  153. destructor TBGLSelectInfo.Destroy;
  154. begin
  155. FNums.Free;
  156. FCodes.Free;
  157. inherited;
  158. end;
  159. function TBGLSelectInfo.GetCount: Integer;
  160. begin
  161. Result := Min(FCodes.Count, FNums.Count);
  162. end;
  163. function TBGLSelectInfo.GetMergedCode: string;
  164. begin
  165. Result := FCodes.DelimitedText;
  166. end;
  167. function TBGLSelectInfo.GetMergedNum: string;
  168. begin
  169. Result := FNums.DelimitedText;
  170. end;
  171. procedure TBGLSelectInfo.SetMergedCode(const Value: string);
  172. begin
  173. if Value <> '' then
  174. FCodes.DelimitedText := Value
  175. else
  176. FCodes.Clear;
  177. end;
  178. procedure TBGLSelectInfo.SetMergedNum(const Value: string);
  179. begin
  180. if Value <> '' then
  181. FNums.DelimitedText := Value
  182. else
  183. FNums.Clear;
  184. end;
  185. { TBGLData }
  186. constructor TBGLData.Create(AProjectData: TObject);
  187. begin
  188. inherited Create(nil);
  189. FProjectData := AProjectData;
  190. end;
  191. destructor TBGLData.Destroy;
  192. begin
  193. inherited;
  194. end;
  195. procedure TBGLData.Open(AConnection: TADOConnection);
  196. begin
  197. atBGL.Connection := AConnection;
  198. cdsBGL.Open;
  199. cdsBGL.AddIndex('idxID', 'ID', []);
  200. cdsBGL.AddIndex('idxCode', 'Code', []);
  201. cdsBGL.IndexName := 'idxID';
  202. cdsBGLView.CloneCursor(cdsBGL, True);
  203. atBGBills.Connection := AConnection;
  204. cdsBGBills.Open;
  205. cdsBGBillsView.CloneCursor(cdsBGBills, True);
  206. cdsBGBills.IndexFieldNames := 'ID';
  207. cdsBGBillsView.MasterSource := dsBGL;
  208. cdsBGBillsView.MasterFields := 'ID';
  209. cdsBGBillsView.IndexFieldNames := 'BGID;ID';
  210. end;
  211. procedure TBGLData.Save;
  212. begin
  213. cdsBGL.ApplyUpdates(0);
  214. cdsBGBills.ApplyUpdates(0);
  215. end;
  216. procedure TBGLData.cdsBGBillsViewAfterInsert(DataSet: TDataSet);
  217. begin
  218. cdsBGBillsViewID.AsInteger := GetNewIDOfIndex(cdsBGBills);
  219. cdsBGBillsViewBGID.AsInteger := cdsBGLViewID.AsInteger;
  220. end;
  221. procedure TBGLData.AddBGL(const sCode: string);
  222. begin
  223. cdsBGLView.DisableControls;
  224. cdsBGLView.Append;
  225. cdsBGLViewCode.AsString := sCode;
  226. cdsBGLView.Post;
  227. cdsBGLView.EnableControls;
  228. end;
  229. procedure TBGLData.cdsBGBillsViewAfterPost(DataSet: TDataSet);
  230. procedure DoB_CodeChange;
  231. var
  232. Rec: TsdDataRecord;
  233. begin
  234. cdsBGBillsViewB_Code.Tag := 0;
  235. cdsBGBillsView.Edit;
  236. with TProjectData(FProjectData).BillsData do
  237. begin
  238. Rec := sddBills.Locate('B_Code', cdsBGBillsViewB_Code.AsString);
  239. if Rec <> nil then
  240. begin
  241. cdsBGBillsViewName.AsString := Rec.ValueByName('Name').AsString;
  242. cdsBGBillsViewUnits.AsString := Rec.ValueByName('Units').AsString;
  243. cdsBGBillsViewPrice.AsString := Rec.ValueByName('Price').AsString;
  244. end;
  245. end;
  246. cdsBGBillsView.Post;
  247. end;
  248. procedure ClearChangeTag;
  249. begin
  250. cdsBGBillsViewB_Code.Tag := 0;
  251. cdsBGBillsViewName.Tag := 0;
  252. cdsBGBillsViewUnits.Tag := 0;
  253. cdsBGBillsViewPrice.Tag := 0;
  254. cdsBGBillsViewQuantity.Tag := 0;
  255. end;
  256. var
  257. fTotalPrice, Differ: Double;
  258. begin
  259. if (cdsBGBillsViewB_Code.Tag = 1) then
  260. DoB_CodeChange;
  261. if (cdsBGBillsViewPrice.Tag = 1) or
  262. (cdsBGBillsViewQuantity.Tag = 1) then
  263. begin
  264. cdsBGBillsViewPrice.Tag := 0;
  265. cdsBGBillsViewQuantity.Tag := 0;
  266. fTotalPrice := TotalPriceRoundTo(cdsBGBillsViewPrice.AsFloat * cdsBGBillsViewQuantity.AsFloat);
  267. UpdateBGLTotalPrice(cdsBGBillsViewBGID.AsInteger, fTotalPrice - cdsBGBillsViewTotalPrice.AsFloat);
  268. UpdateBGLExecutionRate(cdsBGBillsViewBGID.AsInteger);
  269. cdsBGBillsView.Edit;
  270. cdsBGBillsViewTotalPrice.AsFloat := fTotalPrice;
  271. cdsBGBillsView.Post;
  272. end;
  273. ClearChangeTag;
  274. end;
  275. procedure TBGLData.cdsBGBillsViewQuantityChange(Sender: TField);
  276. begin
  277. Sender.Tag := 1;
  278. end;
  279. procedure TBGLData.GatherBGLTotalPrice(ABGLID: Integer);
  280. var
  281. fGather: Double;
  282. begin
  283. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGLID);
  284. cdsBGBills.Filtered := True;
  285. try
  286. fGather := 0;
  287. cdsBGBills.First;
  288. while not cdsBGBills.Eof do
  289. begin
  290. fGather := fGather + cdsBGBillsTotalPrice.AsFloat;
  291. cdsBGBills.Next;
  292. end;
  293. if cdsBGL.FindKey([ABGLID]) then
  294. begin
  295. cdsBGL.Edit;
  296. cdsBGLTotalPrice.AsFloat := fGather;
  297. cdsBGL.Post;
  298. end;
  299. finally
  300. cdsBGBills.Filtered := False;
  301. end;
  302. end;
  303. procedure TBGLData.UpdateBGLTotalPrice(ABGLID: Integer; ADiffer: Double);
  304. begin
  305. if cdsBGL.FindKey([ABGLID]) then
  306. begin
  307. cdsBGL.Edit;
  308. cdsBGLTotalPrice.AsFloat := cdsBGLTotalPrice.AsFloat + ADiffer;
  309. cdsBGL.Post;
  310. end;
  311. end;
  312. procedure TBGLData.ApplyBGL(AOrgBGL, ANewBGL: TBGLSelectInfo);
  313. begin
  314. ApplyBGL(AOrgBGL);
  315. ApplyBGL(ANewBGL);
  316. end;
  317. procedure TBGLData.ApplyBGL(ABGLInfo: TBGLSelectInfo);
  318. var
  319. I: Integer;
  320. begin
  321. for I := 0 to ABGLInfo.Count - 1 do
  322. if LocateBGL(ABGLInfo.Codes[I]) and
  323. cdsBGBills.Locate('BGID;B_Code', VarArrayOf([cdsBGLID.AsInteger, ABGLInfo.B_Code]), []) then
  324. begin
  325. cdsBGBills.Edit;
  326. if ABGLInfo.IsOrg then
  327. cdsBGBillsUsedQuantity.AsFloat := cdsBGBillsUsedQuantity.AsFloat
  328. - QuantityRoundTo(StrToFloatDef(ABGLInfo.Nums[I], 0))
  329. else
  330. cdsBGBillsUsedQuantity.AsFloat := cdsBGBillsUsedQuantity.AsFloat
  331. + QuantityRoundTo(StrToFloatDef(ABGLInfo.Nums[I], 0));
  332. cdsBGBills.Post;
  333. UpdateBGLExecutionRate(cdsBGBillsBGID.AsInteger);
  334. end;
  335. end;
  336. procedure TBGLData.cdsBGBillsViewBeforePost(DataSet: TDataSet);
  337. procedure DisplayErrorMessage(const AHint: string);
  338. begin
  339. cdsBGBillsViewB_Code.Tag := 0;
  340. cdsBGBillsViewName.Tag := 0;
  341. cdsBGBillsViewUnits.Tag := 0;
  342. cdsBGBillsViewPrice.Tag := 0;
  343. cdsBGBillsViewQuantity.Tag := 0;
  344. ShowErrorMessage(AHint);
  345. Abort;
  346. end;
  347. begin
  348. if (cdsBGBillsViewB_Code.Tag = 1) or
  349. (cdsBGBillsViewName.Tag = 1) or
  350. (cdsBGBillsViewUnits.Tag = 1) or
  351. (cdsBGBillsViewPrice.Tag = 1) then
  352. begin
  353. if cdsBGBillsViewLocked.AsBoolean then
  354. DisplayErrorMessage('变更清单已被锁定,不可修改!');
  355. if cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  356. DisplayErrorMessage('变更令已被应用至清单,不可修改!');
  357. end;
  358. if (cdsBGBillsViewQuantity.Tag = 1) then
  359. begin
  360. if cdsBGBillsViewLocked.AsBoolean then
  361. DisplayErrorMessage('变更清单已被锁定,不可修改!');
  362. if (cdsBGBillsViewUsedQuantity.AsFloat <> 0) and
  363. (cdsBGBillsViewQuantity.AsFloat < cdsBGBillsViewUsedQuantity.AsFloat) then
  364. DisplayErrorMessage('变更清单的清单数量应大于已变更数量!');
  365. end;
  366. if cdsBGBillsViewB_Code.Tag = 1 then
  367. if CheckSameB_Code(cdsBGBillsViewBGID.AsInteger, cdsBGBillsViewB_Code.AsString) then
  368. begin
  369. cdsBGBillsViewB_Code.Tag := 0;
  370. ShowErrorMessage('不允许存在同编号变更清单!');
  371. Abort;
  372. end;
  373. end;
  374. procedure TBGLData.cdsBGLViewBeforePost(DataSet: TDataSet);
  375. var
  376. iIncrement: Integer;
  377. sNewCode: string;
  378. begin
  379. if cdsBGLViewCode.Tag = 1 then
  380. begin
  381. cdsBGLViewCode.Tag := 0;
  382. if cdsBGLViewLocked.AsBoolean then
  383. begin
  384. ShowErrorMessage('当前变更令已锁定,不可修改');
  385. Abort;
  386. end;
  387. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  388. begin
  389. ShowErrorMessage('当前变更令下变更清单已被应用到清单,不可修改');
  390. Abort;
  391. end;
  392. end;
  393. sNewCode := cdsBGLViewCode.AsString;
  394. if Pos(';', sNewCode) > 0 then
  395. begin
  396. ShowErrorMessage('变更令号不可输入'';'',请使用其他符号代替!');
  397. Abort;
  398. end;
  399. // 相同变更令号应递增[1],[2]...
  400. iIncrement := 1;
  401. while LocateBGL(sNewCode) and (cdsBGLID.AsInteger <> cdsBGLViewID.AsInteger) do
  402. begin
  403. sNewCode := Format('%s[%d]', [cdsBGLViewCode.AsString, iIncrement]);
  404. Inc(iIncrement);
  405. end;
  406. cdsBGLViewCode.AsString := sNewCode;
  407. end;
  408. procedure TBGLData.DeleteBGBills(ABGID: Integer);
  409. begin
  410. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  411. cdsBGBills.Filtered := True;
  412. try
  413. cdsBGBills.First;
  414. while not cdsBGBills.Eof do
  415. cdsBGBills.Delete;
  416. finally
  417. cdsBGBills.Filtered := False;
  418. end;
  419. end;
  420. procedure TBGLData.cdsBGLViewBeforeDelete(DataSet: TDataSet);
  421. begin
  422. if cdsBGLViewLocked.AsBoolean then
  423. raise Exception.Create('变更令已被锁定,不可删除!');
  424. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  425. raise Exception.Create('变更令下变更清单已被应用到清单,不可删除!');
  426. DeleteBGBills(cdsBGLViewID.AsInteger);
  427. end;
  428. function TBGLData.CheckSameB_Code(ABGID: Integer;
  429. const AB_Code: string): Boolean;
  430. begin
  431. Result := False;
  432. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  433. cdsBGBills.Filtered := True;
  434. try
  435. cdsBGBills.First;
  436. while (not cdsBGBills.Eof) and (not Result) do
  437. begin
  438. Result := Result or SameText(cdsBGBillsB_Code.AsString, AB_Code);
  439. cdsBGBills.Next;
  440. end;
  441. finally
  442. cdsBGBills.Filtered := False;
  443. end;
  444. end;
  445. procedure TBGLData.cdsBGBillsViewBeforeDelete(DataSet: TDataSet);
  446. begin
  447. if cdsBGBillsViewLocked.AsBoolean then
  448. raise Exception.Create('变更清单已被锁定,不可删除!');
  449. if cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  450. raise Exception.Create('变更清单已被应用至清单,不可删除!');
  451. end;
  452. function TBGLData.CheckBGLUsed(ABGID: Integer): Boolean;
  453. begin
  454. Result := False;
  455. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  456. cdsBGBills.Filtered := True;
  457. try
  458. cdsBGBills.First;
  459. while (not cdsBGBills.Eof) and (not Result) do
  460. begin
  461. Result := Result or (cdsBGBillsUsedQuantity.AsFloat <> 0);
  462. cdsBGBills.Next;
  463. end;
  464. finally
  465. cdsBGBills.Filtered := False;
  466. end;
  467. end;
  468. procedure TBGLData.LockAll;
  469. begin
  470. LockAllBGL;
  471. LockAllBGBills;
  472. end;
  473. procedure TBGLData.LockAllBGBills;
  474. begin
  475. cdsBGBills.First;
  476. while not cdsBGBills.Eof do
  477. begin
  478. cdsBGBills.Edit;
  479. cdsBGBillsLocked.AsBoolean := True;
  480. cdsBGBills.Post;
  481. cdsBGBills.Next;
  482. end;
  483. end;
  484. procedure TBGLData.LockAllBGL;
  485. begin
  486. cdsBGL.First;
  487. while not cdsBGL.Eof do
  488. begin
  489. cdsBGL.Edit;
  490. cdsBGLLocked.AsBoolean := True;
  491. cdsBGL.Post;
  492. cdsBGL.Next;
  493. end;
  494. end;
  495. procedure TBGLData.cdsBGBillsViewAfterDelete(DataSet: TDataSet);
  496. begin
  497. GatherBGLTotalPrice(cdsBGLViewID.AsInteger);
  498. UpdateBGLExecutionRate(cdsBGLViewID.AsInteger);
  499. end;
  500. procedure TBGLData.BatchWritePos_Reason;
  501. begin
  502. cdsBGL.First;
  503. while not cdsBGL.Eof do
  504. begin
  505. cdsBGL.Edit;
  506. cdsBGLPos_Reason.AsString := cdsBGLName.AsString;
  507. cdsBGL.Post;
  508. cdsBGL.Next;
  509. end;
  510. end;
  511. procedure TBGLData.cdsBGLViewNewRecord(DataSet: TDataSet);
  512. begin
  513. cdsBGLViewID.AsInteger := GetNewIDOfIndex(cdsBGL);
  514. cdsBGLViewCreatePhaseID.AsInteger := TProjectData(FProjectData).PhaseIndex;
  515. end;
  516. procedure TBGLData.UpdateBGLExecutionRate(ABGLID: Integer);
  517. function GetBGLExecutionTotalPrice: Double;
  518. var
  519. cdsTemp: TClientDataSet;
  520. begin
  521. Result := 0;
  522. cdsTemp := TClientDataSet.Create(nil);
  523. try
  524. cdsTemp.CloneCursor(cdsBGBills, True);
  525. cdsTemp.Filter := Format('BGID = %d', [ABGLID]);
  526. cdsTemp.Filtered := True;
  527. cdsTemp.First;
  528. while not cdsTemp.Eof do
  529. begin
  530. Result := Result + TotalPriceRoundTo(
  531. cdsTemp.FieldByName('UsedQuantity').AsFloat * cdsTemp.FieldByName('Price').AsFloat);
  532. cdsTemp.Next;
  533. end;
  534. finally
  535. cdsTemp.Free;
  536. end;
  537. end;
  538. begin
  539. if cdsBGL.FindKey([ABGLID]) then
  540. begin
  541. cdsBGL.Edit;
  542. cdsBGLExecutionRate.AsFloat := AdvRoundTo(GetBGLExecutionTotalPrice/cdsBGLTotalPrice.AsFloat*100);
  543. cdsBGL.Post;
  544. end;
  545. end;
  546. function TBGLData.AllBGLTotalPrice: Double;
  547. begin
  548. Result := 0;
  549. cdsBGL.First;
  550. while not cdsBGL.Eof do
  551. begin
  552. Result := Result + cdsBGLTotalPrice.AsFloat;
  553. cdsBGL.Next;
  554. end;
  555. end;
  556. procedure TBGLData.cdsBGBillsViewQuantitySetText(Sender: TField;
  557. const Text: String);
  558. begin
  559. Sender.AsFloat := QuantityRoundTo(StrToFloatDef(Text, 0));
  560. end;
  561. procedure TBGLData.cdsBGBillsViewPriceSetText(Sender: TField;
  562. const Text: String);
  563. begin
  564. Sender.AsFloat := PriceRoundTo(StrToFloatDef(Text, 0));
  565. end;
  566. procedure TBGLData.cdsBGLViewCodeChange(Sender: TField);
  567. begin
  568. Sender.Tag := 1;
  569. end;
  570. procedure TBGLData.Close;
  571. begin
  572. cdsBGL.IndexName := '';
  573. cdsBGL.Close;
  574. cdsBGBills.Close;
  575. end;
  576. function TBGLData.LocateBGL(const ACode: string): Boolean;
  577. begin
  578. cdsBGL.IndexName := 'idxCode';
  579. try
  580. Result := cdsBGL.FindKey([ACode]);
  581. finally
  582. cdsBGL.IndexName := 'idxID';
  583. end;
  584. end;
  585. end.