| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace bizHd\deduct\classes;
- use bizHd\balance\classes\BalanceChangeClass;
- use bizHd\balance\classes\BalanceGiveChangeClass;
- use bizHd\balance\classes\BalancePayChangeClass;
- use bizHd\custom\classes\CustomClass;
- use common\components\dict;
- use common\components\util;
- use bizHd\base\classes\BaseClass;
- class DeductClass extends BaseClass
- {
- public static $baseFile = '\bizHd\deduct\models\Deduct';
- public static function doDeduct($shop, $custom, $hd, $amount, $params)
- {
- $currentBalance = $custom->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();
- }
- }
|