1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/../../Common/Global.php');
|
---|
4 |
|
---|
5 | class BankImport
|
---|
6 | {
|
---|
7 | public System $System;
|
---|
8 | public Database $Database;
|
---|
9 | public array $BankAccount;
|
---|
10 |
|
---|
11 | function __construct(System $System)
|
---|
12 | {
|
---|
13 | $this->Database = &$System->Database;
|
---|
14 | $this->System = &$System;
|
---|
15 | }
|
---|
16 |
|
---|
17 | function Import(): string
|
---|
18 | {
|
---|
19 | return '';
|
---|
20 | }
|
---|
21 |
|
---|
22 | function ImportFile($Content, $Ext)
|
---|
23 | {
|
---|
24 | }
|
---|
25 |
|
---|
26 | function PairOperations(): void
|
---|
27 | {
|
---|
28 | $Finance = &ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance;
|
---|
29 | $DbResult = $this->Database->select('FinanceBankImport', '*', 'FinanceOperation IS NULL');
|
---|
30 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
31 | {
|
---|
32 | if (is_numeric($DbRow['VariableSymbol']))
|
---|
33 | {
|
---|
34 | $DbResult2 = $this->Database->select('Subject', 'Id', 'Id='.$DbRow['VariableSymbol']);
|
---|
35 | if ($DbResult2->num_rows == 1)
|
---|
36 | {
|
---|
37 | $DbRow2 = $DbResult2->fetch_assoc();
|
---|
38 | if ($DbRow['Value'] >= 0)
|
---|
39 | {
|
---|
40 | $FinanceGroup = $Finance->GetFinanceGroupById(OPERATION_GROUP_ACCOUNT_IN, 'FinanceOperationGroup');
|
---|
41 | } else
|
---|
42 | {
|
---|
43 | $FinanceGroup = $Finance->GetFinanceGroupById(OPERATION_GROUP_ACCOUNT_OUT, 'FinanceOperationGroup');
|
---|
44 | }
|
---|
45 | $Year = date('Y', MysqlDateToTime($DbRow['Time']));
|
---|
46 | $BillCode = ModuleDocument::Cast($this->System->GetModule('Document'))->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
|
---|
47 | $this->Database->insert('FinanceOperation', array('Subject' => $DbRow2['Id'], 'Cash' => 0,
|
---|
48 | 'ValueUser' => Abs($DbRow['Value']), 'Value' => 0, 'Taxable' => 1, 'BankAccount' => $DbRow['BankAccount'], 'Network' => 1,
|
---|
49 | 'Time' => $DbRow['Time'], 'Text' => $DbRow['Description'], 'BillCode' => $BillCode, 'Group' => $FinanceGroup['Id']));
|
---|
50 | $Id = $this->Database->insert_id;
|
---|
51 | $this->Database->update('FinanceBankImport', 'Id='.$DbRow['Id'], array('FinanceOperation' => $Id));
|
---|
52 |
|
---|
53 | // Execute after insert event
|
---|
54 | $Form = new Form($this->System->FormManager);
|
---|
55 | $Form->SetClass('FinanceOperation');
|
---|
56 | $Form->LoadValuesFromDatabase($Id);
|
---|
57 | if (array_key_exists('AfterInsert', $Form->Definition))
|
---|
58 | {
|
---|
59 | $Class = $Form->Definition['AfterInsert'][0];
|
---|
60 | $Method = $Form->Definition['AfterInsert'][1];
|
---|
61 | $Form->Values = $Class->$Method($Form, $Id);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | class PageImportAPI extends Page
|
---|
70 | {
|
---|
71 | function __construct(System $System)
|
---|
72 | {
|
---|
73 | parent::__construct($System);
|
---|
74 | $this->Title = 'Import plateb přes API';
|
---|
75 | $this->ParentClass = 'PageFinance';
|
---|
76 | }
|
---|
77 |
|
---|
78 | function Import(int $Id): string
|
---|
79 | {
|
---|
80 | $Output = '';
|
---|
81 |
|
---|
82 | $DbResult = $this->Database->select('FinanceBankAccount', '*', 'Id='.$Id);
|
---|
83 | $BankAccount = $DbResult->fetch_assoc();
|
---|
84 |
|
---|
85 | $DbResult = $this->Database->select('FinanceBank', '*', 'Id='.$BankAccount['Bank']);
|
---|
86 | $Bank = $DbResult->fetch_assoc();
|
---|
87 | $Output .= 'Účet: '.$BankAccount['Number'].'/'.$Bank['Code'].' ('.$Bank['Name'].')'."\n";
|
---|
88 |
|
---|
89 | if ($Bank['Code'] == '2010') $Import = new ImportFio($this->System);
|
---|
90 | else if ($Bank['Code'] == '0300') $Import = new ImportPS($this->System);
|
---|
91 | else $Output = $this->SystemMessage('Nepodporované API', 'Pro zvolenou banku není import podporován');
|
---|
92 | if (isset($Import))
|
---|
93 | {
|
---|
94 | $Import->BankAccount = $BankAccount;
|
---|
95 | $Output .= $Import->Import();
|
---|
96 | $Import->PairOperations();
|
---|
97 | }
|
---|
98 | return $Output;
|
---|
99 | }
|
---|
100 |
|
---|
101 | function Show(): string
|
---|
102 | {
|
---|
103 | if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Finance', 'SubjectList'))
|
---|
104 | return 'Nemáte oprávnění';
|
---|
105 |
|
---|
106 | $Output = $this->Import($_GET['i']);
|
---|
107 | return $Output;
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | class PageImportFile extends Page
|
---|
112 | {
|
---|
113 | function __construct(System $System)
|
---|
114 | {
|
---|
115 | parent::__construct($System);
|
---|
116 | $this->Title = 'Import plateb ze souboru';
|
---|
117 | $this->ParentClass = 'PageFinance';
|
---|
118 | }
|
---|
119 |
|
---|
120 | function Show(): string
|
---|
121 | {
|
---|
122 | $Output = '';
|
---|
123 | if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Finance', 'SubjectList')) return 'Nemáte oprávnění';
|
---|
124 | if (array_key_exists('Operation', $_GET))
|
---|
125 | {
|
---|
126 | if ($_GET['Operation'] == 'prepare') $Output .= $this->Prepare();
|
---|
127 | else if ($_GET['Operation'] == 'insert') $Output .= $this->Insert();
|
---|
128 | else $Output .= 'Neplatná akce';
|
---|
129 | } else $Output .= $this->ShowForm();
|
---|
130 | return $Output;
|
---|
131 | }
|
---|
132 |
|
---|
133 | function ShowForm(): string
|
---|
134 | {
|
---|
135 | $Form = new Form($this->System->FormManager);
|
---|
136 | $Form->SetClass('ImportBankFile');
|
---|
137 | $Form->OnSubmit = '?Operation=prepare';
|
---|
138 | $Form->Values['BankAccount'] = $_GET['id'];
|
---|
139 | $Output = $Form->ShowEditForm();
|
---|
140 | return $Output;
|
---|
141 | }
|
---|
142 |
|
---|
143 | function Prepare()
|
---|
144 | {
|
---|
145 | $Form = new Form($this->System->FormManager);
|
---|
146 | $Form->SetClass('ImportBankFile');
|
---|
147 | $Form->LoadValuesFromForm();
|
---|
148 | $File = $Form->Values['File'];
|
---|
149 | $Output = $File->Name.'<br/>';
|
---|
150 | $Output .= $File->GetFullName().'<br/>';
|
---|
151 |
|
---|
152 | $DbResult = $this->Database->select('FinanceBankAccount', '*', 'Id='.$Form->Values['BankAccount']);
|
---|
153 | $BankAccount = $DbResult->fetch_assoc();
|
---|
154 |
|
---|
155 | $DbResult = $this->Database->select('FinanceBank', '*', 'Id='.$BankAccount['Bank']);
|
---|
156 | $Bank = $DbResult->fetch_assoc();
|
---|
157 | $Output .= 'Účet: '.$BankAccount['Number'].'/'.$Bank['Code'].' ('.$Bank['Name'].')';
|
---|
158 |
|
---|
159 | if ($Bank['Code'] == '2010') $Import = new ImportFio($this->System);
|
---|
160 | else if ($Bank['Code'] == '0300') $Import = new ImportPS($this->System);
|
---|
161 | else $Output = $this->SystemMessage('Nepodporované API', 'Pro zvolenou banku není import podporován');
|
---|
162 | $Import->BankAccount = $BankAccount;
|
---|
163 | $Output .= $Import->ImportFile($File->GetContent(), $File->GetExt());
|
---|
164 |
|
---|
165 | return $Output;
|
---|
166 | }
|
---|
167 |
|
---|
168 | function InsertMoney($Subject, $Value, $Cash, $Taxable, $Time, $Text, $Group): void
|
---|
169 | {
|
---|
170 | $Year = date('Y', $Time);
|
---|
171 | $BillCode = ModuleDocument::Cast($this->System->GetModule('Document'))->GetNextDocumentLineNumberId(
|
---|
172 | $Group['DocumentLine'], $Year);
|
---|
173 | $this->Database->insert('FinanceOperation', array('Text' => $Text,
|
---|
174 | 'Subject' => $Subject, 'Cash' => $Cash, 'ValueUser' => $Value, 'Value' => $Value * $Group['ValueSign'],
|
---|
175 | 'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable, 'BillCode' => $BillCode));
|
---|
176 | }
|
---|
177 |
|
---|
178 | function Insert(): string
|
---|
179 | {
|
---|
180 | $Finance = ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance;
|
---|
181 | $Output = '';
|
---|
182 |
|
---|
183 | for ($I = $_POST['ItemCount'] - 1; $I >= 0 ; $I--)
|
---|
184 | {
|
---|
185 | if ($_POST['Money'.$I] >= 0)
|
---|
186 | {
|
---|
187 | $FinanceGroup = $Finance->GetFinanceGroupById(OPERATION_GROUP_ACCOUNT_IN,
|
---|
188 | 'FinanceOperationGroup');
|
---|
189 | } else
|
---|
190 | {
|
---|
191 | $FinanceGroup = $Finance->GetFinanceGroupById(OPERATION_GROUP_ACCOUNT_OUT,
|
---|
192 | 'FinanceOperationGroup');
|
---|
193 | }
|
---|
194 | $Date = explode('-', $_POST['Date'.$I]);
|
---|
195 | $Date = mktime(0, 0, 0, $Date[1], $Date[2], $Date[0]);
|
---|
196 | $this->InsertMoney($_POST['Subject'.$I], Abs($_POST['Money'.$I]),
|
---|
197 | 0, $_POST['Taxable'.$I], $Date, $_POST['Text'.$I], $FinanceGroup);
|
---|
198 | $Output .= $I.', ';
|
---|
199 | ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('Finance', 'NewPaymentInserted');
|
---|
200 | }
|
---|
201 | return $Output;
|
---|
202 | }
|
---|
203 | }
|
---|