BGLDm.pas 18 KB

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