|
|
@@ -650,30 +650,98 @@ class CustomClass extends BaseClass
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 开单余额支付:扣减净 balance 并记购买流水(净余额模型,对照 hd payToChangeBalance)。
|
|
|
+ * 调用方已 lockAccountPair 时,校验 custom/ghs 净余额一致。
|
|
|
*/
|
|
|
- public static function payToChangeBalance($order)
|
|
|
+ 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;
|
|
|
}
|
|
|
- $customId = $order->customId ?? 0;
|
|
|
- $custom = self::getLockById($customId);
|
|
|
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);
|
|
|
- $custom->balance = $newBalance;
|
|
|
- $custom->isDebt = bccomp($newBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
|
|
|
- $custom->save(false, ['balance', 'isDebt']);
|
|
|
+ self::savePairBalanceAfterDeduct($custom, $ghs, $newBalance);
|
|
|
|
|
|
$orderSn = $order->orderSn ?? '';
|
|
|
+ $customId = $custom->id ?? 0;
|
|
|
$capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
|
|
|
$change = [
|
|
|
'relateId' => $order->id ?? 0,
|
|
|
@@ -696,6 +764,7 @@ class CustomClass extends BaseClass
|
|
|
'remark' => '',
|
|
|
];
|
|
|
CustomBalanceChangeClass::add($change, true);
|
|
|
+ self::addGhsOrderBalanceChange($custom, $ghs, $order, $actPrice, $newBalance, false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -741,10 +810,14 @@ class CustomClass extends BaseClass
|
|
|
* 挂账开单:增加客户待结(改造后 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);
|
|
|
+ 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;
|
|
|
@@ -788,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;
|
|
|
@@ -822,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);
|