PHP 订单拆单后明细总金额与订单金额存在差异
2021/10/23 14:10:15
本文主要是介绍PHP 订单拆单后明细总金额与订单金额存在差异,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- 一、场景
- 二、代码
- 三、打印
一、场景
把订单的金额按照数量拆成
number
个订单详情,如果金额是10
,数量是3
,则拆出来的单个金额就是3.33
,就会丢失0.01
。为了解决这一问题,我们需要对比订单详情总金额与订单金额,如果不一致,则调整最后一个
订单详情的金额(增加或减少),使金额保持一致。
二、代码
//订单拆单:把单个订单拆成number个订单详情,订单详情总额不等于订单金额时,调整最后一个订单详情金额 public function test() { //订单数据 $orderData = [ ['id' => 1, 'name' => '数据1', 'number' => 2, 'price' => 10.12], //单条订单数据 ['id' => 2, 'name' => '数据2', 'number' => 3, 'price' => 10.13], ['id' => 3, 'name' => '数据3', 'number' => 6, 'price' => 10.10], ]; $detailData = []; foreach ($orderData as $value) { $price = $value['price']; $number = $value['number']; $tempData = []; for ($i = 0; $i < $number; $i ++) { //把单个订单拆成number个订单详情 $tempData[] = [ 'pid' => $value['id'], 'name' => "{$value['name']}的详情数据", 'price' => ($number > 0) ? sprintf('%.2f', $price / $number) : 0 //订单详情的价格,并保留2位小数 ]; } //金额对不上时调整 self::comparePrice($tempData, $price); $detailData = array_merge($detailData, $tempData); //把单个订单的详情数据追加到总详情数据中 } var_export($detailData); //打印结果数据 } /** * 校验单个订单详情数据的总金额和订单金额是否一致。如果不一致则:调整最后一条的价格 * @param array $tempData 单个订单拆出的详情数据 * @param string $price 单个订单的金额 * @param string $priceField 详情数据的金额字段名 */ public function comparePrice(&$tempData, $price, $priceField = 'price') { $detailTotalPrice = array_sum(array_column($tempData, $priceField)); //订单详情的总金额 if ($detailTotalPrice != $price) { //订单详情的总金额 不等于 订单金额 $maxIndex = count($tempData) - 1; //最后一条数据的索引 $maxIndexPrice = $tempData[$maxIndex][$priceField]; //最后一条数据的金额 $maxIndexNewPrice = sprintf('%.2f', $price - ($detailTotalPrice - $maxIndexPrice)); //根据差额计算最后一条数据的金额 $tempData[$maxIndex][$priceField] = $maxIndexNewPrice; //给最后一条数据的金额赋值 } }
三、打印
array ( 0 => array ( 'pid' => 1, 'name' => '数据1的详情数据', 'price' => '5.06', ), 1 => array ( 'pid' => 1, 'name' => '数据1的详情数据', 'price' => '5.06', ), 2 => array ( 'pid' => 2, 'name' => '数据2的详情数据', 'price' => '3.38', ), 3 => array ( 'pid' => 2, 'name' => '数据2的详情数据', 'price' => '3.38', ), 4 => array ( 'pid' => 2, 'name' => '数据2的详情数据', 'price' => '3.37', ), 5 => array ( 'pid' => 3, 'name' => '数据3的详情数据', 'price' => '1.68', ), 6 => array ( 'pid' => 3, 'name' => '数据3的详情数据', 'price' => '1.68', ), 7 => array ( 'pid' => 3, 'name' => '数据3的详情数据', 'price' => '1.68', ), 8 => array ( 'pid' => 3, 'name' => '数据3的详情数据', 'price' => '1.68', ), 9 => array ( 'pid' => 3, 'name' => '数据3的详情数据', 'price' => '1.68', ), 10 => array ( 'pid' => 3, 'name' => '数据3的详情数据', 'price' => '1.70', ), )
这篇关于PHP 订单拆单后明细总金额与订单金额存在差异的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02phpstrom链接ftp 提示上传成功 但是ftp代码没变-icode9专业技术文章分享
- 2024-06-30百度空间安装易优,如何去除URL中的index.php-icode9专业技术文章分享
- 2024-06-30宝塔Linux服务器隐藏index.php-icode9专业技术文章分享
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。