CommissionEvent.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\event\order;
  3. use app\model\Member;
  4. use app\model\MemberAccount;
  5. use app\model\MemberAccountList;
  6. use app\model\Order;
  7. use app\model\PayDetail;
  8. use app\model\SysConfig;
  9. use support\Db;
  10. use support\exception\BusinessException;
  11. use support\Log;
  12. class CommissionEvent
  13. {
  14. //{
  15. //"config": {
  16. //"name": "名称",
  17. //"valid": 365, //有效天
  18. //"protocol": 229, //服务协议分类ID
  19. //"notice":["", ""] //注意事项数组
  20. //},
  21. //"goods": {
  22. // "goods_id": "", //产品ID
  23. // "goods_sku_id": 0, //产品SKUID
  24. // "amount": "goods_sales_price" //核算金额字段
  25. // },
  26. // "commission": {
  27. // "direct": 0.2, //直属提成
  28. // "indirect": 0.1, //间属提成
  29. // "relate-consum": 0.1, //消费关联提成 带客
  30. // "invite-non-identity": 0.05, //邀请人非身份会员达成提成
  31. // "agent-senior-join": 0.05, //中介加盟合伙人提成
  32. // "agent-senior-strategy": 0.05, //中介战略合伙人提成
  33. // "account":"CASH" //提成写入账户
  34. // },
  35. // "account": {
  36. // "account":"CASH", //达成金额进入账户 不则为空
  37. // "added":0.12 //达成金额增值 不则为0
  38. // },
  39. // "control": {
  40. // "notify": "procMemberPartnerExec", //回调函数
  41. // "if_installment": "N", //是否支持分期
  42. // "if_commission_out": "Y" //提成是否可提现
  43. // }
  44. //}
  45. public function order($params)
  46. {
  47. Log::info("计算消费提成", $params);
  48. $accountListCategory = $params['member_account_list_category'] ?? '消费';
  49. Db::beginTransaction();
  50. try {
  51. $order = Order::where('order_id', $params['orderId'])->where('order_status_payment', 'SUCCESS')->first();
  52. $actualPaymentAmount = PayDetail::whereJsonContains('join_pay_object_json->order_id', $params['orderId'])
  53. ->where('pay_status', 'SUCCESS')
  54. ->whereIn('pay_prepayid', ['WXPAY', 'ALIPAY', 'OFFLINE_WXPAY', 'OFFLINE_ALIPAY', 'MONEY'])
  55. ->sum('pay_amount');
  56. if ($actualPaymentAmount == 0) {
  57. throw new BusinessException("微信、支付宝、现金支付金额为0");
  58. }
  59. // 有传过来的
  60. if (!empty($params['join_invite_member_id'])) {
  61. $inviteMemberId = $params['join_invite_member_id'];
  62. } else {
  63. // 下单人
  64. $member = Member::where('member_id', $order->join_order_member_id)->first();
  65. if (empty($member->join_invite_member_id)) {
  66. throw new BusinessException("没有直属上级");
  67. }
  68. $inviteMemberId = $member->join_invite_member_id;
  69. }
  70. // 上级
  71. $memberUp = Member::where('member_id', $inviteMemberId)->first();
  72. if ($memberUp->member_is_partner == 'Y') {
  73. $identity = 'PARTNER';
  74. // 提成设置
  75. $config = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  76. } elseif ($memberUp->member_is_referrer == 'Y') {
  77. $identity = 'REFERRER';
  78. // 提成设置
  79. $config = SysConfig::where('config_key', 'app-identity-referrer-config')->first();
  80. } else {
  81. throw new BusinessException("直属上级非合伙人或者推荐官");
  82. }
  83. $configParams = json_decode($config->config_value_json, true);
  84. // 记录的JSON
  85. $accountListJson = [
  86. 'line' => 'relate-consum',
  87. 'amount' => $order->order_amount_pay,
  88. 'identity' => $identity,
  89. 'order_id' => $order->order_id,
  90. 'master_member_id' => $order->join_order_member_id
  91. ];
  92. Log::info("提成参数", $accountListJson);
  93. // 提成
  94. $this->commissionToMember($inviteMemberId, $accountListJson, round($configParams['commission']['relate-consum'] * $actualPaymentAmount, 2), $accountListCategory);
  95. Db::commit();
  96. _syslog("提成", "计算提成成功");
  97. } catch (BusinessException $e) {
  98. Db::rollBack();
  99. Log::error("计算消费提成异常", ['msg' => $e->getMessage()]);
  100. _syslog("提成", "计算提成失败:" . $e->getMessage());
  101. } catch (\Exception $e) {
  102. Db::rollBack();
  103. Log::error("计算消费提成异常", ['msg' => $e->getMessage()]);
  104. _syslog("提成", "计算提成失败");
  105. }
  106. }
  107. /**
  108. * @Desc
  109. * @Author Gorden
  110. * @Date 2024/9/25 15:14
  111. *
  112. * @param $memberId
  113. * @param $accountListJson
  114. * @param $money
  115. * @return void
  116. */
  117. private function commissionToMember($memberId, $accountListJson, $money, $accountListCategory)
  118. {
  119. try {
  120. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  121. $account->member_account_income = $account->member_account_income + $money;
  122. $account->member_account_surplus = $account->member_account_surplus + $money;
  123. $account->save();
  124. MemberAccountList::insert([
  125. 'join_list_member_account_nbr' => $memberId . '-CASH',
  126. 'join_member_account_list_member_id' => $memberId,
  127. 'member_account_list_status' => 'ACTIVED',
  128. 'member_account_list_attr' => 'IN',
  129. 'member_account_list_classify' => $accountListJson['identity'],
  130. 'member_account_list_category' => $accountListCategory,
  131. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  132. 'member_account_list_amount' => $money,
  133. 'member_account_list_json' => json_encode($accountListJson),
  134. 'member_account_list_addtimes' => time()
  135. ]);
  136. } catch (\Exception $e) {
  137. Log::error("提成入账失败", ['msg' => $e->getMessage()]);
  138. }
  139. }
  140. }