source: www/finance/finance.php@ 157

Last change on this file since 157 was 157, checked in by george, 16 years ago
  • Upraveno: Soubor include.php ve financích přepracován na třídy ve finance.php.
File size: 5.6 KB
Line 
1<?php
2
3class Finance extends Module
4{
5 var $kWh;
6 var $Internet;
7 var $Sprava;
8 var $DatumOdecteni;
9 var $InternetUsers;
10 var $SpravaUsers;
11 var $InternetSegmentId = 21;
12 var $MaxSpeed;
13 var $RealMaxSpeed;
14 var $SpeedReserve;
15 var $BaseSpeedElement;
16 var $TotalConsumption;
17 var $UserIdNetwork = 46;
18 var $BaseTariffPrice;
19 var $TopTariffPrice;
20 var $TotalPaid;
21 var $TotalInternetPaid;
22 var $Tarify;
23
24 function RecalculateTariffs($Period = 1)
25 {
26 $ResidualPrice = $this->Internet;
27 $ResidualSpeed = $this->MaxSpeed;
28
29 $Tarify = array();
30 $DbResult = $this->Database->select('finance_tariffs', '*', 'period='.$Period.' ORDER BY id,speed_factor');
31 while($Tariff = $DbResult->fetch_array())
32 {
33 $Tarify[$Tariff['id']] = $Tariff;
34 }
35
36 if($Period == 0) $Column = 'now'; else $Column = 'next';
37 $TotalUserCount = 0;
38 $TotalUnits = 0;
39 $TotalTarifUnits = 0;
40 $TotalTarifSpeedUnits = 0;
41 foreach($Tarify as $Index => $Tarif)
42 {
43 $DbResult = $this->Database->select('users', 'COUNT(*)', 'inet_tarif_'.$Column.'='.$Index.' AND inet=1 AND role=2');
44 $Row = $DbResult->fetch_array();
45 $Tarify[$Index]['user_count'] = $Row[0];
46 switch($Tarif['group_id'])
47 {
48 case 1:
49 $TotalUserCount = $TotalUserCount + $Tarify[$Index]['user_count'];
50 $Tarify[$Index]['total_units'] = $Tarify[$Index]['price_units'] * $Tarify[$Index]['user_count'];
51 $TotalTarifUnits = $TotalTarifUnits + $Tarify[$Index]['total_units'];
52
53 if($Index == 3) // Zarizeni site v tarifu 3 neplati penize, ale ma internet
54 $Tarify[$Index]['total_speed_factor'] = $Tarify[$Index]['speed_factor'] * ($Tarify[$Index]['user_count']+1);
55 else $Tarify[$Index]['total_speed_factor'] = $Tarify[$Index]['speed_factor'] * $Tarify[$Index]['user_count'];
56 $TotalTarifSpeedUnits = $TotalTarifSpeedUnits + $Tarify[$Index]['total_speed_factor'];
57 break;
58 case 2:
59 $ResidualPrice -= $Tarif['price'] * $Tarify[$Index]['user_count'];
60 $ResidualSpeed -= $Tarif['min_speed'] * $Tarify[$Index]['user_count'];
61 $Tarify[$Index]['total_units'] = $Tarify[$Index]['price_units'] * $Tarify[$Index]['user_count'];
62 break;
63 case 3:
64 $Tarify[$Index]['total_units'] = $Tarify[$Index]['price_units'] * $Tarify[$Index]['user_count'];
65 break;
66 }
67 }
68 //$PricePerUnit = $TopTariffPrice / ; // $ResidualPrice / $TotalTarifUnits;
69 $SpeedPerUnit = $ResidualSpeed * 1024 / $TotalTarifSpeedUnits;
70
71 // Recalculate price
72 foreach($Tarify as $Index => $Tarif)
73 {
74 switch($Tarif['group_id'])
75 {
76 case 1:
77 $Tarify[$Index]['price'] = $this->BaseTariffPrice + round($this->TopTariffPrice * $Tarif['price_units'] / 10) * 10;
78 $Tarify[$Index]['min_speed'] = round($Tarif['speed_factor'] * $SpeedPerUnit);
79 $Tarify[$Index]['max_speed'] = round($Tarify[$Index]['speed_factor'] * $this->BaseSpeedElement * 1024);
80 break;
81 case 2:
82 $Tarify[$Index]['min_speed'] = $Tarify[$Index]['min_speed'] * 1024;
83 $Tarify[$Index]['max_speed'] = $Tarify[$Index]['max_speed'] * 1024;
84 break;
85 case 3:
86 $Tarify[$Index]['price'] = $this->BaseTariffPrice + round($this->TopTariffPrice * $Tarif['price_units']);
87 $Tarify[$Index]['min_speed'] = $Tarify[$Index]['min_speed'] * 1024;
88 $Tarify[$Index]['max_speed'] = $Tarify[$Index]['max_speed'] * 1024;
89 break;
90 }
91 $Tarify[$Index]['aggregation'] = '1:'.round($Tarify[$Index]['max_speed'] / $Tarify[$Index]['min_speed']);
92 }
93 $this->Tarify = $Tarify;
94 }
95
96 function LoadMonthParameters($Period = 1) // 0 - now, 1 - next month
97 {
98 $DbResult = $this->Database->query("SELECT * FROM finance_charge WHERE period=".$Period);
99 $Row = $DbResult->fetch_array();
100 $this->kWh = $Row['kWh'];
101 $this->Internet = $Row['internet'];
102 $this->Sprava = $Row['administration_per_user'];
103 $this->RealMaxSpeed = $Row['internet_speed'];
104 $this->SpeedReserve = $Row['internet_speed_reserve'];
105 $this->BaseSpeedElement = $Row['base_speed_element'];
106 $this->MaxSpeed = $this->RealMaxSpeed - $this->SpeedReserve;
107 $this->TopTariffPrice = $Row['TopTariffPrice'];
108 $this->BaseTariffPrice = $Row['BaseTariffPrice'];
109
110 $DbResult = $this->Database->query("SELECT COUNT(*) FROM users WHERE inet=1 AND role=2");
111 $Row = $DbResult->fetch_array();
112 $this->InternetUsers = $Row[0];
113
114 $DbResult = $this->Database->query("SELECT COUNT(*) FROM users WHERE overheads=1 AND role=2");
115 $SpravaUsers = $DbResult->fetch_array();
116 $this->SpravaUsers = $SpravaUsers[0];
117 $DbResult = $this->Database->query("SELECT SUM(consumption) FROM network_segments");
118 $TotalConsumption = $DbResult->fetch_array();
119 $this->TotalConsumption = $TotalConsumption[0];
120
121 $DbResult = $this->Database->query("SELECT SUM(`monthly`) as `internet`, SUM(`monthly` - `plus`) as `real` FROM users WHERE role=2");
122 $Row = $DbResult->fetch_array();
123 $this->TotalInternetPaid = $Row['internet'];
124 $this->TotalPaid = $Row['real'];
125
126 $this->RecalculateTariffs($Period);
127 }
128
129 function W2Kc($Spotreba)
130 {
131 return(round($Spotreba * 0.72 * $this->kWh));
132 }
133
134 function GetNextDocumentLineNumber($Id)
135 {
136 $DbResult = $this->Database->query('SELECT Shortcut, NextNumber FROM DocumentLine WHERE Id='.$Id);
137 $DbRow = $DbResult->fetch_assoc();
138 $Result = $DbRow['Shortcut'].$DbRow['NextNumber'];
139 $this->Database->query('UPDATE DocumentLine SET NextNumber = NextNumber + 1 WHERE Id='.$Id);
140 return($Result);
141 }
142}
143
144?>
Note: See TracBrowser for help on using the repository browser.