BGLDm.pas 22 KB

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