ÿØÿà JFIF ÿÛ „ ( %"1"%)+...383,7(-.-
![]() Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20 System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64 User : apache ( 48) PHP Version : 7.4.20 Disable Function : NONE Directory : /proc/self/root/proc/self/root/var/www/html/tien-dien/code/tiendien2/ |
<?php header('Content-Type: application/json'); $electricityTiers = [ ['from' => 0, 'to' => 50, 'price' => 1984], ['from' => 51, 'to' => 100, 'price' => 2050], ['from' => 101, 'to' => 200, 'price' => 2380], ['from' => 201, 'to' => 300, 'price' => 2998], ['from' => 301, 'to' => 400, 'price' => 3350], ['from' => 401, 'to' => INF, 'price' => 3460], ]; define('VAT_RATE', 0.08); function calculateDetailedBill($kwh, $tiers) { $details = []; $remainingKwh = $kwh; $total = 0; foreach ($tiers as $tier) { $tierFrom = $tier['from']; $tierTo = ($tier['to'] === INF) ? INF : $tier['to']; $tierLength = ($tierTo === INF) ? INF : $tierTo - $tierFrom + 1; $used = ($tierLength === INF) ? $remainingKwh : min($remainingKwh, $tierLength); if ($used <= 0) break; $cost = $used * $tier['price']; $details[] = [ 'range' => ($tierTo === INF) ? "$tierFrom trở lên" : "$tierFrom - $tierTo", 'used' => $used, 'price' => $tier['price'], 'cost' => $cost ]; $total += $cost; $remainingKwh -= $used; } return ['total' => $total, 'breakdown' => $details]; } $response = ['room_costs' => []]; $kwhValuesFromPost = []; if (isset($_POST['so_dien_phong']) && is_array($_POST['so_dien_phong'])) { foreach ($_POST['so_dien_phong'] as $val) { $kwh = filter_var($val, FILTER_VALIDATE_FLOAT); $kwhValuesFromPost[] = ($kwh !== false && $kwh >= 0) ? $kwh : 0; } $ten_phong_post = isset($_POST['ten_phong']) && is_array($_POST['ten_phong']) ? $_POST['ten_phong'] : []; $tien_dong = isset($_POST['tien_dong']) ? floatval($_POST['tien_dong']) : 0; if (!empty($kwhValuesFromPost)) { $total_cost_vat = 0; $room_results = []; foreach ($kwhValuesFromPost as $key => $individualKwh) { $bill = calculateDetailedBill($individualKwh, $electricityTiers); $pre_tax = $bill['total']; $vat = $pre_tax * VAT_RATE; $post_tax = $pre_tax + $vat; $total_cost_vat += $post_tax; $room_name = isset($ten_phong_post[$key]) && trim($ten_phong_post[$key]) !== '' ? trim($ten_phong_post[$key]) : 'Phòng ' . ($key + 1); $explanation = []; $explanation[] = "Giai Ä‘oạn 1: Tiá»n Ä‘iện thá»±c tế"; $explanation[] = "Sá» dụng: {$individualKwh} kWh"; $explanation[] = "Tiá»n Ä‘iện chưa VAT: " . number_format($pre_tax) . " VNÄ"; $explanation[] = "VAT 8%: " . number_format($vat) . " VNÄ"; $explanation[] = "Tổng cá»™ng: " . number_format($post_tax) . " VNÄ"; $room_results[$key] = [ 'pre_tax' => $pre_tax, 'post_tax' => $post_tax, 'explanation' => $explanation, 'breakdown' => $bill['breakdown'], 'ten_phong' => $room_name, ]; } if ($tien_dong > 0) { $chenh_lech = $tien_dong - $total_cost_vat; $totalKwhAllRooms = array_sum($kwhValuesFromPost); foreach ($kwhValuesFromPost as $key => $kwh) { $ti_le = $totalKwhAllRooms > 0 ? $kwh / $totalKwhAllRooms : 0; $adjusted = $room_results[$key]['post_tax'] + ($chenh_lech * $ti_le); $room_results[$key]['tien_dong_thuc'] = $adjusted; $room_results[$key]['explanation'][] = "Giai Ä‘oạn 2: Tiá»n chênh lệch"; $room_results[$key]['explanation'][] = "Tổng tiá»n đóng: " . number_format($tien_dong) . " VNÄ"; $room_results[$key]['explanation'][] = "Chênh lệch toà n hệ thống: " . number_format($chenh_lech) . " VNÄ"; $room_results[$key]['explanation'][] = "Tỉ lệ cá»§a phòng nà y: " . round($ti_le * 100, 2) . "%"; $room_results[$key]['explanation'][] = "Tiá»n chênh lệch phòng: " . number_format($chenh_lech * $ti_le) . " VNÄ"; $room_results[$key]['explanation'][] = "Giai Ä‘oạn 3: Tiá»n phải đóng"; $room_results[$key]['explanation'][] = "Tổng cá»™ng phải đóng: " . number_format($adjusted) . " VNÄ"; } } $response['room_costs'] = $room_results; $response['total_cost_vat'] = $total_cost_vat; } } echo json_encode($response); exit; ?>