balance; if ($amount > $currentBalance) { util::fail('余额不够扣'); } $custom->balance = bcsub($custom->balance, $amount, 2); $custom->save(); $hd->balance = bcsub($hd->balance, $amount, 2); $hd->save(); 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; $custom->save(); $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2); $hd->balancePay = $hdBalancePay; $hd->save(); $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; $custom->save(); $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2); $hd->balanceGive = $hdBalanceGive; $hd->save(); 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); } } }