source: trunk/Modules/Finance/Finance.php@ 755

Last change on this file since 755 was 755, checked in by chronos, 9 years ago
  • Modified: Now IS Dashboard is not dependent on other modules. They need to register their view to IS dashboard.
  • Fixed: Wrong sign of created finance operations from bank import.
  • Added: Storno invoices now have special groups in FinanceInvoiceGroup table.

Both Storno and previous storned documents should be linked together using new table FinanceInvoiceStorno.
Also they should be marked as not visible to user.

File size: 34.7 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Bill.php');
4include_once(dirname(__FILE__).'/Manage.php');
5include_once(dirname(__FILE__).'/UserState.php');
6include_once(dirname(__FILE__).'/Import.php');
7include_once(dirname(__FILE__).'/Trade.php');
8
9// TODO: Move constants to Finance module configuration
10define('TARIFF_FREE', 7);
11define('INVOICE_DUE_DAYS', 15);
12define('OPERATION_GROUP_TREASURY_IN', 1);
13define('OPERATION_GROUP_TREASURY_OUT', 2);
14define('OPERATION_GROUP_ACCOUNT_IN', 3);
15define('OPERATION_GROUP_ACCOUNT_OUT', 4);
16define('INVOICE_GROUP_IN', 1);
17define('INVOICE_GROUP_OUT', 2);
18define('VAT_TYPE_BASE', 2);
19define('FINANCE_DIRECTION_OUT', 1);
20define('FINANCE_DIRECTION_IN', 0);
21
22class Finance extends Model
23{
24 var $kWh;
25 var $Internet;
26 var $Sprava;
27 var $DatumOdecteni;
28 var $InternetUsers;
29 var $SpravaUsers;
30 var $MaxSpeed;
31 var $RealMaxSpeed;
32 var $SpeedReserve;
33 var $BaseSpeedElement;
34 var $BaseTariffPrice;
35 var $TopTariffPrice;
36 var $TotalPaid;
37 var $TotalInternetPaid;
38 var $MainSubject;
39 var $BillingPeriods;
40 var $DirectoryId;
41 var $Rounding;
42
43 function LoadMonthParameters($Period = 1) // 0 - now, 1 - next month
44 {
45 $DbResult = $this->Database->query('SELECT * FROM `FinanceBillingPeriod`');
46 while($BillingPeriod = $DbResult->fetch_assoc())
47 $this->BillingPeriods[$BillingPeriod['Id']] = $BillingPeriod;
48
49 // Period parameter is not used as it have to be determined from item replacement
50 $DbResult = $this->Database->query('SELECT * FROM `FinanceCharge` WHERE (`ChangeAction` IS NULL) LIMIT 1');
51 $Row = $DbResult->fetch_array();
52 $this->kWh = $Row['kWh'];
53 $this->Internet = $Row['Internet'];
54 $this->Sprava = $Row['AdministrationPerUser'];
55 $this->RealMaxSpeed = $Row['InternetSpeed'];
56 $this->SpeedReserve = $Row['InternetSpeedReserve'];
57 $this->BaseSpeedElement = $Row['BaseSpeedElement'];
58 $this->MaxSpeed = $this->RealMaxSpeed - $this->SpeedReserve;
59 $this->TopTariffPrice = $Row['TopTariffPrice'];
60 $this->BaseTariffPrice = $Row['BaseTariffPrice'];
61
62 $DbResult = $this->Database->query('SELECT COUNT(*) FROM `Member`');
63 $Row = $DbResult->fetch_row();
64 $this->InternetUsers = $Row[0];
65 $DbResult = $this->Database->query('SELECT COUNT(*) FROM `Member` WHERE (`Blocked`=0) AND (`BillingPeriod` > 1)');
66 $Row = $DbResult->fetch_row();
67 $this->PayingUsers = $Row[0];
68
69 $this->SpravaUsers = $this->PayingUsers;
70
71 $DbResult = $this->Database->query('SELECT SUM(`MemberPayment`.`MonthlyInternet`) AS `MonthlyInternet`, '.
72 'SUM(`MemberPayment`.`MonthlyTotal`) AS `MonthlyTotal` '.
73 'FROM `MemberPayment` JOIN `Member` ON `Member`.`Id`=`MemberPayment`.`Member` WHERE `Member`.`Blocked`=0');
74 $Row = $DbResult->fetch_assoc();
75 $this->TotalInternetPaid = $Row['MonthlyInternet'];
76 $this->TotalPaid = $Row['MonthlyTotal'];
77 $this->Rounding = $this->System->Config['Finance']['Rounding'];
78 }
79
80 function W2Kc($Spotreba)
81 {
82 return(round($Spotreba * 0.72 * $this->kWh));
83 }
84
85 function GetNextDocumentLineNumber($Id, $FinanceYear = 0)
86 {
87 if($FinanceYear == 0)
88 {
89 // Get latest year
90 $DbResult = $this->Database->select('FinanceYear', '*', '1 ORDER BY `Year` DESC LIMIT 1');
91 } else $DbResult = $this->Database->select('FinanceYear', '*', '`Year`='.$FinanceYear);
92 if($DbResult->num_rows == 0) throw new Exception('Rok '.$FinanceYear.' nenalezen');
93 $FinanceYear = $DbResult->fetch_assoc();
94 if($FinanceYear['Closed'] == 1) throw new Exception('Rok '.$FinanceYear['Year'].' je již uzavřen. Nelze do něj přidávat položky.');
95
96 $DbResult = $this->Database->query('SELECT `Shortcut`, `Id` FROM `DocumentLine` WHERE `Id`='.$Id);
97 $DocumentLine = $DbResult->fetch_assoc();
98
99 $DbResult = $this->Database->query('SELECT * FROM `DocumentLineSequence` WHERE '.
100 '`DocumentLine`='.$Id.' AND `FinanceYear`='.$FinanceYear['Id']);
101 $Sequence = $DbResult->fetch_assoc();
102
103 if($Sequence['YearPrefix'] == 1)
104 {
105 $Result = $DocumentLine['Shortcut'].$Sequence['NextNumber'].'/'.$FinanceYear['Year'];
106 } else $Result = $DocumentLine['Shortcut'].$Sequence['NextNumber'];
107
108 $this->Database->query('UPDATE `DocumentLineSequence` SET `NextNumber` = `NextNumber` + 1 '.
109 'WHERE (`DocumentLine`='.$Id.') AND (`FinanceYear`='.$FinanceYear['Id'].')');
110 return($Result);
111 }
112
113 function GetNextDocumentLineNumberId($Id, $FinanceYear = 0)
114 {
115 $Code = $this->GetNextDocumentLineNumber($Id, $FinanceYear);
116 $this->Database->insert('DocumentLineCode', array('DocumentLine' => $Id, 'Name' => $Code));
117 return $this->Database->insert_id;
118 }
119
120 function GetFinanceGroupById($Id, $Table)
121 {
122 $DbResult = $this->Database->query('SELECT * FROM `'.$Table.'` WHERE `Id`= '.$Id);
123 if($DbResult->num_rows == 1) {
124 $Group = $DbResult->fetch_assoc();
125 return($Group);
126 } else die('Finance group not found');
127 }
128
129 function RecalculateMemberPayment()
130 {
131 $Output = 'Aktualizuji finance členů...<br />';
132 $this->Database->query('TRUNCATE TABLE `MemberPayment`');
133 $DbResult = $this->Database->query('SELECT * FROM `Member`');
134 while($Member = $DbResult->fetch_assoc())
135 {
136 $DbResult2 = $this->Database->query('SELECT ((SELECT COALESCE(SUM(`Value`), 0) FROM `FinanceOperation` '.
137 'WHERE `Subject`='.$Member['Subject'].') - (SELECT COALESCE(SUM(`Value`), 0) FROM `FinanceInvoice` '.
138 'WHERE `Subject`='.$Member['Subject'].')) AS `Cash`');
139 $Cash = $DbResult2->fetch_row();
140 $Cash = $Cash[0];
141
142 $DbResult2 = $this->Database->query('SELECT SUM(`Product`.`Consumption`) * `StockSerialNumber`.`Amount` '.
143 'FROM `StockSerialNumber` JOIN `Product` ON `Product`.`Id` = `StockSerialNumber`.`Product` '.
144 'WHERE (`StockSerialNumber`.`Location` = '.$Member['Id'].') AND (`StockSerialNumber`.`TimeElimination` IS NULL)');
145 $ConsumptionPlus = $DbResult2->fetch_row();
146 $ConsumptionPlus = $ConsumptionPlus[0];
147
148 $DbResult2 = $this->Database->query('SELECT SUM(`Service`.`Price`) AS `Price` '.
149 'FROM `ServiceCustomerRel` LEFT JOIN '.
150 '`Service` ON `Service`.`Id` = `ServiceCustomerRel`.`Service` WHERE (`ServiceCustomerRel`.`Customer`='.
151 $Member['Id'].') AND (`ServiceCustomerRel`.`ChangeAction` IS NULL)');
152 $DbRow = $DbResult2->fetch_assoc();
153 $Monthly = 0;
154 if($DbRow['Price'] != '') $MonthlyInet = $DbRow['Price'];
155 else $MonthlyInet = 0;
156
157 $Monthly += $MonthlyInet;
158 $Monthly -= $this->W2Kc($ConsumptionPlus);
159 $Monthly = round($Monthly);
160
161 if($Member['BillingPeriodNext'] == 1)
162 {
163 // Inactive payer
164 $MonthlyInet = 0;
165 $Monthly = 0;
166 $ConsumptionPlus = 0;
167 $Consumption = 0;
168 }
169 $this->Database->insert('MemberPayment', array('Member' => $Member['Id'],
170 'MonthlyInternet' => $MonthlyInet,
171 'MonthlyTotal' => $Monthly, 'MonthlyConsumption' => $this->W2Kc($Consumption),
172 'Cash' => $Cash, 'MonthlyPlus' => $this->W2Kc($ConsumptionPlus)));
173 }
174 $this->System->ModuleManager->Modules['Log']->NewRecord('Finance', 'RecalculateMemberPayment');
175 return($Output);
176 }
177
178 function GetVATByType($TypeId)
179 {
180 $Time = time();
181 $DbResult = $this->Database->select('FinanceVAT', 'Value', '(Type='.$TypeId.
182 ') AND (ValidFrom <= "'.TimeToMysqlDate($Time).'") AND ((ValidTo >= "'.
183 TimeToMysqlDate($Time).'") OR (ValidTo IS NULL)) LIMIT 1');
184 $Row = $DbResult->fetch_array();
185 return($Row[0]);
186 }
187}
188
189class ModuleFinance extends AppModule
190{
191 function __construct($System)
192 {
193 parent::__construct($System);
194 $this->Name = 'Finance';
195 $this->Version = '1.0';
196 $this->Creator = 'Chronos';
197 $this->License = 'GNU/GPLv3';
198 $this->Description = 'Base module for finance management';
199 $this->Dependencies = array('File', 'EmailQueue');
200 }
201
202 function DoInstall()
203 {
204 }
205
206 function DoUninstall()
207 {
208 }
209
210 function DoStart()
211 {
212 global $Config;
213
214 $this->System->RegisterPage(array('finance', 'sprava'), 'PageFinanceManage');
215 $this->System->RegisterPage(array('finance', 'platby'), 'PageFinanceUserState');
216 $this->System->RegisterPage(array('finance', 'import'), 'PageFinanceImportPayment');
217 $this->System->RegisterPage(array('finance', 'zivnost'), 'PageFinanceTaxFiling');
218
219 $this->System->FormManager->RegisterClass('FinanceOperation', array(
220 'Title' => 'Finanční operace',
221 'Table' => 'FinanceOperation',
222 'DefaultSortColumn' => 'Time',
223 'DefaultSortOrder' => 1,
224 'Items' => array(
225 'Group' => array('Type' => 'TFinanceOperationGroup', 'Caption' => 'Skupina', 'Default' => ''),
226 'BillCode' => array('Type' => 'TDocumentLineCode', 'Caption' => 'Označení', 'Default' => '', 'ReadOnly' => true),
227 'Subject' => array('Type' => 'TSubject', 'Caption' => 'Subjekt', 'Default' => ''),
228 'Time' => array('Type' => 'Date', 'Caption' => 'Čas realizace', 'Default' => ''),
229 'Cash' => array('Type' => 'Boolean', 'Caption' => 'Hotově', 'Default' => ''),
230 'Taxable' => array('Type' => 'Boolean', 'Caption' => 'Zdanitelné', 'Default' => ''),
231 'Value' => array('Type' => 'Integer', 'Caption' => 'Částka absolutní', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true),
232 'ValueUser' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč'),
233 'File' => array('Type' => 'TFile', 'Caption' => 'Doklad', 'Default' => '', 'Null' => true),
234 'Text' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => ''),
235 'Network' => array('Type' => 'Boolean', 'Caption' => 'Týkající sítě', 'Default' => ''),
236 'BankAccount' => array('Type' => 'TFinanceBankAccount', 'Caption' => 'Účet', 'Default' => '', 'Null' => true),
237 'Treasury' => array('Type' => 'TFinanceTreasury', 'Caption' => 'Pokladna', 'Default' => '', 'Null' => true),
238 'Generate' => array('Type' => 'Boolean', 'Caption' => 'Generovat', 'Default' => ''),
239 'InvoiceRel' => array('Type' => 'TFinanceInvoiceOperationRelListOperation', 'Caption' => 'Zaplacené faktury', 'Default' => ''),
240 ),
241 'BeforeInsert' => array($this, 'BeforeInsertFinanceOperation'),
242 'AfterInsert' => array($this, 'AfterInsertFinanceOperation'),
243 'BeforeModify' => array($this, 'BeforeModifyFinanceOperation'),
244 ));
245
246 $this->System->FormManager->RegisterClass('FinanceTreasuryIn', $this->System->FormManager->Classes['FinanceOperation']);
247 $this->System->FormManager->Classes['FinanceTreasuryIn']['Title'] = 'Pokladní příjmy';
248 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Group']['Default'] = OPERATION_GROUP_TREASURY_IN;
249 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Group']['Hidden'] = true;
250 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Group']['Filter'] = true;
251 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Default'] = 1;
252 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Hidden'] = true;
253 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
254 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['BankAccount']['Hidden'] = true;
255 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Value']['Hidden'] = true;
256
257 $this->System->FormManager->RegisterClass('FinanceTreasuryOut', $this->System->FormManager->Classes['FinanceOperation']);
258 $this->System->FormManager->Classes['FinanceTreasuryOut']['Title'] = 'Pokladní výdeje';
259 $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['Group']['Default'] = OPERATION_GROUP_TREASURY_OUT;
260 $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['Group']['Hidden'] = true;
261 $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['Group']['Filter'] = true;
262 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Default'] = 1;
263 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Hidden'] = true;
264 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
265 $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['BankAccount']['Hidden'] = true;
266 $this->System->FormManager->Classes['FinanceTreasuryOut']['Items']['Value']['Hidden'] = true;
267
268 $this->System->FormManager->RegisterClass('FinanceAccountIn', $this->System->FormManager->Classes['FinanceOperation']);
269 $this->System->FormManager->Classes['FinanceAccountIn']['Title'] = 'Příjmy na účet';
270 $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Group']['Default'] = OPERATION_GROUP_ACCOUNT_IN;
271 $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Group']['Hidden'] = true;
272 $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Group']['Filter'] = true;
273 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Default'] = 0;
274 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Hidden'] = true;
275 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
276 $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Treasury']['Hidden'] = true;
277 $this->System->FormManager->Classes['FinanceAccountIn']['Items']['Value']['Hidden'] = true;
278
279 $this->System->FormManager->RegisterClass('FinanceAccountOut', $this->System->FormManager->Classes['FinanceOperation']);
280 $this->System->FormManager->Classes['FinanceAccountOut']['Title'] = 'Výdeje z účtu';
281 $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Group']['Default'] = OPERATION_GROUP_ACCOUNT_OUT;
282 $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Group']['Hidden'] = true;
283 $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Group']['Filter'] = true;
284 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Default'] = 0;
285 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Hidden'] = true;
286 $this->System->FormManager->Classes['FinanceTreasuryIn']['Items']['Cash']['Filter'] = false;
287 $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Treasury']['Hidden'] = true;
288 $this->System->FormManager->Classes['FinanceAccountOut']['Items']['Value']['Hidden'] = true;
289
290 $this->System->FormManager->RegisterClass('FinanceOperationGroup', array(
291 'Title' => 'Skupina finančních operací',
292 'Table' => 'FinanceOperationGroup',
293 'Items' => array(
294 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => '0'),
295 'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => '0'),
296 'ValueSign' => array('Type' => 'TFinanceValueSign', 'Caption' => 'Znaménko hodnoty', 'Default' => '0'),
297 'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
298 ),
299 ));
300 $this->System->FormManager->RegisterFormType('TFinanceOperationGroup', array(
301 'Type' => 'Reference',
302 'Table' => 'FinanceOperationGroup',
303 'Id' => 'Id',
304 'Name' => 'Name',
305 'Filter' => '1',
306 ));
307 $this->System->FormManager->RegisterFormType('TFinanceValueSign', array(
308 'Type' => 'Enumeration',
309 'States' => array(-1 => 'Mínus', 1 => 'Plus'),
310 ));
311 $this->System->FormManager->RegisterFormType('TFinanceDirection', array(
312 'Type' => 'Enumeration',
313 'States' => array(0 => 'Příjem', 1 => 'Výdej'),
314 ));
315 $this->System->FormManager->RegisterClass('FinanceInvoice', array(
316 'Title' => 'Faktury',
317 'Table' => 'FinanceInvoice',
318 'DefaultSortColumn' => 'Time',
319 'DefaultSortOrder' => 1,
320 'Items' => array(
321 'Group' => array('Type' => 'TFinanceInvoiceGroup', 'Caption' => 'Skupina', 'Default' => ''),
322 'BillCode' => array('Type' => 'TDocumentLineCode', 'Caption' => 'Označení', 'Default' => '', 'ReadOnly' => true),
323 'Subject' => array('Type' => 'TSubject', 'Caption' => 'Subjekt', 'Default' => ''),
324 'Time' => array('Type' => 'Date', 'Caption' => 'Čas vytvoření', 'Default' => ''),
325 'TimeDue' => array('Type' => 'Date', 'Caption' => 'Čas splatnosti', 'Default' => ''),
326 'TimePayment' => array('Type' => 'Date', 'Caption' => 'Čas zaplacení', 'Default' => '', 'Null' => true),
327 'Value' => array('Type' => 'Integer', 'Caption' => 'Částka absolutní', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true),
328 'ValueUser' => array('Type' => 'Integer', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč', 'ReadOnly' => true,
329 'SQL' => 'SELECT ROUND(SUM(`Price`*`Quantity`), '.$Config['Finance']['Rounding'].') FROM `FinanceInvoiceItem` WHERE `FinanceInvoiceItem`.`FinanceInvoice`=#Id'),
330 'File' => array('Type' => 'TFile', 'Caption' => 'Doklad', 'Default' => '', 'Null' => true),
331 'Generate' => array('Type' => 'Boolean', 'Caption' => 'Generovat', 'Default' => ''),
332 'PeriodFrom' => array('Type' => 'Date', 'Caption' => 'Období od', 'Default' => '', 'Null' => true),
333 'PeriodTo' => array('Type' => 'Date', 'Caption' => 'Období do', 'Default' => '', 'Null' => true),
334 'Cash' => array('Type' => 'Boolean', 'Caption' => 'Platit hotově', 'Default' => ''),
335 'VisibleToUser' => array('Type' => 'Boolean', 'Caption' => 'Viditelné uživatelům', 'Default' => '1'),
336 'Items' => array('Type' => 'TFinanceInvoiceItemListInvoice', 'Caption' => 'Položky', 'Default' => ''),
337 'StornoBy' => array('Type' => 'TFinanceInvoiceStornoListBy', 'Caption' => 'Storno doklady', 'Default' => ''),
338 'StornoOf' => array('Type' => 'TFinanceInvoiceStornoListOf', 'Caption' => 'Původní doklady', 'Default' => ''),
339 'OperationRel' => array('Type' => 'TFinanceInvoiceOperationRelListInvoice', 'Caption' => 'Platba', 'Default' => ''),
340 'OperationRelCount' => array('Type' => 'Integer', 'Caption' => 'Plateb',
341 'ReadOnly' => true, 'SQL' => '(SELECT COUNT(`FinanceInvoiceOperationRel`.`Id`) FROM `FinanceInvoiceOperationRel` '.
342 'WHERE `FinanceInvoiceOperationRel`.`Invoice`=#Id)'),
343 ),
344 'BeforeInsert' => array($this, 'BeforeInsertFinanceInvoice'),
345 'AfterInsert' => array($this, 'AfterInsertFinanceInvoice'),
346 'BeforeModify' => array($this, 'BeforeModifyFinanceInvoice'),
347 ));
348 $this->System->FormManager->RegisterClass('FinanceInvoiceIn', $this->System->FormManager->Classes['FinanceInvoice']);
349 $this->System->FormManager->Classes['FinanceInvoiceIn']['Title'] = 'Přijaté faktury';
350 $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Group']['Default'] = INVOICE_GROUP_IN;
351 $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Group']['Hidden'] = true;
352 $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Group']['Filter'] = true;
353 $this->System->FormManager->Classes['FinanceInvoiceIn']['Items']['Value']['Hidden'] = true;
354
355 $this->System->FormManager->RegisterClass('FinanceInvoiceOut', $this->System->FormManager->Classes['FinanceInvoice']);
356 $this->System->FormManager->Classes['FinanceInvoiceOut']['Title'] = 'Vydané faktury';
357 $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Group']['Default'] = INVOICE_GROUP_OUT;
358 $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Group']['Hidden'] = true;
359 $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Group']['Filter'] = true;
360 $this->System->FormManager->Classes['FinanceInvoiceOut']['Items']['Value']['Hidden'] = true;
361
362 $this->System->FormManager->RegisterClass('FinanceInvoiceGroup', array(
363 'Title' => 'Skupina faktur',
364 'Table' => 'FinanceInvoiceGroup',
365 'Items' => array(
366 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => '0'),
367 'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => '0'),
368 'ValueSign' => array('Type' => 'TFinanceValueSign', 'Caption' => 'Znaménko hodnoty', 'Default' => '0'),
369 'Direction' => array('Type' => 'TFinanceDirection', 'Caption' => 'Směr', 'Default' => '0'),
370 'Items' => array('Type' => 'TFinanceInvoiceListGroup', 'Caption' => 'Faktury', 'Default' => ''),
371 ),
372 ));
373
374 $this->System->FormManager->RegisterClass('FinanceInvoiceStorno', array(
375 'Title' => 'Storno faktur',
376 'Table' => 'FinanceInvoiceStorno',
377 'Items' => array(
378 'StornoBy' => array('Type' => 'TFinanceInvoice', 'Caption' => 'Storno doklad', 'Default' => ''),
379 'StornoOf' => array('Type' => 'TFinanceInvoice', 'Caption' => 'Původní doklad', 'Default' => ''),
380 ),
381 ));
382 $this->System->FormManager->RegisterFormType('TFinanceInvoiceGroup', array(
383 'Type' => 'Reference',
384 'Table' => 'FinanceInvoiceGroup',
385 'Id' => 'Id',
386 'Name' => 'Name',
387 'Filter' => '1',
388 ));
389
390 $this->System->FormManager->RegisterClass('Company', array(
391 'Title' => 'Firma',
392 'Table' => 'Company',
393 'Items' => array(
394 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => '0'),
395 'Subject' => array('Type' => 'TSubject', 'Caption' => 'Subjekt', 'Default' => '0'),
396 ),
397 ));
398 $this->System->FormManager->RegisterClass('FinanceInvoiceItem', array(
399 'Title' => 'Položka faktury',
400 'Table' => 'FinanceInvoiceItem',
401 'Items' => array(
402 'FinanceInvoice' => array('Type' => 'TFinanceInvoice', 'Caption' => 'Faktura', 'Default' => '0'),
403 'Description' => array('Type' => 'String', 'Caption' => 'Popis', 'Default' => 'Položka'),
404 'Price' => array('Type' => 'Float', 'Caption' => 'Částka', 'Default' => '0', 'Suffix' => 'Kč'),
405 'Quantity' => array('Type' => 'Float', 'Caption' => 'Množství', 'Default' => '1'),
406 'VAT' => array('Type' => 'Integer', 'Caption' => 'Daň', 'Default' => '21', 'Suffix' => '%'),
407 'Total' => array('Type' => 'Integer', 'Caption' => 'Celkem', 'Default' => '', 'Suffix' => 'Kč',
408 'ReadOnly' => true, 'SQL' => 'ROUND(`Price` * `Quantity`, '.$Config['Finance']['Rounding'].')'),
409 ),
410 'AfterInsert' => array($this, 'AfterInsertFinanceInvoiceItem'),
411 'AfterModify' => array($this, 'AfterModifyFinanceInvoiceItem'),
412 'AfterDelete' => array($this, 'AfterModifyFinanceInvoiceItem'),
413 ));
414 $this->System->FormManager->RegisterClass('FinanceTreasury', array(
415 'Title' => 'Pokladny',
416 'Table' => 'FinanceTreasury',
417 'DefaultSortColumn' => 'Name',
418 'Items' => array(
419 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
420 'TimeCreate' => array('Type' => 'Date', 'Caption' => 'Čas vytvoření', 'Default' => ''),
421 'State' => array('Type' => 'Float', 'Caption' => 'Stav', 'Default' => '',
422 'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value`) FROM `FinanceOperation` '.
423 'WHERE `FinanceOperation`.`Treasury`=#Id)'),
424 'Operations' => array('Type' => 'TFinanceOperationListTreasury', 'Caption' => 'Operace', 'Default' => ''),
425 ),
426 ));
427 $this->System->FormManager->RegisterFormType('TFinanceTreasury', array(
428 'Type' => 'Reference',
429 'Table' => 'FinanceTreasury',
430 'Id' => 'Id',
431 'Name' => 'Name',
432 'Filter' => '1',
433 ));
434 $this->System->FormManager->RegisterClass('FinanceBankAccount', array(
435 'Title' => 'Účty',
436 'Table' => 'FinanceBankAccount',
437 'DefaultSortColumn' => 'Comment',
438 'Items' => array(
439 'Subject' => array('Type' => 'TSubject', 'Caption' => 'Vlastník', 'Default' => ''),
440 'Comment' => array('Type' => 'String', 'Caption' => 'Komentář', 'Default' => ''),
441 'Number' => array('Type' => 'String', 'Caption' => 'Číslo', 'Default' => ''),
442 'Bank' => array('Type' => 'TFinanceBank', 'Caption' => 'Banka', 'Default' => ''),
443 'TimeCreate' => array('Type' => 'Date', 'Caption' => 'Čas vytvoření', 'Default' => ''),
444 'TimeEnd' => array('Type' => 'Date', 'Caption' => 'Čas zrušení', 'Default' => '', 'Null' => true),
445 'Currency' => array('Type' => 'TCurrency', 'Caption' => 'Měna', 'Default' => ''),
446 'LoginName' => array('Type' => 'String', 'Caption' => 'Přihlašovací jméno / token', 'Default' => ''),
447 'LoginPassword' => array('Type' => 'String', 'Caption' => 'Přihlašovací heslo', 'Default' => ''),
448 'Operations' => array('Type' => 'TFinanceOperationListAccount', 'Caption' => 'Operace', 'Default' => ''),
449 'Use' => array('Type' => 'Boolean', 'Caption' => 'Používat', 'Default' => '0'),
450 'LastImportDate' => array('Type' => 'Date', 'Caption' => 'Datum posledního importu', 'Default' => ''),
451 'LastImportId' => array('Type' => 'String', 'Caption' => 'Id posledního importu', 'Default' => ''),
452 'State' => array('Type' => 'Float', 'Caption' => 'Stav', 'Default' => '',
453 'ReadOnly' => true, 'Suffix' => 'Kč', 'SQL' => '(SELECT SUM(`FinanceOperation`.`Value`) FROM `FinanceOperation` '.
454 'WHERE `FinanceOperation`.`BankAccount`=#Id)'),
455 'AutoImport' => array('Type' => 'Boolean', 'Caption' => 'Automaticky stahovat z banky', 'Default' => ''),
456 ),
457 'ItemActions' => array(
458 array('Caption' => 'Import plateb z banky', 'URL' => '/finance/import-api/?i=#RowId'),
459 array('Caption' => 'Import plateb ze souboru', 'URL' => '/finance/import-soubor/?i=#RowId'),
460 ),
461 ));
462 $this->System->FormManager->RegisterFormType('TFinanceBankAccount', array(
463 'Type' => 'Reference',
464 'Table' => 'FinanceBankAccount',
465 'Id' => 'Id',
466 'Name' => 'Comment',
467 'Filter' => '1',
468 ));
469 $this->System->FormManager->RegisterClass('FinanceBank', array(
470 'Title' => 'Banky',
471 'Table' => 'FinanceBank',
472 'Items' => array(
473 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
474 'Code' => array('Type' => 'String', 'Caption' => 'Český kód', 'Default' => ''),
475 'BIC' => array('Type' => 'String', 'Caption' => 'Kód BIC', 'Default' => ''),
476 'Country' => array('Type' => 'TCountry', 'Caption' => 'Země', 'Default' => ''),
477 ),
478 ));
479 $this->System->FormManager->RegisterFormType('TFinanceBank', array(
480 'Type' => 'Reference',
481 'Table' => 'FinanceBank',
482 'Id' => 'Id',
483 'Name' => 'CONCAT(Name, " (", Code, ")")',
484 'Filter' => '1',
485 ));
486 $this->System->FormManager->RegisterClass('Currency', array(
487 'Title' => 'Měny',
488 'Table' => 'Currency',
489 'Items' => array(
490 'Code' => array('Type' => 'String', 'Caption' => 'Kód', 'Default' => ''),
491 'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
492 'Symbol' => array('Type' => 'String', 'Caption' => 'Symbol', 'Default' => ''),
493 ),
494 ));
495 $this->System->FormManager->RegisterClass('FinanceCharge', array(
496 'Title' => 'Parametry účtování',
497 'Table' => 'FinanceCharge',
498 'Items' => array(
499 'Internet' => array('Type' => 'Integer', 'Caption' => 'Platba Internetu', 'Default' => '0', 'Suffix' => 'Kč'),
500 'InternetSpeed' => array('Type' => 'Integer', 'Caption' => 'Rychlost Internetu', 'Default' => '0', 'Suffix' => 'Mbit/s'),
501 'InternetSpeedReserve' => array('Type' => 'Integer', 'Caption' => 'Rezerva rychlosti', 'Default' => '0', 'Suffix' => 'Mbit/s'),
502 'AdministrationPerUser' => array('Type' => 'Integer', 'Caption' => 'Správa za uživatele', 'Default' => '0', 'Suffix' => 'Kč'),
503 'kWh' => array('Type' => 'Integer', 'Caption' => 'Cena kWh', 'Default' => '0', 'Suffix' => 'Kč'),
504 'BaseSpeedElement' => array('Type' => 'Integer', 'Caption' => 'Základní díl rychlosti', 'Default' => '0', 'Suffix' => 'Mbit/s'),
505 'BaseTariffPrice' => array('Type' => 'Integer', 'Caption' => 'Základní cena tarifu', 'Default' => '0', 'Suffix' => 'Kč'),
506 'TopTariffPrice' => array('Type' => 'Integer', 'Caption' => 'Nejvyšší cena tarifu', 'Default' => '0', 'Suffix' => 'Kč'),
507 'ChangeAction' => array('Type' => 'TActionEnum', 'Caption' => 'Změna - akce', 'Default' => '', 'Null' => true),
508 'ChangeTime' => array('Type' => 'DateTime', 'Caption' => 'Změna - čas', 'Default' => '', 'Null' => true),
509 'ChangeReplaceId' => array('Type' => 'TFinanceCharge', 'Caption' => 'Změna - položka', 'Default' => '0', 'Null' => true),
510 ),
511 ));
512 $this->System->FormManager->RegisterClass('FinanceVAT', array(
513 'Title' => 'Sazby DPH',
514 'Table' => 'FinanceVAT',
515 'Items' => array(
516 'Type' => array('Type' => 'TFinanceVATType', 'Caption' => 'Typ', 'Default' => ''),
517 'ValidFrom' => array('Type' => 'Date', 'Caption' => 'Platnost od', 'Default' => ''),
518 'ValidTo' => array('Type' => 'Date', 'Caption' => 'Platnost do', 'Default' => '', 'Null' => true),
519 'Value' => array('Type' => 'Integer', 'Caption' => 'Hodnota', 'Default' => '', 'Suffix' => '%'),
520 ),
521 ));
522 $this->System->FormManager->RegisterClass('FinanceVATType', array(
523 'Title' => 'Sazby DPH',
524 'Table' => 'FinanceVATType',
525 'Items' => array(
526 'Name' => array('Type' => 'String', 'Caption' => 'Název', 'Default' => ''),
527 ),
528 ));
529 $this->System->FormManager->RegisterClass('Contract', array(
530 'Title' => 'Smlouvy',
531 'Table' => 'Contract',
532 'Items' => array(
533 'DocumentLine' => array('Type' => 'TDocumentLine', 'Caption' => 'Dokladová řada', 'Default' => ''),
534 'BillCode' => array('Type' => 'String', 'Caption' => 'Kód', 'Default' => ''),
535 'Subject' => array('Type' => 'TSubject', 'Caption' => 'Subjekt', 'Default' => ''),
536 'ValidFrom' => array('Type' => 'Date', 'Caption' => 'Platnost od', 'Default' => ''),
537 'ValidTo' => array('Type' => 'Date', 'Caption' => 'Platnost do', 'Default' => '', 'Null' => true),
538 'File' => array('Type' => 'TFile', 'Caption' => 'Soubor', 'Default' => '', 'Null' => true),
539 ),
540 'BeforeInsert' => array($this, 'BeforeInsertFinanceOperation'),
541 ));
542 $this->System->FormManager->RegisterFormType('TContract', array(
543 'Type' => 'Reference',
544 'Table' => 'Contract',
545 'Id' => 'Id',
546 'Name' => 'BillCode',
547 'Filter' => '1',
548 ));
549 $this->System->FormManager->RegisterFormType('TFinanceVAT', array(
550 'Type' => 'Reference',
551 'Table' => 'FinanceVAT',
552 'Id' => 'Id',
553 'Name' => 'Name',
554 'Filter' => '1',
555 ));
556 $this->System->FormManager->RegisterFormType('TFinanceVATType', array(
557 'Type' => 'Reference',
558 'Table' => 'FinanceVATType',
559 'Id' => 'Id',
560 'Name' => 'Name',
561 'Filter' => '1',
562 ));
563 $this->System->FormManager->RegisterFormType('TBankAccount', array(
564 'Type' => 'Reference',
565 'Table' => 'FinanceBankAccount',
566 'Id' => 'Id',
567 'Name' => 'CONCAT(`Comment`, " (", `Number`, "/", '.
568 '(SELECT `FinanceBank`.`Code` FROM `FinanceBank` WHERE `FinanceBank`.`Id`=`FinanceBankAccount`.`Bank`), ")")',
569 'Filter' => '1',
570 ));
571
572
573 $this->System->AddModule(new Bill($this->System));
574 $this->System->AddModule(new Finance($this->System));
575 $this->System->Modules['Finance']->MainSubject = $Config['Finance']['MainSubjectId'];
576 $this->System->Modules['Finance']->DirectoryId = $Config['Finance']['DirectoryId'];
577
578 $this->System->ModuleManager->Modules['IS']->RegisterDashboardItem('Finance',
579 array('ModuleFinance', 'ShowDashboardItem'));
580 }
581
582 function ShowDashboardItem()
583 {
584 $DbResult = $this->Database->select('FinanceOperation', 'ROUND(SUM(`Value`))', '1');
585 $DbRow = $DbResult->fetch_row();
586 $Output = 'Stav placení: '.$DbRow['0'].' Kč<br/>';
587 return $Output;
588 }
589
590 function DoStop()
591 {
592 }
593
594 function BeforeInsertFinanceOperation($Form)
595 {
596 if(array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
597 else $Year = date("Y", $Form->Values['ValidFrom']);
598 $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
599 $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
600 return($Form->Values);
601 }
602
603 function AfterInsertFinanceOperation($Form, $Id)
604 {
605 $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
606 $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
607 ($Form->Values['ValueUser'] * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
608 return($Form->Values);
609 }
610
611 function BeforeModifyFinanceOperation($Form, $Id)
612 {
613 $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceOperationGroup');
614 $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
615 ($Form->Values['ValueUser'] * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
616 return($Form->Values);
617 }
618
619 function BeforeInsertFinanceInvoice($Form)
620 {
621 if(array_key_exists('Time', $Form->Values)) $Year = date("Y", $Form->Values['Time']);
622 else $Year = date("Y", $Form->Values['ValidFrom']);
623 $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
624 $Form->Values['BillCode'] = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($FinanceGroup['DocumentLine'], $Year);
625 return($Form->Values);
626 }
627
628 function AfterInsertFinanceInvoice($Form, $Id)
629 {
630 $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
631 $DbResult = $this->Database->query(str_replace('#Id', $Id, $Form->Definition['Items']['ValueUser']['SQL']));
632 $DbRow = $DbResult->fetch_row();
633 $Sum = $DbRow[0];
634
635 $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
636 ($Sum * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
637 return($Form->Values);
638 }
639
640 function BeforeModifyFinanceInvoice($Form, $Id)
641 {
642 $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($Form->Values['Group'], 'FinanceInvoiceGroup');
643 $DbResult = $this->Database->query(str_replace('#Id', $Id, $Form->Definition['Items']['ValueUser']['SQL']));
644 $DbRow = $DbResult->fetch_row();
645 $Sum = $DbRow[0];
646 $this->Database->query('UPDATE `'.$Form->Definition['Table'].'` SET `Value`= '.
647 ($Sum * $FinanceGroup['ValueSign']).' WHERE `Id`='.$Id);
648 return($Form->Values);
649 }
650
651 function AfterInsertFinanceInvoiceItem($Form, $Id)
652 {
653 $ParentForm = new Form($this->System->FormManager);
654 $ParentForm->SetClass('FinanceInvoice');
655 $ParentForm->LoadValuesFromDatabase($Form->Values['FinanceInvoice']);
656 $this->AfterInsertFinanceInvoice($ParentForm, $Form->Values['FinanceInvoice']);
657 return($Form->Values);
658 }
659
660 function AfterModifyFinanceInvoiceItem($Form, $Id)
661 {
662 $ParentForm = new Form($this->System->FormManager);
663 $ParentForm->SetClass('FinanceInvoice');
664 $ParentForm->LoadValuesFromDatabase($Form->Values['FinanceInvoice']);
665 $this->BeforeModifyFinanceInvoice($ParentForm, $Form->Values['FinanceInvoice']);
666 return($Form->Values);
667 }
668}
Note: See TracBrowser for help on using the repository browser.