feat(quote): 师傅报价支持加价模式与工钱口径优化
- 加价计算:CommissionService 新增 calcQuoteMarkupByOrder,师傅端录入净报价,平台自动计入服务费并生成用户端应付报价 - 订单与结算:PaymentService 与 Master 控制器在选择报价与完成服务时联动刷新佣金快照,确保师傅到手收益准确 - 后台与小程序:后台订单详情展示师傅净报价与服务费明细,师傅端订单列表与详情统一定义工钱口径为预计工钱/实结工钱
这个提交包含在:
@@ -299,44 +299,172 @@ class CommissionService
|
||||
}
|
||||
|
||||
/**
|
||||
* 报价单选定师傅报价并确认金额时:仅在订单自身的创单快照 candidate_tiers 内部重新定档(严禁读取后台新规则)
|
||||
* 基于师傅净报价计算用户端加价后总额(方案A:用户价 = 师傅净报价 + 外加平台服务费)
|
||||
*
|
||||
* @param float $finalAmount 选定报价后的订单金额
|
||||
* @param float $masterAmount 师傅提交的净报价(预期到手金额)
|
||||
* @param array $ruleItem 规则快照项
|
||||
* @return array ['master_amount' => float, 'platform_fee' => float, 'user_amount' => float, 'commission_rate' => float, 'rule_id' => int, 'rule_name' => string, 'calc_mode' => string]
|
||||
*/
|
||||
protected static function computeMarkupByRuleItem(float $masterAmount, array $ruleItem): array
|
||||
{
|
||||
$master = max(0.00, round($masterAmount, 2));
|
||||
$mode = in_array($ruleItem['calc_mode'] ?? 'percent', ['percent', 'fixed', 'mixed'], true)
|
||||
? $ruleItem['calc_mode']
|
||||
: 'percent';
|
||||
$rate = self::normalizeRate($ruleItem['commission_rate'] ?? 0);
|
||||
$fixed = round(max(0.00, (float)($ruleItem['fixed_fee'] ?? 0)), 2);
|
||||
$minC = round(max(0.00, (float)($ruleItem['min_commission'] ?? 0)), 2);
|
||||
$maxC = round(max(0.00, (float)($ruleItem['max_commission'] ?? 0)), 2);
|
||||
|
||||
if ($master <= 0) {
|
||||
return [
|
||||
'master_amount' => 0.00,
|
||||
'platform_fee' => 0.00,
|
||||
'user_amount' => 0.00,
|
||||
'commission_rate' => $mode === 'percent' ? $rate : 0.00,
|
||||
'rule_id' => (int)($ruleItem['rule_id'] ?? 0),
|
||||
'rule_name' => (string)($ruleItem['rule_name'] ?? '全局默认抽佣规则'),
|
||||
'calc_mode' => $mode,
|
||||
];
|
||||
}
|
||||
|
||||
if ($mode === 'fixed') {
|
||||
$rawFee = $fixed;
|
||||
} elseif ($mode === 'mixed') {
|
||||
$rawFee = round($master * $rate / 100 + $fixed, 2);
|
||||
} else {
|
||||
$rawFee = round($master * $rate / 100, 2);
|
||||
}
|
||||
|
||||
$platformFee = $rawFee;
|
||||
if ($minC > 0 && $platformFee < $minC) {
|
||||
$platformFee = $minC;
|
||||
}
|
||||
if ($maxC > 0 && $platformFee > $maxC) {
|
||||
$platformFee = $maxC;
|
||||
}
|
||||
$platformFee = round(max(0.00, $platformFee), 2);
|
||||
$userAmount = round($master + $platformFee, 2);
|
||||
$effectiveRate = $userAmount > 0
|
||||
? self::normalizeRate(round($platformFee / $userAmount * 100, 2))
|
||||
: $rate;
|
||||
|
||||
return [
|
||||
'master_amount' => $master,
|
||||
'platform_fee' => $platformFee,
|
||||
'user_amount' => $userAmount,
|
||||
'commission_rate' => $effectiveRate,
|
||||
'rule_id' => (int)($ruleItem['rule_id'] ?? 0),
|
||||
'rule_name' => (string)($ruleItem['rule_name'] ?? '全局默认抽佣规则'),
|
||||
'calc_mode' => $mode,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 报价单提交报价时:严格基于订单自身创单快照计算外加平台服务费与用户端展示价
|
||||
*
|
||||
* @param array $order fa_order 记录(含 commission_snapshot, commission_rate)
|
||||
* @param float $masterAmount 师傅输入的净报价(期望到手金额)
|
||||
* @return array ['master_amount' => float, 'platform_fee' => float, 'user_amount' => float, 'commission_rate' => float, 'rule_id' => int, 'rule_name' => string, 'calc_mode' => string]
|
||||
*/
|
||||
public static function calcQuoteMarkupByOrder(array $order, float $masterAmount): array
|
||||
{
|
||||
$master = max(0.00, round($masterAmount, 2));
|
||||
$snapshotJson = $order['commission_snapshot'] ?? null;
|
||||
$snapshotRate = $order['commission_rate'] ?? null;
|
||||
|
||||
if (is_string($snapshotJson) && trim($snapshotJson) !== '') {
|
||||
$snap = json_decode($snapshotJson, true);
|
||||
if (is_array($snap)) {
|
||||
if (!empty($snap['candidate_tiers']) && is_array($snap['candidate_tiers'])) {
|
||||
foreach ($snap['candidate_tiers'] as $tier) {
|
||||
if (!is_array($tier)) {
|
||||
continue;
|
||||
}
|
||||
$trial = self::computeMarkupByRuleItem($master, $tier);
|
||||
if (self::matchAmountRange($tier, $trial['user_amount'])) {
|
||||
return $trial;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($snap['fallback_rule']) && is_array($snap['fallback_rule']) && !empty($snap['candidate_tiers'])) {
|
||||
return self::computeMarkupByRuleItem($master, $snap['fallback_rule']);
|
||||
}
|
||||
if (!empty($snap['active_rule']) && is_array($snap['active_rule'])) {
|
||||
return self::computeMarkupByRuleItem($master, $snap['active_rule']);
|
||||
}
|
||||
if (!empty($snap['fallback_rule']) && is_array($snap['fallback_rule'])) {
|
||||
return self::computeMarkupByRuleItem($master, $snap['fallback_rule']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$rate = ($snapshotRate !== null && trim((string)$snapshotRate) !== '' && is_numeric($snapshotRate))
|
||||
? self::normalizeRate($snapshotRate)
|
||||
: self::getGlobalRate();
|
||||
|
||||
return self::computeMarkupByRuleItem($master, [
|
||||
'rule_id' => 0,
|
||||
'rule_name' => '比例快照(' . number_format($rate, 2, '.', '') . '%)',
|
||||
'calc_mode' => 'percent',
|
||||
'commission_rate' => $rate,
|
||||
'fixed_fee' => 0.00,
|
||||
'min_commission' => 0.00,
|
||||
'max_commission' => 0.00,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 报价单选定师傅报价并确认金额时:在订单创单快照内部锁定阶梯与净价外加明细(严禁读取后台新规则)
|
||||
*
|
||||
* @param float $finalAmount 选定报价后的用户实付金额
|
||||
* @param mixed $existingSnapshotJson 订单表已有的 commission_snapshot
|
||||
* @param mixed $existingRate 订单表已有的 commission_rate
|
||||
* @return array ['commission_rate' => float, 'commission_snapshot' => string|null]
|
||||
* @param array $quoteRow 选中的 fa_order_quote 行(含 master_amount, platform_fee, amount)
|
||||
* @return array ['commission_rate' => float, 'commission_snapshot' => string]
|
||||
*/
|
||||
public static function refreshQuoteOrderSnapshotOnAmountConfirmed(float $finalAmount, $existingSnapshotJson, $existingRate): array
|
||||
public static function refreshQuoteOrderSnapshotOnAmountConfirmed(float $finalAmount, $existingSnapshotJson, $existingRate, array $quoteRow): array
|
||||
{
|
||||
$fallbackRate = ($existingRate !== null && trim((string)$existingRate) !== '' && is_numeric($existingRate))
|
||||
? self::normalizeRate($existingRate)
|
||||
: self::DEFAULT_RATE;
|
||||
|
||||
if (!is_string($existingSnapshotJson) || trim($existingSnapshotJson) === '') {
|
||||
return [
|
||||
'commission_rate' => $fallbackRate,
|
||||
'commission_snapshot' => null,
|
||||
$snap = (is_string($existingSnapshotJson) && trim($existingSnapshotJson) !== '')
|
||||
? json_decode($existingSnapshotJson, true)
|
||||
: null;
|
||||
|
||||
if (!is_array($snap)) {
|
||||
$snap = [
|
||||
'version' => 1,
|
||||
'snapshot_time' => time(),
|
||||
'include_diff_in_commission' => 1,
|
||||
'active_rule' => [
|
||||
'rule_id' => 0,
|
||||
'rule_name' => '比例快照(' . number_format($fallbackRate, 2, '.', '') . '%)',
|
||||
'min_amount' => 0.00,
|
||||
'max_amount' => 0.00,
|
||||
'calc_mode' => 'percent',
|
||||
'commission_rate' => $fallbackRate,
|
||||
'fixed_fee' => 0.00,
|
||||
'min_commission' => 0.00,
|
||||
'max_commission' => 0.00,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$snap = json_decode($existingSnapshotJson, true);
|
||||
if (!is_array($snap)) {
|
||||
return [
|
||||
'commission_rate' => $fallbackRate,
|
||||
'commission_snapshot' => $existingSnapshotJson,
|
||||
];
|
||||
}
|
||||
$userQuoteAmount = round(max(0.00, (float)($quoteRow['amount'] ?? $finalAmount)), 2);
|
||||
$masterAmount = round(max(0.00, (float)($quoteRow['master_amount'] ?? 0)), 2);
|
||||
$platformFee = round(max(0.00, (float)($quoteRow['platform_fee'] ?? ($userQuoteAmount - $masterAmount))), 2);
|
||||
|
||||
$chosen = null;
|
||||
if (!empty($snap['candidate_tiers']) && is_array($snap['candidate_tiers'])) {
|
||||
foreach ($snap['candidate_tiers'] as $tier) {
|
||||
if (is_array($tier) && self::matchAmountRange($tier, $finalAmount)) {
|
||||
if (is_array($tier) && self::matchAmountRange($tier, $userQuoteAmount)) {
|
||||
$chosen = $tier;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($chosen === null) {
|
||||
if (!empty($snap['fallback_rule']) && is_array($snap['fallback_rule'])) {
|
||||
$chosen = $snap['fallback_rule'];
|
||||
@@ -345,20 +473,27 @@ class CommissionService
|
||||
}
|
||||
}
|
||||
|
||||
if (is_array($chosen)) {
|
||||
$snap['active_rule'] = $chosen;
|
||||
$snap['confirmed_quote_amount'] = round(max(0.00, $finalAmount), 2);
|
||||
$snap['confirmed_time'] = time();
|
||||
$newRate = self::calcDisplayRateForAmount($finalAmount, $chosen);
|
||||
return [
|
||||
'commission_rate' => $newRate,
|
||||
'commission_snapshot' => json_encode($snap, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
}
|
||||
$snap['active_rule'] = $chosen;
|
||||
$snap['confirmed_quote_amount'] = round(max(0.00, $finalAmount), 2);
|
||||
$snap['confirmed_time'] = time();
|
||||
$snap['quote_pricing_mode'] = 'markup';
|
||||
$snap['quote_markup'] = [
|
||||
'master_amount' => $masterAmount,
|
||||
'platform_fee' => $platformFee,
|
||||
'user_amount' => $userQuoteAmount,
|
||||
'rule_id' => (int)($chosen['rule_id'] ?? 0),
|
||||
'rule_name' => (string)($chosen['rule_name'] ?? '全局默认抽佣规则'),
|
||||
'calc_mode' => (string)($chosen['calc_mode'] ?? 'percent'),
|
||||
'config_rate' => self::normalizeRate($chosen['commission_rate'] ?? $fallbackRate),
|
||||
];
|
||||
|
||||
$newRate = $finalAmount > 0
|
||||
? self::normalizeRate(round(min($finalAmount, $platformFee) / $finalAmount * 100, 2))
|
||||
: self::calcDisplayRateForAmount($userQuoteAmount, $chosen);
|
||||
|
||||
return [
|
||||
'commission_rate' => $fallbackRate,
|
||||
'commission_snapshot' => $existingSnapshotJson,
|
||||
'commission_rate' => $newRate,
|
||||
'commission_snapshot' => json_encode($snap, JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -469,8 +604,45 @@ class CommissionService
|
||||
if (is_string($snapshotJson) && trim($snapshotJson) !== '') {
|
||||
$snap = json_decode($snapshotJson, true);
|
||||
if (is_array($snap) && !empty($snap['active_rule']) && is_array($snap['active_rule'])) {
|
||||
$calcBase = $totalAmount;
|
||||
$includeDiff = isset($snap['include_diff_in_commission']) ? (int)$snap['include_diff_in_commission'] : 1;
|
||||
|
||||
// 净价外加模式报价单:基础部分保证师傅按其净报价全额结算,现场增项差价继续沿用现行抽佣规则
|
||||
if ($overrideAmount === null && ($snap['quote_pricing_mode'] ?? '') === 'markup' && !empty($snap['quote_markup']) && is_array($snap['quote_markup'])) {
|
||||
$masterBase = round(max(0.00, (float)($snap['quote_markup']['master_amount'] ?? 0)), 2);
|
||||
$diffAmt = ((int)($order['diff_status'] ?? 0) === 2)
|
||||
? round(max(0.00, (float)($order['diff_amount'] ?? 0)), 2)
|
||||
: 0.00;
|
||||
if ($diffAmt > $totalAmount) {
|
||||
$diffAmt = $totalAmount;
|
||||
}
|
||||
$basePaid = round(max(0.00, $totalAmount - $diffAmt), 2);
|
||||
$baseMasterSettle = round(min($basePaid, $masterBase), 2);
|
||||
$baseComm = round(max(0.00, $basePaid - $baseMasterSettle), 2);
|
||||
|
||||
$diffComm = 0.00;
|
||||
if ($diffAmt > 0 && $includeDiff === 1) {
|
||||
$diffCalc = self::computeCommissionByRuleItem($diffAmt, $diffAmt, $snap['active_rule']);
|
||||
$diffComm = $diffCalc['commission'];
|
||||
}
|
||||
|
||||
$totalComm = round(min($totalAmount, max(0.00, $baseComm + $diffComm)), 2);
|
||||
$totalSettle = round(max(0.00, $totalAmount - $totalComm), 2);
|
||||
$effectiveRate = $totalAmount > 0
|
||||
? self::normalizeRate(round($totalComm / $totalAmount * 100, 2))
|
||||
: 0.00;
|
||||
$calcBase = $includeDiff === 1 ? $totalAmount : $basePaid;
|
||||
|
||||
return [
|
||||
'commission_rate' => $effectiveRate,
|
||||
'commission' => $totalComm,
|
||||
'settle_amount' => $totalSettle,
|
||||
'calc_mode' => (string)($snap['active_rule']['calc_mode'] ?? 'percent'),
|
||||
'rule_name' => (string)($snap['active_rule']['rule_name'] ?? '报价外加服务费'),
|
||||
'calc_base_amount' => $calcBase,
|
||||
];
|
||||
}
|
||||
|
||||
$calcBase = $totalAmount;
|
||||
if ($overrideAmount === null && $includeDiff === 0 && (int)($order['diff_status'] ?? 0) === 2) {
|
||||
$diffAmt = round(max(0.00, (float)($order['diff_amount'] ?? 0)), 2);
|
||||
if ($diffAmt > 0 && $calcBase >= $diffAmt) {
|
||||
|
||||
在新工单中引用
屏蔽一个用户