Changeset 50 for www/finance/prepocet.php
- Timestamp:
- Feb 24, 2008, 2:07:52 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
www/finance/prepocet.php
r46 r50 1 1 <?php 2 include('../ global.php');2 include('../style.php'); 3 3 include('include.php'); 4 include('bills.php'); 5 6 ShowHeader('Přepočet financí', 'Přepočet financí'); 4 7 5 8 function RecalculateUsersFinance() 6 9 { 7 global $InternetSegmentId, $Tarify, $Sprava, $Database; 8 9 $DbResult = $Database->query("SELECT * FROM users WHERE role=0"); // Select network members only 10 global $InternetSegmentId, $Tarify, $Sprava, $Database, $UserIdNetwork; 11 12 echo("Aktualizuju finance uživatelů...<br>\n"); 13 $DbResult = $Database->query("SELECT * FROM users WHERE role=2 OR id=".$UserIdNetwork); // Select network members only 10 14 while($Row = $DbResult->fetch_array()) 11 15 { … … 14 18 $DbResult2 = $Database->query("SELECT SUM(consumption) FROM network_devices WHERE user=".$Row['id']." AND used=1"); 15 19 $Row4 = $DbResult2->fetch_array(); 16 //DB_Update('users','id='.$Row['id'], array());17 20 18 21 $Price = 0; … … 34 37 $MonthlyInet = $Tarify[$Row['inet_tarif_next']]['price']; 35 38 if($Row['inet'] == 1) $Monthly += $MonthlyInet; 36 if($Row['overheads'] == 1) $Monthly += $Sprava+ W2Kc($Consumption);39 //if($Row['overheads'] == 1) $Monthly += $Sprava; // + W2Kc($Consumption); 37 40 //echo($Row['fullname'].' '.$Row['inet'].' '.$Monthly.'<br>'); 38 41 $Monthly = round($Monthly); 39 42 //echo($Row['fullname'].' '.$Row['inet'].' '.$Monthly.'<br>'); 40 $Database->update('users', 'id='.$Row['id'], array('network_device' => $Price, 'monthly_inet' => $MonthlyInet, 'monthly' => $Monthly, 'consumption' => W2Kc($Consumption), 'cash' => ($Row3[0]-$Row['network_device']), 'plus' => W2Kc($Row4[0]))); 41 } 42 } 43 44 43 $Database->update('users', 'id='.$Row['id'], array('network_device' => $Price, 'monthly_inet' => $MonthlyInet, 'monthly' => $Monthly, 'consumption' => W2Kc($Consumption), 'cash' => $Row3[0], 'plus' => W2Kc($Row4[0]))); 44 } 45 } 46 47 function RecalculateSegmentParameters() 48 { 49 global $Database, $InternetSegmentId; 50 51 echo("Aktualizuju parametry segmentů...<br>\n"); 52 $Database->query("UPDATE network_segments SET users = 0, users_overheads = 0"); // Vynulovat počty uživatelů 53 $DbResult = $Database->query("SELECT * FROM network_segments"); 54 while($Row = $DbResult->fetch_array()) 55 { 56 //echo('Segment '.$Row['name'].'<br>'); 57 $DbResult2 = $Database->query("SELECT users FROM network_segments WHERE id=".$Row['id']); 58 $RowP = $DbResult2->fetch_array(); 59 $DbResult2 = $Database->query("SELECT users_overheads FROM network_segments WHERE id=".$Row['id']); 60 $RowP2 = $DbResult2->fetch_array(); 61 62 $DbResult2 = $Database->query("SELECT SUM(price) as Price, SUM(consumption) as Consumption FROM network_devices WHERE segment=".$Row['id']." AND used=1"); 63 $Row2 = $DbResult2->fetch_array(); 64 $DbResult2 = $Database->query("SELECT COUNT(*) FROM users WHERE network_segment=".$Row['id'].' AND hw=1'); 65 $Row3 = $DbResult2->fetch_array(); 66 $DbResult2 = $Database->query("SELECT COUNT(*) FROM users WHERE network_segment=".$Row['id'].' AND overheads=1'); 67 $Row5 = $DbResult2->fetch_array(); 68 $ID = $Row['parent']; 69 while($ID != 0) 70 { 71 //echo($ID.', '); 72 $DbResult2 = $Database->query("SELECT * FROM network_segments WHERE id=".$ID); 73 $Row4 = $DbResult2->fetch_array(); 74 $Database->update('network_segments', 'id='.$Row4['id'], array('users' => ($Row4['users'] + $Row3[0]), 'users_overheads' => ($Row4['users_overheads'] + $Row5[0]))); 75 $ID = $Row4['parent']; 76 } 77 //echo('Pocet '.$Row3[0].','.$Row['hosts'].'<br>'); 78 $Database->update('network_segments', 'id='.$Row['id'], array('price' => $Row2['Price'], 'users' => ($Row3[0] + $RowP['users']), 'consumption' => $Row2['Consumption'], 'users_overheads' => ($Row5[0] + $RowP2['users_overheads']))); 79 } 80 81 // Zkorigovat segment Internet 82 $DbResult = $Database->select('users', 'COUNT(*)', 'inet_hw=1'); 83 $Row = $DbResult->fetch_array(); 84 $DbResult = $Database->select('users', 'COUNT(*)', 'inet=1'); 85 $Row2 = $DbResult->fetch_array(); 86 $DbResult = $Database->update('network_segments','id='.$InternetSegmentId, array('users' => $Row[0], 'users_overheads' => $Row2[0])); 87 } 88 89 function CreateMonthlyOverallBill() 90 { 91 global $Database, $Sprava, $SpravaUsers, $TotalConsumption, $UserIdNetwork, $Internet, $TotalInternetPaid; 92 93 $TotalConsumptionPrice = W2Kc($TotalConsumption); 94 95 echo("Spotřeba energie: ".-$TotalConsumptionPrice."<br>\n"); 96 echo("Správa sítě: ".-($Sprava * $SpravaUsers)."<br>\n"); 97 echo("Internet: ".-$Internet."<br>\n"); 98 echo("Poplatky učástníků: ".$TotalInternetPaid."<br>\n"); 99 100 $Database->insert('finance_operations', array('money' => -$TotalConsumptionPrice, 'user' => $UserIdNetwork, 'type' => 2, 'date' => 'NOW()', 'comment' => 'Spotřeba energie', 'group' => 2)); 101 $Database->insert('finance_operations', array('money' => -($Sprava * $SpravaUsers), 'user' => $UserIdNetwork, 'type' => 2, 'date' => 'NOW()', 'comment' => 'Správa sítě', 'group' => 2)); 102 $Database->insert('finance_operations', array('money' => -$Internet, 'user' => $UserIdNetwork, 'type' => 2, 'date' => 'NOW()', 'comment' => 'Internet', 'group' => 2)); 103 $Database->insert('finance_operations', array('money' => $TotalInternetPaid, 'user' => $UserIdNetwork, 'type' => 2, 'date' => 'NOW()', 'comment' => 'Poplatky učástníků', 'group' => 2)); 104 } 105 106 function DiscountNetworkDevicePrice() 107 { 108 global $Database; 109 $DbResult = $Database->query("SELECT *,CONCAT(second_name, ' ', first_name) as fullname FROM users WHERE role=2"); 110 while($Row = $DbResult->fetch_array()) 111 { 112 echo($Row['fullname'].': '.($Row['network_device'])."<br>\n"); 113 $Database->insert('finance_operations', array('money' => (-$Row['network_device']), 'user' => $Row['id'], 'type' => 2, 'date' => 'NOW()', 'comment' => 'Jednorázový poplatek za společné zařízení', 'group' => 1)); 114 //echo($Database->LastQuery."<br>\n"); 115 } 116 } 117 118 // Načti poslední měsíční přehled a nastavení 45 119 $DbResult = $Database->select('finance_monthly_overall', '*', '1 ORDER BY date DESC LIMIT 1'); 46 120 $Overall = $DbResult->fetch_array(); 47 121 48 echo('Datum: '.date('j.n.Y')." \n");49 $Mesice = array('', 'Leden','Únor','Březen','Duben','Květen','Červen','Červenec','Srpen','Září','Říjen','Listopad','Prosinec');122 echo('Datum: '.date('j.n.Y')."<br>"); 123 $Mesice = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'); 50 124 $Datum = explode('-', $Overall['date']); 51 $Mesic = date("m")+0; 52 53 // Spočítat ceny segmentů 54 echo("Prepocitavam ceny"); 55 $Database->query("UPDATE network_segments SET users=0"); // Vynulovat počty počítačů 56 $Database->query("UPDATE network_segments SET users_overheads=0"); // Vynulovat počty počítačů 57 $DbResult = $Database->query("SELECT * FROM network_segments"); 58 while($Row = $DbResult->fetch_array()) 59 { 60 //echo('Segment '.$Row['name'].'<br>'); 61 $DbResult2 = $Database->query("SELECT users FROM network_segments WHERE id=".$Row['id']); 62 $RowP = $DbResult2->fetch_array(); 63 $DbResult2 = $Database->query("SELECT users_overheads FROM network_segments WHERE id=".$Row['id']); 64 $RowP2 = $DbResult2->fetch_array(); 65 $DbResult2 = $Database->query("SELECT SUM(price),SUM(consumption) FROM network_devices WHERE segment=".$Row['id']." AND used=1"); 66 $Row2 = $DbResult2->fetch_array(); 67 $DbResult2 = $Database->query("SELECT COUNT(*) FROM users WHERE network_segment=".$Row['id'].' AND hw=1'); 68 $Row3 = $DbResult2->fetch_array(); 69 $DbResult2 = $Database->query("SELECT COUNT(*) FROM users WHERE network_segment=".$Row['id'].' AND overheads=1'); 70 $Row5 = $DbResult2->fetch_array(); 71 $ID = $Row['parent']; 72 while($ID != 0) 73 { 74 //echo($ID.', '); 75 $DbResult2 = $Database->query("SELECT * FROM network_segments WHERE id=".$ID); 76 $Row4 = $DbResult2->fetch_array(); 77 $Database->update('network_segments', 'id='.$Row4['id'], array('users' => ($Row4['users']+$Row3[0]), 'users_overheads' => ($Row4['users_overheads']+$Row5[0]))); 78 $ID = $Row4['parent']; 79 } 80 //echo('Pocet '.$Row3[0].','.$Row['hosts'].'<br>'); 81 $Database->update('network_segments','id='.$Row['id'],array('price' => $Row2[0], 'users' => ($Row3[0]+$RowP['users']), 'consumption' => $Row2[1], 'users_overheads' => ($Row5[0]+$RowP2['users_overheads']))); 82 } 83 84 // Zkorigovat segment Internet 85 $DbResult = $Database->select('users', 'COUNT(*)', 'inet_hw=1'); 86 $Row = $DbResult->fetch_array(); 87 $DbResult = $Database->select('users', 'COUNT(*)', 'inet=1'); 88 $Row2 = $DbResult->fetch_array(); 89 $DbResult = $Database->update('network_segments','id='.$InternetSegmentId, array('users' => $Row[0], 'users_overheads' => $Row2[0])); 90 125 $Mesic = date("m") + 0; 126 127 RecalculateSegmentParameters(); 91 128 RecalculateTariffs(); 92 93 echo("."); 129 //DiscountNetworkDevicePrice(); 94 130 RecalculateUsersFinance(); 95 echo("OK\n");96 131 97 132 // Celkovy prehled 98 133 $DbResult = $Database->query("SELECT SUM(price) FROM network_devices WHERE used=1"); 99 134 $Row4 = $DbResult->fetch_array(); 100 echo("Celkov acena zařízení: Zařízení(".$Row4[0]);135 echo("Celková cena zařízení: Zařízení(".$Row4[0]); 101 136 $DbResult = $Database->query("SELECT SUM(price) FROM network_segments"); 102 137 $Row = $DbResult->fetch_array(); … … 104 139 $DbResult = $Database->query("SELECT SUM(network_device) FROM users"); 105 140 $Row5 = $DbResult->fetch_array(); 106 echo("), U ľivatelé(".$Row5[0].")\n");107 $DbResult = $Database->query("SELECT SUM(cash) FROM users WHERE role= 0");141 echo("), Uživatelé(".$Row5[0].")<br>\n"); 142 $DbResult = $Database->query("SELECT SUM(cash) FROM users WHERE role=2"); 108 143 $Row6 = $DbResult->fetch_array(); 109 echo("Stav pokladny: Uľivatelé(".$Row6[0].")"); 110 $DbResult = $Database->query("SELECT SUM(money) FROM finance_operations"); 111 $Row = $DbResult->fetch_array(); 112 $DbResult = $Database->query("SELECT SUM(price) FROM network_devices WHERE used=1"); 144 echo("Stav pokladny: Uživatelé(".$Row6[0].")"); 145 $DbResult = $Database->query("SELECT SUM(money) FROM finance_operations WHERE `group`=1"); // Položky uživatelů 113 146 $Row2 = $DbResult->fetch_array(); 114 echo(", Operace-Zařízení(".($Row[0]-$Row2[0]).")<br>\n"); 115 116 $DbResult = $Database->query("SELECT SUM(money) FROM finance_operations"); 117 $Row3 = $DbResult->fetch_array(); 118 echo("Rozdíl peněz v operacích: ".($Row3[0]-$Row6[0] - $Row4[0])." <br>\n"); 147 echo(", Operace-Zařízení(".($Row2[0] - $Row4[0]).")<br>\n"); 148 echo("Rozdíl peněz v operacích: ".($Row2[0] - $Row6[0] - $Row4[0])." <br>\n"); 119 149 120 150 … … 123 153 $CelkemSpotreba = W2Kc($Row[0]); 124 154 125 $DbResult = $Database->query("SELECT SUM(plus-monthly) FROM users WHERE role=0"); 126 $Row = $DbResult->fetch_array(); 127 $CelkemZaplaceno = $Row[0]; 128 $SpravaCelkem = $Sprava*$SpravaUsers; 129 echo("Kontrola placení (Zaplaceno-Sprava-Internet): ".(-$CelkemZaplaceno)."-".$SpravaCelkem."-".$Internet."=".(-$CelkemZaplaceno-$SpravaCelkem-$Internet)."<br>\n\n"); 155 $SpravaCelkem = $Sprava * $SpravaUsers; 156 echo("Kontrola placení (Zaplaceno-Sprava-Internet): ".$TotalPaid."-".$SpravaCelkem."-".$Internet."=".($TotalPaid - $SpravaCelkem - $Internet)."<br>\n\n"); 130 157 131 158 // Zkontrolovat odečtení měsíčního poplatku … … 134 161 if($Mesic != $Datum[1]) 135 162 { 136 echo("Odečítám měsíční poplatek: "); 137 138 $DbResult = $Database->query("SELECT * FROM users WHERE role=0"); 139 while($Row = $DbResult->fetch_array()) 140 { 141 echo($Row['fullname'].': '.($Row['plus']-$Row['monthly']).'<br>'); 142 $Database->insert('finance_operations',array('money' => $Row['plus']-$Row['monthly'], 'user' => $Row['id'], 'type' => 2, 'date' => 'NOW()', 'comment' => 'Poplatek za měsíc '.$Mesice[$Mesic])); 143 } 144 $Database->insert('finance_monthly_overall', array('date' => 'NOW()', 'money' => $Internet, 'kWh' => $kWh, 'administration' => $Sprava, 'administration_total' => $SpravaCelkem, 'consumption_total' => $CelkemSpotreba, 'total_paid' => $CelkemZaplaceno)); 145 146 echo('Menim aktualni parametry site.'); 163 echo("Odečítám měsíční poplatek...<br>\n"); 164 165 // Generuj účetní položky 166 $DbResult = $Database->query("SELECT *,CONCAT(second_name, ' ', first_name) as fullname FROM users WHERE role=2"); 167 while($Row = $DbResult->fetch_array()) 168 { 169 echo($Row['fullname'].': '.($Row['plus'] - $Row['monthly'])."<br>\n"); 170 171 $BillId = $InvoiceGenerator->CreateBill($Row['id'], array(array('description' => 'Poplatek za připojení k síti', 'price' => ($Row['monthly'] - $Row['plus']), 'quantity' => 1)), time(), time() + (date("t", time()) - 1) * 24 * 3600); 172 173 $Database->insert('finance_operations',array('money' => ($Row['plus'] - $Row['monthly']), 'user' => $Row['id'], 'type' => 2, 'date' => 'NOW()', 'comment' => 'Poplatek za měsíc '.$Mesice[$Mesic], 'group' => 1, 'bill_id' => $BillId)); 174 } 175 176 echo('Měním aktuální parametry sítě...<br>\n'); 147 177 148 178 // Update finance charge … … 152 182 //print_r($Charge); 153 183 $DbResult = $Database->insert('finance_charge', array('period' => 0, 'internet' => $Charge['internet'], 154 'internet_speed' => $Charge['internet_speed'], 'internet_speed_reserve' => $Charge['internet_speed_reserve'], 'administration_per_user' => $Charge['administration_per_user'], 'kWh' => $Charge['kWh'], 'base_speed_element' => $Charge['base_speed_element'])); 155 156 157 echo('Menim aktualni tarify.'); 184 'internet_speed' => $Charge['internet_speed'], 'internet_speed_reserve' => $Charge['internet_speed_reserve'], 'administration_per_user' => $Charge['administration_per_user'], 'kWh' => $Charge['kWh'], 'base_speed_element' => $Charge['base_speed_element'], 'BaseTariffPrice' => $Charge['BaseTariffPrice'], 'TopTariffPrice' => $Charge['TopTariffPrice'])); 185 186 echo("Přidávám měsíční přehled...<br>\n"); 187 188 $Database->insert('finance_monthly_overall', array('date' => 'NOW()', 'money' => $Internet, 'kWh' => $kWh, 'administration' => $Sprava, 'administration_total' => $SpravaCelkem, 'consumption_total' => $CelkemSpotreba, 'total_paid' => $TotalPaid, 'BaseTariffPrice' => $Charge['BaseTariffPrice'], 'TopTariffPrice' => $Charge['TopTariffPrice'])); 189 190 echo("Měním aktuální tarify....<br>\n"); 158 191 159 192 // Update tarrifs … … 166 199 } 167 200 168 echo( 'Menim aktualni tarify uzivatelu.');201 echo("Měním aktuální tarify uživatelů...<br>\n"); 169 202 170 203 // Update tariff user selection 171 $DbResult = $Database->query("SELECT id,inet_tarif_next FROM users WHERE role= 0");204 $DbResult = $Database->query("SELECT id,inet_tarif_next FROM users WHERE role=2"); 172 205 while($User = $DbResult->fetch_array()) 173 206 { … … 176 209 177 210 RecalculateTariffs(); 178 RecalculateUsersFinance(); 211 RecalculateUsersFinance(); 212 CreateMonthlyOverallBill(); 213 RecalculateUsersFinance(); 179 214 180 215 // Restart traffic shaping 181 216 $Database->update('services_restart', 'id=3', array('changed' => 1)); 182 217 } 218 219 ShowFooter(); 220 183 221 ?>
Note:
See TracChangeset
for help on using the changeset viewer.