|
|
@@ -9,6 +9,7 @@ use bizGhs\custom\classes\CustomClass;
|
|
|
use bizGhs\ghs\classes\GhsBalanceChangeClass;
|
|
|
use bizGhs\order\classes\OrderClearClass;
|
|
|
use bizGhs\order\classes\OrderClass;
|
|
|
+use common\components\dict;
|
|
|
use common\components\util;
|
|
|
|
|
|
/**
|
|
|
@@ -53,16 +54,47 @@ class GhsRechargeSettleService
|
|
|
return $respond;
|
|
|
}
|
|
|
|
|
|
- /** 线上支付已入账后的 FIFO 销账(CustomRechargeClass::thirdPay) */
|
|
|
- public static function onlinePayFifoClear($custom, $ghs, $shop, $amount, $customRecharge, $ghsRecharge = null)
|
|
|
+ /**
|
|
|
+ * 线上充值销账结账单备注: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 判定。
|
|
|
+ */
|
|
|
+ public static function onlinePayFifoClear($custom, $ghs, $shop, $amount, $customRecharge, $ghsRecharge = null, $balanceBeforeRecharge = null)
|
|
|
{
|
|
|
$pair = self::lockAccountPair($custom, $ghs);
|
|
|
$pool = self::money($amount);
|
|
|
|
|
|
self::voidAwaitPayClearBills($pair['custom']->id ?? 0);
|
|
|
|
|
|
+ $payWay = intval(is_object($customRecharge) ? ($customRecharge->payWay ?? dict::getDict('payWay', 'unPay')) : dict::getDict('payWay', 'unPay'));
|
|
|
+ $thirdNo = is_object($customRecharge) ? (string)($customRecharge->returnCode ?? '') : '';
|
|
|
+
|
|
|
$allocate = self::fifoSettleWithPool($pair, $shop, null, $pool, [
|
|
|
- 'remark' => '线上充值销账',
|
|
|
+ 'remark' => self::buildOnlineRechargeClearRemark($payWay),
|
|
|
+ 'payWay' => $payWay,
|
|
|
+ 'thirdNo' => $thirdNo,
|
|
|
+ 'balanceBeforeRecharge' => $balanceBeforeRecharge,
|
|
|
'deduct_balance' => false,
|
|
|
]);
|
|
|
|
|
|
@@ -225,18 +257,49 @@ class GhsRechargeSettleService
|
|
|
$sjId = $shop->sjId ?? 0;
|
|
|
$shopId = $shop->id ?? 0;
|
|
|
|
|
|
+ $payWay = 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(
|
|
|
+ $options['balanceBeforeRecharge'],
|
|
|
+ $planTotal,
|
|
|
+ $pool,
|
|
|
+ $payWay,
|
|
|
+ !empty($options['deduct_balance'])
|
|
|
+ );
|
|
|
+ } elseif (isset($options['onlinePay']) && is_numeric($options['onlinePay'])) {
|
|
|
+ $onlinePay = intval($options['onlinePay']);
|
|
|
+ } else {
|
|
|
+ $onlinePay = self::resolveOnlinePayByPayWay($payWay);
|
|
|
+ }
|
|
|
+
|
|
|
$clear = OrderClearClass::clearWithAmountMap([
|
|
|
'customId' => $customId,
|
|
|
'ghsShopAdminId' => $staffId,
|
|
|
'ghsShopId' => $shopId,
|
|
|
'ghsShopAdminName' => $staffName,
|
|
|
'modifyPrice' => $planTotal,
|
|
|
- 'payWay' => 0,
|
|
|
+ 'payWay' => $payWay,
|
|
|
+ 'onlinePay' => $onlinePay,
|
|
|
'complete' => 0,
|
|
|
'remark' => $options['remark'] ?? '充值销账',
|
|
|
], $plan, $sjId, $shopId);
|
|
|
|
|
|
- OrderClearClass::applyNetBalanceClear($clear, 0, ['skipCashMoney' => true]);
|
|
|
+ OrderClearClass::applyNetBalanceClear($clear, $payWay, [
|
|
|
+ 'skipCashMoney' => true,
|
|
|
+ 'onlinePay' => $onlinePay,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $clearId = intval($clear->id ?? 0);
|
|
|
+ if ($clearId > 0 && !empty($options['thirdNo'])) {
|
|
|
+ $clear = OrderClearClass::getLockById($clearId);
|
|
|
+ if (!empty($clear) && self::modelHasAttr($clear, 'thirdNo')) {
|
|
|
+ $clear->thirdNo = (string)$options['thirdNo'];
|
|
|
+ $clear->save(false, ['thirdNo']);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
$settled = self::money(OrderClearClass::sumClearAmountByClearId($clear->id ?? 0));
|
|
|
if (bccomp($settled, $planTotal, 2) !== 0) {
|
|
|
@@ -276,7 +339,7 @@ class GhsRechargeSettleService
|
|
|
if (empty($custom)) {
|
|
|
util::fail('没有找到客户');
|
|
|
}
|
|
|
- $custom = AccountMoneyClass::ensureCustomMoneyReady($custom, true);
|
|
|
+ AccountMoneyClass::ensureCustomMoneyReady($custom, true);
|
|
|
|
|
|
$ghsId = $custom->ghsId ?? 0;
|
|
|
if (empty($ghs)) {
|
|
|
@@ -285,10 +348,10 @@ class GhsRechargeSettleService
|
|
|
if (empty($ghs)) {
|
|
|
util::fail('没有找到供货商');
|
|
|
}
|
|
|
- $ghs = AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
|
|
|
+ AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
|
|
|
|
|
|
if (bccomp(self::money($custom->balance), self::money($ghs->balance), 2) !== 0) {
|
|
|
- util::fail('余额有问题,请联系管理员');
|
|
|
+ util::fail('客户与供货商余额不一致,请联系管理员');
|
|
|
}
|
|
|
|
|
|
return ['custom' => $custom, 'ghs' => $ghs];
|
|
|
@@ -431,4 +494,47 @@ class GhsRechargeSettleService
|
|
|
return bcadd((string)$value, '0', 2);
|
|
|
}
|
|
|
|
|
|
+ /** 微信/支付宝视为线上付,其余为线下(非充值销账 FIFO 场景兜底) */
|
|
|
+ protected static function resolveOnlinePayByPayWay($payWay)
|
|
|
+ {
|
|
|
+ $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', 'yes'));
|
|
|
+ }
|
|
|
+ return intval(dict::getDict('onlinePay', 'not'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 充值 FIFO 结账单 onlinePay:完全由本次线上来款销账且入账前无正余额 → yes,否则 not。
|
|
|
+ * 余额结账(deduct_balance)或掺杂历史正余额时不标线上。
|
|
|
+ */
|
|
|
+ protected static function resolveClearOnlinePayForRechargeFifo($balanceBeforeRecharge, $settledAmount, $poolAmount, $payWay, $deductBalance = false)
|
|
|
+ {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ $before = bcadd((string)$balanceBeforeRecharge, '0', 2);
|
|
|
+ $settled = bcadd((string)$settledAmount, '0', 2);
|
|
|
+ $pool = bcadd((string)$poolAmount, '0', 2);
|
|
|
+ // 入账前已有正余额:可能掺杂来源不明的历史余额,不标线上
|
|
|
+ if (bccomp($before, '0', 2) > 0) {
|
|
|
+ return intval(dict::getDict('onlinePay', 'not'));
|
|
|
+ }
|
|
|
+ if (bccomp($settled, '0', 2) <= 0 || bccomp($settled, $pool, 2) > 0) {
|
|
|
+ return intval(dict::getDict('onlinePay', 'not'));
|
|
|
+ }
|
|
|
+ return intval(dict::getDict('onlinePay', 'yes'));
|
|
|
+ }
|
|
|
+
|
|
|
}
|