DeductClass.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace bizHd\deduct\classes;
  3. use bizHd\balance\classes\BalanceChangeClass;
  4. use bizHd\balance\classes\BalanceGiveChangeClass;
  5. use bizHd\balance\classes\BalancePayChangeClass;
  6. use bizHd\custom\classes\CustomClass;
  7. use common\components\dict;
  8. use common\components\util;
  9. use bizHd\base\classes\BaseClass;
  10. class DeductClass extends BaseClass
  11. {
  12. public static $baseFile = '\bizHd\deduct\models\Deduct';
  13. public static function doDeduct($shop, $custom, $hd, $amount, $params)
  14. {
  15. $currentBalance = $custom->balance;
  16. if ($amount > $currentBalance) {
  17. util::fail('余额不够扣');
  18. }
  19. $custom->balance = bcsub($custom->balance, $amount, 2);
  20. $hd->balance = bcsub($hd->balance, $amount, 2);
  21. if (floatval($hd->balance) != floatval($custom->balance)) {
  22. util::fail('余额有问题');
  23. }
  24. $remark = $params['remark'] ?? '';
  25. $staffId = $params['staffId'] ?? 0;
  26. $staffName = $params['staffName'] ?? '';
  27. $shopId = $shop->id;
  28. $mainId = $shop->mainId;
  29. $hdId = $hd->id;
  30. $hdName = $hd->name;
  31. $customId = $custom->id;
  32. $customName = $custom->name;
  33. $data = [
  34. 'shopId' => $shopId,
  35. 'mainId' => $mainId,
  36. 'hdId' => $hdId,
  37. 'hdName' => $hdName,
  38. 'amount' => $amount,
  39. 'balance' => $custom->balance,
  40. 'customId' => $customId,
  41. 'customName' => $customName,
  42. 'staffId' => $staffId,
  43. 'staffName' => $staffName,
  44. 'status' => 1,
  45. 'remark' => $remark,
  46. ];
  47. $result = self::add($data, true);
  48. $relateId = $result->id;
  49. $event = '手动减少';
  50. $capitalType = dict::getDict('capitalType', 'deduct', 'id');
  51. $change = [
  52. 'customId' => $customId,
  53. 'customName' => $customName,
  54. 'hdId' => $hdId,
  55. 'hdName' => $hdName,
  56. 'relateId' => $relateId,
  57. 'onlinePay' => 1,
  58. 'capitalType' => $capitalType,
  59. 'amount' => $amount,
  60. 'balance' => $hd->balance,
  61. 'io' => 0,
  62. 'side' => 0,
  63. 'payWay' => 0,
  64. 'event' => $event,
  65. 'staffId' => $staffId,
  66. 'staffName' => $staffName,
  67. 'shopId' => $shopId,
  68. 'mainId' => $mainId,
  69. 'remark' => $remark,
  70. ];
  71. BalanceChangeClass::add($change, true);
  72. // 多处需要同步修改 balance_pay_give
  73. if ($hd->balancePay >= $amount) {
  74. $payAmount = $amount;
  75. $mustUseGiveAmount = 0;
  76. } else {
  77. $payAmount = $hd->balancePay;
  78. $mustUseGiveAmount = bcsub($amount, $payAmount, 2);
  79. if ($mustUseGiveAmount > $hd->balanceGive) {
  80. util::fail('赠送余额异常,不够扣');
  81. }
  82. }
  83. /************************* 使用充值余额 *****************************/
  84. $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
  85. $custom->balancePay = $customBalancePay;
  86. $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
  87. $hd->balancePay = $hdBalancePay;
  88. $payChange = [
  89. 'hdId' => $hdId,
  90. 'hdName' => $hdName,
  91. 'customId' => $customId,
  92. 'customName' => $customName,
  93. 'relateId' => $relateId,
  94. 'onlinePay' => 1,
  95. 'capitalType' => $capitalType,
  96. 'amount' => $payAmount,
  97. 'balance' => $customBalancePay,
  98. 'io' => 0,
  99. 'side' => 0,
  100. 'payWay' => 0,
  101. 'event' => $event,
  102. 'staffId' => $staffId,
  103. 'staffName' => $staffName,
  104. 'shopId' => $shopId,
  105. 'mainId' => $mainId,
  106. 'remark' => '',
  107. ];
  108. BalancePayChangeClass::add($payChange, true);
  109. /***************************** 使用赠送余额 ******************************/
  110. //应用掉的赠送金额
  111. if ($mustUseGiveAmount > 0) {
  112. $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
  113. if ($customBalanceGive < 0) {
  114. util::fail('余额异常哈!请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
  115. }
  116. $custom->balanceGive = $customBalanceGive;
  117. $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
  118. $hd->balanceGive = $hdBalanceGive;
  119. if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
  120. util::fail('余额有问题呢!请联系管理员,编号' . $hdId);
  121. }
  122. $giveChange = [
  123. 'hdId' => $hdId,
  124. 'hdName' => $hdName,
  125. 'customId' => $customId,
  126. 'customName' => $customName,
  127. 'relateId' => $relateId,
  128. 'onlinePay' => 1,
  129. 'capitalType' => $capitalType,
  130. 'amount' => $mustUseGiveAmount,
  131. 'balance' => $customBalanceGive,
  132. 'io' => 0,
  133. 'side' => 0,
  134. 'payWay' => 0,
  135. 'event' => $event,
  136. 'staffId' => $staffId,
  137. 'staffName' => $staffName,
  138. 'shopId' => $shopId,
  139. 'mainId' => $mainId,
  140. 'remark' => '',
  141. ];
  142. BalanceGiveChangeClass::add($giveChange, true);
  143. }
  144. $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
  145. if (floatval($addBalance) != floatval($custom->balance)) {
  146. util::fail('账户余额有问题!请检查哈,编号' . $customId);
  147. }
  148. // 仅对从充值余额(balancePay)扣减的部分扣积分/成长值
  149. $changeAmount = $payAmount;
  150. if (bccomp($changeAmount, 0, 2) === 1) {
  151. $newIntegral = bcsub($custom->integral, $changeAmount, 2);
  152. if (bccomp($newIntegral, 0, 2) === -1) {
  153. $newIntegral = 0;
  154. }
  155. $newGrowth = bcsub($custom->growth, $changeAmount, 2);
  156. if (bccomp($newGrowth, 0, 2) === -1) {
  157. $newGrowth = 0;
  158. }
  159. $custom->integral = $newIntegral;
  160. $custom->growth = $newGrowth;
  161. $memberData = CustomClass::getCustomExpenseLevel($custom->growth, $mainId); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
  162. CustomClass::updateById($customId, [
  163. 'member' => (int) ($memberData['level'] ?? 0),
  164. 'memberName' => (string) ($memberData['name'] ?? ''),
  165. ]);
  166. }
  167. $custom->save();
  168. $hd->save();
  169. }
  170. }