BGLDm.pas 18 KB

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