|
|
@@ -14,6 +14,7 @@ use bizGhs\shop\classes\ShopMoneyClass;
|
|
|
use bizGhs\shop\classes\TotalDebtChangeClass;
|
|
|
use bizGhs\ghs\classes\GhsBalanceClass;
|
|
|
use bizGhs\ghs\classes\GhsRechargeClass;
|
|
|
+use bizGhs\order\classes\OrderClearClass;
|
|
|
use bizGhs\shop\classes\ShopClass;
|
|
|
use bizHd\member\classes\MemberLevelClass;
|
|
|
use bizHd\saas\classes\ApplyClass;
|
|
|
@@ -648,6 +649,94 @@ class CustomClass extends BaseClass
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 开单余额支付:扣减净 balance 并记购买流水(净余额模型,对照 hd payToChangeBalance)。
|
|
|
+ */
|
|
|
+ public static function payToChangeBalance($order)
|
|
|
+ {
|
|
|
+ $actPrice = bcadd((string)($order->actPrice ?? '0'), '0', 2);
|
|
|
+ if (bccomp($actPrice, '0', 2) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $customId = $order->customId ?? 0;
|
|
|
+ $custom = self::getLockById($customId);
|
|
|
+ if (empty($custom)) {
|
|
|
+ util::fail('没有找到客户');
|
|
|
+ }
|
|
|
+ AccountMoneyClass::ensureCustomMoneyReady($custom, true);
|
|
|
+ $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+ if (bccomp($beforeBalance, $actPrice, 2) < 0) {
|
|
|
+ util::fail('余额不足');
|
|
|
+ }
|
|
|
+ $newBalance = bcsub($beforeBalance, $actPrice, 2);
|
|
|
+ $custom->balance = $newBalance;
|
|
|
+ $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
|
|
|
+ $custom->save(false, ['balance', 'isDebt']);
|
|
|
+
|
|
|
+ $orderSn = $order->orderSn ?? '';
|
|
|
+ $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
|
|
|
+ $change = [
|
|
|
+ 'relateId' => $order->id ?? 0,
|
|
|
+ 'customId' => $customId,
|
|
|
+ 'customName' => $custom->name ?? '',
|
|
|
+ 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
|
|
|
+ 'capitalType' => $capitalType,
|
|
|
+ 'amount' => $actPrice,
|
|
|
+ 'balance' => $newBalance,
|
|
|
+ 'io' => 0,
|
|
|
+ 'payWay' => dict::getDict('payWay', 'balancePay'),
|
|
|
+ 'event' => '购买花材,单号:' . $orderSn,
|
|
|
+ 'sjId' => $order->sjId ?? 0,
|
|
|
+ 'shopId' => $order->shopId ?? 0,
|
|
|
+ 'mainId' => $order->mainId ?? 0,
|
|
|
+ 'staffId' => $order->shopAdminId ?? 0,
|
|
|
+ 'staffName' => $order->shopAdminName ?? '',
|
|
|
+ 'side' => 0,
|
|
|
+ 'fromType' => dict::getDict('fromType', 'shop'),
|
|
|
+ 'remark' => '',
|
|
|
+ ];
|
|
|
+ CustomBalanceChangeClass::add($change, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 挂账开单时若账户有正余额且不足本单:用余额先结本单一部分(只改 remainDebtPrice,不另扣 balance)。
|
|
|
+ */
|
|
|
+ protected static function inlineOrderBalancePartialClear($custom, $order, $clearAmount)
|
|
|
+ {
|
|
|
+ $clearAmount = bcadd((string)$clearAmount, '0', 2);
|
|
|
+ if (bccomp($clearAmount, '0', 2) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $orderId = intval($order->id ?? 0);
|
|
|
+ if ($orderId <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $shopId = intval($order->shopId ?? 0);
|
|
|
+ $shop = ShopClass::getById($shopId, true);
|
|
|
+ if (empty($shop)) {
|
|
|
+ util::fail('没有找到门店');
|
|
|
+ }
|
|
|
+ $staffId = intval($order->shopAdminId ?? 0);
|
|
|
+ $staffName = $order->shopAdminName ?? '';
|
|
|
+ $sjId = $shop->sjId ?? 0;
|
|
|
+ $orderSn = $order->orderSn ?? '';
|
|
|
+
|
|
|
+ OrderClearClass::expireAwaitPayClears(intval($custom->id ?? 0));
|
|
|
+ $clear = OrderClearClass::clearWithAmountMap([
|
|
|
+ 'customId' => $custom->id ?? 0,
|
|
|
+ 'ghsShopAdminId' => $staffId,
|
|
|
+ 'ghsShopId' => $shopId,
|
|
|
+ 'ghsShopAdminName' => $staffName,
|
|
|
+ 'modifyPrice' => $clearAmount,
|
|
|
+ 'payWay' => dict::getDict('payWay', 'balancePay'),
|
|
|
+ 'complete' => 0,
|
|
|
+ 'remark' => '开单余额抵挂账',
|
|
|
+ ], [
|
|
|
+ ['orderId' => $orderId, 'clearAmount' => $clearAmount, 'orderSn' => $orderSn],
|
|
|
+ ], $sjId, $shopId);
|
|
|
+ OrderClearClass::applyNetBalanceClear($clear, dict::getDict('payWay', 'balancePay'), ['skipCashMoney' => true]);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 挂账开单:增加客户待结(改造后 balance 减少,只记余额变动,不再记挂账变动)
|
|
|
* ssh 20220306
|
|
|
@@ -656,11 +745,18 @@ class CustomClass extends BaseClass
|
|
|
{
|
|
|
// 挂账开单:合并后 balance 减少,不再写 CustomDebtChange
|
|
|
AccountMoneyClass::ensureCustomMoneyReady($custom, true);
|
|
|
- $amount = $order->remainDebtPrice ?? 0;
|
|
|
+ $amount = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
|
|
|
if (bccomp($amount, '0', 2) <= 0) {
|
|
|
return;
|
|
|
}
|
|
|
- $beforeBalance = $custom->balance ?? '0.00';
|
|
|
+ $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+ $positiveBalance = bccomp($beforeBalance, '0', 2) > 0 ? $beforeBalance : '0.00';
|
|
|
+ if (bccomp($positiveBalance, $amount, 2) >= 0) {
|
|
|
+ util::fail('余额充足,请用余额支付');
|
|
|
+ }
|
|
|
+ if (bccomp($positiveBalance, '0', 2) > 0) {
|
|
|
+ self::inlineOrderBalancePartialClear($custom, $order, $positiveBalance);
|
|
|
+ }
|
|
|
$custom->isDebt = CustomClass::IS_DEBT_YES;
|
|
|
$debtLimit = $custom->debtLimit ?? 0;
|
|
|
if (!$bookOrder) {
|