balance; if ($amount > $currentBalance) { util::fail('余额不够扣'); } $custom->balance = bcsub($custom->balance, $amount, 2); $hd->balance = bcsub($hd->balance, $amount, 2); if (floatval($hd->balance) != floatval($custom->balance)) { util::fail('余额有问题'); } $remark = $params['remark'] ?? ''; $staffId = $params['staffId'] ?? 0; $staffName = $params['staffName'] ?? ''; $shopId = $shop->id; $mainId = $shop->mainId; $hdId = $hd->id; $hdName = $hd->name; $customId = $custom->id; $customName = $custom->name; $data = [ 'shopId' => $shopId, 'mainId' => $mainId, 'hdId' => $hdId, 'hdName' => $hdName, 'amount' => $amount, 'balance' => $custom->balance, 'customId' => $customId, 'customName' => $customName, 'staffId' => $staffId, 'staffName' => $staffName, 'status' => 1, 'remark' => $remark, ]; $result = self::add($data, true); $relateId = $result->id; $event = '手动减少'; $capitalType = dict::getDict('capitalType', 'deduct', 'id'); $change = [ 'customId' => $customId, 'customName' => $customName, 'hdId' => $hdId, 'hdName' => $hdName, 'relateId' => $relateId, 'onlinePay' => 1, 'capitalType' => $capitalType, 'amount' => $amount, 'balance' => $hd->balance, 'io' => 0, 'side' => 0, 'payWay' => 0, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => $remark, ]; BalanceChangeClass::add($change, true); // 多处需要同步修改 balance_pay_give if ($hd->balancePay >= $amount) { $payAmount = $amount; $mustUseGiveAmount = 0; } else { $payAmount = $hd->balancePay; $mustUseGiveAmount = bcsub($amount, $payAmount, 2); if ($mustUseGiveAmount > $hd->balanceGive) { util::fail('赠送余额异常,不够扣'); } } /************************* 使用充值余额 *****************************/ $customBalancePay = bcsub($custom->balancePay, $payAmount, 2); $custom->balancePay = $customBalancePay; $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2); $hd->balancePay = $hdBalancePay; $payChange = [ 'hdId' => $hdId, 'hdName' => $hdName, 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => 1, 'capitalType' => $capitalType, 'amount' => $payAmount, 'balance' => $customBalancePay, 'io' => 0, 'side' => 0, 'payWay' => 0, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => '', ]; BalancePayChangeClass::add($payChange, true); /***************************** 使用赠送余额 ******************************/ //应用掉的赠送金额 if ($mustUseGiveAmount > 0) { $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2); if ($customBalanceGive < 0) { util::fail('余额异常哈!请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive); } $custom->balanceGive = $customBalanceGive; $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2); $hd->balanceGive = $hdBalanceGive; if (floatval($customBalanceGive) != floatval($hdBalanceGive)) { util::fail('余额有问题呢!请联系管理员,编号' . $hdId); } $giveChange = [ 'hdId' => $hdId, 'hdName' => $hdName, 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => 1, 'capitalType' => $capitalType, 'amount' => $mustUseGiveAmount, 'balance' => $customBalanceGive, 'io' => 0, 'side' => 0, 'payWay' => 0, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => '', ]; BalanceGiveChangeClass::add($giveChange, true); } $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2); if (floatval($addBalance) != floatval($custom->balance)) { util::fail('账户余额有问题!请检查哈,编号' . $customId); } // 仅对从充值余额(balancePay)扣减的部分扣积分/成长值 $changeAmount = $payAmount; if (bccomp($changeAmount, 0, 2) === 1) { $newIntegral = bcsub($custom->integral, $changeAmount, 2); if (bccomp($newIntegral, 0, 2) === -1) { $newIntegral = 0; } $newGrowth = bcsub($custom->growth, $changeAmount, 2); if (bccomp($newGrowth, 0, 2) === -1) { $newGrowth = 0; } $custom->integral = $newIntegral; $custom->growth = $newGrowth; $memberData = CustomClass::getCustomExpenseLevel($custom->growth, $mainId); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel CustomClass::updateById($customId, [ 'member' => (int) ($memberData['level'] ?? 0), 'memberName' => (string) ($memberData['name'] ?? ''), ]); } $custom->save(); $hd->save(); } }