1 | unit UCore;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, ExtCtrls, Controls, ActnList, Menus, UFioAPI,
|
---|
9 | URegistry, UApplicationInfo, UCoolTranslator, Registry, DateUtils, Forms,
|
---|
10 | Dialogs, SpecializedList, dom, XMLRead, XMLWrite;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TAccount }
|
---|
15 |
|
---|
16 | TAccount = class
|
---|
17 | Token: string;
|
---|
18 | Name: string;
|
---|
19 | Number: string;
|
---|
20 | Balance: Double;
|
---|
21 | Time: TDateTime;
|
---|
22 | procedure Assign(Source: TAccount);
|
---|
23 | procedure LoadFromRegistry(Context: TRegistryContext);
|
---|
24 | procedure SaveToRegistry(Context: TRegistryContext);
|
---|
25 | end;
|
---|
26 |
|
---|
27 | { TAccountList }
|
---|
28 |
|
---|
29 | TAccountList = class(TListObject)
|
---|
30 | procedure LoadFromRegistry(Context: TRegistryContext);
|
---|
31 | procedure SaveToRegistry(Context: TRegistryContext);
|
---|
32 | procedure LoadToStrings(Strings: TStrings);
|
---|
33 | procedure Assign(Source: TAccountList);
|
---|
34 | end;
|
---|
35 |
|
---|
36 | { TCore }
|
---|
37 |
|
---|
38 | TCore = class(TDataModule)
|
---|
39 | AAbout: TAction;
|
---|
40 | AAccounts: TAction;
|
---|
41 | ActionList1: TActionList;
|
---|
42 | ADownloadInterval: TAction;
|
---|
43 | ADownloadMonthly: TAction;
|
---|
44 | ADownloadNew: TAction;
|
---|
45 | AExit: TAction;
|
---|
46 | ApplicationInfo1: TApplicationInfo;
|
---|
47 | ASettings: TAction;
|
---|
48 | AShow: TAction;
|
---|
49 | CoolTranslator1: TCoolTranslator;
|
---|
50 | ImageList1: TImageList;
|
---|
51 | MenuItem1: TMenuItem;
|
---|
52 | MenuItem2: TMenuItem;
|
---|
53 | MenuItem3: TMenuItem;
|
---|
54 | MenuItem4: TMenuItem;
|
---|
55 | MenuItem5: TMenuItem;
|
---|
56 | MenuItem6: TMenuItem;
|
---|
57 | MenuItem7: TMenuItem;
|
---|
58 | MenuItem8: TMenuItem;
|
---|
59 | MenuItem9: TMenuItem;
|
---|
60 | PopupMenuTray: TPopupMenu;
|
---|
61 | TrayIcon1: TTrayIcon;
|
---|
62 | procedure AAccountsExecute(Sender: TObject);
|
---|
63 | procedure ADownloadIntervalExecute(Sender: TObject);
|
---|
64 | procedure ADownloadMonthlyExecute(Sender: TObject);
|
---|
65 | procedure ADownloadNewExecute(Sender: TObject);
|
---|
66 | procedure AExitExecute(Sender: TObject);
|
---|
67 | procedure ASettingsExecute(Sender: TObject);
|
---|
68 | procedure AShowExecute(Sender: TObject);
|
---|
69 | procedure AAboutExecute(Sender: TObject);
|
---|
70 | procedure DataModuleCreate(Sender: TObject);
|
---|
71 | procedure DataModuleDestroy(Sender: TObject);
|
---|
72 | private
|
---|
73 | function GetFileName: string;
|
---|
74 | public
|
---|
75 | RegistryContext: TRegistryContext;
|
---|
76 | Accounts: TAccountList;
|
---|
77 | CurrentAccount: TAccount;
|
---|
78 | DataFormat: TFioDataFormat;
|
---|
79 | TargetDirectory: string;
|
---|
80 | ReportYear: Integer;
|
---|
81 | ReportId: Integer;
|
---|
82 | ReportTimeFrom: TDateTime;
|
---|
83 | ReportTimeTo: TDateTime;
|
---|
84 | OutputFormat: string;
|
---|
85 | SelectedAccountIndex: Integer;
|
---|
86 | procedure LoadAccount(Account: TAccount);
|
---|
87 | procedure LoadAccounts;
|
---|
88 | procedure SaveToRegistry(Context: TRegistryContext);
|
---|
89 | procedure LoadFromRegistry(Context: TRegistryContext);
|
---|
90 | end;
|
---|
91 |
|
---|
92 | var
|
---|
93 | Core: TCore;
|
---|
94 |
|
---|
95 | implementation
|
---|
96 |
|
---|
97 | {$R *.lfm}
|
---|
98 |
|
---|
99 | uses
|
---|
100 | UFormAbout, UFormMain, UFormSettings, UFormAccounts;
|
---|
101 |
|
---|
102 | resourcestring
|
---|
103 | SDownloadNotSuccess = 'Download failed';
|
---|
104 | SReport = 'Dump %s';
|
---|
105 | SSavedToFile = 'Dump saved to file %s';
|
---|
106 | SDumpFormat = 'Dump %d %t.%f';
|
---|
107 |
|
---|
108 | { TAccount }
|
---|
109 |
|
---|
110 | procedure TAccount.Assign(Source: TAccount);
|
---|
111 | begin
|
---|
112 | Token := Source.Token;
|
---|
113 | Name := Source.Name;
|
---|
114 | Balance := Source.Balance;
|
---|
115 | Time := Source.Time;
|
---|
116 | Number := Source.Number;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | procedure TAccount.LoadFromRegistry(Context: TRegistryContext);
|
---|
120 | begin
|
---|
121 | with TRegistryEx.Create do
|
---|
122 | try
|
---|
123 | RootKey := Context.RootKey;
|
---|
124 | OpenKey(Context.Key, True);
|
---|
125 | Name := UTF8Encode(ReadStringWithDefault('Name', UTF8Decode('UÄet')));
|
---|
126 | Time := ReadDateTimeWithDefault('Time', 0);
|
---|
127 | Token := UTF8Encode(ReadStringWithDefault('Token', ''));
|
---|
128 | Balance := ReadFloatWithDefault('Balance', 0);
|
---|
129 | Number := UTF8Encode(ReadStringWithDefault('Number', ''));
|
---|
130 | finally
|
---|
131 | Free;
|
---|
132 | end;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | procedure TAccount.SaveToRegistry(Context: TRegistryContext);
|
---|
136 | begin
|
---|
137 | with TRegistryEx.Create do
|
---|
138 | try
|
---|
139 | RootKey := Context.RootKey;
|
---|
140 | OpenKey(Context.Key, True);
|
---|
141 | WriteString('Number', UTF8Decode(Number));
|
---|
142 | WriteString('Name', UTF8Decode(Name));
|
---|
143 | WriteDateTime('Time', Time);
|
---|
144 | WriteString('Token', UTF8Decode(Token));
|
---|
145 | WriteFloat('Balance', Balance);
|
---|
146 | finally
|
---|
147 | Free;
|
---|
148 | end;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | { TAccountList }
|
---|
152 |
|
---|
153 | procedure TAccountList.LoadFromRegistry(Context: TRegistryContext);
|
---|
154 | var
|
---|
155 | I: Integer;
|
---|
156 | begin
|
---|
157 | with TRegistryEx.Create do
|
---|
158 | try
|
---|
159 | RootKey := Context.RootKey;
|
---|
160 | OpenKey(Context.Key, True);
|
---|
161 | Count := ReadIntegerWithDefault('Count', 0);
|
---|
162 | finally
|
---|
163 | Free;
|
---|
164 | end;
|
---|
165 | for I := 0 to Count - 1 do begin
|
---|
166 | if not Assigned(Items[I]) then Items[I] := TAccount.Create;
|
---|
167 | TAccount(Items[I]).LoadFromRegistry(RegContext(Context.RootKey, Context.Key + '\' + IntToStr(I)));
|
---|
168 | end;
|
---|
169 | end;
|
---|
170 |
|
---|
171 | procedure TAccountList.SaveToRegistry(Context: TRegistryContext);
|
---|
172 | var
|
---|
173 | I: Integer;
|
---|
174 | begin
|
---|
175 | with TRegistryEx.Create do
|
---|
176 | try
|
---|
177 | RootKey := Context.RootKey;
|
---|
178 | OpenKey(Context.Key, True);
|
---|
179 | WriteInteger('Count', Count);
|
---|
180 | finally
|
---|
181 | Free;
|
---|
182 | end;
|
---|
183 | for I := 0 to Count - 1 do
|
---|
184 | TAccount(Items[I]).SaveToRegistry(RegContext(Context.RootKey, Context.Key + '\' + IntToStr(I)));
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TAccountList.LoadToStrings(Strings: TStrings);
|
---|
188 | var
|
---|
189 | I: Integer;
|
---|
190 | begin
|
---|
191 | while Strings.Count < Count do
|
---|
192 | Strings.Add('');
|
---|
193 | while Strings.Count > Count do
|
---|
194 | Strings.Delete(Strings.Count - 1);
|
---|
195 | for I := 0 to Count - 1 do begin
|
---|
196 | Strings.Strings[I] := TAccount(Items[I]).Name + ' (' + TAccount(Items[I]).Number + ')';
|
---|
197 | Strings.Objects[I] := Items[I];
|
---|
198 | end;
|
---|
199 | end;
|
---|
200 |
|
---|
201 | procedure TAccountList.Assign(Source: TAccountList);
|
---|
202 | var
|
---|
203 | I: Integer;
|
---|
204 | begin
|
---|
205 | //inherited Assign(Source);
|
---|
206 | Count := Source.Count;
|
---|
207 | for I := 0 to Count - 1 do begin
|
---|
208 | if not Assigned(Items[I]) then Items[I] := TAccount.Create;
|
---|
209 | TAccount(Items[I]).Assign(TAccount(Source.Items[I]));
|
---|
210 | end;
|
---|
211 | end;
|
---|
212 |
|
---|
213 | { TCore }
|
---|
214 |
|
---|
215 | procedure TCore.AShowExecute(Sender: TObject);
|
---|
216 | begin
|
---|
217 | Application.Restore;
|
---|
218 | end;
|
---|
219 |
|
---|
220 | procedure TCore.DataModuleCreate(Sender: TObject);
|
---|
221 | begin
|
---|
222 | Accounts := TAccountList.Create;
|
---|
223 | RegistryContext := RegContext(HKEY(ApplicationInfo1.RegistryRoot),
|
---|
224 | ApplicationInfo1.RegistryKey);
|
---|
225 | LoadFromRegistry(RegistryContext);
|
---|
226 | end;
|
---|
227 |
|
---|
228 | procedure TCore.DataModuleDestroy(Sender: TObject);
|
---|
229 | begin
|
---|
230 | SaveToRegistry(RegistryContext);
|
---|
231 | FreeAndNil(Accounts);
|
---|
232 | end;
|
---|
233 |
|
---|
234 | function TCore.GetFileName: string;
|
---|
235 | begin
|
---|
236 | Result := OutputFormat;
|
---|
237 | Result := StringReplace(Result, '%f', DataFormatURL[DataFormat], [rfReplaceAll]);
|
---|
238 | Result := StringReplace(Result, '%d', DateToStr(Now), [rfReplaceAll]);
|
---|
239 | Result := StringReplace(Result, '%t', TimeToStr(Now), [rfReplaceAll]);
|
---|
240 | Result := StringReplace(Result, '%a', CurrentAccount.Number, [rfReplaceAll]);
|
---|
241 | Result := StringReplace(Result, '%n', CurrentAccount.Name, [rfReplaceAll]);
|
---|
242 | Result := StringReplace(Result, '%%', '%', [rfReplaceAll]);
|
---|
243 | end;
|
---|
244 |
|
---|
245 | procedure TCore.LoadAccount(Account: TAccount);
|
---|
246 | var
|
---|
247 | FioAPI: TFioAPI;
|
---|
248 | List: TStringList;
|
---|
249 | FileName: string;
|
---|
250 | XMLDocument: TXMLDocument;
|
---|
251 | Mem: TMemoryStream;
|
---|
252 | RootNode: TDOMNode;
|
---|
253 | Node: TDOMNode;
|
---|
254 | Node2: TDOMNode;
|
---|
255 | begin
|
---|
256 | FioAPI := TFioAPI.Create;
|
---|
257 | List := TStringList.Create;
|
---|
258 | XMLDocument := TXMLDocument.Create;
|
---|
259 | Mem := TMemoryStream.Create;
|
---|
260 | try
|
---|
261 | FioAPI.Format := dfXML;
|
---|
262 | FioAPI.Token := Account.Token;
|
---|
263 | Account.Time := Now;
|
---|
264 | if FioAPI.DownloadInterval(Now, Now, List) then begin
|
---|
265 | List.SaveToStream(Mem);
|
---|
266 | Mem.Position := 0;
|
---|
267 | ReadXMLFile(XMLDocument, Mem);
|
---|
268 | RootNode := XMLDocument.DocumentElement;
|
---|
269 | Node := RootNode.FindNode('Info');
|
---|
270 | Node2 := Node.FindNode('accountId');
|
---|
271 | if Assigned(Node2) then
|
---|
272 | Account.Number := UTF8Encode(Node2.TextContent);
|
---|
273 | Node2 := Node.FindNode('bankId');
|
---|
274 | if Assigned(Node2) then
|
---|
275 | Account.Number := Account.Number + '/' + UTF8Encode(Node2.TextContent);
|
---|
276 | Node2 := Node.FindNode('closingBalance');
|
---|
277 | if Assigned(Node2) then
|
---|
278 | Account.Balance := StrToFloat(StringReplace(Node2.TextContent, '.',
|
---|
279 | DefaultFormatSettings.DecimalSeparator, [rfReplaceAll]));
|
---|
280 | end else begin
|
---|
281 | Account.Number := '';
|
---|
282 | Account.Balance := 0;
|
---|
283 | end;
|
---|
284 | finally
|
---|
285 | Mem.Free;
|
---|
286 | XMLDocument.Free;
|
---|
287 | FioAPI.Free;
|
---|
288 | List.Free;
|
---|
289 | end;
|
---|
290 | end;
|
---|
291 |
|
---|
292 | procedure TCore.LoadAccounts;
|
---|
293 | var
|
---|
294 | I: Integer;
|
---|
295 | begin
|
---|
296 | for I := 0 to Accounts.Count - 1 do
|
---|
297 | LoadAccount(TAccount(Accounts[I]));
|
---|
298 | end;
|
---|
299 |
|
---|
300 | procedure TCore.AAboutExecute(Sender: TObject);
|
---|
301 | begin
|
---|
302 | FormAbout.ApplicationInfo := ApplicationInfo1;
|
---|
303 | FormAbout.ShowModal;
|
---|
304 | end;
|
---|
305 |
|
---|
306 | procedure TCore.SaveToRegistry(Context: TRegistryContext);
|
---|
307 | begin
|
---|
308 | with TRegistryEx.Create do
|
---|
309 | try
|
---|
310 | RootKey := Context.RootKey;
|
---|
311 | OpenKey(Context.Key, True);
|
---|
312 | WriteString('TargetDir', UTF8Decode(TargetDirectory));
|
---|
313 | WriteInteger('DataFormat', Integer(DataFormat));
|
---|
314 | WriteDate('ReportTimeFrom', ReportTimeFrom);
|
---|
315 | WriteDateTime('ReportTimeTo', ReportTimeTo);
|
---|
316 | WriteInteger('ReportYear', ReportYear);
|
---|
317 | WriteInteger('ReportId', ReportId);
|
---|
318 | WriteString('Language', CoolTranslator1.Language.Code);
|
---|
319 | WriteString('FileNameFormat', UTF8Decode(OutputFormat));
|
---|
320 | WriteInteger('SelectedAccountIndex', SelectedAccountIndex);
|
---|
321 | finally
|
---|
322 | Free;
|
---|
323 | end;
|
---|
324 | Accounts.SaveToRegistry(RegContext(Context.RootKey, Context.Key + '\Account'));
|
---|
325 | end;
|
---|
326 |
|
---|
327 | procedure TCore.LoadFromRegistry(Context: TRegistryContext);
|
---|
328 | begin
|
---|
329 | with TRegistryEx.Create do
|
---|
330 | try
|
---|
331 | RootKey := Context.RootKey;
|
---|
332 | OpenKey(Context.Key, True);
|
---|
333 | TargetDirectory := UTF8Encode(ReadStringWithDefault('TargetDir', UTF8Decode(ExtractFileDir(Application.ExeName))));
|
---|
334 | DataFormat := TFioDataFormat(ReadIntegerWithDefault('DataFormat', Integer(dfXML)));
|
---|
335 | ReportTimeFrom := ReadDateTimeWithDefault('ReportTimeFrom', Now);
|
---|
336 | ReportTimeTo := ReadDateTimeWithDefault('ReportTimeTo', Now);
|
---|
337 | ReportYear := ReadIntegerWithDefault('ReportYear', YearOf(Now));
|
---|
338 | ReportId := ReadIntegerWithDefault('ReportId', MonthOf(Now));
|
---|
339 | CoolTranslator1.Language := CoolTranslator1.Languages.SearchByCode(ReadStringWithDefault('Language', 'cs'));
|
---|
340 | OutputFormat := UTF8Encode(ReadStringWithDefault('FileNameFormat', UTF8Decode(SDumpFormat)));
|
---|
341 | SelectedAccountIndex := ReadIntegerWithDefault('SelectedAccountIndex', 0);
|
---|
342 | finally
|
---|
343 | Free;
|
---|
344 | end;
|
---|
345 | Accounts.LoadFromRegistry(RegContext(Context.RootKey, Context.Key + '\Account'));
|
---|
346 | end;
|
---|
347 |
|
---|
348 | procedure TCore.AExitExecute(Sender: TObject);
|
---|
349 | begin
|
---|
350 | Application.Terminate;
|
---|
351 | end;
|
---|
352 |
|
---|
353 | procedure TCore.ASettingsExecute(Sender: TObject);
|
---|
354 | begin
|
---|
355 | FormSettings.Load;
|
---|
356 | if FormSettings.ShowModal = mrOk then begin
|
---|
357 | FormSettings.Save;
|
---|
358 | SaveToRegistry(RegistryContext);
|
---|
359 | end;
|
---|
360 | end;
|
---|
361 |
|
---|
362 | procedure TCore.ADownloadNewExecute(Sender: TObject);
|
---|
363 | var
|
---|
364 | FioAPI: TFioAPI;
|
---|
365 | List: TStringList;
|
---|
366 | FileName: string;
|
---|
367 | begin
|
---|
368 | FioAPI := TFioAPI.Create;
|
---|
369 | List := TStringList.Create;
|
---|
370 | try
|
---|
371 | FioAPI.Format := DataFormat;
|
---|
372 | FioAPI.Token := CurrentAccount.Token;
|
---|
373 | if FioAPI.DownloadLast(List) then begin
|
---|
374 | FileName := TargetDirectory + DirectorySeparator + GetFileName;
|
---|
375 | List.SaveToFile(UTF8Decode(FileName));
|
---|
376 | ShowMessage(Format(SSavedToFile, [FileName]));
|
---|
377 | end else ShowMessage(SDownloadNotSuccess);
|
---|
378 | finally
|
---|
379 | FioAPI.Free;
|
---|
380 | List.Free;
|
---|
381 | end;
|
---|
382 | end;
|
---|
383 |
|
---|
384 | procedure TCore.ADownloadMonthlyExecute(Sender: TObject);
|
---|
385 | var
|
---|
386 | FioAPI: TFioAPI;
|
---|
387 | List: TStringList;
|
---|
388 | FileName: string;
|
---|
389 | begin
|
---|
390 | FioAPI := TFioAPI.Create;
|
---|
391 | List := TStringList.Create;
|
---|
392 | try
|
---|
393 | FioAPI.Format := DataFormat;
|
---|
394 | FioAPI.Token := CurrentAccount.Token;
|
---|
395 | if FioAPI.DownloadMonthly(ReportYear, ReportId, List) then begin
|
---|
396 | FileName := TargetDirectory + DirectorySeparator + GetFileName;
|
---|
397 | List.SaveToFile(UTF8Decode(FileName));
|
---|
398 | ShowMessage(Format(SSavedToFile, [FileName]));
|
---|
399 | end else ShowMessage(SDownloadNotSuccess);
|
---|
400 | finally
|
---|
401 | FioAPI.Free;
|
---|
402 | List.Free;
|
---|
403 | end;
|
---|
404 | end;
|
---|
405 |
|
---|
406 | procedure TCore.ADownloadIntervalExecute(Sender: TObject);
|
---|
407 | var
|
---|
408 | FioAPI: TFioAPI;
|
---|
409 | List: TStringList;
|
---|
410 | FileName: string;
|
---|
411 | begin
|
---|
412 | FioAPI := TFioAPI.Create;
|
---|
413 | List := TStringList.Create;
|
---|
414 | try
|
---|
415 | FioAPI.Format := DataFormat;
|
---|
416 | FioAPI.Token := CurrentAccount.Token;
|
---|
417 | if FioAPI.DownloadInterval(ReportTimeFrom, ReportTimeTo, List) then begin
|
---|
418 | FileName := TargetDirectory + DirectorySeparator + GetFileName;
|
---|
419 | List.SaveToFile(UTF8Decode(FileName));
|
---|
420 | ShowMessage(Format(SSavedToFile, [FileName]));
|
---|
421 | end else ShowMessage(SDownloadNotSuccess);
|
---|
422 | finally
|
---|
423 | FioAPI.Free;
|
---|
424 | List.Free;
|
---|
425 | end;
|
---|
426 | end;
|
---|
427 |
|
---|
428 | procedure TCore.AAccountsExecute(Sender: TObject);
|
---|
429 | begin
|
---|
430 | TAccountList(FormAccounts.Accounts).Assign(Accounts);
|
---|
431 | if FormAccounts.ShowModal = mrOk then begin
|
---|
432 | Accounts.Assign(TAccountList(FormAccounts.Accounts));
|
---|
433 | end;
|
---|
434 | end;
|
---|
435 |
|
---|
436 | end.
|
---|
437 |
|
---|