source: trunk/Modules/FinanceBankAPI/FinanceBankAPI.php@ 893

Last change on this file since 893 was 893, checked in by chronos, 4 years ago
  • Modified: More work on modules models initialization.
File size: 5.1 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/FileImport.php');
4include_once(dirname(__FILE__).'/ImportPS.php');
5include_once(dirname(__FILE__).'/ImportFio.php');
6include_once(dirname(__FILE__).'/../Scheduler/Scheduler.php');
7
8class FinanceBankImport extends Model
9{
10 static function GetDesc(): ModelDesc
11 {
12 $Desc = new ModelDesc('FinanceBankImport');
13 $Desc->AddReference('BankAccount', 'BankAccount');
14 $Desc->AddDate('Time');
15 $Desc->AddString('Identification');
16 $Desc->AddString('AccountNumber');
17 $Desc->AddString('BankCode');
18 $Desc->AddString('VariableSymbol');
19 $Desc->AddString('ConstantSymbol');
20 $Desc->AddString('SpecificSymbol');
21 $Desc->AddFloat('Value');
22 $Desc->AddReference('Currency', 'Currency');
23 $Desc->AddString('OffsetAccountName');
24 $Desc->AddReference('FinanceOperation', 'FinanceOperation');
25 return $Desc;
26 }
27}
28
29class ModuleFinanceBankAPI extends AppModule
30{
31 function __construct(System $System)
32 {
33 parent::__construct($System);
34 $this->Name = 'FinanceBankAPI';
35 $this->Version = '1.0';
36 $this->Creator = 'Chronos';
37 $this->License = 'GNU/GPLv3';
38 $this->Description = 'Communication through API to various banks, manual file import';
39 $this->Dependencies = array('Finance', 'Scheduler', 'IS');
40 }
41
42 function GetModels(): array
43 {
44 return array('FinanceBankImport');
45 }
46
47 function DoStart(): void
48 {
49 $this->System->FormManager->RegisterClass('ImportBankFile', array(
50 'Title' => 'Import souborů s platbami',
51 'Table' => 'FinanceBank',
52 'SubmitText' => 'Načíst',
53 'Items' => array(
54 'BankAccount' => array('Type' => 'TBankAccount', 'Caption' => 'Bankovní účet', 'Default' => ''),
55 'File' => array('Type' => 'File', 'Caption' => 'Soubor', 'Default' => ''),
56 ),
57 ));
58 $this->System->FormManager->RegisterClass('FinanceBankImport', array(
59 'Title' => 'Import plateb z účtu',
60 'Table' => 'FinanceBankImport',
61 'DefaultSortColumn' => 'Time',
62 'DefaultSortOrder' => 1,
63 'Items' => array(
64 'BankAccount' => array('Type' => 'TBankAccount', 'Caption' => 'Účet', 'Default' => ''),
65 'Time' => array('Type' => 'Date', 'Caption' => 'Čas', 'Default' => ''),
66 'Identification' => array('Type' => 'String', 'Caption' => 'Kód operace', 'Default' => ''),
67 'AccountNumber' => array('Type' => 'String', 'Caption' => 'Číslo účtu', 'Default' => ''),
68 'BankCode' => array('Type' => 'String', 'Caption' => 'Kód banky', 'Default' => ''),
69 'VariableSymbol' => array('Type' => 'String', 'Caption' => 'Variabilní symbol', 'Default' => ''),
70 'ConstantSymbol' => array('Type' => 'String', 'Caption' => 'Konstantní symbol', 'Default' => ''),
71 'SpecificSymbol' => array('Type' => 'String', 'Caption' => 'Specifický symbol', 'Default' => ''),
72 'Value' => array('Type' => 'Float', 'Caption' => 'Částka', 'Default' => '', 'Suffix' => 'Kč'),
73 'Currency' => array('Type' => 'TCurrency', 'Caption' => 'Měna', 'Default' => ''),
74 'Description' => array('Type' => 'String', 'Caption' => 'Popis operace', 'Default' => ''),
75 'OffsetAccountName' => array('Type' => 'String', 'Caption' => 'Jméno protiúčtu', 'Default' => ''),
76 'FinanceOperation' => array('Type' => 'TFinanceOperation', 'Caption' => 'Přiřazená operace', 'Default' => '', 'Null' => true, 'OnPreset' => array($this, 'PresetItem')),
77 ),
78 ));
79
80 $this->System->RegisterPage(['finance', 'import-api'], 'PageImportAPI');
81 $this->System->RegisterPage(['finance', 'import-soubor'], 'PageImportFile');
82
83 ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('FinanceBankAPI',
84 array($this, 'ShowDashboardItem'));
85 }
86
87 function ShowDashboardItem(): string
88 {
89 $DbResult = $this->Database->select('FinanceBankImport', 'COUNT(*)', '`FinanceOperation` IS NULL');
90 $DbRow = $DbResult->fetch_row();
91 $Output = 'Nezpárovaných plateb: '.$DbRow['0'].'<br/>';
92 return $Output;
93 }
94
95 function PresetItem(array $Item): array
96 {
97 $Preset = array();
98 if ($Item['Value'] < 0) $OperationGroupId = OPERATION_GROUP_ACCOUNT_OUT;
99 else $OperationGroupId = OPERATION_GROUP_ACCOUNT_IN;
100 $FinanceGroup = ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance->GetFinanceGroupById($OperationGroupId, 'FinanceOperationGroup');
101
102 $Preset = array(
103 'presetTime' => $Item['Time'],
104 'presetValueUser' => abs($Item['Value']),
105 'presetTaxable' => 1,
106 'presetText' => $Item['Description'],
107 'presetNetwork' => 1,
108 'presetBankAccount' => $Item['BankAccount'],
109 'presetGroup' => $FinanceGroup['Id']);
110 return $Preset;
111 }
112}
113
114class ScheduleBankImport extends SchedulerTask
115{
116 function Execute(): string
117 {
118 $Output = '';
119 $DbResult = $this->Database->select('FinanceBankAccount', '`Id`, `Comment`',
120 '(`AutoImport`=1) AND (`TimeCreate` < NOW()) AND '.
121 '((`TimeEnd` IS NULL) OR (`TimeEnd` > NOW()))');
122 while ($DbRow = $DbResult->fetch_assoc())
123 {
124 echo($DbRow['Comment']."\n");
125 $Page = new PageImportAPI($this->System);
126 $Output .= $Page->Import($DbRow['Id']);
127 }
128 return $Output;
129 }
130}
Note: See TracBrowser for help on using the repository browser.