source: www/finance/manage.php@ 164

Last change on this file since 164 was 164, checked in by george, 16 years ago
  • Opraveno: Mazání položek ze seznamu online uživatelů.
  • Upraveno: Zobrazování členů sítě správcům financí.
  • Opraveno: Další menší opravy v souvislosti s předěláváním tabulky users.
File size: 16.3 KB
Line 
1<?php
2include_once('../global.php');
3
4class FinanceManagePage extends Page
5{
6 var $FullTitle = 'Správa financí';
7 var $ShortTitle = 'Správa financí';
8
9 function Show()
10 {
11 if(!$this->System->Modules['User']->CheckPermission('Finance', 'Manage')) return('Nemáte oprávnění');
12
13 if(array_key_exists('Operation', $_GET)) $Operation = $_GET['Operation']; else $Operation = '';
14 //$Operation = 'ImportOldData';
15 switch($Operation)
16 {
17 case 'Bills':
18 $Output = $this->BillManage();
19 break;
20 case 'ConvertPDFToFile':
21 $Output = $this->ConvertPDFDataToFiles();
22 break;
23 case 'ImportOldData':
24 $Output = $this->ImportOldData();
25 break;
26 case 'NewDeviceForm':
27 $Output = $this->ShowNewDeviceForm();
28 break;
29 case 'NewDeviceInsert':
30 $Output = $this->ShowNewDeviceInsert();
31 break;
32 case 'NewDeviceHistoryForm':
33 $Output = $this->ShowNewDeviceHistoryForm();
34 break;
35 case 'NewDeviceHistoryInsert':
36 $Output = $this->ShowNewDeviceHistoryInsert();
37 break;
38 case 'NewPaymentInsert':
39 $Output = $this->ShowNewPaymentInsert();
40 break;
41 case 'NewPaymentForm':
42 $Output = $this->ShowNewPaymentForm();
43 break;
44 case 'NewInvoiceInsert':
45 $Output = $this->ShowNewInvoiceInsert();
46 break;
47 case 'NewInvoiceForm':
48 $Output = $this->ShowNewInvoiceForm();
49 break;
50 default:
51 $Output = '<a href="?Operation=NewDeviceForm">Přidat nové zařízení</a><br />';
52 $Output .= '<a href="?Operation=NewDeviceHistoryForm">Přidat nový záznam historie zařízení</a><br />';
53 $Output .= '<a href="?Operation=NewPaymentForm">Přidat novou platbu</a><br />';
54 $Output .= '<a href="?Operation=NewInvoiceForm">Přidat novou fakturu</a><br />';
55 $Output .= '<a href="?Operation=ImportOldData">Importovat stará data</a><br />';
56 $Output .= '<a href="?Operation=ConvertPDFToFile">Převést data z databáze do souborů</a><br />';
57 $Output .= '<a href="?Operation=Bills">Správa dokladů</a><br />';
58 $Output .= '<a href="prepocet.php">Přepočet financí</a><br />';
59 $Output .= '<a href="clenove.php">Seznam subjektů</a><br />';
60 $Output .= '<a href="zivnost.php">Živnost</a><br />';
61 }
62 return($Output);
63 }
64
65 function BillManage()
66 {
67 $Output = '';
68 if(array_key_exists('subject', $_GET))
69 {
70 $DbResult = $this->Database->select('FinanceBills', '*', 'Subject='.$_GET['subject']);
71 while($Item = $DbResult->fetch_array())
72 {
73 $Output .= '<a href="?Operation=Bills&amp;bill='.$Item['Id'].'">faktura '.$Item['Id'].'</a> <a href="?Operation=Bills&amp;billpdf='.$Item['Id'].'">Uložené PDF</a> <a href="?Operation=Bills&amp;billpdf2='.$Item['Id'].'">Generované PDF</a> <a href="?Operation=Bills&amp;regenerate='.$Item['Id'].'">Přegenerovat</a><br />';
74 }
75 } else
76 if(array_key_exists('billpdf', $_GET))
77 {
78 $Output .= $this->System->Modules['Bill']->ShowStoredBill($_GET['billpdf']);
79 } else
80 if(array_key_exists('billpdf2', $_GET))
81 {
82 $Output .= $this->System->Modules['Bill']->ShowGeneratedBill($_GET['billpdf2']);
83 } else
84 if(array_key_exists('regenerate', $_GET))
85 {
86 $Output .= $this->System->Modules['Bill']->RegeneratePDF($_GET['regenerate']);
87 } else
88 if(array_key_exists('bill', $_GET))
89 {
90 $Output .= $this->System->Modules['Bill']->GenerateBill($_GET['bill']);
91 } else
92 if(array_key_exists('generate', $_GET))
93 {
94 $Output .= $this->System->Modules['Bill']->CreateBill(1, array(array('Description' => 'Poplatek za připojení k síti', 'Price' => 1000, 'Quantity' => 1)), time(), time());
95 } else
96 {
97 //ShowHeader('Faktury', 'Faktury');
98 $Output .= 'Faktury:<br />';
99 $DbResult = $this->Database->select('Subject', '*', '1 ORDER BY Name');
100 while($Subject = $DbResult->fetch_array())
101 {
102 $Output .= '<a href="?Operation=Bills&amp;subject='.$Subject['Id'].'">'.$Subject['Name'].'</a><br />';
103 }
104 //ShowFooter();
105 }
106 return($Output);
107 }
108
109 function CheckAdvancesAndLiabilities($Subject)
110 {
111 global $LastInsertTime;
112
113 do {
114 $DbResult = $this->Database->select('FinanceAdvances', 'SUM(Value)', 'Subject='.$Subject.' AND Direction = "In"');
115 $DbRow = $DbResult->fetch_array();
116 $Advances = $DbRow[0];
117 $DbResult = $this->Database->select('FinanceClaimsLiabilities', '*', 'Subject='.$Subject.' AND TimePayment IS NULL AND Value > 0 ORDER BY TimeCreation LIMIT 1');
118 //echo($Database->error);
119 if($DbResult->num_rows > 0)
120 {
121 $OpenedClaim = $DbResult->fetch_array();
122 if($Advances > $OpenedClaim['Value'])
123 {
124 $this->Database->update('FinanceClaimsLiabilities', 'Id='.$OpenedClaim['Id'], array('TimePayment' => TimeToMysqlDateTime($LastInsertTime)));
125 $this->Database->insert('FinanceAdvances', array('Subject' => $OpenedClaim['Subject'], 'Value' => -$OpenedClaim['Value'], 'TimeCreation' => TimeToMysqlDateTime($LastInsertTime), 'CashFlowId' => $OpenedClaim['Id'], 'Direction' => 'In'));
126 //echo($Database->LastQuery);
127 } else break;
128 } else break;
129 } while(($Advances > $OpenedClaim['Value']));
130 }
131
132 function InsertLiability($Subject, $Value, $Time, $BillId, $Text, $TimePayment = '')
133 {
134 global $LastInsertTime;
135
136 if($TimePayment != '') $TimePayment = TimeToMysqlDateTime($TimePayment);
137 $this->Database->insert('FinanceClaimsLiabilities', array('Text' => $Text, 'Subject' => $Subject, 'TimeCreation' => TimeToMysqlDateTime($Time), 'TimeDue' => TimeToMysqlDateTime($Time + 3600*24*15), 'TimePayment' => $TimePayment, 'Value' => $Value, 'Bill' => $BillId));
138 $Output = '.'; //$this->Database->LastQuery.'<br />';
139 $LastInsertTime = $Time;
140 $this->CheckAdvancesAndLiabilities($Subject);
141 return($Output);
142 }
143
144 function InsertMoney($Subject, $Value, $Cash, $Taxable, $Time, $Text)
145 {
146 global $LastInsertTime;
147
148 $this->Database->insert('FinanceCashFlow', array('Text' => $Text, 'Subject' => $Subject, 'Cash' => $Cash, 'Value' => $Value, 'Time' => TimeToMysqlDateTime($Time), 'Taxable' => $Taxable));
149 if($Value >= 0)
150 {
151 $this->Database->insert('FinanceAdvances', array('Subject' => $Subject, 'Value' => $Value, 'TimeCreation' => TimeToMysqlDateTime($Time), 'CashFlowId' => $this->Database->insert_id, 'Direction' => 'In'));
152 }
153 $LastInsertTime = $Time;
154 $this->CheckAdvancesAndLiabilities($Subject);
155 }
156
157 function ShowNewDeviceForm()
158 {
159 $Form = new Form('NewNetworkDevice');
160 $Form->OnSubmit = '?Operation=NewDeviceInsert';
161 $Output = $Form->ShowEditForm();
162 return($Output);
163 }
164
165 function ShowNewDeviceInsert()
166 {
167 $Form = new Form('NewNetworkDevice');
168 $Form->LoadValuesFromForm();
169 $Form->SaveValuesToDatabase(0);
170 $Output = $this->SystemMessage('Finance', 'Zařízení vloženo.');
171 $this->System->Modules['Log']->NewRecord('Finance', 'NewDeviceInserted');
172 return($Output);
173 }
174
175 function ShowNewDeviceHistoryForm()
176 {
177 $Form = new Form('NewNetworkDeviceHistory');
178 $Form->OnSubmit = '?Operation=NewDeviceHistoryInsert';
179 $Output = $Form->ShowEditForm();
180 return($Output);
181 }
182
183 function ShowNewDeviceHistoryInsert()
184 {
185 $Form = new Form('NewNetworkDeviceHistory');
186 $Form->LoadValuesFromForm();
187 $Form->SaveValuesToDatabase(0);
188 $Output = $this->SystemMessage('Finance', 'Záznam historie zařízení vložen.');
189 $this->System->Modules['Log']->NewRecord('Finance', 'NewDeviceHistoryInserted');
190 return($Output);
191 }
192
193 function ShowNewPaymentForm()
194 {
195 $Form = new Form('NewPayment');
196 $Form->OnSubmit = '?Operation=NewPaymentInsert';
197 $Output = $Form->ShowEditForm();
198 return($Output);
199 }
200
201 function ShowNewPaymentInsert()
202 {
203 $Form = new Form('NewPayment');
204 $Form->LoadValuesFromForm();
205 $this->InsertMoney($Form->Values['Subject'], $Form->Values['Value'], $Form->Values['Cash'], $Form->Values['Taxable'], $Form->Values['Time'], $Form->Values['Text']);
206 $Output = $this->SystemMessage('Finance', 'Platba vložena.');
207 $this->System->Modules['Log']->NewRecord('Finance', 'NewPaymentInserted');
208 return($Output);
209 }
210
211 function ShowNewInvoiceForm()
212 {
213 $Form = new Form('NewInvoice');
214 $Form->OnSubmit = '?Operation=NewInvoiceInsert';
215 $Output = $Form->ShowEditForm();
216 return($Output);
217 }
218
219 function ShowNewInvoiceInsert()
220 {
221 $Form = new Form('NewInvoice');
222 $Form->LoadvaluesFromForm();
223 //print_r($Form->Values);
224 //$this->InsertLiability($Form->Values['Subject'], $Form->Values['Value'], $Form->Values['Time'], $BillId, $Text, $TimePayment = '')
225 $Output = $this->SystemMessage('Finance', 'Faktura vložena..');
226 $this->System->Modules['Log']->NewRecord('Finance', 'NewInvoiceInserted');
227 return($Output);
228 }
229
230 function ImportOldData()
231 {
232 $Output = '';
233 $this->Database->query('TRUNCATE TABLE FinanceCashFlow');
234 $this->Database->query('TRUNCATE TABLE FinanceClaimsLiabilities');
235 $this->Database->query('TRUNCATE TABLE FinanceAdvances');
236 $this->Database->query('TRUNCATE TABLE FinanceSmallAssets');
237
238 // Move time of device price transformation
239 $this->Database->query('UPDATE finance_operations SET date="2007-11-30" WHERE comment = "Jednorázový poplatek za společné zařízení"');
240
241 // Transfer finance before era
242 $DbResult = $this->Database->query('SELECT * FROM Subject');
243 while($Subject = $DbResult->fetch_assoc())
244 {
245 $DbResult2 = $this->Database->query('SELECT SUM(money) FROM finance_operations WHERE user='.$Subject['Id'].' AND date < "2007-12-01"');
246 $Row = $DbResult2->fetch_row();
247 $TotalAdvance = $Row[0];
248 //echo($User['second_name'].' '.$User['first_name'].' '.$TotalAdvance.'<br />');
249 // Záloha
250 if($TotalAdvance > 0)
251 {
252 $this->InsertMoney($Subject['Id'], $TotalAdvance, 0, 1, mktime(0, 0, 0, 12, 1, 2007), 'Přijatá záloha (z období před daňovou evidencí)');
253 }
254 // Dluh
255 if($TotalAdvance < 0)
256 {
257 $this->InsertLiability($Subject['Id'], (-$Row[0]), mktime(0, 0, 0, 12, 1, 2007), 0, 'Připojení k síti (z období před daňovou evidencí)');
258 //echo($Database->LastQuery.'<br />');
259 }
260 }
261
262 // Transfer finance after era
263 // $DbResult = $Database->query('SELECT * FROM users WHERE role=2');
264 // while($User = $DbResult->fetch_array())
265 // {
266 $DbResult2 = $this->Database->query('SELECT finance_operations.* FROM finance_operations JOIN Subject ON Subject.Id = finance_operations.user WHERE finance_operations.date >= "2007-12-01"');
267 while($Operation = $DbResult2->fetch_assoc())
268 {
269 //echo($Operation['comment'].'<br />');
270 if(substr($Operation['comment'], 0, 19) == 'Poplatek za měsíc')
271 {
272 $this->InsertLiability($Operation['user'], -$Operation['money'], MysqlDateToTime($Operation['date']), $Operation['bill_id'], 'Připojení k síti');
273 $Output .= $Operation['user'].' '.$Operation['money'].' Připojení k síti<br />';
274 } else
275 if($Operation['comment'] == 'Vklad')
276 {
277 $this->InsertMoney($Operation['user'], $Operation['money'], 0, 1, MysqlDateToTime($Operation['date']), 'Přijatá záloha');
278 //echo($Operation['user'].' '.$Operation['money'].' Přijatá záloha<br />');
279 } else
280 if($Operation['comment'] == 'Internet')
281 {
282 $this->InsertLiability($Operation['user'], $Operation['money'], MysqlDateToTime($Operation['date']), $Operation['bill_id'], 'Měsíční paušál za Internet', MysqlDateToTime($Operation['date']));
283 $this->InsertMoney($Operation['user'], $Operation['money'], 0, 1, MysqlDateToTime($Operation['date']), 'Měsíční paušál za Internet');
284 } else
285 if($Operation['comment'] == 'Sociální pojištění')
286 {
287 $this->InsertLiability($Operation['user'], $Operation['money'], MysqlDateToTime($Operation['date']), $Operation['bill_id'], $Operation['comment'], MysqlDateToTime($Operation['date']));
288 $this->InsertMoney($Operation['user'], $Operation['money'], 0, 1, MysqlDateToTime($Operation['date']), $Operation['comment']);
289 } else
290 {
291 $DbResult = $this->Database->query('SELECT Id FROM Subject WHERE Id='.$Operation['user']);
292 while($User = $DbResult->fetch_array())
293 $this->InsertLiability($Operation['user'], -$Operation['money'], MysqlDateToTime($Operation['date']), $Operation['bill_id'], $Operation['comment'], MysqlDateToTime($Operation['date']));
294 $Output .= $Operation['user'].' '.$Operation['comment'].' '.MysqlDateToTime($Operation['date']).'<br />';
295
296// if(($Operation['role'] == 2))
297 // {
298 $Output .= 'A';
299 $this->Database->insert('FinanceAdvances', array('Subject' => $Operation['user'], 'Value' => $Operation['money'], 'TimeCreation' => MysqlDateToTime($Operation['date']), 'CashFlowId' => 0, 'Direction' => 'In'));
300 $this->CheckAdvancesAndLiabilities($Operation['user']);
301 //} else InsertMoney($Operation['user'], $Operation['money'], 0, 1, MysqlDateToTime($Operation['date']), $Operation['comment']);
302 }
303 }
304 // }
305
306 // Import small asset
307 $Subject = 1;
308
309 // Převod pro minulý rok
310 $Time = mktime(0, 0, 0, 12, 10, 2007);
311 $DbResult = $this->Database->query('SELECT * FROM network_devices WHERE used <> 0 AND date < "'.TimeToMysqlDateTime($Time).'" AND (id <> 73)');
312 $Items = array();
313 $TotalPrice = 0;
314 while($Device = $DbResult->fetch_array())
315 {
316 //$Database->insert('FinanceSmallAsset', array());
317 $Items[] = array('Description' => $Device['name'], 'Quantity' => $Device['count'], 'Price' => 0); //$Device['price']);
318 $LastId = $Device['id'];
319 $TotalPrice += $Device['price'] * $Device['count'];
320 if($TotalPrice > 55000) break;
321 }
322 //print_r($Items);
323 $BillId = $this->System->Modules['Bill']->CreateBill($Subject, $Items, $Time, $Time);
324 $DbResult = $this->Database->query('UPDATE network_devices SET TimeEnlistment = "'.TimeToMysqlDateTime($Time).'" WHERE used <> 0 AND date < "'.TimeToMysqlDateTime($Time).'" AND (id <> 73) AND (id <= '.$LastId.')');
325 //echo($Database->error);
326 $this->InsertLiability($Subject, 0, $Time, $BillId, 'Nákup infrastruktury', $Time);
327 //InsertMoney($Subject, 0, 0, 1, $Time, 'Nákup infrastruktury');
328
329 //echo($LastId);
330
331 // Převod tento rok
332 $Time = mktime(0, 0, 0, 1, 14, 2008);
333 $DbResult = $this->Database->query('SELECT * FROM network_devices WHERE (used <> 0) AND (id > '.$LastId.') AND date < "'.TimeToMysqlDateTime($Time).'"');
334 $Items = array();
335 $TotalPrice = 0;
336 while($Device = $DbResult->fetch_array())
337 {
338 // $Database->insert('FinanceSmallAsset', array());
339 $Items[] = array('Description' => $Device['name'], 'Quantity' => $Device['count'], 'Price' => 0); //$Device['price']);
340 $TotalPrice += $Device['price'] * $Device['count'];
341 }
342 //print_r($Items);
343 $BillId = $this->System->Modules['Bill']->CreateBill($Subject, $Items, $Time, $Time);
344 $DbResult = $this->Database->query('UPDATE network_devices SET TimeEnlistment = "'.TimeToMysqlDateTime($Time).'" WHERE used <> 0 AND date < "'.TimeToMysqlDateTime($Time).'" AND (id > '.$LastId.')');
345 $this->InsertLiability($Subject, 0, $Time, $BillId, 'Nákup infrastruktury', $Time);
346 //InsertMoney($Subject, 0, 0, 1, $Time, 'Nákup infrastruktury');
347
348
349 // Make absolute value in monthly overall
350 //$Database->query('UPDATE finance_monthly_overall SET total_paid = ABS(total_paid)');
351 //$Database->query('UPDATE finance_monthly_overall SET member_count = (SELECT COUNT(*) FROM users WHERE users.role=2 AND users.membership_date < finance_monthly_overall.date)');
352 return($Output);
353 }
354
355 function ConvertPDFDataToFiles()
356 {
357 $DbResult = $this->Database->query('SELECT * FROM FinanceBills');
358 while($Bill = $DbResult->fetch_array())
359 {
360 file_put_contents('doklady/doklad_'.$Bill['id'].'.pdf', $Bill['pdf']);
361 echo($Bill['id'].',');
362 }
363 }
364
365 function CheckPDFFiles()
366 {
367 global $Database, $InvoiceGenerator;
368
369 $DbResult = $this->Database->query('SELECT * FROM FinanceBills');
370 while($Bill = $DbResult->fetch_array())
371 {
372 file_put_contents('doklady2/doklad-'.$Bill['id'].'.pdf', file_get_contents('doklady/doklad-'.$Bill['id'].'.pdf'));
373 //echo($InvoiceGenerator->HasPDFFile($Bill['id']).',');
374 }
375 }
376
377}
378
379$System->AddModule(new FinanceManagePage());
380$System->Modules['FinanceManagePage']->GetOutput();
381
382?>
Note: See TracBrowser for help on using the repository browser.