精度问题

main
岩仔88 6 days ago
parent 7823b1fed1
commit 8672a42684

@ -339,7 +339,7 @@ class AppointmentController extends Controller
// 2. 按类型汇总(个检type=1, 团检type=2) // 2. 按类型汇总(个检type=1, 团检type=2)
$charge_amount = round($orders->sum('true_price'), 2); $charge_amount = round($orders->sum('true_price'), 2);
$refund_amount = round($refund_orders->sum('true_price'), 2); $refund_amount = round($refund_orders->sum('true_price'), 2);
$total_amount = round($charge_amount - $refund_amount, 2); $total_amount = round(round($charge_amount - $refund_amount, 4), 2);
$type_summary = []; $type_summary = [];
foreach ([1 => '个人', 2 => '团检'] as $type_val => $type_name) { foreach ([1 => '个人', 2 => '团检'] as $type_val => $type_name) {
@ -409,7 +409,7 @@ class AppointmentController extends Controller
$price = floatval($item['价格']); $price = floatval($item['价格']);
if (isset($item['优惠方式']) && $item['优惠方式'] === '打折') { if (isset($item['优惠方式']) && $item['优惠方式'] === '打折') {
$price = $price * floatval($item['优惠值']); $price = round($price * floatval($item['优惠值']), 2);
} }
$key = $matched ? $alias : '__unmatched__'; $key = $matched ? $alias : '__unmatched__';
@ -420,7 +420,7 @@ class AppointmentController extends Controller
'item_names' => [], 'item_names' => [],
]; ];
} }
$statistics[$key]['amount'] += $price; $statistics[$key]['amount'] = round($statistics[$key]['amount'] + $price, 2);
if (!$matched) { if (!$matched) {
$statistics[$key]['item_names'][] = $item['名称']; $statistics[$key]['item_names'][] = $item['名称'];
} }
@ -429,14 +429,22 @@ class AppointmentController extends Controller
// 格式化金额,未匹配项去重 // 格式化金额,未匹配项去重
$categories = []; $categories = [];
$category_total = 0;
foreach ($statistics as $item) { foreach ($statistics as $item) {
$item['amount'] = round($item['amount'], 2); $item['amount'] = round($item['amount'], 2);
$category_total = round($category_total + $item['amount'], 2);
if (isset($item['item_names'])) { if (isset($item['item_names'])) {
$item['item_names'] = array_values(array_unique($item['item_names'])); $item['item_names'] = array_values(array_unique($item['item_names']));
} }
$categories[] = $item; $categories[] = $item;
} }
// 合计金额取分类汇总,确保合计与各项之和一致
if ($category_total > 0) {
$charge_amount = $category_total;
}
$total_amount = round($charge_amount - $refund_amount, 2);
return Yo::echo([ return Yo::echo([
'type_summary' => $type_summary, 'type_summary' => $type_summary,
'charge_amount' => $charge_amount, 'charge_amount' => $charge_amount,

Loading…
Cancel
Save