|
|
@@ -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,19 +649,187 @@ class CustomClass extends BaseClass
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 调用方已 lockAccountPair 时,校验 custom/ghs 净余额一致。
|
|
|
+ */
|
|
|
+ protected static function assertPairBalanceEqual($custom, $ghs)
|
|
|
+ {
|
|
|
+ if (empty($ghs)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $customBal = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+ $ghsBal = bcadd((string)($ghs->balance ?? '0'), '0', 2);
|
|
|
+ if (bccomp($customBal, $ghsBal, 2) !== 0) {
|
|
|
+ util::fail('客户与供货商余额不一致,请联系管理员');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开单扣减净 balance:写 custom(及成对 ghs),调用方须已 FOR UPDATE 锁行。
|
|
|
+ */
|
|
|
+ protected static function savePairBalanceAfterDeduct($custom, $ghs, $newBalance)
|
|
|
+ {
|
|
|
+ $newBalance = bcadd((string)$newBalance, '0', 2);
|
|
|
+ $custom->balance = $newBalance;
|
|
|
+ $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
|
|
|
+ $custom->save(false, ['balance', 'isDebt']);
|
|
|
+ if (empty($ghs)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
|
|
|
+ $ghs->balance = $newBalance;
|
|
|
+ $ghs->debt = bccomp($newBalance, '0', 2) < 0 ? 2 : 1;
|
|
|
+ $ghs->save(false, ['balance', 'debt']);
|
|
|
+ self::assertPairBalanceEqual($custom, $ghs);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected static function addGhsOrderBalanceChange($custom, $ghs, $order, $amount, $newBalance, $bookOrder = false)
|
|
|
+ {
|
|
|
+ if (empty($ghs)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $orderSn = $order->orderSn ?? '';
|
|
|
+ $event = '购买花材,单号:' . $orderSn;
|
|
|
+ if ($bookOrder) {
|
|
|
+ $event = '预订单多退少补,单号:' . $orderSn;
|
|
|
+ }
|
|
|
+ $customShopId = $custom->shopId ?? 0;
|
|
|
+ $customShop = ShopClass::getById($customShopId, true);
|
|
|
+ $gbData = [
|
|
|
+ 'ghsId' => $ghs->id ?? 0,
|
|
|
+ 'relateId' => $order->id ?? 0,
|
|
|
+ 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
|
|
|
+ 'capitalType' => dict::getDict('capitalType', 'xhGhsOrder', 'id'),
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'balance' => $newBalance,
|
|
|
+ 'io' => 0,
|
|
|
+ 'side' => 0,
|
|
|
+ 'onlinePay' => 1,
|
|
|
+ 'payWay' => $order->payWay ?? 0,
|
|
|
+ 'fromType' => dict::getDict('fromType', 'shop'),
|
|
|
+ 'event' => $event,
|
|
|
+ 'sjId' => $customShop->sjId ?? ($custom->sjId ?? 0),
|
|
|
+ 'mainId' => $customShop->mainId ?? 0,
|
|
|
+ 'shopId' => $customShopId,
|
|
|
+ 'remark' => '',
|
|
|
+ ];
|
|
|
+ GhsBalanceChangeClass::add($gbData, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开单余额支付:扣减净 balance 并记购买流水(须传入 payAfter 已 lockAccountPair 的 custom/ghs)。
|
|
|
+ */
|
|
|
+ public static function payToChangeBalance($order, $custom, $ghs = null)
|
|
|
+ {
|
|
|
+ $actPrice = bcadd((string)($order->actPrice ?? '0'), '0', 2);
|
|
|
+ if (bccomp($actPrice, '0', 2) <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (empty($custom)) {
|
|
|
+ util::fail('没有找到客户');
|
|
|
+ }
|
|
|
+ AccountMoneyClass::ensureCustomMoneyReady($custom, true);
|
|
|
+ if (!empty($ghs)) {
|
|
|
+ AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
|
|
|
+ self::assertPairBalanceEqual($custom, $ghs);
|
|
|
+ }
|
|
|
+ $beforeBalance = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+ if (bccomp($beforeBalance, $actPrice, 2) < 0) {
|
|
|
+ util::fail('余额不足');
|
|
|
+ }
|
|
|
+ $newBalance = bcsub($beforeBalance, $actPrice, 2);
|
|
|
+ self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
|
|
|
+
|
|
|
+ $orderSn = $order->orderSn ?? '';
|
|
|
+ $customId = $custom->id ?? 0;
|
|
|
+ $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);
|
|
|
+ self::addGhsOrderBalanceChange($custom, $ghs, $order, $actPrice, $newBalance, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 挂账开单时若账户有正余额且不足本单:用余额先结本单一部分(只改 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
|
|
|
*/
|
|
|
- public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
|
|
|
+ public static function cgDebtAmountAdd($custom, $order, $bookOrder = false, $ghs = null)
|
|
|
{
|
|
|
// 挂账开单:合并后 balance 减少,不再写 CustomDebtChange
|
|
|
AccountMoneyClass::ensureCustomMoneyReady($custom, true);
|
|
|
- $amount = $order->remainDebtPrice ?? 0;
|
|
|
+ if (!empty($ghs)) {
|
|
|
+ AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
|
|
|
+ self::assertPairBalanceEqual($custom, $ghs);
|
|
|
+ }
|
|
|
+ $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) {
|
|
|
@@ -692,8 +861,7 @@ class CustomClass extends BaseClass
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $custom->balance = $newBalance;
|
|
|
- $custom->save(false, ['balance', 'isDebt']);
|
|
|
+ self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
|
|
|
|
|
|
$relateId = $order->id ?? 0;
|
|
|
$customId = $custom->id ?? 0;
|
|
|
@@ -726,6 +894,7 @@ class CustomClass extends BaseClass
|
|
|
'remark' => '',
|
|
|
];
|
|
|
CustomBalanceChangeClass::add($change, true);
|
|
|
+ self::addGhsOrderBalanceChange($custom, $ghs, $order, $amount, $newBalance, $bookOrder);
|
|
|
|
|
|
$mainId = $order->mainId ?? 0;
|
|
|
$main = MainClass::getLockById($mainId);
|