BGLDm.pas 18 KB

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