支付金额取整

This commit is contained in:
Dev
2026-10-28 15:37:49 +08:00
parent 9622b908d6
commit f552fb957f
4 changed files with 134 additions and 24 deletions
@@ -202,16 +202,19 @@ class OrderService
}
$winBid = DemandBiddingService::getOne(['demand_id' => $demandId, 'bidding_status' => DemandBiddingService::STATUS_BIDDING_WIN]);
$orderInfo = self::where(['parent_order_no' => $demand->parent_order_no])->find();
$ratio = 0.5; //分期比率50%
// 首付为总金额 50% 四舍五入到整数元,尾款为剩余未付金额
list($firstAmount, $finalAmount) = self::splitDemandPayment($totalCost);
if (!$orderInfo) {
$promoterId = PromotionService::getPromoterId(intval($demand->user_id));
$installments = 2;//分期数
for ($i = 1; $i <= $installments; $i++) {
$total_amount = $i == 1 ? $totalCost * $ratio : $totalCost * (1 - $ratio);
$installments = [
self::ORDER_TYPE_FIRST_PAYMENT => $firstAmount,
self::ORDER_TYPE_FINAL_PAYMENT => $finalAmount,
];
foreach ($installments as $orderType => $total_amount) {
$goodsDataList = [[
'spu_id' => 0,
'sku_id' => 0,
'goods_name' => '需求签约-' . ($i == 1 ? '首付款' : '尾款') . $demand->parent_order_no,
'goods_name' => '需求签约-' . ($orderType == self::ORDER_TYPE_FIRST_PAYMENT ? '首付款' : '尾款') . $demand->parent_order_no,
'goods_picture_url' => 'https://soft-market.oss-cn-shenzhen.aliyuncs.com/images/contract.png',
'origin_price' => $total_amount,
'actual_price' => $total_amount,
@@ -229,7 +232,7 @@ class OrderService
'parent_order_no' => $demand->parent_order_no,
'order_no' => generate_order_sn(),
'order_status' => OrderService::ORDER_STATUS_WAIT_PAY,
'order_type' => $i,
'order_type' => $orderType,
'total_amount' => $total_amount,
'payment_amount' => $total_amount,
'goods_amount' => $total_amount,
@@ -259,7 +262,7 @@ class OrderService
if (!$contractInfo) {
//创建合同数据
$firstContract = ContractService::getFirstContract($demandId);
$depositAmount = $firstContract ? $firstContract->deposit_amount : $formData['totalCost'] * $ratio;
$depositAmount = $firstContract ? $firstContract->deposit_amount : $firstAmount;
ContractService::createData($demandId, $winBid->id ?? 0, $depositAmount, $formData);
}
return $contractInfo;
@@ -268,6 +271,61 @@ class OrderService
}
}
/**
* 需求分期。首付为总金额的 50% 四舍五入到整数元,尾款为总额减去首付
* @param float|string $totalCost
* @return array [首付款, 尾款],均为两位小数字符串
* @throws \Exception
*/
public static function splitDemandPayment($totalCost)
{
$total = bcadd((string)$totalCost, '0', 2);
if (bccomp($total, '0.01', 2) < 0) {
throw new \Exception('合同金额不能小于0.01元');
}
$half = bcmul($total, '0.5', 4);
$first = bcadd(bcadd($half, '0.5', 0), '0', 2);
if (bccomp($first, $total, 2) === 1) {
$first = $total;
}
$final = bcsub($total, $first, 2);
return [$first, $final];
}
/**
* 把订单和订单商品金额改成指定金额
* @param \think\Model $order
* @param float|string $amount
* @return \think\Model
* @throws \Exception
*/
public static function applyOrderAmount($order, $amount)
{
$amount = bcadd((string)$amount, '0', 2);
$order->total_amount = $amount;
$order->goods_amount = $amount;
$order->payment_amount = $amount;
if ($order->save() === false) {
throw new \Exception('更新订单金额失败');
}
$goodsList = $order->orderGoods;
if ($goodsList) {
foreach ($goodsList as $goods) {
$goods->origin_price = $amount;
$goods->actual_price = $amount;
$goods->item_total_amount = $amount;
$goods->item_payment_amount = $amount;
$goods->goods_payment_price = $amount;
if ($goods->save() === false) {
throw new \Exception('更新订单商品金额失败');
}
}
}
return $order;
}
public static function getDemandOrder($parentOrderNo, $contractType)
{
$res = self::getOne(['parent_order_no' => $parentOrderNo, 'order_type' => $contractType], ['orderGoods', 'orderPayment']);