���� 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/tiendien/ |
<?php header('Content-Type: application/json'); // Biểu giá điện bậc thang (giống như trong file HTML/JS) $electricityTiers = [ ['from' => 0,'to' => 50, 'price' => 1984], // Bậc 1 ['from' => 51,'to' => 100, 'price' => 2050], // Bậc 2 ['from' => 101,'to' => 200, 'price' => 2380], // Bậc 3 ['from' => 201,'to' => 300, 'price' => 2998], // Bậc 4 ['from' => 301,'to' => 400, 'price' => 3350], // Bậc 5 ['from' => 401,'to' => 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. */ 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; } function tien_dien_moi_phong($so_ky,$mang_so_ky,$mang_bac_tien) { if (!is_numeric($so_ky) || $so_ky < 0 || empty($mang_so_ky)) { return 0; } $count_so_ky_input = count($mang_so_ky); $gia_tien = 0; $dinh_muc = 0; $so_ky_new = $so_ky/$count_so_ky_input; foreach ($mang_bac_tien as $key=>$tier) { $from_new = $tier['from']/$count_so_ky_input; $to_new = $tier['to']/$count_so_ky_input; if($from_new<=$so_ky_new && $to_new>=$so_ky_new){ $dinh_muc = $key; break; } } //echo $dinh_muc.'<br>'; $tong_gia_tien = 0; $tong_so_ky_da_dung_trong_moi_bac = 0; $dinh_muc = $dinh_muc+1; for($i=0;$i<=$dinh_muc;$i++){ if($i<$dinh_muc) { if($mang_bac_tien[$i]['to']!='INF') { $so_ky_da_dung_trong_moi_bac = $mang_bac_tien[$i]['to'] / $count_so_ky_input; $tong_so_ky_da_dung_trong_moi_bac += $so_ky_da_dung_trong_moi_bac; if($i<=5) { $gia_tien = $mang_bac_tien[$i]['price'] * ($so_ky_da_dung_trong_moi_bac); }else{ $gia_tien = $mang_bac_tien[(count($mang_bac_tien)-1)]['price'] * ($so_ky_da_dung_trong_moi_bac); } }else{ $gia_tien = $mang_bac_tien[(count($mang_bac_tien)-1)]['price'] * ($so_ky-$tong_so_ky_da_dung_trong_moi_bac); } //echo $gia_tien.'<br>'; }elseif($i==$dinh_muc){ if($i<=5) { $gia_tien = $mang_bac_tien[$i]['price']*($so_ky-$tong_so_ky_da_dung_trong_moi_bac); }else{ $gia_tien = $mang_bac_tien[(count($mang_bac_tien)-1)]['price'] * ($so_ky-$tong_so_ky_da_dung_trong_moi_bac); } //echo $gia_tien.':2<br>'; } $tong_gia_tien += $gia_tien; } return $tong_gia_tien; } $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)) { $totalKwhAllRooms = array_sum($kwhValuesFromPost); $calculatedRoomCosts = []; $total_cost_vat = 0; 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 = tien_dien_moi_phong($individualKwh,$kwhValuesFromPost,$electricityTiers); //echo $roomCostPreTax.'-------'; // Tính VAT cho phòng này $vatAmountForRoom = $roomCostPreTax * VAT_RATE; // Tiền phòng sau thuế $roomCostPostTax = $roomCostPreTax + $vatAmountForRoom; $arr_result[] = $roomCostPostTax; $total_cost_vat += $roomCostPostTax; } $calculatedRoomCosts[] = [ 'pre_tax' => $roomCostPreTax, 'post_tax' => $roomCostPostTax ]; } $chenh_lech = 0; $arr_tien_dong_thuc = array(); if($tien_dong>0){ $tong_so_kw = array_sum($kwhValuesFromPost); $chenh_lech = $tien_dong-$total_cost_vat; foreach($kwhValuesFromPost as $key=>$kw_tmp){ $tien_dong_thuc = $kw_tmp * $chenh_lech/$tong_so_kw; /*if($tien_dong_thuc>0) { $tien_du_or_bu = $arr_result[$key] + $tien_dong_thuc; }else{ $tien_du_or_bu = $arr_result[$key] + $tien_dong_thuc; }*/ $tien_du_or_bu = $arr_result[$key] + $tien_dong_thuc; $arr_tien_dong_thuc[] = [ 'tien_dong_thuc' => $tien_du_or_bu ]; } } $response['room_costs'] = $calculatedRoomCosts; $response['total_cost_vat'] = $total_cost_vat; $response['result_tien'] = $arr_tien_dong_thuc; } } echo json_encode($response); exit; ?>