Reward.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace addons\shopro\service\commission;
  3. // use addons\shopro\library\Oper;
  4. use app\admin\model\shopro\commission\Log as LogModel;
  5. use app\admin\model\shopro\commission\Order as OrderModel;
  6. use app\admin\model\shopro\commission\Reward as RewardModel;
  7. use app\admin\model\shopro\user\User as UserModel;
  8. use addons\shopro\service\Wallet as WalletService;
  9. /**
  10. * 结算奖金
  11. */
  12. class Reward
  13. {
  14. protected $event = '';
  15. /**
  16. * 执行奖金计划,直接派发佣金
  17. *
  18. * @param string $event 分佣的事件
  19. */
  20. public function __construct($event)
  21. {
  22. $this->event = $event;
  23. }
  24. /**
  25. * 执行奖金计划, 派发整单佣金
  26. *
  27. * @param mixed $commissionOrder|$commissionOrderId 分销订单
  28. */
  29. public function runCommissionRewardByOrder($commissionOrder)
  30. {
  31. if (is_numeric($commissionOrder)) {
  32. $commissionOrder = OrderModel::find($commissionOrder);
  33. }
  34. // 未找到分销订单
  35. if (!$commissionOrder) {
  36. return false;
  37. }
  38. // 已经操作过了
  39. if ($commissionOrder['commission_reward_status'] !== RewardModel::COMMISSION_REWARD_STATUS_PENDING) {
  40. return false;
  41. }
  42. $rewardEvent = $commissionOrder['reward_event'];
  43. // 不满足分佣事件
  44. if ($rewardEvent !== $this->event && $this->event !== 'admin') {
  45. return false;
  46. }
  47. // 更新分销订单结算状态
  48. $commissionOrder->commission_reward_status = RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED;
  49. $commissionOrder->commission_time = time();
  50. $commissionOrder->save();
  51. // 防止重复添加佣金
  52. $commissionRewards = RewardModel::where([
  53. 'commission_order_id' => $commissionOrder['id'],
  54. 'status' => RewardModel::COMMISSION_REWARD_STATUS_PENDING,
  55. ])->select();
  56. // 添加分销商收益、余额添加钱包、更新分销佣金结算状态、提醒分销商到账
  57. if (count($commissionRewards) > 0) {
  58. foreach ($commissionRewards as $commissionReward) {
  59. $this->runCommissionReward($commissionReward);
  60. }
  61. }
  62. return true;
  63. }
  64. /**
  65. * 执行奖金计划,直接派发佣金
  66. *
  67. * @param mixed $commissionReward|$commissionRewardId 奖金记录
  68. */
  69. public function runCommissionReward($commissionReward)
  70. {
  71. if (is_numeric($commissionReward)) {
  72. $commissionReward = RewardModel::find($commissionReward);
  73. }
  74. // 未找到奖金记录
  75. if (!$commissionReward) {
  76. return false;
  77. }
  78. if ($commissionReward->status == RewardModel::COMMISSION_REWARD_STATUS_PENDING) {
  79. $rewardAgent = new Agent($commissionReward->agent_id);
  80. if ($rewardAgent->isAgentAvaliable()) {
  81. $rewardAgent->agent->setInc('total_income', $commissionReward->commission);
  82. $commissionReward->status = RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED;
  83. $commissionReward->commission_time = time();
  84. $commissionReward->save();
  85. WalletService::change($rewardAgent->user, $commissionReward->type, $commissionReward->commission, 'reward_income', $commissionReward);
  86. LogModel::add($rewardAgent->user->id, 'reward', [
  87. 'type' => $this->event,
  88. 'reward' => $commissionReward
  89. ]);
  90. }
  91. }
  92. return true;
  93. }
  94. /**
  95. * 扣除/取消 分销订单
  96. *
  97. * @param mixed $commissionOrder|$commissionOrderId 分销订单
  98. * @param array $deductOrderMoney 扣除分销商的订单业绩 默认扣除
  99. * @param array $deleteReward 扣除分销订单奖金 默认扣除
  100. */
  101. public function backCommissionRewardByOrder($commissionOrder, $deductOrderMoney = true, $deleteReward = true)
  102. {
  103. if ($this->event !== 'admin' && $this->event !== 'refund') {
  104. return false;
  105. }
  106. if ($this->event === 'refund') {
  107. $config = new Config();
  108. $deductOrderMoney = $config->getRefundCommissionOrder();
  109. $deleteReward = $config->getRefundCommissionReward();
  110. }
  111. if (!$deductOrderMoney && !$deleteReward) {
  112. return false;
  113. }
  114. if (is_numeric($commissionOrder)) {
  115. $commissionOrder = OrderModel::find($commissionOrder);
  116. }
  117. // 未找到分销订单
  118. if (!$commissionOrder) {
  119. return false;
  120. }
  121. // 扣除分销商的订单业绩
  122. if ($deductOrderMoney) {
  123. // 变更分销订单状态
  124. if ($commissionOrder->commission_order_status == OrderModel::COMMISSION_ORDER_STATUS_YES) { // 扣除
  125. $commissionOrder->commission_order_status = OrderModel::COMMISSION_ORDER_STATUS_BACK;
  126. $commissionOrder->save();
  127. $orderAgent = new Agent($commissionOrder->agent_id);
  128. // 扣除分销订单业绩
  129. if($commissionOrder->self_buy) {
  130. $orderAgent->agent->setDec('child_order_money_0', $commissionOrder->amount);
  131. $orderAgent->agent->setDec('child_order_count_0', 1);
  132. }else {
  133. $orderAgent->agent->setDec('child_order_money_1', $commissionOrder->amount);
  134. $orderAgent->agent->setDec('child_order_count_1', 1);
  135. }
  136. // 重新计算分销链条业绩
  137. $orderAgent->createAsyncAgentUpgrade();
  138. LogModel::add($orderAgent->user->id, 'order', [
  139. 'type' => $this->event,
  140. 'order' => $commissionOrder,
  141. 'buyer' => UserModel::find($commissionOrder->buyer_id)
  142. ]);
  143. }
  144. if ($commissionOrder->commission_order_status == OrderModel::COMMISSION_ORDER_STATUS_NO) { // 取消
  145. $commissionOrder->commission_order_status = OrderModel::COMMISSION_ORDER_STATUS_CANCEL;
  146. $commissionOrder->save();
  147. }
  148. }
  149. // 变更分销订单佣金执行状态
  150. if ($deleteReward) {
  151. if ($commissionOrder->commission_reward_status == RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) { // 扣除
  152. $commissionOrder->commission_reward_status = RewardModel::COMMISSION_REWARD_STATUS_BACK;
  153. $commissionOrder->save();
  154. // 防止重复扣除佣金
  155. $commissionRewards = RewardModel::where([
  156. 'commission_order_id' => $commissionOrder->id,
  157. 'status' => RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED,
  158. ])->select();
  159. if (count($commissionRewards) > 0) {
  160. // 扣除分销佣金
  161. foreach ($commissionRewards as $commissionReward) {
  162. $this->backCommissionReward($commissionReward);
  163. }
  164. }
  165. }
  166. }
  167. if ($commissionOrder->commission_reward_status == RewardModel::COMMISSION_REWARD_STATUS_PENDING) { // 取消
  168. $commissionOrder->commission_reward_status = RewardModel::COMMISSION_REWARD_STATUS_CANCEL;
  169. $commissionOrder->save();
  170. $commissionRewards = RewardModel::where([
  171. 'commission_order_id' => $commissionOrder->id,
  172. 'status' => RewardModel::COMMISSION_REWARD_STATUS_PENDING
  173. ])->select();
  174. // 取消分销佣金
  175. if (count($commissionRewards) > 0) {
  176. foreach ($commissionRewards as $commissionReward) {
  177. $this->cancelCommissionReward($commissionReward);
  178. }
  179. }
  180. }
  181. return true;
  182. }
  183. /**
  184. * 扣除单笔分销佣金
  185. *
  186. * @param mixed $commissionReward|$commissionRewardId 奖金记录
  187. */
  188. public function backCommissionReward($commissionReward)
  189. {
  190. if (is_numeric($commissionReward)) {
  191. $commissionReward = RewardModel::find($commissionReward);
  192. }
  193. // 未找到奖金记录
  194. if (!$commissionReward) {
  195. return false;
  196. }
  197. if ($commissionReward->status == RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED) {
  198. $rewardAgent = new Agent($commissionReward->agent_id);
  199. if ($rewardAgent->agent->total_income < $commissionReward->commission) {
  200. $rewardAgent->agent->total_income = 0;
  201. $rewardAgent->agent->save();
  202. } else {
  203. $rewardAgent->agent->setDec('total_income', $commissionReward->commission);
  204. }
  205. WalletService::change($rewardAgent->user, $commissionReward->type, -$commissionReward->commission, 'reward_back', $commissionReward);
  206. $commissionReward->status = RewardModel::COMMISSION_REWARD_STATUS_BACK;
  207. $commissionReward->save();
  208. LogModel::add($rewardAgent->user->id, 'reward', [
  209. 'type' => $this->event,
  210. 'reward' => $commissionReward,
  211. ]);
  212. }
  213. return true;
  214. }
  215. /**
  216. * 取消单笔分销佣金
  217. *
  218. * @param mixed $commissionReward|$commissionRewardId 奖金记录
  219. */
  220. public function cancelCommissionReward($commissionReward)
  221. {
  222. if (is_numeric($commissionReward)) {
  223. $commissionReward = RewardModel::find($commissionReward);
  224. }
  225. // 未找到奖金记录
  226. if (!$commissionReward) {
  227. return false;
  228. }
  229. if ($commissionReward->status == RewardModel::COMMISSION_REWARD_STATUS_PENDING) {
  230. $commissionReward->status = RewardModel::COMMISSION_REWARD_STATUS_CANCEL;
  231. $commissionReward->save();
  232. $rewardAgent = new Agent($commissionReward->agent_id);
  233. LogModel::add($rewardAgent->user->id, 'reward', [
  234. 'type' => $this->event,
  235. 'reward' => $commissionReward,
  236. ]);
  237. }
  238. return true;
  239. }
  240. }