source: trunk/Modules/FinanceBankAPI/FinanceBankAPI.php

Last change on this file was 914, checked in by chronos, 3 years ago
  • Added: Made IS dashboard numbers as links with filters.
File size: 5.4 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 GetModelDesc(): ModelDesc
11 {
12 $Desc = new ModelDesc(self::GetClassName());
13 $Desc->AddReference('BankAccount', FinanceBankAccount::GetClassName());
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::GetClassName());
23 $Desc->AddString('OffsetAccountName');
24 $Desc->AddReference('FinanceOperation', FinanceOperation::GetClassName());
25 return $Desc;
26 }
27}
28
29class ModuleFinanceBankAPI extends Module
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(ModuleFinance::GetName(), ModuleScheduler::GetName(), ModuleIS::GetName(),
40 ModuleDocument::GetName());
41 $this->Models = array(FinanceBankImport::GetClassName());
42 }
43
44 function DoStart(): void
45 {
46 $this->System->FormManager->RegisterClass('ImportBankFile', array(
47 'Title' => 'Import souborů s platbami',
48 'Table' => 'FinanceBank',
49 'SubmitText' => 'Načíst',
50 'Items' => array(
51 'BankAccount' => array('Type' => 'TBankAccount', 'Caption' => 'Bankovní účet', 'Default' => ''),
52 'File' => array('Type' => 'File', 'Caption' => 'Soubor', 'Default' => ''),
53 ),
54 ));
55 $this->System->FormManager->RegisterClass('FinanceBankImport', array(
56 'Title' => 'Import plateb z účtu',
57 'Table' => 'FinanceBankImport',
58 'DefaultSortColumn' => 'Time',
59 'DefaultSortOrder' => 1,
60 'Items' => array(
61 'BankAccount' => array('Type' => 'TBankAccount', 'Caption' => 'Účet', 'Default' => ''),
62 'Time' => array('Type' => 'Date', 'Caption' => 'Čas', 'Default' => ''),
63 'Identification' => array('Type' => 'String', 'Caption' => 'Kód operace', 'Default' => ''),
64 'AccountNumber' => array('Type' => 'String', 'Caption' => 'Číslo účtu', 'Default' => ''),
65 'BankCode' => array('Type' => 'String', 'Caption' => 'Kód banky', 'Default' => ''),
66 'VariableSymbol' => array('Type' => 'String', 'Caption' => 'Variabilní symbol', 'Default' => ''),
67 'ConstantSymbol' => array('Type' => 'String', 'Caption' => 'Konstantní symbol', 'Default' => ''),
68 'SpecificSymbol' => array('Type' => 'String', 'Caption' => 'Specifický symbol', 'Default' => ''),
69 'Value' => array('Type' => 'Float', 'Caption' => 'Částka', 'Default' => '', 'Suffix' => 'Kč'),
70 'Currency' => array('Type' => 'TCurrency', 'Caption' => 'Měna', 'Default' => ''),
71 'Description' => array('Type' => 'String', 'Caption' => 'Popis operace', 'Default' => ''),
72 'OffsetAccountName' => array('Type' => 'String', 'Caption' => 'Jméno protiúčtu', 'Default' => ''),
73 'FinanceOperation' => array('Type' => 'TFinanceOperation', 'Caption' => 'Přiřazená operace', 'Default' => '', 'Null' => true, 'OnPreset' => array($this, 'PresetItem')),
74 ),
75 ));
76
77 $this->System->RegisterPage(['finance', 'import-api'], 'PageImportAPI');
78 $this->System->RegisterPage(['finance', 'import-soubor'], 'PageImportFile');
79
80 ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('FinanceBankAPI',
81 array($this, 'ShowDashboardItem'));
82 }
83
84 function ShowDashboardItem(): string
85 {
86 $DbResult = $this->Database->select('FinanceBankImport', 'COUNT(*)', '`FinanceOperation` IS NULL');
87 $DbRow = $DbResult->fetch_row();
88 $Output = 'Nezpárovaných plateb: <a href="'.$this->System->Link('/is/?a=list&amp;t=FinanceBankImport&amp;filter=1&amp;FilterFinanceOperation=1&amp;FilterOpFinanceOperation=isnull').'">'.$DbRow['0'].'</a><br/>';
89 return $Output;
90 }
91
92 function PresetItem(array $Item): array
93 {
94 $Preset = array();
95 if ($Item['Value'] < 0) $OperationGroupId = OPERATION_GROUP_ACCOUNT_OUT;
96 else $OperationGroupId = OPERATION_GROUP_ACCOUNT_IN;
97 $FinanceGroup = ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance->GetFinanceGroupById($OperationGroupId, 'FinanceOperationGroup');
98
99 $Preset = array(
100 'presetTime' => $Item['Time'],
101 'presetValueUser' => abs($Item['Value']),
102 'presetTaxable' => 1,
103 'presetText' => $Item['Description'],
104 'presetNetwork' => 1,
105 'presetBankAccount' => $Item['BankAccount'],
106 'presetGroup' => $FinanceGroup['Id']);
107 return $Preset;
108 }
109}
110
111class ScheduleBankImport extends SchedulerTask
112{
113 function Execute(): string
114 {
115 $Output = '';
116 $DbResult = $this->Database->select('FinanceBankAccount', '`Id`, `Comment`',
117 '(`AutoImport`=1) AND (`TimeCreate` < NOW()) AND '.
118 '((`TimeEnd` IS NULL) OR (`TimeEnd` > NOW()))');
119 while ($DbRow = $DbResult->fetch_assoc())
120 {
121 echo($DbRow['Comment']."\n");
122 $Page = new PageImportAPI($this->System);
123 $Output .= $Page->Import($DbRow['Id']);
124 }
125 return $Output;
126 }
127}
Note: See TracBrowser for help on using the repository browser.