BGLDm.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. ShowErrorMessage(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. cdsBGBillsViewB_Code.Tag := 0;
  371. ShowErrorMessage('不允许存在同编号变更清单!');
  372. Abort;
  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. ShowErrorMessage('当前变更令已锁定,不可修改');
  386. Abort;
  387. end;
  388. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  389. begin
  390. ShowErrorMessage('当前变更令下变更清单已被应用到清单,不可修改');
  391. Abort;
  392. end;
  393. end;
  394. sNewCode := cdsBGLViewCode.AsString;
  395. if Pos(';', sNewCode) > 0 then
  396. begin
  397. ShowErrorMessage('变更令号不可输入'';'',请使用其他符号代替!');
  398. Abort;
  399. end;
  400. // 相同变更令号应递增[1],[2]...
  401. iIncrement := 1;
  402. while LocateBGL(sNewCode) and (cdsBGLID.AsInteger <> cdsBGLViewID.AsInteger) do
  403. begin
  404. sNewCode := Format('%s[%d]', [cdsBGLViewCode.AsString, iIncrement]);
  405. Inc(iIncrement);
  406. end;
  407. cdsBGLViewCode.AsString := sNewCode;
  408. end;
  409. procedure TBGLData.DeleteBGBills(ABGID: Integer);
  410. begin
  411. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  412. cdsBGBills.Filtered := True;
  413. try
  414. cdsBGBills.First;
  415. while not cdsBGBills.Eof do
  416. cdsBGBills.Delete;
  417. finally
  418. cdsBGBills.Filtered := False;
  419. end;
  420. end;
  421. procedure TBGLData.cdsBGLViewBeforeDelete(DataSet: TDataSet);
  422. begin
  423. if cdsBGLViewLocked.AsBoolean then
  424. raise Exception.Create('变更令已被锁定,不可删除!');
  425. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  426. raise Exception.Create('变更令下变更清单已被应用到清单,不可删除!');
  427. DeleteBGBills(cdsBGLViewID.AsInteger);
  428. end;
  429. function TBGLData.CheckSameB_Code(ABGID: Integer;
  430. const AB_Code: string): Boolean;
  431. begin
  432. Result := False;
  433. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  434. cdsBGBills.Filtered := True;
  435. try
  436. cdsBGBills.First;
  437. while (not cdsBGBills.Eof) and (not Result) do
  438. begin
  439. Result := Result or SameText(cdsBGBillsB_Code.AsString, AB_Code);
  440. cdsBGBills.Next;
  441. end;
  442. finally
  443. cdsBGBills.Filtered := False;
  444. end;
  445. end;
  446. procedure TBGLData.cdsBGBillsViewBeforeDelete(DataSet: TDataSet);
  447. begin
  448. if cdsBGBillsViewLocked.AsBoolean then
  449. raise Exception.Create('变更清单已被锁定,不可删除!');
  450. if cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  451. raise Exception.Create('变更清单已被应用至清单,不可删除!');
  452. end;
  453. function TBGLData.CheckBGLUsed(ABGID: Integer): Boolean;
  454. begin
  455. Result := False;
  456. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  457. cdsBGBills.Filtered := True;
  458. try
  459. cdsBGBills.First;
  460. while (not cdsBGBills.Eof) and (not Result) do
  461. begin
  462. Result := Result or (cdsBGBillsUsedQuantity.AsFloat <> 0);
  463. cdsBGBills.Next;
  464. end;
  465. finally
  466. cdsBGBills.Filtered := False;
  467. end;
  468. end;
  469. procedure TBGLData.LockAll;
  470. begin
  471. LockAllBGL;
  472. LockAllBGBills;
  473. end;
  474. procedure TBGLData.LockAllBGBills;
  475. begin
  476. cdsBGBills.First;
  477. while not cdsBGBills.Eof do
  478. begin
  479. cdsBGBills.Edit;
  480. cdsBGBillsLocked.AsBoolean := True;
  481. cdsBGBills.Post;
  482. cdsBGBills.Next;
  483. end;
  484. end;
  485. procedure TBGLData.LockAllBGL;
  486. begin
  487. cdsBGL.First;
  488. while not cdsBGL.Eof do
  489. begin
  490. cdsBGL.Edit;
  491. cdsBGLLocked.AsBoolean := True;
  492. cdsBGL.Post;
  493. cdsBGL.Next;
  494. end;
  495. end;
  496. procedure TBGLData.cdsBGBillsViewAfterDelete(DataSet: TDataSet);
  497. begin
  498. GatherBGLTotalPrice(cdsBGLViewID.AsInteger);
  499. UpdateBGLExecutionRate(cdsBGLViewID.AsInteger);
  500. end;
  501. procedure TBGLData.BatchWritePos_Reason;
  502. begin
  503. cdsBGL.First;
  504. while not cdsBGL.Eof do
  505. begin
  506. cdsBGL.Edit;
  507. cdsBGLPos_Reason.AsString := cdsBGLName.AsString;
  508. cdsBGL.Post;
  509. cdsBGL.Next;
  510. end;
  511. end;
  512. procedure TBGLData.cdsBGLViewNewRecord(DataSet: TDataSet);
  513. begin
  514. cdsBGLViewID.AsInteger := GetNewIDOfIndex(cdsBGL);
  515. cdsBGLViewCreatePhaseID.AsInteger := TProjectData(FProjectData).PhaseIndex;
  516. end;
  517. procedure TBGLData.UpdateBGLExecutionRate(ABGLID: Integer);
  518. function GetBGLExecutionTotalPrice: Double;
  519. var
  520. cdsTemp: TClientDataSet;
  521. begin
  522. Result := 0;
  523. cdsTemp := TClientDataSet.Create(nil);
  524. try
  525. cdsTemp.CloneCursor(cdsBGBills, True);
  526. cdsTemp.Filter := Format('BGID = %d', [ABGLID]);
  527. cdsTemp.Filtered := True;
  528. cdsTemp.First;
  529. while not cdsTemp.Eof do
  530. begin
  531. Result := Result + TotalPriceRoundTo(
  532. cdsTemp.FieldByName('UsedQuantity').AsFloat * cdsTemp.FieldByName('Price').AsFloat);
  533. cdsTemp.Next;
  534. end;
  535. finally
  536. cdsTemp.Free;
  537. end;
  538. end;
  539. begin
  540. if cdsBGL.FindKey([ABGLID]) then
  541. begin
  542. cdsBGL.Edit;
  543. if cdsBGLTotalPrice.AsFloat <> 0 then
  544. cdsBGLExecutionRate.AsFloat := AdvRoundTo(GetBGLExecutionTotalPrice/cdsBGLTotalPrice.AsFloat*100)
  545. else
  546. cdsBGLExecutionRate.AsFloat := 0;
  547. cdsBGL.Post;
  548. end;
  549. end;
  550. function TBGLData.AllBGLTotalPrice: Double;
  551. begin
  552. Result := 0;
  553. cdsBGL.First;
  554. while not cdsBGL.Eof do
  555. begin
  556. Result := Result + cdsBGLTotalPrice.AsFloat;
  557. cdsBGL.Next;
  558. end;
  559. end;
  560. procedure TBGLData.cdsBGBillsViewQuantitySetText(Sender: TField;
  561. const Text: String);
  562. begin
  563. Sender.AsFloat := QuantityRoundTo(StrToFloatDef(Text, 0));
  564. end;
  565. procedure TBGLData.cdsBGBillsViewPriceSetText(Sender: TField;
  566. const Text: String);
  567. begin
  568. Sender.AsFloat := PriceRoundTo(StrToFloatDef(Text, 0));
  569. end;
  570. procedure TBGLData.cdsBGLViewCodeChange(Sender: TField);
  571. begin
  572. Sender.Tag := 1;
  573. end;
  574. procedure TBGLData.Close;
  575. begin
  576. cdsBGL.IndexName := '';
  577. cdsBGL.Close;
  578. cdsBGBills.Close;
  579. end;
  580. function TBGLData.LocateBGL(const ACode: string): Boolean;
  581. begin
  582. cdsBGL.IndexName := 'idxCode';
  583. try
  584. Result := cdsBGL.FindKey([ACode]);
  585. finally
  586. cdsBGL.IndexName := 'idxID';
  587. end;
  588. end;
  589. function TBGLData.LocateBGBills(ABGID: Integer;
  590. const AB_Code: string): Boolean;
  591. begin
  592. Result := False;
  593. cdsBGBills.First;
  594. while (not cdsBGBills.Eof) do
  595. begin
  596. if (cdsBGBillsBGID.AsInteger = ABGID) and
  597. SameText(cdsBGBillsB_Code.AsString, AB_Code) then
  598. begin
  599. Result := True;
  600. Break;
  601. end;
  602. cdsBGBills.Next;
  603. end;
  604. end;
  605. end.