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