PageRenderTime 66ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Gedemin/Transaction/gdv_frmAcctLedger_unit.pas

http://gedemin.googlecode.com/
Pascal | 1681 lines | 1455 code | 181 blank | 45 comment | 190 complexity | dbd6b546477c0f947ab433419ac6a040 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. unit gdv_frmAcctLedger_unit;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. gdv_frmAcctBaseForm_unit, Db, IBCustomDataSet, flt_sqlFilter,
  6. gd_ReportMenu, Menus, gd_MacrosMenu, ActnList, gdv_frAcctCompany_unit,
  7. gdv_frAcctAnalytics_unit, gdv_frAcctSum_unit, gdv_frAcctQuantity_unit,
  8. StdCtrls, Grids, DBGrids, gsDBGrid, gsIBGrid, Mask, xDateEdits, Buttons,
  9. ExtCtrls, TB2Item, TB2Dock, TB2Toolbar, gdv_frAcctAnalyticsGroup_unit,
  10. gd_ClassList, IBSQL, at_classes, gdcBaseInterface, Storages, gsStorage_CompPath,
  11. AcctStrings, AcctUtils, gd_security, Contnrs, IBDataBase, gdv_AcctConfig_unit,
  12. gsIBLookupComboBox, gdv_frAcctBaseAnalyticGroup, gdvParamPanel, gdcConstants,
  13. gd_createable_form, gdv_frAcctTreeAnalytic_unit, gdv_frAcctTreeAnalyticLine_unit,
  14. gdvAcctBase, gdvAcctLedger, gsPeriodEdit;
  15. type
  16. Tgdv_ValueList = class;
  17. Tgdv_SaldoValue = class
  18. private
  19. FSaldoName: string;
  20. FSaldoValue: Currency;
  21. procedure SetSaldoName(const Value: string);
  22. procedure SetSaldoValue(const Value: Currency);
  23. public
  24. property SaldoName: string read FSaldoName write SetSaldoName;
  25. property SaldoValue: Currency read FSaldoValue write SetSaldoValue;
  26. end;
  27. Tgdv_SaldoValues = class(TObjectList)
  28. private
  29. function GetSaldoList(Index: Integer): Tgdv_SaldoValue;
  30. public
  31. constructor Create;
  32. function IndexOf(FieldName: string): Integer;
  33. function AddSaldoValue(SaldoName: string; SaldoValue: Currency): Integer;
  34. property SaldoList[Index: Integer]: Tgdv_SaldoValue read GetSaldoList; default;
  35. end;
  36. Tgdv_Value = class
  37. private
  38. FValues: Tgdv_ValueList;
  39. FValue: string;
  40. FSaldoValues: Tgdv_SaldoValues;
  41. function GetValues: Tgdv_ValueList;
  42. procedure SetValue(const Value: string);
  43. function GetSaldoValues: Tgdv_SaldoValues;
  44. public
  45. destructor Destroy; override;
  46. property Value: string read FValue write SetValue;
  47. property Values: Tgdv_ValueList read GetValues;
  48. property SaldoValues: Tgdv_SaldoValues read GetSaldoValues;
  49. end;
  50. Tgdv_ValueList = class
  51. private
  52. FValues: TStringList;
  53. function GetItems(Index: Integer): Tgdv_Value;
  54. public
  55. destructor Destroy; override;
  56. procedure Clear;
  57. function IndexOf(Value: string): Integer;
  58. function Add(Value: string): Tgdv_Value;
  59. function Add2(Value: string): Integer;
  60. property Items[Index: Integer]: Tgdv_Value read GetItems; default;
  61. end;
  62. Tgdv_frmAcctLedger = class(Tgdv_frmAcctBaseForm)
  63. cbShowDebit: TCheckBox;
  64. cbShowCredit: TCheckBox;
  65. cbShowCorrSubAccount: TCheckBox;
  66. frAcctAnalyticsGroup: TfrAcctAnalyticsGroup;
  67. cbEnchancedSaldo: TCheckBox;
  68. cbSumNull: TCheckBox;
  69. frAcctTreeAnalytic: Tgdv_frAcctTreeAnalytic;
  70. ibdsMain: TgdvAcctLedger;
  71. procedure actSaveConfigUpdate(Sender: TObject);
  72. procedure FormDestroy(Sender: TObject);
  73. procedure actRunUpdate(Sender: TObject);
  74. procedure FormCreate(Sender: TObject);
  75. procedure ibdsMainAfterOpen(DataSet: TDataSet);
  76. procedure ibdsMainCalcAggregates(DataSet: TDataSet;
  77. var Accept: Boolean);
  78. procedure actSaveGridSettingUpdate(Sender: TObject);
  79. procedure actClearGridSettingUpdate(Sender: TObject);
  80. private
  81. FEntryDateIsFirst: Boolean;
  82. FEntryDateInFields: Boolean;
  83. FSortFieldIndex: Integer;
  84. FSaldoValueList: Tgdv_ValueList;
  85. FTotals: TgdvLedgerTotals;
  86. FNeedUpdateControls: boolean;
  87. procedure OnAnalyticGroupSelect(Sender: TObject);
  88. procedure UpdateEntryDateIsFirst;
  89. protected
  90. function GetGdvObject: TgdvAcctBase; override;
  91. procedure SetParams; override;
  92. function GetSaldoBeginSQL: string;
  93. procedure FillBeginSaldoStructire;
  94. procedure CalcBeginSaldo;
  95. procedure InitColumns; override;
  96. procedure UpdateControls; override;
  97. procedure DoBeforeBuildReport; override;
  98. procedure DoLoadConfig(const Config: TBaseAcctConfig);override;
  99. procedure DoSaveConfig(Config: TBaseAcctConfig);override;
  100. procedure DoAfterBuildReport; override;
  101. class function ConfigClassName: string; override;
  102. procedure Go_to(NewWindow: Boolean = false); override;
  103. function CanGo_to: boolean; override;
  104. function CompareParams(WithDate: Boolean = True): boolean; override;
  105. public
  106. { Public declarations }
  107. procedure BuildAcctReport; override;
  108. procedure LoadSettings; override;
  109. procedure SaveSettings; override;
  110. end;
  111. var
  112. gdv_frmAcctLedger: Tgdv_frmAcctLedger;
  113. implementation
  114. uses
  115. gdv_frmAcctAccCard_Unit, IBHeader
  116. {must be placed after Windows unit!}
  117. {$IFDEF LOCALIZATION}
  118. , gd_localization_stub
  119. {$ENDIF}
  120. , gd_KeyAssoc;
  121. {$R *.DFM}
  122. { Tgdv_frmAcctLedger }
  123. procedure Tgdv_frmAcctLedger.FormCreate(Sender: TObject);
  124. begin
  125. FTotals := TgdvLedgerTotals.Create;
  126. TgdvAcctLedger(gdvObject).Totals := FTotals;
  127. FNeedUpdateControls:= False;
  128. inherited;
  129. FNeedUpdateControls:= True;
  130. UpdateControls;
  131. frAcctAnalyticsGroup.OnSelect := OnAnalyticGroupSelect;
  132. OnAnalyticGroupSelect(frAcctAnalyticsGroup);
  133. end;
  134. procedure Tgdv_frmAcctLedger.DoBeforeBuildReport;
  135. var
  136. I, Index: Integer;
  137. FieldName: string;
  138. begin
  139. inherited;
  140. FTotals.Clear;
  141. UpdateEntryDateIsFirst;
  142. FSortFieldIndex := - 1;
  143. //???? ???? ?????? ?????????, ?? ????? ?? ?????????? ? ????????? ?????? ??
  144. //????????????
  145. if FEntryDateInFields then
  146. begin
  147. for I := 0 to BaseAcctFieldCount - 1 do
  148. begin
  149. if not (I in [baNCU_Debit_Index, baNCU_Credit_Index, baCurr_Debit_Index,
  150. baCurr_Credit_Index]) then
  151. begin
  152. FieldName := BaseAcctFieldList[I].FieldName;
  153. Index := FFieldInfos.IndexByFieldName(FieldName);
  154. if Index >= - 1 then
  155. begin
  156. FFieldInfos[Index].Total := False;
  157. end;
  158. end;
  159. end;
  160. end;
  161. end;
  162. procedure Tgdv_frmAcctLedger.LoadSettings;
  163. {@UNFOLD MACRO INH_CRFORM_PARAMS(VAR)}
  164. {M}VAR
  165. {M} Params, LResult: Variant;
  166. {M} tmpStrings: TStackStrings;
  167. {END MACRO}
  168. var
  169. ComponentPath: string;
  170. begin
  171. {@UNFOLD MACRO INH_CRFORM_WITHOUTPARAMS('TGDV_FRMACCTLEDGER', 'LOADSETTINGS', KEYLOADSETTINGS)}
  172. {M} try
  173. {M} if Assigned(gdcMethodControl) and Assigned(ClassMethodAssoc) then
  174. {M} begin
  175. {M} SetFirstMethodAssoc('TGDV_FRMACCTLEDGER', KEYLOADSETTINGS);
  176. {M} tmpStrings := TStackStrings(ClassMethodAssoc.IntByKey[KEYLOADSETTINGS]);
  177. {M} if (tmpStrings = nil) or (tmpStrings.IndexOf('TGDV_FRMACCTLEDGER') = -1) then
  178. {M} begin
  179. {M} Params := VarArrayOf([GetGdcInterface(Self)]);
  180. {M} if gdcMethodControl.ExecuteMethodNew(ClassMethodAssoc, Self, 'TGDV_FRMACCTLEDGER',
  181. {M} 'LOADSETTINGS', KEYLOADSETTINGS, Params, LResult) then exit;
  182. {M} end else
  183. {M} if tmpStrings.LastClass.gdClassName <> 'TGDV_FRMACCTLEDGER' then
  184. {M} begin
  185. {M} Inherited;
  186. {M} Exit;
  187. {M} end;
  188. {M} end;
  189. {END MACRO}
  190. inherited;
  191. if UserStorage <> nil then
  192. begin
  193. ComponentPath := BuildComponentPath(Self);
  194. frAcctAnalyticsGroup.ppMain.Unwraped := UserStorage.ReadBoolean(ComponentPath, 'AnalyticGroupUnwraped', True);
  195. frAcctTreeAnalytic.ppMain.Unwraped := UserStorage.ReadBoolean(ComponentPath, 'TreeAnalyticUnwraped', True);
  196. // Panel2.Height := UserStorage.ReadInteger(BuildComponentPath(Self), 'PanelHeight', 159);
  197. end;
  198. {@UNFOLD MACRO INH_CRFORM_FINALLY('TGDV_FRMACCTLEDGER', 'LOADSETTINGS', KEYLOADSETTINGS)}
  199. {M}finally
  200. {M} if Assigned(gdcMethodControl) and Assigned(ClassMethodAssoc) then
  201. {M} ClearMacrosStack('TGDV_FRMACCTLEDGER', 'LOADSETTINGS', KEYLOADSETTINGS);
  202. {M}end;
  203. {END MACRO}
  204. end;
  205. procedure Tgdv_frmAcctLedger.SaveSettings;
  206. {@UNFOLD MACRO INH_CRFORM_PARAMS(VAR)}
  207. {M}VAR
  208. {M} Params, LResult: Variant;
  209. {M} tmpStrings: TStackStrings;
  210. {END MACRO}
  211. var
  212. ComponentPath: string;
  213. begin
  214. {@UNFOLD MACRO INH_CRFORM_WITHOUTPARAMS('TGDV_FRMACCTLEDGER', 'SAVESETTINGS', KEYSAVESETTINGS)}
  215. {M} try
  216. {M} if Assigned(gdcMethodControl) and Assigned(ClassMethodAssoc) then
  217. {M} begin
  218. {M} SetFirstMethodAssoc('TGDV_FRMACCTLEDGER', KEYSAVESETTINGS);
  219. {M} tmpStrings := TStackStrings(ClassMethodAssoc.IntByKey[KEYSAVESETTINGS]);
  220. {M} if (tmpStrings = nil) or (tmpStrings.IndexOf('TGDV_FRMACCTLEDGER') = -1) then
  221. {M} begin
  222. {M} Params := VarArrayOf([GetGdcInterface(Self)]);
  223. {M} if gdcMethodControl.ExecuteMethodNew(ClassMethodAssoc, Self, 'TGDV_FRMACCTLEDGER',
  224. {M} 'SAVESETTINGS', KEYSAVESETTINGS, Params, LResult) then exit;
  225. {M} end else
  226. {M} if tmpStrings.LastClass.gdClassName <> 'TGDV_FRMACCTLEDGER' then
  227. {M} begin
  228. {M} Inherited;
  229. {M} Exit;
  230. {M} end;
  231. {M} end;
  232. {END MACRO}
  233. inherited;
  234. if UserStorage <> nil then
  235. begin
  236. ComponentPath := BuildComponentPath(Self);
  237. UserStorage.WriteBoolean(ComponentPath, 'AnalyticGroupUnwraped', frAcctAnalyticsGroup.ppMain.Unwraped);
  238. UserStorage.WriteBoolean(ComponentPath, 'TreeAnalyticUnwraped', frAcctTreeAnalytic.ppMain.Unwraped);
  239. // UserStorage.WriteInteger(BuildComponentPath(Self), 'PanelHeight', Panel2.Height);
  240. end;
  241. {@UNFOLD MACRO INH_CRFORM_FINALLY('TGDV_FRMACCTLEDGER', 'SAVESETTINGS', KEYSAVESETTINGS)}
  242. {M}finally
  243. {M} if Assigned(gdcMethodControl) and Assigned(ClassMethodAssoc) then
  244. {M} ClearMacrosStack('TGDV_FRMACCTLEDGER', 'SAVESETTINGS', KEYSAVESETTINGS);
  245. {M}end;
  246. {END MACRO}
  247. end;
  248. procedure Tgdv_frmAcctLedger.UpdateControls;
  249. begin
  250. if not FNeedUpdateControls then Exit;
  251. inherited;
  252. frAcctAnalyticsGroup.UpdateAnalyticsList(FAccountIDs);
  253. end;
  254. procedure Tgdv_frmAcctLedger.DoLoadConfig(const Config: TBaseAcctConfig);
  255. var
  256. C: TAccLedgerConfig;
  257. begin
  258. inherited;
  259. if Config is TAccLedgerConfig then
  260. begin
  261. C := Config as TAccLedgerConfig;
  262. cbShowDebit.Checked := C.ShowDebit;
  263. cbShowCredit.Checked := C.ShowCredit;
  264. cbShowCorrSubAccount.Checked := C.ShowCorrSubAccounts;
  265. C.AnalyticsGroup.Position := 0;
  266. frAcctAnalyticsGroup.UpdateAnalyticsList(FAccountIDs);
  267. frAcctAnalyticsGroup.LoadFromStream(C.AnalyticsGroup);
  268. frAcctAnalyticsGroup.AnalyticListFields := C.AnalyticListField;
  269. cbSumNull.Checked := C.SumNull;
  270. cbEnchancedSaldo.Checked := C.EnchancedSaldo;
  271. frAcctTreeAnalytic.TreeAnalitic := C.TreeAnalytic;
  272. end;
  273. end;
  274. procedure Tgdv_frmAcctLedger.DoSaveConfig(Config: TBaseAcctConfig);
  275. var
  276. C: TAccLedgerConfig;
  277. begin
  278. inherited;
  279. if Config is TAccLedgerConfig then
  280. begin
  281. C := Config as TAccLedgerConfig;
  282. C.ShowDebit := cbShowDebit.Checked;
  283. C.ShowCredit := cbShowCredit.Checked;
  284. C.ShowCorrSubAccounts := cbShowCorrSubAccount.Checked;
  285. C.AnalyticsGroup.Size := 0;
  286. frAcctAnalyticsGroup.SaveToStream(C.AnalyticsGroup);
  287. C.AnalyticListField := frAcctAnalyticsGroup.AnalyticListFields;
  288. C.SumNull := cbSumNull.Checked;
  289. C.EnchancedSaldo := cbEnchancedSaldo.Checked;
  290. C.TreeAnalytic := frAcctTreeAnalytic.TreeAnalitic;
  291. end;
  292. end;
  293. procedure Tgdv_frmAcctLedger.DoAfterBuildReport;
  294. begin
  295. if cbShowDebit.Checked or cbShowCredit.Checked then
  296. begin
  297. InitColumns;
  298. ibgrMain.ResizeColumns;
  299. ibgrMain.Columns.EndUpdate;
  300. end else
  301. inherited;
  302. end;
  303. class function Tgdv_frmAcctLedger.ConfigClassName: string;
  304. begin
  305. Result := 'TAccLedgerConfig'
  306. end;
  307. procedure Tgdv_frmAcctLedger.InitColumns;
  308. var
  309. I, J: Integer;
  310. FI: TgdvFieldInfo;
  311. DisplayFields: string;
  312. FIndex, DIndex: Integer;
  313. C: TgsColumn;
  314. P: Integer;
  315. begin
  316. inherited;
  317. if (FFieldInfos <> nil) and (ibgrMain.Conditions.Count > 0) then
  318. begin
  319. for I := 0 to ibgrMain.Conditions.Count - 1 do
  320. begin
  321. C := FindColumn(UpperCase(ibgrMain.Conditions[I].FieldName));
  322. if (C <> nil) and (C.Field <> nil) then
  323. begin
  324. FIndex := C.Field.Index;
  325. DisplayFields := ibgrMain.Conditions[I].DisplayFields;
  326. for J := 0 to FFieldInfos.Count - 1 do
  327. begin
  328. FI := FFieldInfos[J];
  329. if FI.Condition then
  330. begin
  331. P := Pos(FI.FieldName + ';', DisplayFields);
  332. if P > 0 then
  333. Delete(DisplayFields, P, Length(FI.FieldName + ';'));
  334. C := FindColumn(FI.FieldName);
  335. if (C <> nil) and (C.Field <> nil) then
  336. begin
  337. DIndex := C.Field.Index;
  338. if DIndex >= FIndex then
  339. begin
  340. if (DisplayFields > '') and (DisplayFields[Length(DisplayFields)] <> ';') then
  341. DisplayFields := DisplayFields + ';';
  342. DisplayFields := DisplayFields + FI.FieldName + ';';
  343. end;
  344. end;
  345. end;
  346. end;
  347. ibgrMain.Conditions[I].DisplayFields := DisplayFields;
  348. end;
  349. end;
  350. end;
  351. end;
  352. procedure Tgdv_frmAcctLedger.Go_to(NewWindow: Boolean = false);
  353. var
  354. F: TField;
  355. C: TAccCardConfig;
  356. FI: TgdvFieldInfo;
  357. A: String;
  358. I, iTmp: Integer;
  359. wY, wQ, wM, wBY, wBM, wBD, wEY, wEM, wED, wQBM, WQEM: word;
  360. FieldName, sDName, sMName, sQName, sYName, sTmp: string;
  361. Form: TCreateableForm;
  362. dtBegin, dtEnd: TDateTime;
  363. function GetMonthLastDay(const AMonth, AYear: word): word;
  364. begin
  365. Result:= 28;
  366. case AMonth of
  367. 1, 3, 5, 7, 8, 10, 12:
  368. Result:= 31;
  369. 4, 6, 9, 11:
  370. Result:= 30;
  371. 2:
  372. if IsLeapYear(AYear) then
  373. Result:= 29
  374. else
  375. Result:= 28;
  376. end;
  377. end;
  378. procedure GetQuarterMonths(const AQuarter: word; var ABegin, AEnd: word);
  379. begin
  380. case AQuarter of
  381. 1:begin
  382. ABegin:= 1;
  383. AEnd:= 3;
  384. end;
  385. 2:begin
  386. ABegin:= 4;
  387. AEnd:= 6;
  388. end;
  389. 3:begin
  390. ABegin:= 7;
  391. AEnd:= 9;
  392. end;
  393. 4:begin
  394. ABegin:= 10;
  395. AEnd:= 12;
  396. end;
  397. end;
  398. end;
  399. begin
  400. Form := gd_createable_form.FindForm(Tgdv_frmAcctAccCard);
  401. F := ibgrMain.SelectedField;
  402. if F <> nil then
  403. begin
  404. C := TAccCardConfig.Create;
  405. try
  406. DoSaveConfig(C);
  407. // ???????????????? - ???? ??????????? ? Tgdv_frmAcctBaseForm.DoSaveConfig
  408. //C.CompanyKey := frAcctCompany.iblCompany.CurrentKeyInt;
  409. //C.AllHoldingCompanies := frAcctCompany.cbAllCompanies.Checked;
  410. C.IncCorrSubAccounts := False;
  411. C.CorrAccounts := '';
  412. FI := FFieldInfos.FindInfo(F.FieldName);
  413. if (FI <> nil) and (FI is TgdvLedgerFieldInfo) then
  414. begin
  415. C.CorrAccounts := GetAlias(TgdvLedgerFieldInfo(FI).AccountKey);
  416. C.AccountPart := TgdvLedgerFieldInfo(FI).AccountPart;
  417. end;
  418. iTmp:= 0;
  419. A := '';
  420. sDName:= ''; sMName:= ''; sQName:= ''; sYName:= '';
  421. for I := 0 to frAcctAnalyticsGroup.Selected.Count - 1 do
  422. begin
  423. if frAcctAnalyticsGroup.Selected[I].FieldName = ENTRYDATE then begin
  424. iTmp:= 1;
  425. sDName:= 'NAME' + IntToStr(I);
  426. end
  427. else if frAcctAnalyticsGroup.Selected[I].FieldName = MONTH then begin
  428. iTmp:= iTmp + 2;
  429. sMName:= 'NAME' + IntToStr(I);
  430. end
  431. else if frAcctAnalyticsGroup.Selected[I].FieldName = 'QUARTER' then begin
  432. iTmp:= iTmp + 4;
  433. sQName:= 'NAME' + IntToStr(I);
  434. end
  435. else if frAcctAnalyticsGroup.Selected[I].FieldName = 'YEAR' then begin
  436. iTmp:= iTmp + 8;
  437. sYName:= 'NAME' + IntToStr(I);
  438. end;
  439. if (frAcctAnalyticsGroup.Selected[I].Field <> nil) and
  440. (frAcctAnalyticsGroup.Selected[I].FieldName <> ENTRYDATE) then
  441. begin
  442. FieldName := Format('c%d', [I]);
  443. F := gdvObject.FindField(FieldName);
  444. if (F <> nil) and not F.IsNull then
  445. begin
  446. if A > '' then A := A + #13#10;
  447. A := A + frAcctAnalyticsGroup.Selected[I].FieldName + '=' + Trim(F.AsString);
  448. end;
  449. end;
  450. end;
  451. dtBegin:= DateBegin;
  452. dtEnd:= DateEnd;
  453. case iTmp of
  454. 1, 3, 5, 7, 9, 11, 13, 15:begin // EntryDate ? ?? ????? ??? ???
  455. sTmp:= gdvObject.FieldByName(sDName).AsString;
  456. dtBegin:= EncodeDate(StrToInt(Copy(sTmp, 1, 4)), StrToInt(Copy(sTmp, 6, 2)), StrToInt(Copy(sTmp, 9, 2)));
  457. dtEnd:= dtBegin;
  458. end;
  459. 2, 6:begin // ?????? ?????
  460. wM:= StrToInt(gdvObject.FieldByName(sMName).AsString);
  461. DecodeDate(dtBegin, wBY, wBM, wBD);
  462. DecodeDate(dtEnd, wEY, wEM, wED);
  463. if wBY = wEY then begin
  464. if wBM = wM then
  465. dtBegin:= EncodeDate(wBY, wM, wBD)
  466. else
  467. dtBegin:= EncodeDate(wBY, wM, 1);
  468. if wEM = wM then
  469. dtEnd:= EncodeDate(wBY, wM, wED)
  470. else
  471. dtEnd:= EncodeDate(wBY, wM, GetMonthLastDay(wM, wBY));
  472. end
  473. else if (wEY - wBY = 1) and (wBM > wEM) then begin
  474. if wM >= wBM then begin
  475. if wM = wBM then
  476. dtBegin:= EncodeDate(wBY, wM, wBD)
  477. else
  478. dtBegin:= EncodeDate(wBY, wM, 1);
  479. dtEnd:= EncodeDate(wBY, wM, GetMonthLastDay(wM, wBY))
  480. end
  481. else begin
  482. dtBegin:= EncodeDate(wEY, wM, 1);
  483. if wM = wEM then
  484. dtEnd:= EncodeDate(wEY, wM, wED)
  485. else
  486. dtEnd:= EncodeDate(wEY, wM, GetMonthLastDay(wM, wEY));
  487. end;
  488. end
  489. else if (wEY - wBY = 2) and (wM > wEM) and (wM < wBM) then begin
  490. dtBegin:= EncodeDate(wBY + 1, wM, 1);
  491. dtEnd:= EncodeDate(wBY + 1, wM, GetMonthLastDay(wM, wBY + 1));
  492. end;
  493. end;
  494. 4:begin // ?????? ???????
  495. wQ:= StrToInt(gdvObject.FieldByName(sQName).AsString);
  496. DecodeDate(dtBegin, wBY, wBM, wBD);
  497. DecodeDate(dtEnd, wEY, wEM, wED);
  498. GetQuarterMonths(wQ, wQBM, wQEM);
  499. if wBY = wEY then begin
  500. if (wQBM <= wBM) and (wQEM >= wEM) then begin
  501. dtBegin:= EncodeDate(wBY, wBM, wBD);
  502. dtEnd:= EncodeDate(wBY, wEM, wED);
  503. end
  504. else if (wQBM <= wBM) and (wQEM >= wBM) then begin
  505. dtBegin:= EncodeDate(wBY, wBM, wBD);
  506. dtEnd:= EncodeDate(wBY, wQEM, GetMonthLastDay(wQEM, wBY));
  507. end
  508. else if (wQBM <= wEM) and (wQEM >= wEM) then begin
  509. dtBegin:= EncodeDate(wBY, wQBM, 1);
  510. dtEnd:= EncodeDate(wBY, wEM, wED);
  511. end
  512. else begin
  513. dtBegin:= EncodeDate(wBY, wQBM, 1);
  514. dtEnd:= EncodeDate(wBY, wQEM, GetMonthLastDay(wQEM, wBY));
  515. end;
  516. end
  517. else if (wEY - wBY = 1) and (wBM > wEM) then begin
  518. if (wQBM <= wBM) and (wQEM >= wBM) then begin
  519. dtBegin:= EncodeDate(wBY, wBM, wBD);
  520. dtEnd:= EncodeDate(wBY, wQEM, GetMonthLastDay(wQEM, wBY));
  521. end
  522. else if (wQBM <= wEM) and (wQEM >= wEM) then begin
  523. dtBegin:= EncodeDate(wEY, wQBM, 1);
  524. dtEnd:= EncodeDate(wEY, wEM, wED);
  525. end
  526. else begin
  527. if wBM < wQBM then begin
  528. dtBegin:= EncodeDate(wBY, wQBM, 1);
  529. dtEnd:= EncodeDate(wBY, wQEM, GetMonthLastDay(wQEM, wBY));
  530. end
  531. else begin
  532. dtBegin:= EncodeDate(wEY, wQBM, 1);
  533. dtEnd:= EncodeDate(wEY, wQEM, GetMonthLastDay(wQEM, wEY));
  534. end;
  535. end;
  536. end
  537. else if (wEY - wBY = 2) and (wQBM > wEM) and (wQEM < wBM) then begin
  538. dtBegin:= EncodeDate(wBY + 1, wQBM, 1);
  539. dtEnd:= EncodeDate(wBY + 1, wQEM, GetMonthLastDay(wQEM, wBY + 1));
  540. end;
  541. end;
  542. 8:begin // ?????? ???
  543. wY:= StrToInt(gdvObject.FieldByName(sYName).AsString);
  544. DecodeDate(dtBegin, wBY, wBM, wBD);
  545. DecodeDate(dtEnd, wEY, wEM, wED);
  546. if wY = wBY then
  547. dtBegin:= EncodeDate(wY, wBM, wBD)
  548. else
  549. dtBegin:= EncodeDate(wY, 1, 1);
  550. if wY = wEY then
  551. dtEnd:= EncodeDate(wY, wEM, wED)
  552. else
  553. dtEnd:= EncodeDate(wY, 12, 31);
  554. end;
  555. 10, 14:begin // ????? ? ???
  556. wY:= StrToInt(gdvObject.FieldByName(sYName).AsString);
  557. wM:= StrToInt(gdvObject.FieldByName(sMName).AsString);
  558. DecodeDate(dtBegin, wBY, wBM, wBD);
  559. DecodeDate(dtEnd, wEY, wEM, wED);
  560. if (wBY = wY) and (wBM = wM) then
  561. dtBegin:= EncodeDate(wY, wM, wBD)
  562. else
  563. dtBegin:= EncodeDate(wY, wM, 1);
  564. if (wEY = wY) and (wEM = wM) then
  565. dtEnd:= EncodeDate(wY, wM, wED)
  566. else
  567. dtEnd:= EncodeDate(wY, wM, GetMonthLastDay(wM, wY));
  568. end;
  569. 12:begin // ??? ? ???????
  570. wQ:= StrToInt(gdvObject.FieldByName(sQName).AsString);
  571. wY:= StrToInt(gdvObject.FieldByName(sYName).AsString);
  572. DecodeDate(dtBegin, wBY, wBM, wBD);
  573. DecodeDate(dtEnd, wEY, wEM, wED);
  574. GetQuarterMonths(wQ, wQBM, wQEM);
  575. if (wEY = wBY) and (wQBM <= wBM) and (wQEM >= wEM) then begin
  576. dtBegin:= EncodeDate(wY, wBM, wBD);
  577. dtEnd:= EncodeDate(wY, wEM, wED);
  578. end
  579. else if (wY = wBY) and (wQBM <= wBM) and (wQEM >= wBM) then begin
  580. dtBegin:= EncodeDate(wY, wBM, wBD);
  581. dtEnd:= EncodeDate(wY, wQEM, GetMonthLastDay(wQEM, wY));
  582. end
  583. else if (wY = wEY) and (wQBM <= wEM) and (wQEM >= wEM) then begin
  584. dtBegin:= EncodeDate(wY, wQBM, 1);
  585. dtEnd:= EncodeDate(wY, wEM, wED);
  586. end
  587. else begin
  588. dtBegin:= EncodeDate(wY, wQBM, 1);
  589. dtEnd:= EncodeDate(wY, wQEM, GetMonthLastDay(wQEM, wY));
  590. end;
  591. end;
  592. end;
  593. // ??????? ??????????? ?? ?????????? ?? "??????????? ?? ??????????"
  594. if A > '' then
  595. begin
  596. if C.Analytics > '' then
  597. C.Analytics := C.Analytics + #13#10 + A
  598. else
  599. C.Analytics := A;
  600. end;
  601. if not NewWindow or (Form = nil) then
  602. begin
  603. with Tgdv_frmAcctAccCard(Tgdv_frmAcctAccCard.CreateAndAssign(Application)) do
  604. begin
  605. DateBegin := dtBegin;
  606. DateEnd := dtEnd;
  607. Show;
  608. Execute(C);
  609. end;
  610. end else
  611. begin
  612. with Tgdv_frmAcctAccCard(Tgdv_frmAcctAccCard.Create(Application)) do
  613. begin
  614. DateBegin := dtBegin;
  615. DateEnd := dtEnd;
  616. Show;
  617. Execute(C);
  618. end;
  619. end;
  620. finally
  621. C.Free;
  622. end;
  623. end;
  624. end;
  625. procedure Tgdv_frmAcctLedger.BuildAcctReport;
  626. begin
  627. if (frAcctAnalyticsGroup.Selected.Count = 0) and not gdvObject.MakeEmpty then
  628. begin
  629. MessageBox(0,
  630. PChar(MSG_INPUTANGROUPANALYTIC),
  631. '????????',
  632. MB_OK or MB_ICONEXCLAMATION or MB_TASKMODAL);
  633. if not actShowParamPanel.Checked then
  634. actShowParamPanel.Execute;
  635. end
  636. else
  637. inherited;
  638. end;
  639. function Tgdv_frmAcctLedger.GetSaldoBeginSQL: string;
  640. var
  641. I: Integer;
  642. DebitCreditSQL: string;
  643. F: TatRelationField;
  644. SelectClause, FromClause, FromClause1, GroupClause, OrderClause: string;
  645. IDSelect, NameSelect, WhereClause, QuantityGroup: String;
  646. Alias, Name: string;
  647. CurrId, HavingClause: string;
  648. NcuDecDig, CurrDecDig: String;
  649. AnalyticFilter: string;
  650. K: Integer;
  651. ValueAlias, QuantityAlias: String;
  652. Strings: TgdvCorrFieldInfoList;
  653. SortName, SortSelect: string;
  654. VKeyAlias: string;
  655. NcuBegin, CurrBegin: string;
  656. begin
  657. Result := '';
  658. Strings := TgdvCorrFieldInfoList.Create;
  659. try
  660. DebitCreditSQL := '';
  661. FromClause := '';
  662. FromClause1 := '';
  663. GroupClause := '';
  664. OrderClause := '';
  665. IDSelect := '';
  666. NameSelect := '';
  667. WhereClause := '';
  668. frAcctAnalytics.Alias := 'e';
  669. AnalyticFilter := frAcctAnalytics.Condition;
  670. if AnalyticFilter > '' then
  671. AnalyticFilter := ' AND '#13#10 + AnalyticFilter + #13#10;
  672. if frAcctSum.InCurr and (frAcctSum.CurrKey > 0) then
  673. CurrId := Format(' AND e.currkey = %d'#13#10, [frAcctSum.CurrKey])
  674. else
  675. CurrId := '';
  676. NcuDecDig := Format('NUMERIC(15, %d)', [frAcctSum.NcuDecDigits]);
  677. CurrDecDig := Format('NUMERIC(15, %d)', [frAcctSum.CurrDecDigits]);
  678. SelectClause := '';
  679. for I := 0 to frAcctAnalyticsGroup.Selected.Count - 1 do
  680. begin
  681. F := frAcctAnalyticsGroup.Selected[I].Field;
  682. if (F = nil) or (F.FieldName = ENTRYDATE) then Break;
  683. Alias := Format('c%d', [I]);
  684. Name := Format('NAME%d', [I]);
  685. SortName := Format('s%d', [I]);
  686. //GetDebitSumSelectClause;
  687. //GetCreditSumSelectClause;
  688. if IDSelect > '' then IDSelect := IDSelect + ', '#13#10;
  689. if F <> nil then
  690. begin
  691. if F.ReferencesField <> nil then
  692. IDSelect := IDSelect + Format(' SUBSTRING(%s.%s from 1 for 180) AS %s', [Alias,
  693. F.ReferencesField.FieldName, Alias])
  694. else
  695. begin
  696. IDSelect := IDSelect + Format(' SUBSTRING(e.%s from 1 for 180) AS %s', [F.FieldName, Alias]);
  697. end;
  698. end else
  699. begin
  700. IDSelect := IDSelect + Format(' SUBSTRING(g_d_getdateparam(e.entrydate, %s) from 1 for 180)AS %s',
  701. [frAcctAnalyticsGroup.Selected[I].Additional, Alias])
  702. end;
  703. if NameSelect > '' then NameSelect := NameSelect + ', '#13#10;
  704. if F <> nil then
  705. begin
  706. if F.ReferencesField <> nil then
  707. begin
  708. if F.Field.RefListFieldName = '' then
  709. NameSelect := NameSelect + Format(' SUBSTRING(%s.%s from 1 for 180) AS %s', [Alias,
  710. F.References.ListField.FieldName, Name])
  711. else
  712. NameSelect := NameSelect + Format(' SUBSTRING(%s.%s from 1 for 180) AS %s', [Alias,
  713. F.Field.RefListFieldName, Name]);
  714. end else
  715. begin
  716. NameSelect := NameSelect + Format(' SUBSTRING(e.%s from 1 for 180) AS %s',
  717. [F.FieldName, Name]);
  718. end;
  719. end else
  720. begin
  721. NameSelect := NameSelect + Format(' SUBSTRING(g_d_getdateparam(e.entrydate, %s) from 1 for 180)AS %s',
  722. [frAcctAnalyticsGroup.Selected[I].Additional, Name])
  723. end;
  724. if SortSelect > '' then SortSelect := SortSelect + ', '#13#10;
  725. if F <> nil then
  726. begin
  727. if F.ReferencesField <> nil then
  728. begin
  729. if F.Field.RefListFieldName = '' then
  730. SortSelect := SortSelect + Format(' SUBSTRING(%s.%s from 1 for 180) AS %s', [Alias,
  731. F.References.ListField.FieldName, SortName])
  732. else
  733. SortSelect := SortSelect + Format(' SUBSTRING(%s.%s from 1 for 180) AS %s', [Alias,
  734. F.Field.RefListFieldName, SortName])
  735. end else
  736. begin
  737. SortSelect := SortSelect + Format(' SUBSTRING(e.%s from 1 for 180) AS %s',
  738. [F.FieldName, SortName]);
  739. end
  740. end else
  741. begin
  742. SortSelect := SortSelect + Format(' SUBSTRING(g_d_getdateparam(e.entrydate, %s) from 1 for 180)AS %s',
  743. [frAcctAnalyticsGroup.Selected[I].Additional, SortName])
  744. end;
  745. SelectClause := SortSelect + ', '#13#10 + IDSelect + ', '#13#10 + NameSelect;
  746. if (F <> nil) and (F.ReferencesField <> nil) then
  747. begin
  748. FromClause := FromClause + Format(' LEFT JOIN %s %s ON %s.%s = e.%s'#13#10,
  749. [F.References.RelationName, Alias, Alias, F.ReferencesField.FieldName,
  750. F.FieldName]);
  751. end;
  752. if GroupClause > '' then GroupClause := GroupClause + ', ';
  753. if F <> nil then
  754. begin
  755. if F.ReferencesField <> nil then
  756. begin
  757. if F.Field.RefListFieldName = '' then
  758. GroupClause := GroupClause + Format('%s.%s, %s.%s', [Alias,
  759. F.References.ListField.FieldName, Alias, F.ReferencesField.FieldName])
  760. else
  761. GroupClause := GroupClause + Format('%s.%s, %s.%s', [Alias,
  762. F.Field.RefListFieldName, Alias, F.ReferencesField.FieldName]);
  763. end else
  764. begin
  765. GroupClause := GroupClause + Format('e.%s', [F.FieldName])
  766. end;
  767. end else
  768. begin
  769. GroupClause := GroupClause + Format('g_d_getdateparam(e.entrydate, %s)',
  770. [frAcctAnalyticsGroup.Selected[I].Additional])
  771. end;
  772. //****
  773. if OrderClause > '' then OrderClause := OrderClause + ', ';
  774. if F <> nil then
  775. begin
  776. if F.ReferencesField <> nil then
  777. begin
  778. if F.Field.RefListFieldName = '' then
  779. OrderClause := OrderClause + Format('%s.%s, %s.%s', [Alias,
  780. F.References.ListField.FieldName, Alias, F.ReferencesField.FieldName])
  781. else
  782. OrderClause := OrderClause + Format('%s.%s, %s.%s', [Alias,
  783. F.Field.RefListFieldName, Alias, F.ReferencesField.FieldName])
  784. end else
  785. begin
  786. OrderClause := OrderClause + Format('e.%s', [F.FieldName])
  787. end;
  788. end else
  789. begin
  790. OrderClause := OrderClause + Format('g_d_getdateparam(e.entrydate, %s)',
  791. [frAcctAnalyticsGroup.Selected[I].Additional])
  792. end;
  793. //****
  794. QuantityGroup := '';
  795. if FValueList.Count > 0 then
  796. begin
  797. for K := 0 to FValueList.Count - 1 do
  798. begin
  799. VKeyAlias := gdvObject.GetKeyAlias(FValueList.Names[K]);
  800. ValueAlias := 'v_' + gdvObject.GetKeyAlias(FValueList.Names[K]);
  801. QuantityAlias := 'q_' + gdvObject.GetKeyAlias(FValueList.Names[K]);
  802. if not FEntryDateIsFirst then
  803. begin
  804. SelectClause := SelectClause + ','#13#10 +
  805. Format(' SUM(IIF(e.accountpart = ''D'', %s.quantity, 0)) - '#13#10 +
  806. ' SUM(IIF(e.accountpart = ''C'', %s.quantity, 0)) AS Q_B_S_%s'#13#10,
  807. [QuantityAlias, QuantityAlias, VKeyAlias]);
  808. if I = 0 then
  809. begin
  810. FromClause := FromClause + #13#10 +
  811. Format(' LEFT JOIN ac_quantity %s ON %s.entrykey = e.id AND '#13#10 +
  812. ' %s.valuekey = %s ', [QuantityAlias, QuantityAlias,
  813. QuantityAlias, FValueList.Names[K]]);
  814. end;
  815. end;
  816. end;
  817. end;
  818. end;
  819. if not FEntryDateIsFirst then
  820. begin
  821. NcuBegin :=
  822. Format(' CAST(SUM(e.debitncu - e.creditncu) / %d AS %s) AS NCU_BEGIN_SALDO '#13#10,
  823. [frAcctSum.NcuScale, NcuDecDig]);
  824. if frAcctSum.InCurr then
  825. begin
  826. CurrBegin :=
  827. Format(' CAST(SUM(e.debitcurr - e.creditcurr) / %d AS %s) AS CURR_BEGIN_SALDO '#13#10,
  828. [frAcctSum.CurrScale, CurrDecDig]);
  829. end else
  830. begin
  831. CurrBegin :=
  832. Format(' CAST(0 AS %s) AS CURR_BEGIN_SALDO '#13#10, [CurrDecDig]);
  833. end;
  834. HavingClause := {GetHavingClause}'';
  835. if HavingClause > '' then HavingClause := HavingClause + ' OR '#13#10 ;
  836. HavingClause := HavingClause + ' SUM(e.debitncu - e.creditncu) <> 0 '#13#10 ;
  837. if frAcctSum.InCurr then
  838. begin
  839. HavingClause := HavingClause +
  840. ' OR SUM(e.debitcurr) <> 0 OR SUM(e.creditcurr) <> 0 ';
  841. end;
  842. HavingClause := 'HAVING ' + HavingClause;
  843. DebitCreditSQL := Format(cBeginSaldoSQLTemplate, [SelectClause, NcuBegin,
  844. CurrBegin, FromClause, Format('e.accountkey IN(%s) AND ', [IDList(FAccountIDs)]), frAcctCompany.CompanyList,
  845. CurrId, gdvObject.InternalMovementClause + AnalyticFilter, GroupClause, HavingClause]);
  846. end;
  847. DebitCreditSQL := DebitCreditSQL + #13#10'ORDER BY ' + OrderClause;
  848. Result := DebitCreditSQL;
  849. finally
  850. Strings.Free;
  851. end;
  852. end;
  853. procedure Tgdv_frmAcctLedger.FillBeginSaldoStructire;
  854. var
  855. SQL: TIBSQL;
  856. Name: string;
  857. Index: Integer;
  858. I, J: Integer;
  859. F: TatRelationField;
  860. V: Tgdv_Value;
  861. VKeyAlias: string;
  862. begin
  863. if FEntryDateInFields and not FEntryDateIsFirst then
  864. begin
  865. if FSaldoValueList = nil then
  866. FSaldoValueList := Tgdv_ValueList.Create
  867. else
  868. FSaldoValueList.Clear;
  869. SQL := TIBSQL.Create(nil);
  870. try
  871. SQL.Transaction := gdcBaseManager.ReadTransaction;
  872. SQL.SQL.Text := GetSaldoBeginSQL;
  873. SQL.ParamByName('begindate').AsDateTime := Self.DateBegin;
  874. SQL.ExecQuery;
  875. while not SQL.Eof do
  876. begin
  877. V := nil;
  878. for I := 0 to frAcctAnalyticsGroup.Selected.Count - 1 do
  879. begin
  880. F := frAcctAnalyticsGroup.Selected[I].Field;
  881. if (F = nil) or (F.FieldName = ENTRYDATE) then Break;
  882. Name := Format('c%d', [I]);
  883. if V = nil then
  884. begin
  885. Index := FSaldoValueList.IndexOf(SQL.FieldByName(Name).AsString);
  886. if Index = - 1 then
  887. begin
  888. Index := FSaldoValueList.Add2(SQL.FieldByName(Name).AsString);
  889. end;
  890. V := FSaldoValueList.Items[Index];
  891. end else
  892. begin
  893. Index := V.Values.IndexOf(SQL.FieldByName(Name).AsString);
  894. if Index = - 1 then
  895. begin
  896. Index := V.Values.Add2(SQL.FieldByName(Name).AsString);
  897. end;
  898. V := V.Values.Items[Index];
  899. end;
  900. end;
  901. if V <> nil then
  902. begin
  903. V.SaldoValues.AddSaldoValue('NCU_BEGIN_SALDO',
  904. SQL.FieldByName('NCU_BEGIN_SALDO').AsCurrency);
  905. V.SaldoValues.AddSaldoValue('CURR_BEGIN_SALDO',
  906. SQL.FieldByName('CURR_BEGIN_SALDO').AsCurrency);
  907. for J := 0 to FValueList.Count - 1 do
  908. begin
  909. VKeyAlias := gdvObject.GetKeyAlias(FValueList.Names[J]);
  910. Name := Format('Q_B_S_%s', [VKeyAlias]);
  911. V.SaldoValues.AddSaldoValue(Name, SQL.FieldByName(Name).AsCurrency);
  912. end;
  913. end;
  914. SQL.Next;
  915. end;
  916. finally
  917. SQL.Free;
  918. end;
  919. end;
  920. end;
  921. procedure Tgdv_frmAcctLedger.CalcBeginSaldo;
  922. var
  923. I, J: Integer;
  924. V: Tgdv_Value;
  925. F: TatRelationField;
  926. Name, VKeyAlias: string;
  927. Index: Integer;
  928. C: Currency;
  929. Values, OldValues: TStringList;
  930. B: Boolean;
  931. SV: Tgdv_SaldoValues;
  932. procedure SetSaldo(SV: Tgdv_SaldoValues);
  933. var
  934. Index, J: Integer;
  935. C: Currency;
  936. begin
  937. Index := SV.IndexOf('NCU_BEGIN_SALDO');
  938. if Index > - 1 then
  939. begin
  940. C := SV[Index].SaldoValue;
  941. if C > 0 then
  942. begin
  943. gdvObject.FieldByName('NCU_BEGIN_DEBIT').AsCurrency := C;
  944. gdvObject.FieldByName('NCU_BEGIN_CREDIT').AsCurrency := 0;
  945. end else
  946. begin
  947. gdvObject.FieldByName('NCU_BEGIN_DEBIT').AsCurrency := 0;
  948. gdvObject.FieldByName('NCU_BEGIN_CREDIT').AsCurrency := - C;
  949. end;
  950. end;
  951. Index := SV.IndexOf('CURR_BEGIN_SALDO');
  952. if Index > - 1 then
  953. begin
  954. C := SV[Index].SaldoValue;
  955. if C > 0 then
  956. begin
  957. gdvObject.FieldByName('CURR_BEGIN_DEBIT').AsCurrency := C;
  958. gdvObject.FieldByName('CURR_BEGIN_CREDIT').AsCurrency := 0;
  959. end else
  960. begin
  961. gdvObject.FieldByName('CURR_BEGIN_DEBIT').AsCurrency := 0;
  962. gdvObject.FieldByName('CURR_BEGIN_CREDIT').AsCurrency := - C;
  963. end;
  964. end;
  965. for J := 0 to FValueList.Count - 1 do
  966. begin
  967. VKeyAlias := gdvObject.GetKeyAlias(FValueList.Names[J]);
  968. Name := Format('Q_B_S_%s', [VKeyAlias]);
  969. Index := SV.IndexOf(Name);
  970. if Index > - 1 then
  971. begin
  972. C := SV[Index].SaldoValue;
  973. if C > 0 then
  974. begin
  975. gdvObject.FieldByName(Format('Q_B_D_%s', [VKeyAlias])).AsCurrency := C;
  976. gdvObject.FieldByName(Format('Q_B_C_%s', [VKeyAlias])).AsCurrency := 0;
  977. end else
  978. begin
  979. gdvObject.FieldByName(Format('Q_B_D_%s', [VKeyAlias])).AsCurrency := 0;
  980. gdvObject.FieldByName(Format('Q_B_C_%s', [VKeyAlias])).AsCurrency := - C;
  981. end;
  982. end;
  983. end;
  984. end;
  985. function Changed: boolean;
  986. var
  987. J: Integer;
  988. begin
  989. Result := False;
  990. //????????? ?????????? ?? ???????? ????????
  991. for J := 0 to FTotals.Count - 1 do
  992. begin
  993. F := FTotals[J].atRelationField;
  994. if (F = nil) or (F.FieldName = ENTRYDATE) then Break;
  995. Result := FTotals[J].Field.AsString <> Values[J];
  996. end;
  997. end;
  998. function Get_gdvValue: Tgdv_Value;
  999. var
  1000. I, Index: Integer;
  1001. Name: string;
  1002. begin
  1003. Result := nil;
  1004. for I := 0 to FTotals.Count - 1 do
  1005. begin
  1006. F := FTotals[I].atRelationField;
  1007. if (F = nil) or (F.FieldName = ENTRYDATE) then Break;
  1008. Name := Format('c%d', [I]);
  1009. if Result = nil then
  1010. begin
  1011. Index := FSaldoValueList.IndexOf(gdvObject.FieldByName(Name).AsString);
  1012. if Index = - 1 then
  1013. begin
  1014. Result := nil;
  1015. Break;
  1016. end;
  1017. Result := FSaldoValueList.Items[Index];
  1018. end else
  1019. begin
  1020. Index := Result.Values.IndexOf(gdvObject.FieldByName(Name).AsString);
  1021. if Index = - 1 then
  1022. begin
  1023. Result := nil;
  1024. Break;
  1025. end;
  1026. Result := Result.Values.Items[Index];
  1027. end;
  1028. end;
  1029. end;
  1030. begin
  1031. gdvObject.DisableControls;
  1032. try
  1033. gdvObject.First;
  1034. Values := TStringList.Create;
  1035. OldValues := TStringList.Create;
  1036. try
  1037. for I := 0 to FTotals.Count - 1 do
  1038. begin
  1039. F := FTotals[I].atRelationField;
  1040. if (F = nil) or (F.FieldName = ENTRYDATE) then Break;
  1041. Values.Add('####2@##!@#@#$#$'{FTotals[I].Field.AsString});
  1042. end;
  1043. SV := Tgdv_SaldoValues.Create;
  1044. try
  1045. SV.AddSaldoValue('NCU_BEGIN_SALDO', 0);
  1046. SV.AddSaldoValue('CURR_BEGIN_SALDO', 0);
  1047. for J := 0 to FValueList.Count - 1 do
  1048. begin
  1049. VKeyAlias := gdvObject.GetKeyAlias(FValueList.Names[J]);
  1050. Name := Format('Q_B_S_%s', [VKeyAlias]);
  1051. SV.AddSaldoValue(Name, 0);
  1052. end;
  1053. while not gdvObject.Eof do
  1054. begin
  1055. gdvObject.Edit;
  1056. //????????? ?????????? ?? ???????? ????????
  1057. B := Changed;
  1058. if B then
  1059. begin
  1060. //?????????? ????? ???????? ?????????
  1061. for J := 0 to FTotals.Count - 1 do
  1062. begin
  1063. F := FTotals[J].atRelationField;
  1064. if (F = nil) or (F.FieldName = ENTRYDATE) then Break;
  1065. Values[J] := FTotals[J].Field.AsString;
  1066. end;
  1067. V := Get_gdvValue;
  1068. if V <> nil then
  1069. begin
  1070. //??????????? ???????? ?????????? ??????
  1071. SetSaldo(V.SaldoValues);
  1072. end else
  1073. begin
  1074. gdvObject.FieldByName('NCU_BEGIN_DEBIT').AsCurrency := 0;
  1075. gdvObject.FieldByName('NCU_BEGIN_CREDIT').AsCurrency := 0;
  1076. gdvObject.FieldByName('CURR_BEGIN_DEBIT').AsCurrency := 0;
  1077. gdvObject.FieldByName('CURR_BEGIN_CREDIT').AsCurrency := 0;
  1078. for J := 0 to FValueList.Count - 1 do
  1079. begin
  1080. VKeyAlias := gdvObject.GetKeyAlias(FValueList.Names[J]);
  1081. gdvObject.FieldByName(Format('Q_B_D_%s', [VKeyAlias])).AsCurrency := 0;
  1082. gdvObject.FieldByName(Format('Q_B_C_%s', [VKeyAlias])).AsCurrency := 0;
  1083. end;
  1084. end;
  1085. end else
  1086. begin
  1087. SetSaldo(SV);
  1088. end;
  1089. //????????? ???????? ??????
  1090. C := gdvObject.FieldByName('NCU_BEGIN_DEBIT').AsCurrency -
  1091. gdvObject.FieldByName('NCU_BEGIN_CREDIT').AsCurrency +
  1092. gdvObject.FieldByName('NCU_DEBIT').AsCurrency -
  1093. gdvObject.FieldByName('NCU_CREDIT').AsCurrency;
  1094. if C > 0 then
  1095. begin
  1096. gdvObject.FieldByName('NCU_END_DEBIT').AsCurrency := C;
  1097. gdvObject.FieldByName('NCU_END_CREDIT').AsCurrency := 0;
  1098. end else
  1099. begin
  1100. gdvObject.FieldByName('NCU_END_DEBIT').AsCurrency := 0;
  1101. gdvObject.FieldByName('NCU_END_CREDIT').AsCurrency := - C;
  1102. end;
  1103. Index := SV.IndexOf('NCU_BEGIN_SALDO');
  1104. SV[Index].SaldoValue := C;
  1105. C := gdvObject.FieldByName('CURR_BEGIN_DEBIT').AsCurrency -
  1106. gdvObject.FieldByName('CURR_BEGIN_CREDIT').AsCurrency +
  1107. gdvObject.FieldByName('CURR_DEBIT').AsCurrency -
  1108. gdvObject.FieldByName('CURR_CREDIT').AsCurrency;
  1109. if C > 0 then
  1110. begin
  1111. gdvObject.FieldByName('CURR_END_DEBIT').AsCurrency := C;
  1112. gdvObject.FieldByName('CURR_END_CREDIT').AsCurrency := 0;
  1113. end else
  1114. begin
  1115. gdvObject.FieldByName('CURR_END_DEBIT').AsCurrency := 0;
  1116. gdvObject.FieldByName('CURR_END_CREDIT').AsCurrency := - C;
  1117. end;
  1118. Index := SV.IndexOf('CURR_BEGIN_SALDO');
  1119. SV[Index].SaldoValue := C;
  1120. for J := 0 to FValueList.Count - 1 do
  1121. begin
  1122. VKeyAlias := gdvObject.GetKeyAlias(FValueList.Names[J]);
  1123. C := gdvObject.FieldByName(Format('Q_B_D_%s', [VKeyAlias])).AsCurrency -
  1124. gdvObject.FieldByName(Format('Q_B_C_%s', [VKeyAlias])).AsCurrency +
  1125. gdvObject.FieldByName(Format('Q_D_%s', [VKeyAlias])).AsCurrency -
  1126. gdvObject.FieldByName(Format('Q_C_%s', [VKeyAlias])).AsCurrency;
  1127. if C > 0 then
  1128. begin
  1129. gdvObject.FieldByName(Format('Q_E_D_%s', [VKeyAlias])).AsCurrency := C;
  1130. gdvObject.FieldByName(Format('Q_E_C_%s', [VKeyAlias])).AsCurrency := 0;
  1131. end else
  1132. begin
  1133. gdvObject.FieldByName(Format('Q_E_D_%s', [VKeyAlias])).AsCurrency := 0;
  1134. gdvObject.FieldByName(Format('Q_E_C_%s', [VKeyAlias])).AsCurrency := - C;
  1135. end;
  1136. Index := SV.IndexOf(Format('Q_B_S_%s', [VKeyAlias]));
  1137. SV[Index].SaldoValue := C;
  1138. end;
  1139. gdvObject.Post;
  1140. gdvObject.Next;
  1141. end;
  1142. finally
  1143. SV.Free;
  1144. end;
  1145. finally
  1146. Values.Free;
  1147. OldValues.Free;
  1148. end;
  1149. finally
  1150. gdvObject.EnableControls;
  1151. end;
  1152. end;
  1153. procedure Tgdv_frmAcctLedger.OnAnalyticGroupSelect(Sender: TObject);
  1154. begin
  1155. frAcctTreeAnalytic.UpdateAnalyticsList(frAcctAnalyticsGroup.Selected);
  1156. end;
  1157. function Tgdv_frmAcctLedger.CanGo_to: boolean;
  1158. const
  1159. cTotal = '?????:';
  1160. var
  1161. i: integer;
  1162. begin
  1163. if gdvObject.Active then
  1164. begin
  1165. Result:= True;
  1166. for i := 0 to ibgrMain.Columns.Count - 1 do
  1167. if Assigned(gdvObject.FindField(ibgrMain.Columns[i].FieldName))
  1168. and (gdvObject.FieldByName(ibgrMain.Columns[i].FieldName).AsString = cTotal) then
  1169. begin
  1170. Result:= False;
  1171. Exit;
  1172. end;
  1173. end
  1174. else
  1175. Result := False;
  1176. end;
  1177. function Tgdv_frmAcctLedger.CompareParams(WithDate: Boolean = True): Boolean;
  1178. var
  1179. Stream: TMemoryStream;
  1180. begin
  1181. Result := inherited CompareParams(WithDate)
  1182. and ((FConfig as TAccLedgerConfig).ShowDebit = cbShowDebit.Checked)
  1183. and ((FConfig as TAccLedgerConfig).ShowCredit = cbShowCredit.Checked)
  1184. and ((FConfig as TAccLedgerConfig).ShowCorrSubAccounts = cbShowCorrSubAccount.Checked)
  1185. and ((FConfig as TAccLedgerConfig).AnalyticListField = frAcctAnalyticsGroup.AnalyticListFields)
  1186. and ((FConfig as TAccLedgerConfig).SumNull = cbSumNull.Checked)
  1187. and ((FConfig as TAccLedgerConfig).EnchancedSaldo = cbEnchancedSaldo.Checked)
  1188. and ((FConfig as TAccLedgerConfig).TreeAnalytic = frAcctTreeAnalytic.TreeAnalitic);
  1189. if Result then
  1190. begin
  1191. Stream := TMemoryStream.Create;
  1192. try
  1193. Stream.Size := 0;
  1194. frAcctAnalyticsGroup.SaveToStream(Stream);
  1195. Result := ((FConfig as TAccLedgerConfig).AnalyticsGroup.Size = Stream.Size)
  1196. and (CompareMem(((FConfig as TAccLedgerConfig).AnalyticsGroup as TMemoryStream).Memory, Stream.Memory, Stream.Size));
  1197. finally
  1198. FreeAndNil(Stream);
  1199. end;
  1200. end;
  1201. end;
  1202. procedure Tgdv_frmAcctLedger.actSaveConfigUpdate(Sender: TObject);
  1203. begin
  1204. TAction(Sender).Enabled := frAcctAnalyticsGroup.Selected.Count > 0;
  1205. end;
  1206. procedure Tgdv_frmAcctLedger.FormDestroy(Sender: TObject);
  1207. begin
  1208. inherited;
  1209. FTotals.Free;
  1210. FSaldoValueList.Free;
  1211. end;
  1212. procedure Tgdv_frmAcctLedger.actRunUpdate(Sender: TObject);
  1213. begin
  1214. inherited;
  1215. UpdateEntryDateIsFirst;
  1216. cbEnchancedSaldo.Enabled := not FEntryDateIsFirst;
  1217. end;
  1218. procedure Tgdv_frmAcctLedger.UpdateEntryDateIsFirst;
  1219. var
  1220. I: Integer;
  1221. begin
  1222. FEntryDateIsFirst := False;
  1223. FEntryDateInFields := False;
  1224. for I := 0 to frAcctAnalyticsGroup.Selected.Count - 1 do
  1225. begin
  1226. FEntryDateInFields := (frAcctAnalyticsGroup.Selected[I].FieldName = ENTRYDATE) or
  1227. (not Assigned(frAcctAnalyticsGroup.Selected[I].Field));
  1228. if FEntryDateInFields then
  1229. begin
  1230. FEntryDateIsFirst := I = 0;
  1231. Break;
  1232. end;
  1233. end;
  1234. end;
  1235. procedure Tgdv_frmAcctLedger.SetParams;
  1236. var
  1237. I: Integer;
  1238. begin
  1239. inherited;
  1240. if not gdvObject.MakeEmpty then
  1241. begin
  1242. for I := 0 to frAcctAnalyticsGroup.Selected.Count - 1 do
  1243. TgdvAcctLedger(gdvObject).AddGroupBy(frAcctAnalyticsGroup.Selected[I]);
  1244. for I := 0 to frAcctTreeAnalytic.Count - 1 do
  1245. TgdvAcctLedger(gdvObject).AddAnalyticLevel(frAcctTreeAnalytic.Lines[I].Field.FieldName, frAcctTreeAnalytic.Lines[I].Levels.Text);
  1246. TgdvAcctLedger(gdvObject).ShowDebit := cbShowDebit.Checked;
  1247. TgdvAcctLedger(gdvObject).ShowCredit := cbShowCredit.Checked;
  1248. TgdvAcctLedger(gdvObject).ShowCorrSubAccounts := cbShowCorrSubAccount.Checked;
  1249. TgdvAcctLedger(gdvObject).EnchancedSaldo := cbEnchancedSaldo.Checked;
  1250. TgdvAcctLedger(gdvObject).SumNull := cbSumNull.Checked;
  1251. end;
  1252. end;
  1253. function Tgdv_frmAcctLedger.GetGdvObject: TgdvAcctBase;
  1254. begin
  1255. Result := ibdsMain;
  1256. end;
  1257. { Tgdv_ValueList }
  1258. function Tgdv_ValueList.Add(Value: string): Tgdv_Value;
  1259. begin
  1260. Result := Tgdv_Value.Create;
  1261. Result.Value := Value;
  1262. if FValues = nil then
  1263. FValues := TStringList.Create;
  1264. FValues.AddObject(Value, Result);
  1265. end;
  1266. function Tgdv_ValueList.Add2(Value: string): Integer;
  1267. var
  1268. V: Tgdv_Value;
  1269. begin
  1270. V := Tgdv_Value.Create;
  1271. V.Value := Value;
  1272. if FValues = nil then
  1273. FValues := TStringList.Create;
  1274. Result := FValues.AddObject(Value, V);
  1275. end;
  1276. procedure Tgdv_ValueList.Clear;
  1277. var
  1278. I: Integer;
  1279. begin
  1280. if FValues <> nil then
  1281. begin
  1282. for I := 0 to FValues.Count - 1 do
  1283. begin
  1284. Tgdv_Value(FValues.Objects[I]).Free;
  1285. FValues.Objects[I] := nil;
  1286. end;
  1287. FValues.Clear;
  1288. end;
  1289. end;
  1290. destructor Tgdv_ValueList.Destroy;
  1291. begin
  1292. Clear;
  1293. inherited;
  1294. end;
  1295. function Tgdv_ValueList.GetItems(Index: Integer): Tgdv_Value;
  1296. begin
  1297. if FValues = nil then
  1298. FValues := TStringList.Create;
  1299. Result := Tgdv_Value(FValues.Objects[Index]);
  1300. end;
  1301. function Tgdv_ValueList.IndexOf(Value: string): Integer;
  1302. begin
  1303. Result := -1 ;
  1304. if FValues <> nil then
  1305. begin
  1306. FValues.Sorted := True;
  1307. Result := FValues.IndexOf(Value);
  1308. end;
  1309. end;
  1310. { Tgdv_Value }
  1311. destructor Tgdv_Value.Destroy;
  1312. begin
  1313. FValues.Free;
  1314. FSaldoValues.Free;
  1315. inherited;
  1316. end;
  1317. function Tgdv_Value.GetSaldoValues: Tgdv_SaldoValues;
  1318. begin
  1319. if FSaldoValues = nil then
  1320. FSaldoValues := Tgdv_SaldoValues.Create;
  1321. Result := FSaldoValues;
  1322. end;
  1323. function Tgdv_Value.GetValues: Tgdv_ValueList;
  1324. begin
  1325. if FValues = nil then
  1326. FValues := Tgdv_ValueList.Create;
  1327. Result := FValues;
  1328. end;
  1329. procedure Tgdv_Value.SetValue(const Value: string);
  1330. begin
  1331. FValue := Value;
  1332. end;
  1333. { Tgdv_FieldValue }
  1334. procedure Tgdv_SaldoValue.SetSaldoName(const Value: string);
  1335. begin
  1336. FSaldoName := Value;
  1337. end;
  1338. procedure Tgdv_SaldoValue.SetSaldoValue(const Value: Currency);
  1339. begin
  1340. FSaldoValue := Value;
  1341. end;
  1342. { Tgdv_FieldValues }
  1343. function Tgdv_SaldoValues.AddSaldoValue(SaldoName: string;
  1344. SaldoValue: Currency): integer;
  1345. var
  1346. V: Tgdv_SaldoValue;
  1347. begin
  1348. V := Tgdv_SaldoValue.Create;
  1349. V.SaldoName := SaldoName;
  1350. V.SaldoValue := SaldoValue;
  1351. Result := Add(V);
  1352. end;
  1353. constructor Tgdv_SaldoValues.Create;
  1354. begin
  1355. OwnsObjects := True;
  1356. end;
  1357. function Tgdv_SaldoValues.GetSaldoList(Index: Integer): Tgdv_SaldoValue;
  1358. begin
  1359. Result := Tgdv_SaldoValue(inherited Items[Index])
  1360. end;
  1361. function Tgdv_SaldoValues.IndexOf(FieldName: string): Integer;
  1362. var
  1363. I: Integer;
  1364. begin
  1365. Result := -1;
  1366. for I := 0 to Count - 1 do
  1367. begin
  1368. if SaldoList[I].SaldoName = FieldName then
  1369. begin
  1370. Result := I;
  1371. Exit;
  1372. end;
  1373. end;
  1374. end;
  1375. procedure Tgdv_frmAcctLedger.ibdsMainAfterOpen(DataSet: TDataSet);
  1376. var
  1377. Values, OldValues: TStringList;
  1378. I, J, K: Integer;
  1379. Insert: Boolean;
  1380. //?????? ???????????? ?????????
  1381. Count: Integer;
  1382. const
  1383. cTotal = '?????:'; // ???? ???????? ?????, ?? ???? ???????? ? CanGo_to
  1384. begin
  1385. inherited;
  1386. if not FMakeEmpty then
  1387. begin
  1388. gdvObject.DisableControls;
  1389. try
  1390. for I := 0 to gdvObject.Fields.Count - 1 do
  1391. gdvObject.Fields[I].Required := False;
  1392. Values := TStringList.Create;
  1393. OldValues := TStringList.Create;
  1394. try
  1395. //?????????????? ????
  1396. FTotals.InitField(gdvObject);
  1397. //?????????????? ?????? ????????
  1398. for I := 0 to FTotals.Count - 1 do
  1399. begin
  1400. Values.Add(FTotals[I].Field.AsString);
  1401. end;
  1402. if (not gdvObject.UseEntryBalance) and FEntryDateInFields and not FEntryDateIsFirst then
  1403. begin
  1404. FillBeginSaldoStructire;
  1405. CalcBeginSaldo;
  1406. end;
  1407. Count := 0;
  1408. gdvObject.First;
  1409. while not gdvObject.Eof do
  1410. begin
  1411. //????????? ?????????? ?? ???????? ????????
  1412. for I := 0 to FTotals.Count - 1 do
  1413. begin
  1414. Insert := FTotals[i].Field.AsString <> Values[I];
  1415. if Insert then
  1416. begin
  1417. //????????? ?????? ????????
  1418. OldValues.Assign(Values);
  1419. //?????????? ????? ???????? ?????????
  1420. for J := 0 to FTotals.Count - 1 do
  1421. begin
  1422. Values[J] := FTotals[J].Field.AsString;
  1423. end;
  1424. for J := FTotals.Count - 2 downto I do
  1425. begin
  1426. if (cbSumNull.Checked or (OldValues[J] > '')) and FTotals[J].Total then
  1427. begin
  1428. gdvObject.Insert;
  1429. FTotals[J].ValueField.AsString := cTotal;
  1430. FTotals[J].SetValues;
  1431. for K := 0 to I do
  1432. begin
  1433. FTotals[K].Field.AsString := OldValues[K];
  1434. end;
  1435. gdvObject.FieldByName('sortfield').AsInteger := J + 2;
  1436. gdvObject.Post;
  1437. gdvObject.Next;
  1438. end;
  1439. FTotals[J].DropValues;
  1440. end;
  1441. Break;
  1442. end;
  1443. end;
  1444. FTotals.Calc;
  1445. gdvObject.Next;
  1446. Inc(Count);
  1447. end;
  1448. if Count > 1 then
  1449. begin
  1450. for J := FTotals.Count - 2 downto 0 do
  1451. begin
  1452. if (cbSumNull.Checked or (OldValues[J] > '')) and FTotals[J].Total then
  1453. begin
  1454. gdvObject.Append;
  1455. FTotals[J].ValueField.AsString := cTotal;
  1456. FTotals[J].SetValues;
  1457. gdvObject.Post;
  1458. gdvObject.Next;
  1459. end;
  1460. FTotals[J].DropValues;
  1461. end;
  1462. end;
  1463. finally
  1464. Values.Free;
  1465. OldValues.Free;
  1466. end;
  1467. finally
  1468. gdvObject.First;
  1469. gdvObject.EnableControls;
  1470. end;
  1471. end;
  1472. end;
  1473. procedure Tgdv_frmAcctLedger.ibdsMainCalcAggregates(DataSet: TDataSet;
  1474. var Accept: Boolean);
  1475. var
  1476. F: TField;
  1477. begin
  1478. inherited;
  1479. if FSortFieldIndex = - 1 then
  1480. begin
  1481. F := gdvObject.FindField('sortfield');
  1482. if F <> nil then
  1483. FSortFieldIndex := F.Index;
  1484. end;
  1485. Accept := (FSortFieldIndex > -1) and (gdvObject.Fields[FSortFieldIndex].AsInteger = 1);
  1486. end;
  1487. procedure Tgdv_frmAcctLedger.actSaveGridSettingUpdate(Sender: TObject);
  1488. begin
  1489. TAction(Sender).Enabled := (iblConfiguratior.CurrentKey > '') and (not cbShowDebit.Checked) and (not cbShowCredit.Checked);
  1490. FSaveGridSetting := (iblConfiguratior.CurrentKey > '') and (not cbShowDebit.Checked) and (not cbShowCredit.Checked);
  1491. end;
  1492. procedure Tgdv_frmAcctLedger.actClearGridSettingUpdate(Sender: TObject);
  1493. begin
  1494. TAction(Sender).Enabled := iblConfiguratior.CurrentKey > '';
  1495. end;
  1496. initialization
  1497. RegisterFrmClass(Tgdv_frmAcctLedger);
  1498. finalization
  1499. UnRegisterFrmClass(Tgdv_frmAcctLedger);
  1500. end.