BGLDm.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. 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. cdsBGLViewApprovalCode: TWideStringField;
  62. cdsBGLViewCreatePhaseID: TIntegerField;
  63. cdsBGLViewExecutionRate: TFloatField;
  64. cdsBGLViewBGLType: TWideStringField;
  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. cdsBGBillsView: TClientDataSet;
  79. cdsBGBillsViewID: TIntegerField;
  80. cdsBGBillsViewBGID: TIntegerField;
  81. cdsBGBillsViewB_Code: TWideStringField;
  82. cdsBGBillsViewName: TWideStringField;
  83. cdsBGBillsViewUnits: TWideStringField;
  84. cdsBGBillsViewPrice: TFloatField;
  85. cdsBGBillsViewQuantity: TFloatField;
  86. cdsBGBillsViewTotalPrice: TFloatField;
  87. cdsBGBillsViewUsedQuantity: TFloatField;
  88. cdsBGLIsCloud: TBooleanField;
  89. cdsBGLWebID: TIntegerField;
  90. cdsBGLViewIsCloud: TBooleanField;
  91. cdsBGLViewWebID: TIntegerField;
  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. public
  118. constructor Create(AProjectData: TObject);
  119. destructor Destroy; override;
  120. procedure Open(AConnection: TADOConnection);
  121. procedure Close;
  122. procedure Save;
  123. function GetBGLCanEdit(ASerialNo: Integer): Boolean;
  124. function AllBGLTotalPrice: Double;
  125. function AllCloudBGLWebID: string;
  126. procedure LoadCloudBGL(const ABGLs: string);
  127. procedure AddBGL(const sCode: string);
  128. procedure ApplyBGL(AOrgBGL, ANewBGL: TBGLSelectInfo); overload;
  129. procedure BatchWritePos_Reason;
  130. property ProjectData: TObject read FProjectData;
  131. end;
  132. implementation
  133. uses
  134. ZhAPI, Math, ProjectData, BillsDm, Variants, UtilMethods, superobject;
  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. fNum: Double;
  324. begin
  325. for I := 0 to ABGLInfo.Count - 1 do
  326. if LocateBGL(ABGLInfo.Codes[I]) and
  327. LocateBGBills(cdsBGLID.AsInteger, ABGLInfo.B_Code) then
  328. begin
  329. cdsBGBills.Edit;
  330. fNum := QuantityRoundTo(StrToFloatDef(ABGLInfo.Nums[I], 0));
  331. if ABGLInfo.IsOrg then
  332. cdsBGBillsUsedQuantity.AsFloat := QuantityRoundTo(cdsBGBillsUsedQuantity.AsFloat - fNum)
  333. else
  334. cdsBGBillsUsedQuantity.AsFloat := QuantityRoundTo(cdsBGBillsUsedQuantity.AsFloat + fNum);
  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 cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  357. DisplayErrorMessage('变更令已被应用至清单,不可修改!');
  358. end;
  359. if (cdsBGBillsViewQuantity.Tag = 1) then
  360. begin
  361. if (cdsBGBillsViewUsedQuantity.AsFloat <> 0) and
  362. (cdsBGBillsViewQuantity.AsFloat < cdsBGBillsViewUsedQuantity.AsFloat) then
  363. DisplayErrorMessage('变更清单的清单数量应大于已变更数量!');
  364. end;
  365. if cdsBGBillsViewB_Code.Tag = 1 then
  366. if CheckSameB_Code(cdsBGBillsViewBGID.AsInteger, cdsBGBillsViewB_Code.AsString) then
  367. begin
  368. DisplayErrorMessage('不允许存在同编号变更清单!');
  369. end;
  370. end;
  371. procedure TBGLData.cdsBGLViewBeforePost(DataSet: TDataSet);
  372. var
  373. iIncrement: Integer;
  374. sNewCode: string;
  375. begin
  376. if cdsBGLViewIsCloud.AsBoolean then
  377. begin
  378. cdsBGLViewCode.Tag := 0;
  379. WarningMessage('当前变更令不允许编辑。');
  380. DataSet.Cancel;
  381. Abort;
  382. end;
  383. // 变更令号不可为空
  384. if cdsBGLViewCode.AsString = '' then
  385. begin
  386. cdsBGLViewCode.Tag := 0;
  387. if cdsBGL.FindKey([cdsBGLViewID.AsInteger]) then
  388. if cdsBGLCode.AsString <> '' then
  389. WarningMessage('变更令号不允许为空,如需删除,请点击右键进行删除。');
  390. DataSet.Cancel;
  391. Abort;
  392. end;
  393. if cdsBGLViewCode.Tag = 1 then
  394. begin
  395. cdsBGLViewCode.Tag := 0;
  396. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  397. begin
  398. ErrorMessage('当前变更令下变更清单已被应用到清单,不可修改!');
  399. Abort;
  400. end;
  401. sNewCode := cdsBGLViewCode.AsString;
  402. if Pos(';', sNewCode) > 0 then
  403. begin
  404. ErrorMessage('变更令号不可输入'';'',请使用其他符号代替!');
  405. Abort;
  406. end;
  407. // 相同变更令号应递增[1],[2]...
  408. iIncrement := 1;
  409. while LocateBGL(sNewCode) and (cdsBGLID.AsInteger <> cdsBGLViewID.AsInteger) do
  410. begin
  411. sNewCode := Format('%s[%d]', [cdsBGLViewCode.AsString, iIncrement]);
  412. Inc(iIncrement);
  413. end;
  414. if cdsBGLViewCode.AsString <> sNewCode then
  415. begin
  416. cdsBGLViewCode.AsString := sNewCode;
  417. cdsBGLViewCode.Tag := 0;
  418. end;
  419. end;
  420. end;
  421. procedure TBGLData.DeleteBGBills(ABGID: Integer);
  422. begin
  423. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  424. cdsBGBills.Filtered := True;
  425. try
  426. cdsBGBills.First;
  427. while not cdsBGBills.Eof do
  428. cdsBGBills.Delete;
  429. finally
  430. cdsBGBills.Filtered := False;
  431. end;
  432. end;
  433. procedure TBGLData.cdsBGLViewBeforeDelete(DataSet: TDataSet);
  434. begin
  435. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  436. raise Exception.Create('变更令下变更清单已被应用到清单,不可删除!');
  437. DeleteBGBills(cdsBGLViewID.AsInteger);
  438. end;
  439. function TBGLData.CheckSameB_Code(ABGID: Integer;
  440. const AB_Code: string): Boolean;
  441. begin
  442. Result := False;
  443. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  444. cdsBGBills.Filtered := True;
  445. try
  446. cdsBGBills.First;
  447. while (not cdsBGBills.Eof) and (not Result) do
  448. begin
  449. Result := Result or SameText(cdsBGBillsB_Code.AsString, AB_Code);
  450. cdsBGBills.Next;
  451. end;
  452. finally
  453. cdsBGBills.Filtered := False;
  454. end;
  455. end;
  456. procedure TBGLData.cdsBGBillsViewBeforeDelete(DataSet: TDataSet);
  457. begin
  458. if cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  459. raise Exception.Create('变更清单已被应用至清单,不可删除!');
  460. end;
  461. function TBGLData.CheckBGLUsed(ABGID: Integer): Boolean;
  462. begin
  463. Result := False;
  464. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  465. cdsBGBills.Filtered := True;
  466. try
  467. cdsBGBills.First;
  468. while (not cdsBGBills.Eof) and (not Result) do
  469. begin
  470. Result := Result or (cdsBGBillsUsedQuantity.AsFloat <> 0);
  471. cdsBGBills.Next;
  472. end;
  473. finally
  474. cdsBGBills.Filtered := False;
  475. end;
  476. end;
  477. procedure TBGLData.cdsBGBillsViewAfterDelete(DataSet: TDataSet);
  478. begin
  479. GatherBGLTotalPrice(cdsBGLViewID.AsInteger);
  480. UpdateBGLExecutionRate(cdsBGLViewID.AsInteger);
  481. end;
  482. procedure TBGLData.BatchWritePos_Reason;
  483. begin
  484. cdsBGL.First;
  485. while not cdsBGL.Eof do
  486. begin
  487. cdsBGL.Edit;
  488. cdsBGLPos_Reason.AsString := cdsBGLName.AsString;
  489. cdsBGL.Post;
  490. cdsBGL.Next;
  491. end;
  492. end;
  493. procedure TBGLData.cdsBGLViewNewRecord(DataSet: TDataSet);
  494. begin
  495. cdsBGLViewID.AsInteger := GetNewIDOfIndex(cdsBGL);
  496. cdsBGLViewCreatePhaseID.AsInteger := TProjectData(FProjectData).PhaseIndex;
  497. end;
  498. procedure TBGLData.UpdateBGLExecutionRate(ABGLID: Integer);
  499. function GetBGLExecutionTotalPrice: Double;
  500. var
  501. cdsTemp: TClientDataSet;
  502. begin
  503. Result := 0;
  504. cdsTemp := TClientDataSet.Create(nil);
  505. try
  506. cdsTemp.CloneCursor(cdsBGBills, True);
  507. cdsTemp.Filter := Format('BGID = %d', [ABGLID]);
  508. cdsTemp.Filtered := True;
  509. cdsTemp.First;
  510. while not cdsTemp.Eof do
  511. begin
  512. Result := Result + TotalPriceRoundTo(
  513. cdsTemp.FieldByName('UsedQuantity').AsFloat * cdsTemp.FieldByName('Price').AsFloat);
  514. cdsTemp.Next;
  515. end;
  516. finally
  517. cdsTemp.Free;
  518. end;
  519. end;
  520. begin
  521. if cdsBGL.FindKey([ABGLID]) then
  522. begin
  523. cdsBGL.Edit;
  524. if cdsBGLTotalPrice.AsFloat <> 0 then
  525. cdsBGLExecutionRate.AsFloat := AdvRoundTo(GetBGLExecutionTotalPrice/cdsBGLTotalPrice.AsFloat*100)
  526. else
  527. cdsBGLExecutionRate.AsFloat := 0;
  528. cdsBGL.Post;
  529. end;
  530. end;
  531. function TBGLData.AllBGLTotalPrice: Double;
  532. begin
  533. Result := 0;
  534. cdsBGL.First;
  535. while not cdsBGL.Eof do
  536. begin
  537. Result := Result + cdsBGLTotalPrice.AsFloat;
  538. cdsBGL.Next;
  539. end;
  540. end;
  541. procedure TBGLData.cdsBGBillsViewQuantitySetText(Sender: TField;
  542. const Text: String);
  543. begin
  544. Sender.AsFloat := QuantityRoundTo(StrToFloatDef(Text, 0));
  545. end;
  546. procedure TBGLData.cdsBGBillsViewPriceSetText(Sender: TField;
  547. const Text: String);
  548. begin
  549. Sender.AsFloat := PriceRoundTo(StrToFloatDef(Text, 0));
  550. end;
  551. procedure TBGLData.cdsBGLViewCodeChange(Sender: TField);
  552. begin
  553. Sender.Tag := 1;
  554. end;
  555. procedure TBGLData.Close;
  556. begin
  557. cdsBGL.IndexName := '';
  558. cdsBGL.Close;
  559. cdsBGBills.Close;
  560. end;
  561. function TBGLData.LocateBGL(const ACode: string): Boolean;
  562. begin
  563. cdsBGL.IndexName := 'idxCode';
  564. try
  565. Result := cdsBGL.FindKey([ACode]);
  566. finally
  567. cdsBGL.IndexName := 'idxID';
  568. end;
  569. end;
  570. function TBGLData.LocateBGBills(ABGID: Integer;
  571. const AB_Code: string): Boolean;
  572. begin
  573. Result := False;
  574. cdsBGBills.First;
  575. while (not cdsBGBills.Eof) do
  576. begin
  577. if (cdsBGBillsBGID.AsInteger = ABGID) and
  578. SameText(cdsBGBillsB_Code.AsString, AB_Code) then
  579. begin
  580. Result := True;
  581. Break;
  582. end;
  583. cdsBGBills.Next;
  584. end;
  585. end;
  586. function TBGLData.AllCloudBGLWebID: string;
  587. begin
  588. Result := '';
  589. cdsBGL.First;
  590. while not cdsBGL.Eof do
  591. begin
  592. if cdsBGLIsCloud.AsBoolean then
  593. begin
  594. if Result = '' then
  595. Result := IntToStr(cdsBGLWebID.AsInteger)
  596. else
  597. Result := Result + ',' + IntToStr(cdsBGLWebID.AsInteger);
  598. end;
  599. cdsBGL.Next;
  600. end;
  601. end;
  602. procedure TBGLData.LoadCloudBGL(const ABGLs: string);
  603. procedure AddCloudBGLBills(ABGLID: Integer; ABGBills: ISuperObject);
  604. var
  605. i, iNewID: Integer;
  606. vJ: ISuperObject;
  607. begin
  608. for i := 0 to ABGBills.AsArray.Length - 1 do
  609. begin
  610. iNewID := GetNewIDOfIndex(cdsBGBills);
  611. vJ := ABGBills.AsArray.O[i];
  612. cdsBGBills.Append;
  613. cdsBGBillsID.AsInteger := iNewID;
  614. cdsBGBillsBGID.AsInteger := ABGLID;
  615. cdsBGBillsB_Code.AsString := vJ.S['lnum'];
  616. cdsBGBillsName.AsString := vJ.S['lname'];
  617. cdsBGBillsUnits.AsString := vJ.S['unit'];
  618. cdsBGBillsPrice.AsFloat := vJ.D['unitprice'];
  619. cdsBGBillsQuantity.AsFloat := vJ.D['samount'];
  620. cdsBGBillsTotalPrice.AsFloat := TotalPriceRoundTo(cdsBGBillsPrice.AsFloat * cdsBGBillsQuantity.AsFloat);
  621. cdsBGBills.Post;
  622. UpdateBGLTotalPrice(ABGLID, cdsBGBillsTotalPrice.AsFloat);
  623. end;
  624. end;
  625. procedure AddCloudBGL(ABGL: ISuperObject);
  626. var
  627. iNewID, iCreatePhaseID: Integer;
  628. vBGBills: ISuperObject;
  629. begin
  630. iNewID := GetNewIDOfIndex(cdsBGL);
  631. iCreatePhaseID := TProjectData(FProjectData).PhaseIndex;
  632. cdsBGL.Append;
  633. cdsBGLID.AsInteger := iNewID;
  634. cdsBGLCode.AsString := ABGL.S['pnum'];
  635. cdsBGLName.AsString := ABGL.S['pname'];
  636. cdsBGLPos_Reason.AsString := ABGL.S['description'];
  637. cdsBGLDirection.AsString := ABGL.S['basis'];
  638. cdsBGLDrawingCode.AsString := ABGL.S['cnum'];
  639. cdsBGLApprovalCode.AsString := ABGL.S['bnum'];
  640. cdsBGLCreatePhaseID.AsInteger := iCreatePhaseID;
  641. cdsBGLBGLType.AsString := ABGL.S['changeNature'];
  642. cdsBGLIsCloud.AsBoolean := True;
  643. cdsBGLWebID.AsInteger := ABGL.I['cid'];
  644. cdsBGL.Post;
  645. AddCloudBGLBills(iNewID, ABGL.O['changeBills']);
  646. end;
  647. var
  648. vJ: ISuperObject;
  649. i: Integer;
  650. begin
  651. vJ := SO(ABGLs);
  652. try
  653. if not Assigned(vJ.AsArray) then Exit;
  654. for i := 0 to vJ.AsArray.Length - 1 do
  655. AddCloudBGL(vJ.AsArray.O[i]);
  656. finally
  657. vJ := nil;
  658. end;
  659. end;
  660. function TBGLData.GetBGLCanEdit(ASerialNo: Integer): Boolean;
  661. var
  662. i: Integer;
  663. bk: TBookmark;
  664. begin
  665. cdsBGLView.DisableControls;
  666. bk := cdsBGLView.GetBookmark;
  667. cdsBGLView.First;
  668. i := 0;
  669. while (i < ASerialNo) and not cdsBGLView.Eof do
  670. begin
  671. cdsBGLView.Next;
  672. Inc(i);
  673. end;
  674. if i = ASerialNo then
  675. Result := not cdsBGLViewIsCloud.AsBoolean
  676. else
  677. Result := False;
  678. cdsBGLView.GotoBookmark(bk);
  679. cdsBGLView.FreeBookmark(bk);
  680. cdsBGLView.EnableControls;
  681. end;
  682. end.