���� 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/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) ? $kwh : $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; } $tien_dong = $_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; $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'] ]; } // Giai đoạn 2 & 3 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;