|
|
@@ -26,6 +26,8 @@ use common\components\util;
|
|
|
*/
|
|
|
class GhsRechargeSettleService
|
|
|
{
|
|
|
+ /** 结账单掺杂历史正余额时的 payWay(dict balancePay=2) */
|
|
|
+ const CLEAR_PAY_WAY_BALANCE = 2;
|
|
|
|
|
|
/** 商家帮充 + 按来款 FIFO 销账(CustomController) */
|
|
|
public static function merchantRechargeWithAutoClear($custom, $amount, $shop, $staff, $payWay, $params = [])
|
|
|
@@ -37,13 +39,18 @@ class GhsRechargeSettleService
|
|
|
|
|
|
self::voidAwaitPayClearBills($custom->id ?? 0);
|
|
|
|
|
|
+ // 入账前净余额:决定结账单线上/线下及 payWay
|
|
|
+ $balanceBeforeRecharge = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+
|
|
|
$respond = CustomClass::rechargeBalance($custom, $pool, $shop, $staff, $payWay, $params);
|
|
|
$custom = $respond['custom'] ?? $custom;
|
|
|
$ghs = $respond['ghs'] ?? $ghs;
|
|
|
$customRecharge = $respond['customRecharge'] ?? null;
|
|
|
|
|
|
$allocate = self::fifoSettleWithPool($pair, $shop, $staff, $pool, [
|
|
|
- 'remark' => $params['remark'] ?? '充值销账',
|
|
|
+ 'payWay' => $payWay,
|
|
|
+ 'balanceBeforeRecharge' => $balanceBeforeRecharge,
|
|
|
+ 'rechargeSource' => 'merchant',
|
|
|
'deduct_balance' => false,
|
|
|
]);
|
|
|
|
|
|
@@ -54,28 +61,6 @@ class GhsRechargeSettleService
|
|
|
return $respond;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 线上充值销账结账单备注:hd/ghs 共用 xhClear,须写清渠道。
|
|
|
- */
|
|
|
- public static function buildOnlineRechargeClearRemark($payWay)
|
|
|
- {
|
|
|
- $base = '线上充值销账';
|
|
|
- $payWay = intval($payWay);
|
|
|
- $wxPay = intval(dict::getDict('payWay', 'wxPay'));
|
|
|
- $aliPay = intval(dict::getDict('payWay', 'alipay'));
|
|
|
- if ($payWay === $wxPay) {
|
|
|
- return $base . '-微信';
|
|
|
- }
|
|
|
- if ($payWay === $aliPay) {
|
|
|
- return $base . '-支付宝';
|
|
|
- }
|
|
|
- $nameMap = dict::getDict('payWayName');
|
|
|
- if (isset($nameMap[$payWay]) && $nameMap[$payWay] !== '') {
|
|
|
- return $base . '-' . $nameMap[$payWay];
|
|
|
- }
|
|
|
- return $base;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 线上支付已入账后的 FIFO 销账(CustomRechargeClass::thirdPay)。
|
|
|
* $balanceBeforeRecharge 为本次入账前客户净余额,用于 xhClear.onlinePay 判定。
|
|
|
@@ -91,10 +76,10 @@ class GhsRechargeSettleService
|
|
|
$thirdNo = is_object($customRecharge) ? (string)($customRecharge->returnCode ?? '') : '';
|
|
|
|
|
|
$allocate = self::fifoSettleWithPool($pair, $shop, null, $pool, [
|
|
|
- 'remark' => self::buildOnlineRechargeClearRemark($payWay),
|
|
|
'payWay' => $payWay,
|
|
|
'thirdNo' => $thirdNo,
|
|
|
'balanceBeforeRecharge' => $balanceBeforeRecharge,
|
|
|
+ 'rechargeSource' => 'online',
|
|
|
'deduct_balance' => false,
|
|
|
]);
|
|
|
|
|
|
@@ -178,9 +163,11 @@ class GhsRechargeSettleService
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ $balanceBeforeRecharge = self::money($pair['custom']->balance ?? 0);
|
|
|
$allocate = self::fifoSettleWithPool($pair, $shop, $staff, $pool, [
|
|
|
- 'remark' => '余额结账',
|
|
|
'deduct_balance' => true,
|
|
|
+ 'balanceBeforeRecharge' => $balanceBeforeRecharge,
|
|
|
+ 'rechargeSource' => 'merchant',
|
|
|
]);
|
|
|
|
|
|
return true;
|
|
|
@@ -257,21 +244,25 @@ class GhsRechargeSettleService
|
|
|
$sjId = $shop->sjId ?? 0;
|
|
|
$shopId = $shop->id ?? 0;
|
|
|
|
|
|
- $payWay = isset($options['payWay']) && is_numeric($options['payWay'])
|
|
|
+ $actualPayWay = isset($options['payWay']) && is_numeric($options['payWay'])
|
|
|
? intval($options['payWay'])
|
|
|
: intval(dict::getDict('payWay', 'unPay'));
|
|
|
- // 线上充值销账:仅当本次来款足额销账且入账前无正余额时标 onlinePay=yes
|
|
|
if (array_key_exists('balanceBeforeRecharge', $options)) {
|
|
|
- $onlinePay = self::resolveClearOnlinePayForRechargeFifo(
|
|
|
+ $payMeta = self::resolveClearPayMetaForRechargeFifo(
|
|
|
$options['balanceBeforeRecharge'],
|
|
|
$planTotal,
|
|
|
$pool,
|
|
|
- $payWay,
|
|
|
+ $actualPayWay,
|
|
|
+ $options['rechargeSource'] ?? 'merchant',
|
|
|
!empty($options['deduct_balance'])
|
|
|
);
|
|
|
+ $payWay = $payMeta['payWay'];
|
|
|
+ $onlinePay = $payMeta['onlinePay'];
|
|
|
} elseif (isset($options['onlinePay']) && is_numeric($options['onlinePay'])) {
|
|
|
+ $payWay = $actualPayWay;
|
|
|
$onlinePay = intval($options['onlinePay']);
|
|
|
} else {
|
|
|
+ $payWay = $actualPayWay;
|
|
|
$onlinePay = self::resolveOnlinePayByPayWay($payWay);
|
|
|
}
|
|
|
|
|
|
@@ -284,7 +275,7 @@ class GhsRechargeSettleService
|
|
|
'payWay' => $payWay,
|
|
|
'onlinePay' => $onlinePay,
|
|
|
'complete' => 0,
|
|
|
- 'remark' => $options['remark'] ?? '充值销账',
|
|
|
+ 'remark' => '',
|
|
|
], $plan, $sjId, $shopId);
|
|
|
|
|
|
OrderClearClass::applyNetBalanceClear($clear, $payWay, [
|
|
|
@@ -507,34 +498,69 @@ class GhsRechargeSettleService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 充值 FIFO 结账单 onlinePay:完全由本次线上来款销账且入账前无正余额 → yes,否则 not。
|
|
|
- * 余额结账(deduct_balance)或掺杂历史正余额时不标线上。
|
|
|
+ * 充值 FIFO 结账单 onlinePay / payWay。
|
|
|
+ * 入账前无正余额:hd 线上充→线上+实付方式;商家手动充→线下+实付方式。
|
|
|
+ * 入账前仍有正余额:一律线下,payWay=2(余额支付)。
|
|
|
*/
|
|
|
- protected static function resolveClearOnlinePayForRechargeFifo($balanceBeforeRecharge, $settledAmount, $poolAmount, $payWay, $deductBalance = false)
|
|
|
- {
|
|
|
+ protected static function resolveClearPayMetaForRechargeFifo(
|
|
|
+ $balanceBeforeRecharge,
|
|
|
+ $settledAmount,
|
|
|
+ $poolAmount,
|
|
|
+ $actualPayWay,
|
|
|
+ $rechargeSource,
|
|
|
+ $deductBalance = false
|
|
|
+ ) {
|
|
|
+ $notOnline = intval(dict::getDict('onlinePay', 'not'));
|
|
|
+ $yesOnline = intval(dict::getDict('onlinePay', 'yes'));
|
|
|
+
|
|
|
if ($deductBalance) {
|
|
|
- return intval(dict::getDict('onlinePay', 'not'));
|
|
|
- }
|
|
|
- $payWay = intval($payWay);
|
|
|
- $wxPay = intval(dict::getDict('payWay', 'wxPay'));
|
|
|
- $aliPay = intval(dict::getDict('payWay', 'alipay'));
|
|
|
- if (!in_array($payWay, [$wxPay, $aliPay], true)) {
|
|
|
- return intval(dict::getDict('onlinePay', 'not'));
|
|
|
- }
|
|
|
- if ($balanceBeforeRecharge === null) {
|
|
|
- return self::resolveOnlinePayByPayWay($payWay);
|
|
|
+ return [
|
|
|
+ 'onlinePay' => $notOnline,
|
|
|
+ 'payWay' => self::CLEAR_PAY_WAY_BALANCE,
|
|
|
+ ];
|
|
|
}
|
|
|
+
|
|
|
$before = bcadd((string)$balanceBeforeRecharge, '0', 2);
|
|
|
$settled = bcadd((string)$settledAmount, '0', 2);
|
|
|
$pool = bcadd((string)$poolAmount, '0', 2);
|
|
|
- // 入账前已有正余额:可能掺杂来源不明的历史余额,不标线上
|
|
|
+ $actualPayWay = intval($actualPayWay);
|
|
|
+
|
|
|
+ // 客户仍有正余额:掺杂历史余额,线下 + 余额支付
|
|
|
if (bccomp($before, '0', 2) > 0) {
|
|
|
- return intval(dict::getDict('onlinePay', 'not'));
|
|
|
+ return [
|
|
|
+ 'onlinePay' => $notOnline,
|
|
|
+ 'payWay' => self::CLEAR_PAY_WAY_BALANCE,
|
|
|
+ ];
|
|
|
}
|
|
|
+
|
|
|
if (bccomp($settled, '0', 2) <= 0 || bccomp($settled, $pool, 2) > 0) {
|
|
|
- return intval(dict::getDict('onlinePay', 'not'));
|
|
|
+ return [
|
|
|
+ 'onlinePay' => $notOnline,
|
|
|
+ 'payWay' => $actualPayWay,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 入账前无余额或负余额:hdApp 线上充值 → 线上 + 微信/支付宝等实付
|
|
|
+ if ($rechargeSource === 'online') {
|
|
|
+ $wxPay = intval(dict::getDict('payWay', 'wxPay'));
|
|
|
+ $aliPay = intval(dict::getDict('payWay', 'alipay'));
|
|
|
+ if (in_array($actualPayWay, [$wxPay, $aliPay], true)) {
|
|
|
+ return [
|
|
|
+ 'onlinePay' => $yesOnline,
|
|
|
+ 'payWay' => $actualPayWay,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return [
|
|
|
+ 'onlinePay' => $notOnline,
|
|
|
+ 'payWay' => $actualPayWay,
|
|
|
+ ];
|
|
|
}
|
|
|
- return intval(dict::getDict('onlinePay', 'yes'));
|
|
|
+
|
|
|
+ // ghsApp 商家手动充值:线下 + 所选支付方式
|
|
|
+ return [
|
|
|
+ 'onlinePay' => $notOnline,
|
|
|
+ 'payWay' => $actualPayWay,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
}
|