DeductClass.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 common\components\dict;
  7. use common\components\util;
  8. use Yii;
  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. $custom->save();
  21. $hd->balance = bcsub($hd->balance, $amount, 2);
  22. $hd->save();
  23. if (floatval($hd->balance) != floatval($custom->balance)) {
  24. util::fail('余额有问题');
  25. }
  26. $remark = $params['remark'] ?? '';
  27. $staffId = $params['staffId'] ?? 0;
  28. $staffName = $params['staffName'] ?? '';
  29. $shopId = $shop->id;
  30. $mainId = $shop->mainId;
  31. $hdId = $hd->id;
  32. $hdName = $hd->name;
  33. $customId = $custom->id;
  34. $customName = $custom->name;
  35. $data = [
  36. 'shopId' => $shopId,
  37. 'mainId' => $mainId,
  38. 'hdId' => $hdId,
  39. 'hdName' => $hdName,
  40. 'amount' => $amount,
  41. 'balance' => $custom->balance,
  42. 'customId' => $customId,
  43. 'customName' => $customName,
  44. 'staffId' => $staffId,
  45. 'staffName' => $staffName,
  46. 'status' => 1,
  47. 'remark' => $remark,
  48. ];
  49. $result = self::add($data, true);
  50. $relateId = $result->id;
  51. $event = '手动减少';
  52. $capitalType = dict::getDict('capitalType', 'deduct', 'id');
  53. $change = [
  54. 'customId' => $customId,
  55. 'customName' => $customName,
  56. 'hdId' => $hdId,
  57. 'hdName' => $hdName,
  58. 'relateId' => $relateId,
  59. 'onlinePay' => 1,
  60. 'capitalType' => $capitalType,
  61. 'amount' => $amount,
  62. 'balance' => $hd->balance,
  63. 'io' => 0,
  64. 'side' => 0,
  65. 'payWay' => 0,
  66. 'event' => $event,
  67. 'staffId' => $staffId,
  68. 'staffName' => $staffName,
  69. 'shopId' => $shopId,
  70. 'mainId' => $mainId,
  71. 'remark' => $remark,
  72. ];
  73. BalanceChangeClass::add($change, true);
  74. // 多处需要同步修改 balance_pay_give
  75. if ($hd->balancePay >= $amount) {
  76. $payAmount = $amount;
  77. $mustUseGiveAmount = 0;
  78. } else {
  79. $payAmount = $hd->balancePay;
  80. $mustUseGiveAmount = bcsub($amount, $payAmount, 2);
  81. if ($mustUseGiveAmount > $hd->balanceGive) {
  82. util::fail('赠送余额异常,不够扣');
  83. }
  84. }
  85. /************************* 使用充值余额 *****************************/
  86. $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
  87. $custom->balancePay = $customBalancePay;
  88. $custom->save();
  89. $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
  90. $hd->balancePay = $hdBalancePay;
  91. $hd->save();
  92. $payChange = [
  93. 'hdId' => $hdId,
  94. 'hdName' => $hdName,
  95. 'customId' => $customId,
  96. 'customName' => $customName,
  97. 'relateId' => $relateId,
  98. 'onlinePay' => 1,
  99. 'capitalType' => $capitalType,
  100. 'amount' => $payAmount,
  101. 'balance' => $customBalancePay,
  102. 'io' => 0,
  103. 'side' => 0,
  104. 'payWay' => 0,
  105. 'event' => $event,
  106. 'staffId' => $staffId,
  107. 'staffName' => $staffName,
  108. 'shopId' => $shopId,
  109. 'mainId' => $mainId,
  110. 'remark' => '',
  111. ];
  112. BalancePayChangeClass::add($payChange, true);
  113. /***************************** 使用赠送余额 ******************************/
  114. //应用掉的赠送金额
  115. if ($mustUseGiveAmount > 0) {
  116. $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
  117. if ($customBalanceGive < 0) {
  118. util::fail('余额异常哈!请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
  119. }
  120. $custom->balanceGive = $customBalanceGive;
  121. $custom->save();
  122. $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
  123. $hd->balanceGive = $hdBalanceGive;
  124. $hd->save();
  125. if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
  126. util::fail('余额有问题呢!请联系管理员,编号' . $hdId);
  127. }
  128. $giveChange = [
  129. 'hdId' => $hdId,
  130. 'hdName' => $hdName,
  131. 'customId' => $customId,
  132. 'customName' => $customName,
  133. 'relateId' => $relateId,
  134. 'onlinePay' => 1,
  135. 'capitalType' => $capitalType,
  136. 'amount' => $mustUseGiveAmount,
  137. 'balance' => $customBalanceGive,
  138. 'io' => 0,
  139. 'side' => 0,
  140. 'payWay' => 0,
  141. 'event' => $event,
  142. 'staffId' => $staffId,
  143. 'staffName' => $staffName,
  144. 'shopId' => $shopId,
  145. 'mainId' => $mainId,
  146. 'remark' => '',
  147. ];
  148. BalanceGiveChangeClass::add($giveChange, true);
  149. }
  150. $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
  151. if (floatval($addBalance) != floatval($custom->balance)) {
  152. util::fail('账户余额有问题!请检查哈,编号' . $customId);
  153. }
  154. }
  155. }