BGLDm.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. unit BGLDm;
  2. interface
  3. uses
  4. SysUtils, Classes, DB, DBClient, Provider, ADODB, sdIDTree,
  5. sdDB;
  6. type
  7. TAfterCurrentBGLChanged = procedure of object;
  8. TBGLSelectInfo = class(TObject)
  9. private
  10. FB_Code: string;
  11. FName: string;
  12. FUnits: string;
  13. FPrice: Double;
  14. FIsOrg: Boolean;
  15. FTotalNum: Double;
  16. FNums: TStrings;
  17. FCodes: TStrings;
  18. procedure SetMergedCode(const Value: string);
  19. procedure SetMergedNum(const Value: string);
  20. function GetCount: Integer;
  21. function GetMergedCode: string;
  22. function GetMergedNum: string;
  23. public
  24. constructor Create(ARec: TsdDataRecord; ATotalNum: Double; AIsOrg: Boolean);
  25. destructor Destroy; override;
  26. procedure Clear;
  27. property MergedCode: string read GetMergedCode write SetMergedCode;
  28. property MergedNum: string read GetMergedNum write SetMergedNum;
  29. property Codes: TStrings read FCodes;
  30. property Nums: TStrings read FNums;
  31. property Count: Integer read GetCount;
  32. property TotalNum: Double read FTotalNum write FTotalNum;
  33. property IsOrg: Boolean read FIsOrg;
  34. property B_Code: string read FB_Code;
  35. property Name: string read FName;
  36. property Units: string read FUnits;
  37. property Price: Double read FPrice;
  38. end;
  39. TBGLData = class(TDataModule)
  40. atBGL: TADOTable;
  41. dspBGL: TDataSetProvider;
  42. cdsBGL: TClientDataSet;
  43. cdsBGLID: TIntegerField;
  44. cdsBGLCode: TWideStringField;
  45. cdsBGLName: TWideStringField;
  46. cdsBGLTotalPrice: TFloatField;
  47. cdsBGLPos_Reason: TMemoField;
  48. cdsBGLDirection: TMemoField;
  49. cdsBGLDrawingCode: TWideStringField;
  50. cdsBGLApprovalCode: TWideStringField;
  51. cdsBGLCreatePhaseID: TIntegerField;
  52. cdsBGLExecutionRate: TFloatField;
  53. cdsBGLBGLType: TWideStringField;
  54. cdsBGLView: TClientDataSet;
  55. cdsBGLViewID: TIntegerField;
  56. cdsBGLViewCode: TWideStringField;
  57. cdsBGLViewName: TWideStringField;
  58. cdsBGLViewTotalPrice: TFloatField;
  59. cdsBGLViewPos_Reason: TMemoField;
  60. cdsBGLViewDirection: TMemoField;
  61. cdsBGLViewDrawingCode: TWideStringField;
  62. cdsBGLViewApprovalCode: TWideStringField;
  63. cdsBGLViewCreatePhaseID: TIntegerField;
  64. cdsBGLViewExecutionRate: TFloatField;
  65. cdsBGLViewBGLType: TWideStringField;
  66. dsBGL: TDataSource;
  67. atBGBills: TADOTable;
  68. dspBGBills: TDataSetProvider;
  69. cdsBGBills: TClientDataSet;
  70. cdsBGBillsID: TIntegerField;
  71. cdsBGBillsBGID: TIntegerField;
  72. cdsBGBillsB_Code: TWideStringField;
  73. cdsBGBillsName: TWideStringField;
  74. cdsBGBillsUnits: TWideStringField;
  75. cdsBGBillsPrice: TFloatField;
  76. cdsBGBillsQuantity: TFloatField;
  77. cdsBGBillsTotalPrice: TFloatField;
  78. cdsBGBillsUsedQuantity: TFloatField;
  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. cdsBGLIsCloud: TBooleanField;
  90. cdsBGLWebID: TIntegerField;
  91. cdsBGLViewIsCloud: TBooleanField;
  92. cdsBGLViewWebID: TIntegerField;
  93. procedure cdsBGBillsViewAfterInsert(DataSet: TDataSet);
  94. procedure cdsBGBillsViewAfterPost(DataSet: TDataSet);
  95. procedure cdsBGBillsViewQuantityChange(Sender: TField);
  96. procedure cdsBGBillsViewBeforePost(DataSet: TDataSet);
  97. procedure cdsBGLViewBeforePost(DataSet: TDataSet);
  98. procedure cdsBGLViewBeforeDelete(DataSet: TDataSet);
  99. procedure cdsBGBillsViewBeforeDelete(DataSet: TDataSet);
  100. procedure cdsBGBillsViewAfterDelete(DataSet: TDataSet);
  101. procedure cdsBGLViewNewRecord(DataSet: TDataSet);
  102. procedure cdsBGBillsViewQuantitySetText(Sender: TField;
  103. const Text: String);
  104. procedure cdsBGBillsViewPriceSetText(Sender: TField;
  105. const Text: String);
  106. procedure cdsBGLViewCodeChange(Sender: TField);
  107. procedure cdsBGLViewAfterScroll(DataSet: TDataSet);
  108. private
  109. FProjectData: TObject;
  110. FAfterCurrentBGLChanged: TAfterCurrentBGLChanged;
  111. procedure GatherBGLTotalPrice(ABGLID: Integer);
  112. procedure UpdateBGLTotalPrice(ABGLID: Integer; ADiffer: Double);
  113. procedure UpdateBGLExecutionRate(ABGLID: Integer);
  114. procedure ApplyBGL(ABGLInfo: TBGLSelectInfo); overload;
  115. function DeleteWeb(AWebID: Integer): Boolean;
  116. procedure DeleteBGBills(ABGID: Integer);
  117. function CheckSameB_Code(ABGID: Integer; const AB_Code: string): Boolean;
  118. function CheckBGLUsed(ABGID: Integer): Boolean;
  119. function LocateBGL(const ACode: string): Boolean;
  120. function LocateBGBills(ABGID: Integer; const AB_Code: string): Boolean;
  121. function GetTotalPrice: Double;
  122. public
  123. constructor Create(AProjectData: TObject);
  124. destructor Destroy; override;
  125. procedure Open(AConnection: TADOConnection);
  126. procedure Close;
  127. procedure Save;
  128. function GetBGLCanEdit(ASerialNo: Integer): Boolean;
  129. function AllBGLTotalPrice: Double;
  130. function AllCloudBGLWebID: string;
  131. procedure LoadCloudBGL(const ABGLs: string);
  132. procedure AddBGL(const sCode: string);
  133. procedure ApplyBGL(AOrgBGL, ANewBGL: TBGLSelectInfo); overload;
  134. procedure BatchWritePos_Reason;
  135. property ProjectData: TObject read FProjectData;
  136. property TotalPrice: Double read GetTotalPrice;
  137. property AfterCurrentBGLChanged: TAfterCurrentBGLChanged read FAfterCurrentBGLChanged write FAfterCurrentBGLChanged;
  138. end;
  139. implementation
  140. uses
  141. ZhAPI, Math, ProjectData, BillsDm, Variants, UtilMethods, superobject,
  142. PHPWebDm, Forms, Controls;
  143. {$R *.dfm}
  144. { TBGLSelectInfo }
  145. procedure TBGLSelectInfo.Clear;
  146. begin
  147. FCodes.Clear;
  148. FNums.Clear;
  149. end;
  150. constructor TBGLSelectInfo.Create(ARec: TsdDataRecord;
  151. ATotalNum: Double; AIsOrg: Boolean);
  152. begin
  153. FB_Code := ARec.ValueByName('B_Code').AsString;
  154. FName := ARec.ValueByName('Name').AsString;
  155. FUnits := ARec.ValueByName('Units').AsString;
  156. FPrice := ARec.ValueByName('Price').AsFloat;
  157. FTotalNum := ATotalNum;
  158. FIsOrg := AIsOrg;
  159. FCodes := TStringList.Create;
  160. FCodes.Delimiter := ';';
  161. FNums := TStringList.Create;
  162. FNums.Delimiter := ';';
  163. end;
  164. destructor TBGLSelectInfo.Destroy;
  165. begin
  166. FNums.Free;
  167. FCodes.Free;
  168. inherited;
  169. end;
  170. function TBGLSelectInfo.GetCount: Integer;
  171. begin
  172. Result := Min(FCodes.Count, FNums.Count);
  173. end;
  174. function TBGLSelectInfo.GetMergedCode: string;
  175. begin
  176. Result := FCodes.DelimitedText;
  177. end;
  178. function TBGLSelectInfo.GetMergedNum: string;
  179. begin
  180. Result := FNums.DelimitedText;
  181. end;
  182. procedure TBGLSelectInfo.SetMergedCode(const Value: string);
  183. begin
  184. if Value <> '' then
  185. FCodes.DelimitedText := Value
  186. else
  187. FCodes.Clear;
  188. end;
  189. procedure TBGLSelectInfo.SetMergedNum(const Value: string);
  190. begin
  191. if Value <> '' then
  192. FNums.DelimitedText := Value
  193. else
  194. FNums.Clear;
  195. end;
  196. { TBGLData }
  197. constructor TBGLData.Create(AProjectData: TObject);
  198. begin
  199. inherited Create(nil);
  200. FProjectData := AProjectData;
  201. end;
  202. destructor TBGLData.Destroy;
  203. begin
  204. inherited;
  205. end;
  206. procedure TBGLData.Open(AConnection: TADOConnection);
  207. begin
  208. atBGL.Connection := AConnection;
  209. cdsBGL.Open;
  210. cdsBGL.AddIndex('idxID', 'ID', []);
  211. cdsBGL.AddIndex('idxCode', 'Code', []);
  212. cdsBGL.IndexName := 'idxID';
  213. cdsBGLView.CloneCursor(cdsBGL, True);
  214. atBGBills.Connection := AConnection;
  215. cdsBGBills.Open;
  216. cdsBGBillsView.CloneCursor(cdsBGBills, True);
  217. cdsBGBills.IndexFieldNames := 'ID';
  218. cdsBGBillsView.MasterSource := dsBGL;
  219. cdsBGBillsView.MasterFields := 'ID';
  220. cdsBGBillsView.IndexFieldNames := 'BGID;ID';
  221. end;
  222. procedure TBGLData.Save;
  223. begin
  224. cdsBGL.ApplyUpdates(0);
  225. cdsBGBills.ApplyUpdates(0);
  226. end;
  227. procedure TBGLData.cdsBGBillsViewAfterInsert(DataSet: TDataSet);
  228. begin
  229. cdsBGBillsViewID.AsInteger := GetNewIDOfIndex(cdsBGBills);
  230. cdsBGBillsViewBGID.AsInteger := cdsBGLViewID.AsInteger;
  231. end;
  232. procedure TBGLData.AddBGL(const sCode: string);
  233. begin
  234. cdsBGLView.DisableControls;
  235. cdsBGLView.Append;
  236. cdsBGLViewCode.AsString := sCode;
  237. cdsBGLView.Post;
  238. cdsBGLView.EnableControls;
  239. end;
  240. procedure TBGLData.cdsBGBillsViewAfterPost(DataSet: TDataSet);
  241. procedure DoB_CodeChange;
  242. var
  243. Rec: TsdDataRecord;
  244. begin
  245. cdsBGBillsViewB_Code.Tag := 0;
  246. cdsBGBillsView.Edit;
  247. with TProjectData(FProjectData).BillsData do
  248. begin
  249. Rec := sddBills.Locate('B_Code', cdsBGBillsViewB_Code.AsString);
  250. if Rec <> nil then
  251. begin
  252. cdsBGBillsViewName.AsString := Rec.ValueByName('Name').AsString;
  253. cdsBGBillsViewUnits.AsString := Rec.ValueByName('Units').AsString;
  254. cdsBGBillsViewPrice.AsString := Rec.ValueByName('Price').AsString;
  255. end;
  256. end;
  257. cdsBGBillsView.Post;
  258. end;
  259. procedure ClearChangeTag;
  260. begin
  261. cdsBGBillsViewB_Code.Tag := 0;
  262. cdsBGBillsViewName.Tag := 0;
  263. cdsBGBillsViewUnits.Tag := 0;
  264. cdsBGBillsViewPrice.Tag := 0;
  265. cdsBGBillsViewQuantity.Tag := 0;
  266. end;
  267. var
  268. fTotalPrice, Differ: Double;
  269. begin
  270. if (cdsBGBillsViewB_Code.Tag = 1) then
  271. DoB_CodeChange;
  272. if (cdsBGBillsViewPrice.Tag = 1) or
  273. (cdsBGBillsViewQuantity.Tag = 1) then
  274. begin
  275. cdsBGBillsViewPrice.Tag := 0;
  276. cdsBGBillsViewQuantity.Tag := 0;
  277. fTotalPrice := TotalPriceRoundTo(cdsBGBillsViewPrice.AsFloat * cdsBGBillsViewQuantity.AsFloat);
  278. UpdateBGLTotalPrice(cdsBGBillsViewBGID.AsInteger, fTotalPrice - cdsBGBillsViewTotalPrice.AsFloat);
  279. UpdateBGLExecutionRate(cdsBGBillsViewBGID.AsInteger);
  280. cdsBGBillsView.Edit;
  281. cdsBGBillsViewTotalPrice.AsFloat := fTotalPrice;
  282. cdsBGBillsView.Post;
  283. end;
  284. ClearChangeTag;
  285. end;
  286. procedure TBGLData.cdsBGBillsViewQuantityChange(Sender: TField);
  287. begin
  288. Sender.Tag := 1;
  289. end;
  290. procedure TBGLData.GatherBGLTotalPrice(ABGLID: Integer);
  291. var
  292. fGather: Double;
  293. begin
  294. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGLID);
  295. cdsBGBills.Filtered := True;
  296. try
  297. fGather := 0;
  298. cdsBGBills.First;
  299. while not cdsBGBills.Eof do
  300. begin
  301. fGather := fGather + cdsBGBillsTotalPrice.AsFloat;
  302. cdsBGBills.Next;
  303. end;
  304. if cdsBGL.FindKey([ABGLID]) then
  305. begin
  306. cdsBGL.Edit;
  307. cdsBGLTotalPrice.AsFloat := fGather;
  308. cdsBGL.Post;
  309. end;
  310. finally
  311. cdsBGBills.Filtered := False;
  312. end;
  313. end;
  314. procedure TBGLData.UpdateBGLTotalPrice(ABGLID: Integer; ADiffer: Double);
  315. begin
  316. if cdsBGL.FindKey([ABGLID]) then
  317. begin
  318. cdsBGL.Edit;
  319. cdsBGLTotalPrice.AsFloat := cdsBGLTotalPrice.AsFloat + ADiffer;
  320. cdsBGL.Post;
  321. end;
  322. end;
  323. procedure TBGLData.ApplyBGL(AOrgBGL, ANewBGL: TBGLSelectInfo);
  324. begin
  325. ApplyBGL(AOrgBGL);
  326. ApplyBGL(ANewBGL);
  327. end;
  328. procedure TBGLData.ApplyBGL(ABGLInfo: TBGLSelectInfo);
  329. var
  330. I: Integer;
  331. fNum: Double;
  332. begin
  333. for I := 0 to ABGLInfo.Count - 1 do
  334. if LocateBGL(ABGLInfo.Codes[I]) and
  335. LocateBGBills(cdsBGLID.AsInteger, ABGLInfo.B_Code) then
  336. begin
  337. cdsBGBills.Edit;
  338. fNum := QuantityRoundTo(StrToFloatDef(ABGLInfo.Nums[I], 0));
  339. if ABGLInfo.IsOrg then
  340. cdsBGBillsUsedQuantity.AsFloat := QuantityRoundTo(cdsBGBillsUsedQuantity.AsFloat - fNum)
  341. else
  342. cdsBGBillsUsedQuantity.AsFloat := QuantityRoundTo(cdsBGBillsUsedQuantity.AsFloat + fNum);
  343. cdsBGBills.Post;
  344. UpdateBGLExecutionRate(cdsBGBillsBGID.AsInteger);
  345. end;
  346. end;
  347. procedure TBGLData.cdsBGBillsViewBeforePost(DataSet: TDataSet);
  348. procedure DisplayErrorMessage(const AHint: string);
  349. begin
  350. cdsBGBillsViewB_Code.Tag := 0;
  351. cdsBGBillsViewName.Tag := 0;
  352. cdsBGBillsViewUnits.Tag := 0;
  353. cdsBGBillsViewPrice.Tag := 0;
  354. cdsBGBillsViewQuantity.Tag := 0;
  355. ErrorMessage(AHint);
  356. Abort;
  357. end;
  358. begin
  359. if (cdsBGBillsViewB_Code.Tag = 1) or
  360. (cdsBGBillsViewName.Tag = 1) or
  361. (cdsBGBillsViewUnits.Tag = 1) or
  362. (cdsBGBillsViewPrice.Tag = 1) then
  363. begin
  364. if cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  365. DisplayErrorMessage('变更令已被应用至清单,不可修改!');
  366. end;
  367. if (cdsBGBillsViewQuantity.Tag = 1) then
  368. begin
  369. if (cdsBGBillsViewUsedQuantity.AsFloat <> 0) and
  370. (cdsBGBillsViewQuantity.AsFloat < cdsBGBillsViewUsedQuantity.AsFloat) then
  371. DisplayErrorMessage('变更清单的清单数量应大于已变更数量!');
  372. end;
  373. if cdsBGBillsViewB_Code.Tag = 1 then
  374. if CheckSameB_Code(cdsBGBillsViewBGID.AsInteger, cdsBGBillsViewB_Code.AsString) then
  375. begin
  376. DisplayErrorMessage('不允许存在同编号变更清单!');
  377. end;
  378. end;
  379. procedure TBGLData.cdsBGLViewBeforePost(DataSet: TDataSet);
  380. var
  381. iIncrement: Integer;
  382. sNewCode: string;
  383. begin
  384. if cdsBGLViewIsCloud.AsBoolean then
  385. begin
  386. cdsBGLViewCode.Tag := 0;
  387. WarningMessage('当前变更令不允许编辑。');
  388. DataSet.Cancel;
  389. Abort;
  390. end;
  391. // 变更令号不可为空
  392. if cdsBGLViewCode.AsString = '' then
  393. begin
  394. cdsBGLViewCode.Tag := 0;
  395. if cdsBGL.FindKey([cdsBGLViewID.AsInteger]) then
  396. if cdsBGLCode.AsString <> '' then
  397. WarningMessage('变更令号不允许为空,如需删除,请点击右键进行删除。');
  398. DataSet.Cancel;
  399. Abort;
  400. end;
  401. if cdsBGLViewCode.Tag = 1 then
  402. begin
  403. cdsBGLViewCode.Tag := 0;
  404. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  405. begin
  406. ErrorMessage('当前变更令下变更清单已被应用到清单,不可修改!');
  407. Abort;
  408. end;
  409. sNewCode := cdsBGLViewCode.AsString;
  410. if Pos(';', sNewCode) > 0 then
  411. begin
  412. ErrorMessage('变更令号不可输入'';'',请使用其他符号代替!');
  413. Abort;
  414. end;
  415. // 相同变更令号应递增[1],[2]...
  416. iIncrement := 1;
  417. while LocateBGL(sNewCode) and (cdsBGLID.AsInteger <> cdsBGLViewID.AsInteger) do
  418. begin
  419. sNewCode := Format('%s[%d]', [cdsBGLViewCode.AsString, iIncrement]);
  420. Inc(iIncrement);
  421. end;
  422. if cdsBGLViewCode.AsString <> sNewCode then
  423. begin
  424. cdsBGLViewCode.AsString := sNewCode;
  425. cdsBGLViewCode.Tag := 0;
  426. end;
  427. end;
  428. end;
  429. procedure TBGLData.DeleteBGBills(ABGID: Integer);
  430. begin
  431. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  432. cdsBGBills.Filtered := True;
  433. try
  434. cdsBGBills.First;
  435. while not cdsBGBills.Eof do
  436. cdsBGBills.Delete;
  437. finally
  438. cdsBGBills.Filtered := False;
  439. end;
  440. end;
  441. procedure TBGLData.cdsBGLViewBeforeDelete(DataSet: TDataSet);
  442. begin
  443. if CheckBGLUsed(cdsBGLViewID.AsInteger) then
  444. raise Exception.Create('变更令下变更清单已被应用到清单,不可删除!');
  445. //if cdsBGLViewIsCloud.AsBoolean then
  446. //raise Exception.Create('云端获取的变更令不允许删除!');
  447. if cdsBGLViewIsCloud.AsBoolean and not DeleteWeb(cdsBGLViewWebID.AsInteger) then
  448. raise Exception.Create('同步删除云端数据失败,请重试');
  449. DeleteBGBills(cdsBGLViewID.AsInteger);
  450. end;
  451. function TBGLData.CheckSameB_Code(ABGID: Integer;
  452. const AB_Code: string): Boolean;
  453. begin
  454. Result := False;
  455. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  456. cdsBGBills.Filtered := True;
  457. try
  458. cdsBGBills.First;
  459. while (not cdsBGBills.Eof) and (not Result) do
  460. begin
  461. Result := Result or SameText(cdsBGBillsB_Code.AsString, AB_Code);
  462. cdsBGBills.Next;
  463. end;
  464. finally
  465. cdsBGBills.Filtered := False;
  466. end;
  467. end;
  468. procedure TBGLData.cdsBGBillsViewBeforeDelete(DataSet: TDataSet);
  469. begin
  470. if cdsBGBillsViewUsedQuantity.AsFloat <> 0 then
  471. raise Exception.Create('变更清单已被应用至清单,不可删除!');
  472. if cdsBGLViewIsCloud.AsBoolean then
  473. raise Exception.Create('云端获取的变更清单不允许删除!');
  474. end;
  475. function TBGLData.CheckBGLUsed(ABGID: Integer): Boolean;
  476. begin
  477. Result := False;
  478. cdsBGBills.Filter := 'BGID = ' + IntToStr(ABGID);
  479. cdsBGBills.Filtered := True;
  480. try
  481. cdsBGBills.First;
  482. while (not cdsBGBills.Eof) and (not Result) do
  483. begin
  484. Result := Result or (cdsBGBillsUsedQuantity.AsFloat <> 0);
  485. cdsBGBills.Next;
  486. end;
  487. finally
  488. cdsBGBills.Filtered := False;
  489. end;
  490. end;
  491. procedure TBGLData.cdsBGBillsViewAfterDelete(DataSet: TDataSet);
  492. begin
  493. GatherBGLTotalPrice(cdsBGLViewID.AsInteger);
  494. UpdateBGLExecutionRate(cdsBGLViewID.AsInteger);
  495. end;
  496. procedure TBGLData.BatchWritePos_Reason;
  497. begin
  498. cdsBGL.First;
  499. while not cdsBGL.Eof do
  500. begin
  501. cdsBGL.Edit;
  502. cdsBGLPos_Reason.AsString := cdsBGLName.AsString;
  503. cdsBGL.Post;
  504. cdsBGL.Next;
  505. end;
  506. end;
  507. procedure TBGLData.cdsBGLViewNewRecord(DataSet: TDataSet);
  508. begin
  509. cdsBGLViewID.AsInteger := GetNewIDOfIndex(cdsBGL);
  510. cdsBGLViewCreatePhaseID.AsInteger := TProjectData(FProjectData).PhaseIndex;
  511. end;
  512. procedure TBGLData.UpdateBGLExecutionRate(ABGLID: Integer);
  513. function GetBGLExecutionTotalPrice: Double;
  514. var
  515. cdsTemp: TClientDataSet;
  516. begin
  517. Result := 0;
  518. cdsTemp := TClientDataSet.Create(nil);
  519. try
  520. cdsTemp.CloneCursor(cdsBGBills, True);
  521. cdsTemp.Filter := Format('BGID = %d', [ABGLID]);
  522. cdsTemp.Filtered := True;
  523. cdsTemp.First;
  524. while not cdsTemp.Eof do
  525. begin
  526. Result := Result + TotalPriceRoundTo(
  527. cdsTemp.FieldByName('UsedQuantity').AsFloat * cdsTemp.FieldByName('Price').AsFloat);
  528. cdsTemp.Next;
  529. end;
  530. finally
  531. cdsTemp.Free;
  532. end;
  533. end;
  534. begin
  535. if cdsBGL.FindKey([ABGLID]) then
  536. begin
  537. cdsBGL.Edit;
  538. if cdsBGLTotalPrice.AsFloat <> 0 then
  539. cdsBGLExecutionRate.AsFloat := AdvRoundTo(GetBGLExecutionTotalPrice/cdsBGLTotalPrice.AsFloat*100)
  540. else
  541. cdsBGLExecutionRate.AsFloat := 0;
  542. cdsBGL.Post;
  543. end;
  544. end;
  545. function TBGLData.AllBGLTotalPrice: Double;
  546. begin
  547. Result := 0;
  548. cdsBGL.First;
  549. while not cdsBGL.Eof do
  550. begin
  551. Result := Result + cdsBGLTotalPrice.AsFloat;
  552. cdsBGL.Next;
  553. end;
  554. end;
  555. procedure TBGLData.cdsBGBillsViewQuantitySetText(Sender: TField;
  556. const Text: String);
  557. begin
  558. Sender.AsFloat := QuantityRoundTo(StrToFloatDef(Text, 0));
  559. end;
  560. procedure TBGLData.cdsBGBillsViewPriceSetText(Sender: TField;
  561. const Text: String);
  562. begin
  563. Sender.AsFloat := PriceRoundTo(StrToFloatDef(Text, 0));
  564. end;
  565. procedure TBGLData.cdsBGLViewCodeChange(Sender: TField);
  566. begin
  567. Sender.Tag := 1;
  568. end;
  569. procedure TBGLData.Close;
  570. begin
  571. cdsBGL.IndexName := '';
  572. cdsBGL.Close;
  573. cdsBGBills.Close;
  574. end;
  575. function TBGLData.LocateBGL(const ACode: string): Boolean;
  576. begin
  577. cdsBGL.IndexName := 'idxCode';
  578. try
  579. Result := cdsBGL.FindKey([ACode]);
  580. finally
  581. cdsBGL.IndexName := 'idxID';
  582. end;
  583. end;
  584. function TBGLData.LocateBGBills(ABGID: Integer;
  585. const AB_Code: string): Boolean;
  586. begin
  587. Result := False;
  588. cdsBGBills.First;
  589. while (not cdsBGBills.Eof) do
  590. begin
  591. if (cdsBGBillsBGID.AsInteger = ABGID) and
  592. SameText(cdsBGBillsB_Code.AsString, AB_Code) then
  593. begin
  594. Result := True;
  595. Break;
  596. end;
  597. cdsBGBills.Next;
  598. end;
  599. end;
  600. function TBGLData.AllCloudBGLWebID: string;
  601. begin
  602. Result := '';
  603. cdsBGL.First;
  604. while not cdsBGL.Eof do
  605. begin
  606. if cdsBGLIsCloud.AsBoolean then
  607. begin
  608. if Result = '' then
  609. Result := IntToStr(cdsBGLWebID.AsInteger)
  610. else
  611. Result := Result + ',' + IntToStr(cdsBGLWebID.AsInteger);
  612. end;
  613. cdsBGL.Next;
  614. end;
  615. end;
  616. procedure TBGLData.LoadCloudBGL(const ABGLs: string);
  617. procedure AddCloudBGLBills(ABGLID: Integer; ABGBills: ISuperObject);
  618. var
  619. i, iNewID: Integer;
  620. vJ: ISuperObject;
  621. begin
  622. for i := 0 to ABGBills.AsArray.Length - 1 do
  623. begin
  624. iNewID := GetNewIDOfIndex(cdsBGBills);
  625. vJ := ABGBills.AsArray.O[i];
  626. cdsBGBills.Append;
  627. cdsBGBillsID.AsInteger := iNewID;
  628. cdsBGBillsBGID.AsInteger := ABGLID;
  629. cdsBGBillsB_Code.AsString := vJ.S['lnum'];
  630. cdsBGBillsName.AsString := vJ.S['lname'];
  631. cdsBGBillsUnits.AsString := vJ.S['unit'];
  632. cdsBGBillsPrice.AsFloat := vJ.D['unitprice'];
  633. cdsBGBillsQuantity.AsFloat := vJ.D['samount'];
  634. cdsBGBillsTotalPrice.AsFloat := TotalPriceRoundTo(cdsBGBillsPrice.AsFloat * cdsBGBillsQuantity.AsFloat);
  635. cdsBGBills.Post;
  636. UpdateBGLTotalPrice(ABGLID, cdsBGBillsTotalPrice.AsFloat);
  637. end;
  638. end;
  639. procedure AddCloudBGL(ABGL: ISuperObject);
  640. var
  641. iNewID, iCreatePhaseID: Integer;
  642. vBGBills: ISuperObject;
  643. begin
  644. iNewID := GetNewIDOfIndex(cdsBGL);
  645. iCreatePhaseID := TProjectData(FProjectData).PhaseIndex;
  646. cdsBGL.Append;
  647. cdsBGLID.AsInteger := iNewID;
  648. cdsBGLCode.AsString := ABGL.S['pnum'];
  649. cdsBGLName.AsString := ABGL.S['pname'];
  650. cdsBGLPos_Reason.AsString := ABGL.S['description'];
  651. cdsBGLDirection.AsString := ABGL.S['basis'];
  652. cdsBGLDrawingCode.AsString := ABGL.S['cnum'];
  653. cdsBGLApprovalCode.AsString := ABGL.S['bnum'];
  654. cdsBGLCreatePhaseID.AsInteger := iCreatePhaseID;
  655. cdsBGLBGLType.AsString := ABGL.S['changeNature'];
  656. cdsBGLIsCloud.AsBoolean := True;
  657. cdsBGLWebID.AsInteger := ABGL.I['cid'];
  658. cdsBGL.Post;
  659. AddCloudBGLBills(iNewID, ABGL.O['changeBills']);
  660. end;
  661. var
  662. vJ: ISuperObject;
  663. i: Integer;
  664. begin
  665. vJ := SO(ABGLs);
  666. try
  667. if not Assigned(vJ.AsArray) then Exit;
  668. for i := 0 to vJ.AsArray.Length - 1 do
  669. AddCloudBGL(vJ.AsArray.O[i]);
  670. finally
  671. vJ := nil;
  672. end;
  673. end;
  674. function TBGLData.GetBGLCanEdit(ASerialNo: Integer): Boolean;
  675. var
  676. i: Integer;
  677. bk: TBookmark;
  678. begin
  679. cdsBGLView.DisableControls;
  680. bk := cdsBGLView.GetBookmark;
  681. cdsBGLView.First;
  682. i := 0;
  683. while (i < ASerialNo) and not cdsBGLView.Eof do
  684. begin
  685. cdsBGLView.Next;
  686. Inc(i);
  687. end;
  688. if i = ASerialNo then
  689. Result := not cdsBGLViewIsCloud.AsBoolean
  690. else
  691. Result := False;
  692. cdsBGLView.GotoBookmark(bk);
  693. cdsBGLView.FreeBookmark(bk);
  694. cdsBGLView.EnableControls;
  695. end;
  696. procedure TBGLData.cdsBGLViewAfterScroll(DataSet: TDataSet);
  697. begin
  698. if Assigned(FAfterCurrentBGLChanged) then
  699. FAfterCurrentBGLChanged;
  700. end;
  701. function TBGLData.DeleteWeb(AWebID: Integer): Boolean;
  702. var
  703. sgs: TStrings;
  704. sUrl, sInfo: string;
  705. begin
  706. Screen.Cursor := crHourGlass;
  707. sgs := TStringList.Create;
  708. try
  709. sgs.Add(Format('cid=%d', [AWebID]));
  710. sUrl := PHPWeb.MeasureURL + 'change/delete';
  711. Result := PHPWeb.UrlGet(sUrl, sgs, sInfo) = 1;
  712. finally
  713. sgs.Free;
  714. Screen.Cursor := crDefault;
  715. end;
  716. end;
  717. function TBGLData.GetTotalPrice: Double;
  718. var
  719. fTotalPrice: Double;
  720. begin
  721. fTotalPrice := 0;
  722. cdsBGL.First;
  723. while not cdsBGL.Eof do
  724. begin
  725. fTotalPrice := fTotalPrice + cdsBGLTotalPrice.AsFloat;
  726. cdsBGL.next;
  727. end;
  728. Result := fTotalPrice;
  729. end;
  730. end.