1 | <?php
|
---|
2 |
|
---|
3 | class PageFinanceManage extends Page
|
---|
4 | {
|
---|
5 | var $FullTitle = 'Správa financí';
|
---|
6 | var $ShortTitle = 'Správa financí';
|
---|
7 | var $ParentClass = 'PageFinance';
|
---|
8 |
|
---|
9 | function Show()
|
---|
10 | {
|
---|
11 | $Output = '';
|
---|
12 | if (!$this->System->User->CheckPermission('Finance', 'Manage'))
|
---|
13 | return ('Nemáte oprávnění');
|
---|
14 |
|
---|
15 | if (array_key_exists('Operation', $_GET)) $Operation = $_GET['Operation'];
|
---|
16 | else $Operation = '';
|
---|
17 | switch ($Operation)
|
---|
18 | {
|
---|
19 | case 'Recalculate':
|
---|
20 | $Output .= $this->System->Modules['Finance']->RecalculateMemberPayment();
|
---|
21 | break;
|
---|
22 | case 'ShowMonthlyPayment':
|
---|
23 | $Output = $this->ShowMonthlyPayment();
|
---|
24 | break;
|
---|
25 | case 'ProcessMonthlyPayment':
|
---|
26 | $Output = $this->ProcessMonthlyPayment();
|
---|
27 | break;
|
---|
28 | case 'GenerateBills':
|
---|
29 | $Output = $this->GenerateBills();
|
---|
30 | break;
|
---|
31 | case 'RegenerateInvoice':
|
---|
32 | $Output = $this->GenerateInvoice('AND (Id='.$_GET['i'].')');
|
---|
33 | break;
|
---|
34 | case 'RegenerateOperation':
|
---|
35 | $Output = $this->GenerateOperation('AND (Id='.$_GET['i'].')');
|
---|
36 | break;
|
---|
37 | default:
|
---|
38 | //$Output .= '<a href="?Operation=Recalculate">Přepočet financí</a><br />';
|
---|
39 | $Output .= '<a href="?Operation=ShowMonthlyPayment">Měsíční vyúčtování</a><br />';
|
---|
40 | $Output .= '<a href="'.$this->System->Link('/finance/zivnost/').'">Živnost</a><br />';
|
---|
41 | $Output .= '<a href="?Operation=GenerateBills">Generovat chybějící doklady</a><br />';
|
---|
42 | $Output .= '<a href="'.$this->System->Link('/finance/import/').'">Import plateb</a><br />';
|
---|
43 | }
|
---|
44 | return ($Output);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /* Get first day and last day of given billing period. Periods are aligned with year start/end. */
|
---|
48 | function GetBillingPeriod($Period)
|
---|
49 | {
|
---|
50 | $Time = time();
|
---|
51 | $Year = date('Y', $Time);
|
---|
52 |
|
---|
53 | $MonthCount = $this->System->Modules['Finance']->BillingPeriods[$Period]['MonthCount'];
|
---|
54 | if ($MonthCount <= 0) return (array('From' => NULL, 'To' => NULL, 'MonthCount' => 0));
|
---|
55 | $MonthCurrent = date('n', $Time);
|
---|
56 |
|
---|
57 | /* Get start and end of aligned period */
|
---|
58 | $MonthFrom = floor(($MonthCurrent - 1) / $MonthCount) * $MonthCount + 1;
|
---|
59 | $MonthTo = $MonthFrom + $MonthCount - 1;
|
---|
60 |
|
---|
61 | /* Use period from current month to end month so months before current month are cut out */
|
---|
62 | $MonthCount = $MonthTo - $MonthCurrent + 1;
|
---|
63 | $MonthFrom = $MonthCurrent;
|
---|
64 |
|
---|
65 | /* Get first and last day of period */
|
---|
66 | $PeriodFrom = mktime(0, 0, 0, $MonthFrom, 1, $Year);
|
---|
67 | $PeriodTo = mktime(0, 0, 0, $MonthTo, date('t', mktime(0, 0, 0, $MonthTo, 1, $Year)), $Year);
|
---|
68 |
|
---|
69 | return (array('From' => $PeriodFrom, 'To' => $PeriodTo, 'MonthCount' => $MonthCount));
|
---|
70 | }
|
---|
71 |
|
---|
72 | function ShowMonthlyPayment()
|
---|
73 | {
|
---|
74 | if (!$this->System->User->CheckPermission('Finance', 'Manage')) return ('Nemáte oprávnění');
|
---|
75 | $SQL = 'SELECT `Member`.*, `MemberPayment`.`MonthlyTotal` AS `Monthly`, '.
|
---|
76 | '`MemberPayment`.`Cash` AS `Cash`, '.
|
---|
77 | '(SELECT GROUP_CONCAT(`Service`.`Name`) FROM `ServiceCustomerRel` LEFT JOIN `Service` '.
|
---|
78 | 'ON `Service`.`Id`=`ServiceCustomerRel`.`Service` WHERE `ServiceCustomerRel`.`Customer`=`Member`.`Id` AND '.
|
---|
79 | '`ServiceCustomerRel`.`ChangeAction` IS NULL) AS `ServicesNextMonth`, '.
|
---|
80 | 'UNIX_TIMESTAMP(`Member`.`BillingPeriodLastDate`) AS `LastDate`, `Subject`.`Name` AS `SubjectName`, '.
|
---|
81 | '`FinanceBillingPeriod`.`Name` AS `BillingPeriodName` '.
|
---|
82 | 'FROM `MemberPayment` JOIN `Member` ON `Member`.`Id`=`MemberPayment`.`Member` JOIN `Subject` '.
|
---|
83 | 'ON `Subject`.`Id`=`Member`.`Subject` LEFT JOIN `FinanceBillingPeriod` ON '.
|
---|
84 | '`FinanceBillingPeriod`.`Id`=`Member`.`BillingPeriod` WHERE (`Member`.`Blocked` = 0)'.
|
---|
85 | 'AND (`Member`.`BillingPeriod` > 1) AND (`MemberPayment`.`MonthlyTotal` != 0)';
|
---|
86 |
|
---|
87 | $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$SQL.') AS T');
|
---|
88 | $DbRow = $DbResult->fetch_row();
|
---|
89 | $PageList = GetPageList('MonthlyPayment', $DbRow[0]);
|
---|
90 |
|
---|
91 | $Output = $PageList['Output'];
|
---|
92 | $Output .= '<table class="WideTable" style="font-size: small;">';
|
---|
93 |
|
---|
94 | $TableColumns = array(
|
---|
95 | array('Name' => 'SubjectName', 'Title' => 'Jméno'),
|
---|
96 | array('Name' => 'Monthly', 'Title' => 'Platba'),
|
---|
97 | array('Name' => 'Cash', 'Title' => 'Kredit'),
|
---|
98 | array('Name' => 'LastDate', 'Title' => 'Poslední fakturace'),
|
---|
99 | array('Name' => 'ServicesNextMonth', 'Title' => 'Služby'),
|
---|
100 | array('Name' => 'BillingPeriodName', 'Title' => 'Perioda'),
|
---|
101 | );
|
---|
102 | $Order = GetOrderTableHeader('MonthlyPayment', $TableColumns, 'SubjectName', 0);
|
---|
103 | $Output .= $Order['Output'];
|
---|
104 |
|
---|
105 | $Query = $SQL.' '.$Order['SQL'].$PageList['SQLLimit'];
|
---|
106 |
|
---|
107 | $DbResult = $this->Database->query($Query);
|
---|
108 | while ($Row = $DbResult->fetch_assoc())
|
---|
109 | {
|
---|
110 | $Output .= '<tr>'.
|
---|
111 | '<td>'.$Row['SubjectName'].'</td>'.
|
---|
112 | '<td>'.$Row['Monthly'].'</td>'.
|
---|
113 | '<td>'.$Row['Cash'].'</td>'.
|
---|
114 | '<td>'.date('j.n.Y', $Row['LastDate']).'</td>'.
|
---|
115 | '<td>'.$Row['ServicesNextMonth'].'</td>'.
|
---|
116 | '<td>'.$Row['BillingPeriodName'].'</td>'.
|
---|
117 | '</tr>';
|
---|
118 | }
|
---|
119 | $Output .= '</table>';
|
---|
120 | $Output .= $PageList['Output'];
|
---|
121 | $Output .= '<a href="?Operation=ProcessMonthlyPayment">Generovat faktury</a>';
|
---|
122 | return ($Output);
|
---|
123 | }
|
---|
124 |
|
---|
125 | function InsertInvoice($Subject, $TimeCreation, $TimeDue, $Items,
|
---|
126 | $Group, $PeriodFrom, $PeriodTo)
|
---|
127 | {
|
---|
128 | global $LastInsertTime;
|
---|
129 |
|
---|
130 | $Year = date('Y', $TimeCreation);
|
---|
131 | $BillCode = $this->System->Modules['Finance']->GetNextDocumentLineNumberId($Group['DocumentLine'], $Year);
|
---|
132 | $SumValue = 0;
|
---|
133 | foreach ($Items as $Item) {
|
---|
134 | $SumValue = $SumValue + $Item['Price'] * $Item['Quantity'];
|
---|
135 | }
|
---|
136 | $Finance = &$this->System->Modules['Finance'];
|
---|
137 | $SumValue = round($SumValue, $Finance->Rounding);
|
---|
138 | $this->Database->insert('FinanceInvoice', array(
|
---|
139 | 'Subject' => $Subject, 'Time' => TimeToMysqlDateTime($TimeCreation),
|
---|
140 | 'TimeDue' => TimeToMysqlDateTime($TimeDue), 'Value' => $SumValue * $Group['ValueSign'],
|
---|
141 | 'BillCode' => $BillCode,
|
---|
142 | 'PeriodFrom' => TimeToMysqlDate($PeriodFrom), 'PeriodTo' => TimeToMysqlDate($PeriodTo),
|
---|
143 | 'Generate' => 1, 'Group' => $Group['Id']));
|
---|
144 | $InvoiceId = $this->Database->insert_id;
|
---|
145 | foreach ($Items as $Item)
|
---|
146 | $this->Database->insert('FinanceInvoiceItem', array('FinanceInvoice' => $InvoiceId,
|
---|
147 | 'Description' => $Item['Description'], 'Price' => $Item['Price'],
|
---|
148 | 'Quantity' => $Item['Quantity'], 'VAT' => $Item['VAT']));
|
---|
149 | //$LastInsertTime = $Time;
|
---|
150 | //$this->CheckAdvancesAndLiabilities($Subject);
|
---|
151 | return ($InvoiceId);
|
---|
152 | }
|
---|
153 |
|
---|
154 | function ProduceInvoices()
|
---|
155 | {
|
---|
156 | $Output = '';
|
---|
157 |
|
---|
158 | // Produce accounting items
|
---|
159 | $DbResult = $this->Database->query('SELECT `Member`.*, `MemberPayment`.`MonthlyTotal` AS `MonthlyTotal`, '.
|
---|
160 | 'UNIX_TIMESTAMP(`Member`.`BillingPeriodLastDate`) AS `BillingPeriodLastUnixTime`, `Subject`.`Name` AS `SubjectName`,'.
|
---|
161 | '`MemberPayment`.`MonthlyPlus` AS `MonthlyPlus` '.
|
---|
162 | 'FROM `MemberPayment` JOIN `Member` ON `Member`.`Id`=`MemberPayment`.`Member` '.
|
---|
163 | 'JOIN `Subject` ON `Subject`.`Id`=`Member`.`Subject`');
|
---|
164 | while ($Member = $DbResult->fetch_assoc())
|
---|
165 | {
|
---|
166 | $Output .= $Member['SubjectName'].': ';
|
---|
167 | $Period = $this->GetBillingPeriod($Member['BillingPeriod']);
|
---|
168 |
|
---|
169 | /* Check if need to produce new invoice for customer */
|
---|
170 | if (($Period['MonthCount'] > 0) and ($Member['Blocked'] == 0) and
|
---|
171 | ($Period['From'] > $Member['BillingPeriodLastUnixTime']))
|
---|
172 | {
|
---|
173 | $InvoiceItems = array();
|
---|
174 | $MonthlyTotal = 0;
|
---|
175 | $DbResult2 = $this->Database->query('SELECT `Service`.* '.
|
---|
176 | 'FROM `ServiceCustomerRel` LEFT JOIN `Service` '.
|
---|
177 | 'ON `Service`.`Id`=`ServiceCustomerRel`.`Service` '.
|
---|
178 | 'WHERE (`ServiceCustomerRel`.`Customer`='.
|
---|
179 | $Member['Id'].') AND (`ServiceCustomerRel`.`ChangeAction` IS NULL) ');
|
---|
180 | while ($Service = $DbResult2->fetch_assoc())
|
---|
181 | {
|
---|
182 | $InvoiceItems[] = array('Description' => $Service['Name'], 'Price' => $Service['Price'],
|
---|
183 | 'Quantity' => $Period['MonthCount'], 'VAT' => $this->System->Modules['Finance']->GetVATByType($Service['VAT']));
|
---|
184 | $MonthlyTotal += $Service['Price'];
|
---|
185 | }
|
---|
186 | $PayPerPeriod = $MonthlyTotal * $Period['MonthCount'];
|
---|
187 | // We can't produce negative invoice except storno invoice.
|
---|
188 | // TODO: In case of negative invoice it is not sufficient to reverse invoicing direction
|
---|
189 | // Other subject should invoice only positive items. Negative items should be somehow removed.
|
---|
190 | if ($MonthlyTotal >= 0)
|
---|
191 | {
|
---|
192 | $InvoiceGroupId = INVOICE_GROUP_OUT;
|
---|
193 | } else {
|
---|
194 | $InvoiceGroupId = INVOICE_GROUP_IN;
|
---|
195 | }
|
---|
196 |
|
---|
197 | // Load invoice group
|
---|
198 | $FinanceGroup = $this->System->Modules['Finance']->GetFinanceGroupById($InvoiceGroupId, 'FinanceInvoiceGroup');
|
---|
199 | foreach ($InvoiceItems as $Index => $Item)
|
---|
200 | {
|
---|
201 | $InvoiceItems[$Index]['Price'] = $Item['Price'] * $FinanceGroup['ValueSign'];
|
---|
202 | }
|
---|
203 |
|
---|
204 | if ($PayPerPeriod != 0)
|
---|
205 | {
|
---|
206 | $TimePeriodText = date('j.n.Y', $Period['From']).' - '.date('j.n.Y', $Period['To']);
|
---|
207 | $Output .= $TimePeriodText.': '.$MonthlyTotal.' * '.$Period['MonthCount'].' = '.$PayPerPeriod;
|
---|
208 | $this->InsertInvoice($Member['Subject'], time(), time() + 3600 * 24 * INVOICE_DUE_DAYS,
|
---|
209 | $InvoiceItems, $FinanceGroup, $Period['From'], $Period['To']);
|
---|
210 |
|
---|
211 | $Output .= $this->SendPaymentEmail($Member['Id']);
|
---|
212 | }
|
---|
213 | /* Update last billing day */
|
---|
214 | $this->Database->update('Member', '`Id`='.$Member['Id'],
|
---|
215 | array('BillingPeriodLastDate' => TimeToMysqlDateTime($Period['To'])));
|
---|
216 | }
|
---|
217 | $Output .= "\n";
|
---|
218 | }
|
---|
219 | return ($Output);
|
---|
220 | }
|
---|
221 |
|
---|
222 | function TableUpdateChanges($Table)
|
---|
223 | {
|
---|
224 | $Time = time();
|
---|
225 | $DbResult = $this->Database->select($Table, '*', '(`ChangeAction` IS NOT NULL) AND '.
|
---|
226 | '(`ChangeTime` <= "'.TimeToMysqlDateTime($Time).'") ORDER BY `ChangeTime` ASC');
|
---|
227 | while ($Service = $DbResult->fetch_assoc())
|
---|
228 | {
|
---|
229 | if ($Service['ChangeAction'] == 'add')
|
---|
230 | {
|
---|
231 | unset($Service['Id']);
|
---|
232 | unset($Service['ChangeReplaceId']);
|
---|
233 | unset($Service['ChangeAction']);
|
---|
234 | unset($Service['ChangeTime']);
|
---|
235 | $this->Database->insert($Table, $Service);
|
---|
236 | } else
|
---|
237 | if ($Service['ChangeAction'] == 'modify')
|
---|
238 | {
|
---|
239 | unset($Service['Id']);
|
---|
240 | unset($Service['ChangeAction']);
|
---|
241 | $ReplaceId = $Service['ChangeReplaceId'];
|
---|
242 | unset($Service['ChangeReplaceId']);
|
---|
243 | unset($Service['ChangeTime']);
|
---|
244 | $this->Database->update($Table, '`Id`='.$ReplaceId, $Service);
|
---|
245 | } else
|
---|
246 | if ($Service['ChangeAction'] == 'delete')
|
---|
247 | {
|
---|
248 | $this->Database->delete($Table, '`Id`='.$Service['ReplaceId']);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | $this->Database->delete($Table, '(`ChangeAction` IS NOT NULL) AND (`ChangeTime` <= "'.TimeToMysqlDateTime($Time).'")');
|
---|
252 | }
|
---|
253 |
|
---|
254 | function ProcessTableUpdates()
|
---|
255 | {
|
---|
256 | // Update customers
|
---|
257 | $Output = 'Měním zákazníky...'."\n";
|
---|
258 | $this->TableUpdateChanges('Member');
|
---|
259 |
|
---|
260 | // Update finance charge
|
---|
261 | $Output = 'Měním aktuální parametry sítě...'."\n";
|
---|
262 | $this->TableUpdateChanges('FinanceCharge');
|
---|
263 |
|
---|
264 | // Update services
|
---|
265 | $Output .= 'Aktualizuji služby....'."\n";
|
---|
266 | $this->TableUpdateChanges('Service');
|
---|
267 |
|
---|
268 | // Update customer service selections
|
---|
269 | $Output .= 'Aktualizuji výběr služeb zákazníků....'."\n";
|
---|
270 | $this->TableUpdateChanges('ServiceCustomerRel');
|
---|
271 |
|
---|
272 | return ($Output);
|
---|
273 | }
|
---|
274 |
|
---|
275 | function ProcessMonthlyPayment()
|
---|
276 | {
|
---|
277 | if (!$this->System->User->CheckPermission('Finance', 'Manage')) return ('Nemáte oprávnění');
|
---|
278 | $Output = '';
|
---|
279 |
|
---|
280 | $Output .= $this->ProcessTableUpdates();
|
---|
281 |
|
---|
282 | $Finance = &$this->System->Modules['Finance'];
|
---|
283 | $Finance->LoadMonthParameters(0);
|
---|
284 |
|
---|
285 | // Načti poslední měsíční přehled a nastavení
|
---|
286 | $DbResult = $this->Database->select('FinanceMonthlyOverall', '*', '1 ORDER BY `Date` DESC LIMIT 1');
|
---|
287 | $Overall = $DbResult->fetch_array();
|
---|
288 |
|
---|
289 | $Output .= 'Datum: '.date('j.n.Y')."\n";
|
---|
290 |
|
---|
291 | $DateParts = explode('-', $Overall['Date']);
|
---|
292 | $MonthLast = $DateParts[1];
|
---|
293 | $MonthCurrent = date('m') + 0;
|
---|
294 |
|
---|
295 | $Output .= $Finance->RecalculateMemberPayment();
|
---|
296 |
|
---|
297 | $DbResult = $this->Database->query('SELECT SUM(`Cash`) FROM `MemberPayment`');
|
---|
298 | $Row = $DbResult->fetch_row();
|
---|
299 | $TotalMemberCash = $Row[0];
|
---|
300 | $Output .= 'Stav pokladny: Členové('.round($TotalMemberCash).')'."\n";
|
---|
301 |
|
---|
302 | $DbResult = $this->Database->query('SELECT SUM(`Product`.`Consumption`) AS `Consumption` FROM `StockSerialNumber` '.
|
---|
303 | 'JOIN `Product` ON `StockSerialNumber`.`Product` = `Product`.`Id` WHERE (`StockSerialNumber`.`TimeElimination` IS NULL) ');
|
---|
304 | $Row = $DbResult->fetch_row();
|
---|
305 | $TotalConsumption = $Row[0];
|
---|
306 | $TotalConsumptionCost = $Finance->W2Kc($TotalConsumption);
|
---|
307 |
|
---|
308 | $SpravaCelkem = $Finance->Sprava * $Finance->SpravaUsers;
|
---|
309 | $Output .= 'Kontrola placení (Zaplaceno-Sprava-Internet): '.$Finance->TotalPaid.'-'.$SpravaCelkem.'-'.$Finance->Internet.'='.($Finance->TotalPaid - $SpravaCelkem - $Finance->Internet)."\n";
|
---|
310 |
|
---|
311 | // Zkontrolovat odečtení měsíčního poplatku
|
---|
312 | $Output .= 'Kontrola odečtení poplatků: Poslední měsíc-'.$MonthLast.' Aktuální měsíc-'.$MonthCurrent."\n";
|
---|
313 | if ($MonthCurrent != $MonthLast)
|
---|
314 | {
|
---|
315 | $Output .= 'Odečítám pravidelný poplatek...'."\n";
|
---|
316 | $Output .= $this->ProduceInvoices();
|
---|
317 |
|
---|
318 | $Output .= 'Přidávám měsíční přehled...'."\n";
|
---|
319 | $DbResult = $this->Database->query('SELECT * FROM `FinanceCharge` WHERE (`ChangeAction` IS NULL) LIMIT 1');
|
---|
320 | $Charge = $DbResult->fetch_assoc();
|
---|
321 | $this->Database->insert('FinanceMonthlyOverall', array('Date' => 'NOW()',
|
---|
322 | 'Money' => $Finance->Internet, 'kWh' => $Finance->kWh,
|
---|
323 | 'Administration' => $Finance->Sprava, 'AdministrationTotal' => $SpravaCelkem,
|
---|
324 | 'ConsumptionTotal' => $TotalConsumptionCost, 'TotalPaid' => $Finance->TotalPaid,
|
---|
325 | 'BaseTariffPrice' => $Charge['BaseTariffPrice'],
|
---|
326 | 'TopTariffPrice' => $Charge['TopTariffPrice'], 'MemberCount' => $Finance->InternetUsers));
|
---|
327 |
|
---|
328 | $Finance->RecalculateMemberPayment();
|
---|
329 |
|
---|
330 | // Restart traffic shaping
|
---|
331 | //$this->Database->update('NetworkConfiguration', 'Id = 3', array('Changed' => 1));
|
---|
332 | //flush();
|
---|
333 | //$this->GenerateBills();
|
---|
334 | $this->System->ModuleManager->Modules['Log']->NewRecord('Finance', 'ProcessMonthlyPayment', $Output);
|
---|
335 | }
|
---|
336 | $Output = str_replace("\n", '<br/>', $Output);
|
---|
337 | return ($Output);
|
---|
338 | }
|
---|
339 |
|
---|
340 | function SendPaymentEmail($MemberId, $FileId = '')
|
---|
341 | {
|
---|
342 | global $Config;
|
---|
343 |
|
---|
344 | $DbResult = $this->Database->select('Member', '*', '`Id`='.$MemberId);
|
---|
345 | $Member = $DbResult->fetch_assoc();
|
---|
346 |
|
---|
347 | $DbResult = $this->Database->select('MemberPayment', '*', '`Member`='.$MemberId);
|
---|
348 | $MemberPayment = $DbResult->fetch_assoc();
|
---|
349 |
|
---|
350 | $DbResult = $this->Database->select('Subject', 'Name', '`Id`='.$Member['Subject']);
|
---|
351 | $Subject = $DbResult->fetch_assoc();
|
---|
352 |
|
---|
353 | $DbResult = $this->Database->select('User', '*', '`Id`='.$Member['ResponsibleUser']);
|
---|
354 | $User = $DbResult->fetch_assoc();
|
---|
355 |
|
---|
356 | $DbResult = $this->Database->select('Subject', '*', '`Id`='.$Config['Finance']['MainSubjectId']);
|
---|
357 | $MainSubject = $DbResult->fetch_assoc();
|
---|
358 |
|
---|
359 | $Period = $this->GetBillingPeriod($Member['BillingPeriod']);
|
---|
360 |
|
---|
361 | $DbResult = $this->Database->query('SELECT `FinanceBankAccount`.*, '.
|
---|
362 | 'CONCAT(`FinanceBankAccount`.`Number`, "/", `FinanceBank`.`Code`) AS `NumberFull` FROM `FinanceBankAccount` '.
|
---|
363 | 'JOIN `FinanceBank` ON `FinanceBank`.`Id`=`FinanceBankAccount`.`Bank` '.
|
---|
364 | 'WHERE (`FinanceBankAccount`.`Subject`='.$Config['Finance']['MainSubjectId'].') '.
|
---|
365 | 'AND (`FinanceBankAccount`.`Use`=1)');
|
---|
366 | $MainSubjectAccount = $DbResult->fetch_assoc();
|
---|
367 |
|
---|
368 | if ($User['Email'] != '')
|
---|
369 | {
|
---|
370 | $Title = 'Pravidelné vyúčtování služeb';
|
---|
371 | $Content = 'Vyúčtovaní zákazníka <strong>'.$Subject['Name'].'</strong> zastoupeného uživatelem <strong>'.
|
---|
372 | $User['Name'].'</strong> ke dni <strong>'.$this->System->HumanDate(time()).'</strong>.<br/><br/>'."\n".
|
---|
373 | 'Vaše aktuální služby: ';
|
---|
374 | $DbResult = $this->Database->query('SELECT GROUP_CONCAT(`Service`.`Name`) AS `Name` FROM `ServiceCustomerRel` LEFT JOIN `Service` '.
|
---|
375 | 'ON `Service`.`Id`=`ServiceCustomerRel`.`Service` WHERE (`ServiceCustomerRel`.`Customer`='.$Member['Id'].') '.
|
---|
376 | 'AND (`ServiceCustomerRel`.`ChangeAction` IS NULL)');
|
---|
377 | $Service = $DbResult->fetch_assoc();
|
---|
378 | $Content .= '<strong>'.$Service['Name'].'</strong><br />'."\n".
|
---|
379 | 'Vaše platební období: <strong>'.$this->System->Modules['Finance']->BillingPeriods[$Member['BillingPeriod']]['Name'].'</strong><br />'."\n".
|
---|
380 | 'Pravidelná platba za období: <strong>'.($MemberPayment['MonthlyTotal'] * $Period['MonthCount']).' Kč</strong><br />'."\n".
|
---|
381 | 'Bankovní účet: <strong>'.$MainSubjectAccount['NumberFull'].'</strong><br/>'."\n".
|
---|
382 | 'Variabilní symbol: <strong>'.$Member['Subject'].'</strong><br/>'."\n".
|
---|
383 | 'Stav vašeho účtu: <strong>'.($MemberPayment['Cash'] - $MemberPayment['MonthlyTotal'] * $Period['MonthCount']).' Kč</strong><br /><br />'."\n";
|
---|
384 | $Content .= 'Nové finanční operace:<br/>'.
|
---|
385 | '<table style="margin-left: auto; margin-right: auto; border-style: solid; border-width: 1px; border-collapse: collapse;">'.
|
---|
386 | '<tr><th style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center; font-weight: bold;">Čas</th>'.
|
---|
387 | '<th style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center; font-weight: bold;">Popis</th>'.
|
---|
388 | '<th style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center; font-weight: bold;">Částka [Kč]</th></tr>'."\n";
|
---|
389 | $DbResult = $this->Database->query('SELECT T1.* FROM ((SELECT `Text`, `Time`, `Value`, `File` FROM `FinanceOperation` WHERE (`Subject`='.$Member['Subject'].')) UNION ALL '.
|
---|
390 | '(SELECT (SELECT GROUP_CONCAT(`Description` SEPARATOR ", ") FROM `FinanceInvoiceItem` '.
|
---|
391 | 'WHERE `FinanceInvoiceItem`.`FinanceInvoice` = `FinanceInvoice`.`Id`) AS `Text`, '.
|
---|
392 | '`Time`, -`Value`, `File` FROM `FinanceInvoice` WHERE (`Subject`='.
|
---|
393 | $Member['Subject'].')) ORDER BY `Time` DESC) AS `T1` WHERE (`T1`.`Time` > "'.$Member['BillingPeriodLastDate'].'")');
|
---|
394 | while ($DbRow = $DbResult->fetch_assoc())
|
---|
395 | {
|
---|
396 | $Text = $DbRow['Text'];
|
---|
397 | $Content .= '<tr><td style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center;">'.HumanDate($DbRow['Time']).'</td>'.
|
---|
398 | '<td style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center;">'.$Text.'</td>'.
|
---|
399 | '<td style="border-style: solid; border-width: 1px; padding: 1px 5px 1px 5px; text-align: center;">'.$DbRow['Value'].'</td></tr>'."\n";
|
---|
400 | }
|
---|
401 | $Content .= '</table><br />'."\n".
|
---|
402 | 'Pro aktuální informace, prohlížení elektronických dokladů a možnost změny údajů se prosím přihlašte na stránkách '.
|
---|
403 | '<a href="http://'.$Config['Web']['Host'].$Config['Web']['RootFolder'].'">http://'.
|
---|
404 | $Config['Web']['Host'].$Config['Web']['RootFolder'].'</a>.<br /><br />'."\n";
|
---|
405 |
|
---|
406 | $Content .= '<br />Tento email je generován automaticky. V případě zjištění nesrovnalostí napište zpět.';
|
---|
407 | $this->System->ModuleManager->Modules['EmailQueue']->AddItem($User['Name'].' <'.$User['Email'].'>', $Title, $Content,
|
---|
408 | $Config['Web']['Admin'].' <'.$Config['Web']['AdminEmail'].'>');
|
---|
409 | $Output = '';
|
---|
410 | } else $Output = 'Uživatel '.$User['Name'].' nemá email.';
|
---|
411 | return $Output;
|
---|
412 | }
|
---|
413 |
|
---|
414 | function GenerateInvoice($Where)
|
---|
415 | {
|
---|
416 | $Output = '';
|
---|
417 | $DbResult = $this->Database->query('SELECT * FROM `FinanceInvoice` WHERE (`BillCode` <> "") '.
|
---|
418 | 'AND (`Value` != 0) AND (`Generate` = 1)'.$Where);
|
---|
419 | while ($Row = $DbResult->fetch_assoc())
|
---|
420 | {
|
---|
421 | if ($Row['File'] == null)
|
---|
422 | {
|
---|
423 | $DbResult2 = $this->Database->insert('File', array('Name' => '', 'Size' => 0,
|
---|
424 | 'Directory' => $this->System->Modules['Finance']->DirectoryId, 'Time' => 'NOW()'));
|
---|
425 | $FileId = $this->Database->insert_id;
|
---|
426 | } else $FileId = $Row['File'];
|
---|
427 | $FileName = 'doklad-'.$FileId.'.pdf';
|
---|
428 | $Bill = new BillInvoice($this->System);
|
---|
429 | $Bill->Database = &$this->System->Database;
|
---|
430 | $Bill->System = &$this->System;
|
---|
431 | $Bill->InvoiceId = $Row['Id'];
|
---|
432 | $FullFileName = $this->System->Modules['File']->GetDir($this->System->Modules['Finance']->DirectoryId).$FileName;
|
---|
433 | $Bill->SaveToFile($FullFileName);
|
---|
434 | if (file_exists($FullFileName))
|
---|
435 | {
|
---|
436 | $this->Database->update('File', 'Id='.$FileId, array('Name' => $FileName, 'Size' => filesize($FullFileName)));
|
---|
437 | $this->Database->update('FinanceInvoice', 'Id='.$Row['Id'], array('File' => $FileId));
|
---|
438 | $Output .= 'Faktura '.$Row['Id'].' vygenerována do souboru '.$FileName.'<br/>'."\n";
|
---|
439 | } else $Output .= 'Soubor "'.$FullFileName.'" se nepodařilo uložit.';
|
---|
440 | }
|
---|
441 | return $Output;
|
---|
442 | }
|
---|
443 |
|
---|
444 | function GenerateOperation($Where)
|
---|
445 | {
|
---|
446 | $Output = '';
|
---|
447 | $DbResult = $this->Database->query('SELECT * FROM `FinanceOperation` WHERE (`BillCode` <> "") '.
|
---|
448 | 'AND (`Value` != 0) AND (`Generate` = 1)'.$Where);
|
---|
449 | while ($Row = $DbResult->fetch_assoc())
|
---|
450 | {
|
---|
451 | if ($Row['File'] == null)
|
---|
452 | {
|
---|
453 | $DbResult2 = $this->Database->insert('File', array('Name' => '', 'Size' => 0,
|
---|
454 | 'Directory' => $this->System->Modules['Finance']->DirectoryId, 'Time' => 'NOW()'));
|
---|
455 | $FileId = $this->Database->insert_id;
|
---|
456 | } else $FileId = $Row['File'];
|
---|
457 | $FileName = 'doklad2-'.$FileId.'.pdf';
|
---|
458 | $Bill = new BillOperation($this->System);
|
---|
459 | $Bill->Database = &$this->System->Database;
|
---|
460 | $Bill->System = &$this->System;
|
---|
461 | $Bill->OperationId = $Row['Id'];
|
---|
462 | $FullFileName = $this->System->Modules['File']->GetDir($this->System->Modules['Finance']->DirectoryId).$FileName;
|
---|
463 | $Bill->SaveToFile($FullFileName);
|
---|
464 | if (file_exists($FullFileName))
|
---|
465 | {
|
---|
466 | $this->Database->update('File', 'Id='.$FileId, array('Name' => $FileName, 'Size' => filesize($FullFileName)));
|
---|
467 | $this->Database->update('FinanceOperation', 'Id='.$Row['Id'], array('File' => $FileId));
|
---|
468 | $Output .= 'Doklad pro platbu '.$Row['Id'].' vygenerován do souboru '.$FileName.'<br/>'."\n";
|
---|
469 | } else $Output .= 'Soubor "'.$FullFileName.'" se nepodařilo uložit.';
|
---|
470 | }
|
---|
471 | return $Output;
|
---|
472 | }
|
---|
473 |
|
---|
474 | function GenerateBills()
|
---|
475 | {
|
---|
476 | $Output = '';
|
---|
477 | // Generate PDF files for new invoices and operations
|
---|
478 | $Output .= $this->GenerateInvoice(' AND (`File` IS NULL)');
|
---|
479 | $Output .= $this->GenerateOperation(' AND (`File` IS NULL)');
|
---|
480 | return ($Output);
|
---|
481 | }
|
---|
482 | }
|
---|