1 | <?php
|
---|
2 |
|
---|
3 | class PageFinanceUserState extends Page
|
---|
4 | {
|
---|
5 | function __construct(System $System)
|
---|
6 | {
|
---|
7 | parent::__construct($System);
|
---|
8 | $this->Title = 'Stav financí';
|
---|
9 | $this->Description = 'Stav financí účastníka';
|
---|
10 | $this->ParentClass = 'PageUser';
|
---|
11 | }
|
---|
12 |
|
---|
13 | function ShowFinanceOperation(int $SubjectId): string
|
---|
14 | {
|
---|
15 | $UserOperationTableQuery = '((SELECT `Text`, `FinanceOperation`.`Time`, `Value`, `File`.`Hash` AS `FileHash`, `BillCode`, NULL AS `PeriodFrom`, NULL AS `PeriodTo`, `DocumentLineCode`.`Name` AS `BillName` '.
|
---|
16 | 'FROM `FinanceOperation` '.
|
---|
17 | 'LEFT JOIN `DocumentLineCode` ON `DocumentLineCode`.`Id`=`FinanceOperation`.`BillCode` '.
|
---|
18 | 'LEFT JOIN `File` ON `File`.`Id`=`FinanceOperation`.`File` '.
|
---|
19 | 'WHERE (`Subject`='.$SubjectId.') '.
|
---|
20 | ') UNION ALL '.
|
---|
21 | '(SELECT (SELECT GROUP_CONCAT(`Description` SEPARATOR ",") FROM `FinanceInvoiceItem` WHERE `FinanceInvoice`=`FinanceInvoice`.`Id`) AS `Text`, '.
|
---|
22 | '`FinanceInvoice`.`Time`, -`Value`, `File`.`Hash` AS `FileHash`, `BillCode`, `PeriodFrom`, `PeriodTo`, `DocumentLineCode`.`Name` AS `BillName` '.
|
---|
23 | 'FROM `FinanceInvoice` '.
|
---|
24 | 'LEFT JOIN `DocumentLineCode` ON `DocumentLineCode`.`Id`=`FinanceInvoice`.`BillCode` '.
|
---|
25 | 'LEFT JOIN `File` ON `File`.`Id`=`FinanceInvoice`.`File` '.
|
---|
26 | 'WHERE (`Subject`='.$SubjectId.') AND (`VisibleToUser` = 1)))';
|
---|
27 |
|
---|
28 | $Output = '<div style="text-align:center">Výpis finančních operací</div>';
|
---|
29 | $DbResult = $this->Database->query('SELECT COUNT(*) FROM '.$UserOperationTableQuery.' AS `T1`');
|
---|
30 | $DbRow = $DbResult->fetch_row();
|
---|
31 | $PageList = GetPageList('FinanceOperation', $DbRow[0]);
|
---|
32 |
|
---|
33 | $Output .= $PageList['Output'];
|
---|
34 | $Output .= '<table class="WideTable" style="font-size: small;">';
|
---|
35 |
|
---|
36 | $TableColumns = array(
|
---|
37 | array('Name' => 'Time', 'Title' => 'Datum'),
|
---|
38 | array('Name' => 'Text', 'Title' => 'Popis'),
|
---|
39 | array('Name' => 'Value', 'Title' => 'Změna [Kč]'),
|
---|
40 | array('Name' => 'State', 'Title' => 'Zůstatek [Kč]'),
|
---|
41 | array('Name' => 'PeriodFrom', 'Title' => 'Období'),
|
---|
42 | array('Name' => 'BillCode', 'Title' => 'Doklad'),
|
---|
43 | );
|
---|
44 | $Order = GetOrderTableHeader('FinanceOperation', $TableColumns, 'Time', 1);
|
---|
45 | $Output .= $Order['Output'];
|
---|
46 |
|
---|
47 | $StateQuery = 'SELECT SUM(`T2`.`Value`) FROM '.$UserOperationTableQuery.
|
---|
48 | ' AS `T2` WHERE `T2`.`Time` <= `T1`.`Time` ';
|
---|
49 | $Query = 'SELECT *, ('.$StateQuery.') AS `State` FROM '.$UserOperationTableQuery.' AS `T1` '.$Order['SQL'].$PageList['SQLLimit'];
|
---|
50 |
|
---|
51 | $DbResult = $this->Database->query($Query);
|
---|
52 | $SumValue = 0;
|
---|
53 | while ($Row = $DbResult->fetch_assoc())
|
---|
54 | {
|
---|
55 | $Row['State'] = round($Row['State'], 2);
|
---|
56 | if ($Row['State'] > 0) $Row['State'] = '<span style="color:green;">'.$Row['State'].'</span>';
|
---|
57 | if ($Row['State'] < 0) $Row['State'] = '<span style="color:red;">'.$Row['State'].'</span>';
|
---|
58 | if ($Row['Value'] == -0) $Row['Value'] = 0;
|
---|
59 | if ($Row['Value'] > 0) $Row['Value'] = '+'.$Row['Value'];
|
---|
60 | if ($Row['BillName'] == '') $Row['BillName'] = 'PDF';
|
---|
61 | if ($Row['FileHash'] != '') $Invoice = '<a href="'.$this->System->Link('/file?h='.$Row['FileHash']).'">'.$Row['BillName'].'</a>';
|
---|
62 | else $Invoice = NotBlank($Row['BillName']);
|
---|
63 | if ($Row['PeriodFrom'] != '') $Period = HumanDate($Row['PeriodFrom']).' - '.HumanDate($Row['PeriodTo']);
|
---|
64 | else $Period = ' ';
|
---|
65 | $Output .= '<tr><td style="text-align: right;">'.HumanDate($Row['Time']).'</td>'.
|
---|
66 | '<td style="text-align: left;">'.$Row['Text'].'</td>'.
|
---|
67 | '<td style="text-align: right;">'.round($Row['Value'], 2).'</td>'.
|
---|
68 | '<td style="text-align: right;">'.$Row['State'].'</td>'.
|
---|
69 | '<td>'.$Period.'</td>'.
|
---|
70 | '<td>'.$Invoice.'</td></tr>';
|
---|
71 | $SumValue = $SumValue + $Row['Value'];
|
---|
72 | }
|
---|
73 | $Output .= '</table>';
|
---|
74 | $Output .= $PageList['Output'];
|
---|
75 | return $Output;
|
---|
76 | }
|
---|
77 |
|
---|
78 | function Show(): string
|
---|
79 | {
|
---|
80 | $Finance = &ModuleFinance::Cast($this->System->GetModule('Finance'))->Finance;
|
---|
81 | $Finance->LoadMonthParameters(0);
|
---|
82 |
|
---|
83 | // Determine which customer should be displayed
|
---|
84 | if (array_key_exists('i', $_GET))
|
---|
85 | {
|
---|
86 | if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Finance', 'Manage')) return 'Nemáte oprávnění';
|
---|
87 | $CustomerId = $_GET['i'];
|
---|
88 | } else
|
---|
89 | {
|
---|
90 | if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('Finance', 'DisplaySubjectState')) return 'Nemáte oprávnění';
|
---|
91 | $UserId = ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'];
|
---|
92 | $DbResult = $this->Database->query('SELECT `Customer` FROM `UserCustomerRel` WHERE `User`='.$UserId.' LIMIT 1');
|
---|
93 | if ($DbResult->num_rows > 0)
|
---|
94 | {
|
---|
95 | $CustomerUserRel = $DbResult->fetch_assoc();
|
---|
96 | $CustomerId = $CustomerUserRel['Customer'];
|
---|
97 | } else return $this->SystemMessage('Chyba', 'Nejste zákazníkem');
|
---|
98 | }
|
---|
99 |
|
---|
100 | // Load customer info
|
---|
101 | $DbResult = $this->Database->query('SELECT * FROM `Member` WHERE `Id`='.$CustomerId);
|
---|
102 | if ($DbResult->num_rows == 1)
|
---|
103 | {
|
---|
104 | $Customer = $DbResult->fetch_assoc();
|
---|
105 | } else return $this->SystemMessage('Položka nenalezena', 'Zákazník nenalezen');
|
---|
106 |
|
---|
107 | // Load subject info
|
---|
108 | $DbResult = $this->Database->query('SELECT * FROM `Subject` WHERE `Id`='.$Customer['Subject']);
|
---|
109 | if ($DbResult->num_rows == 1)
|
---|
110 | {
|
---|
111 | $Subject = $DbResult->fetch_assoc();
|
---|
112 | } else return $this->SystemMessage('Položka nenalezena', 'Subjekt nenalezen');
|
---|
113 |
|
---|
114 | $Output = '<table width="100%" border="0" cellspacing="0" cellpadding="3"><tr><td valign="top">';
|
---|
115 |
|
---|
116 | // Account state
|
---|
117 | $UserOperationTableQuery = '((SELECT `Text`, `Time`, `Value`, `File`, `BillCode`, NULL AS `PeriodFrom`, NULL AS `PeriodTo` '.
|
---|
118 | 'FROM `FinanceOperation` WHERE (`Subject`='.$Subject['Id'].')) UNION ALL '.
|
---|
119 | '(SELECT (SELECT GROUP_CONCAT(`Description` SEPARATOR ",") FROM `FinanceInvoiceItem` WHERE `FinanceInvoice`=`FinanceInvoice`.`Id`) AS `Text`, '.
|
---|
120 | '`Time`, -`Value`, `File`, `BillCode`, `PeriodFrom`, `PeriodTo` FROM `FinanceInvoice` WHERE (`Subject`='.$Subject['Id'].')) ORDER BY `Time` DESC) AS `T1`';
|
---|
121 | $DbResult = $this->Database->query('SELECT SUM(`T1`.`Value`) AS `Total` FROM '.$UserOperationTableQuery);
|
---|
122 | $DbRow = $DbResult->fetch_array();
|
---|
123 | $Total = $DbRow['Total'];
|
---|
124 | $Output .= 'Stav účtu: <strong>'.round($Total, 2).' Kč</strong><br /><br />';
|
---|
125 |
|
---|
126 | // Tabulka operaci
|
---|
127 | $Output .= $this->ShowFinanceOperation($Subject['Id']);
|
---|
128 |
|
---|
129 | $Output .= '</td><td valign="top">';
|
---|
130 |
|
---|
131 | $DbResult = $this->Database->query('SELECT FinanceBankAccount.*, CONCAT(FinanceBankAccount.Number, "/", FinanceBank.Code) AS NumberFull FROM FinanceBankAccount '.
|
---|
132 | 'JOIN FinanceBank ON FinanceBank.Id=FinanceBankAccount.Bank '.
|
---|
133 | 'WHERE (FinanceBankAccount.`Subject`='.$this->System->Config['Finance']['MainSubjectId'].') AND (FinanceBankAccount.`Use`=1)');
|
---|
134 | $SubjectFromAccount = $DbResult->fetch_assoc();
|
---|
135 | $Account = $SubjectFromAccount['NumberFull'];
|
---|
136 |
|
---|
137 | $Output .= 'Účet pro platby: <strong>'.$Account.'</strong><br/>';
|
---|
138 | $Output .= 'Variabilní symbol pro platby: <strong>'.$Subject['Id'].'</strong><br/>';
|
---|
139 | $Output .= 'Subjekt: <strong>'.$Subject['Name'].'</strong><br/>';
|
---|
140 | $Output .= '<br/>';
|
---|
141 |
|
---|
142 | $Total = 0;
|
---|
143 | $Output .= 'Rozpis měsíčního poplatku:<br><table class="WideTable">'.
|
---|
144 | '<tr><th>Služba</th><th>Cena [Kč]</th></tr>';
|
---|
145 | $DbResult = $this->Database->query('SELECT `Service`.`Name`, `Service`.`Price` FROM `ServiceCustomerRel` '.
|
---|
146 | 'LEFT JOIN `Service` ON `Service`.`Id`=`ServiceCustomerRel`.`Service` '.
|
---|
147 | 'WHERE (`ServiceCustomerRel`.`Customer`='.$Customer['Id'].') AND (`ServiceCustomerRel`.`ChangeAction` IS NULL)');
|
---|
148 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
149 | {
|
---|
150 | $Output .= '<tr><td>'.$DbRow['Name'].'</td><td>'.$DbRow['Price'].'</td></tr>';
|
---|
151 | $Total += $DbRow['Price'];
|
---|
152 | }
|
---|
153 |
|
---|
154 | $Output .= '<tr><td><strong>Celkem</strong></td><td><strong>'.$Total.'</strong></td></tr></table>';
|
---|
155 | //echo('Tarif pro příští měsíc: '.$Tarify[$Row2['inet_tarif_next']]['name'].'<br><br>');
|
---|
156 | $Output .= '<br />';
|
---|
157 |
|
---|
158 | $Output .= '</td></tr></table>';
|
---|
159 | return $Output;
|
---|
160 | }
|
---|
161 | }
|
---|