source: trunk/UCore.pas

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