CommissionEvent.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. } elseif ($memberUp->member_is_franchisee == 'Y') {
  81. $identity = 'FRANCHISEE';
  82. // 提成设置
  83. $config = SysConfig::where('config_key', 'app-identity-franchisee-config')->first();
  84. } else {
  85. throw new BusinessException("直属上级非会员合伙人、加盟合伙人和推荐官");
  86. }
  87. $configParams = json_decode($config->config_value_json, true);
  88. // 记录的JSON
  89. $accountListJson = [
  90. 'line' => 'relate-consum',
  91. 'amount' => $order->order_amount_pay,
  92. 'identity' => $identity,
  93. 'order_id' => $order->order_id,
  94. 'master_member_id' => $order->join_order_member_id
  95. ];
  96. Log::info("提成参数", $accountListJson);
  97. // 提成
  98. $this->commissionToMember($inviteMemberId, $accountListJson, round($configParams['commission']['relate-consum'] * $actualPaymentAmount, 2), $accountListCategory);
  99. Db::commit();
  100. _syslog("提成", "计算提成成功");
  101. } catch (BusinessException $e) {
  102. Db::rollBack();
  103. Log::error("计算消费提成异常", ['msg' => $e->getMessage()]);
  104. _syslog("提成", "计算提成失败:" . $e->getMessage());
  105. } catch (\Exception $e) {
  106. Db::rollBack();
  107. Log::error("计算消费提成异常", ['msg' => $e->getMessage()]);
  108. _syslog("提成", "计算提成失败");
  109. }
  110. }
  111. /**
  112. * @Desc
  113. * @Author Gorden
  114. * @Date 2024/9/25 15:14
  115. *
  116. * @param $memberId
  117. * @param $accountListJson
  118. * @param $money
  119. * @return void
  120. */
  121. private function commissionToMember($memberId, $accountListJson, $money, $accountListCategory)
  122. {
  123. try {
  124. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  125. $account->member_account_income = $account->member_account_income + $money;
  126. $account->member_account_surplus = $account->member_account_surplus + $money;
  127. $account->save();
  128. MemberAccountList::insert([
  129. 'join_list_member_account_nbr' => $memberId . '-CASH',
  130. 'join_member_account_list_member_id' => $memberId,
  131. 'member_account_list_status' => 'ACTIVED',
  132. 'member_account_list_attr' => 'IN',
  133. 'member_account_list_classify' => $accountListJson['identity'],
  134. 'member_account_list_category' => $accountListCategory,
  135. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  136. 'member_account_list_amount' => $money,
  137. 'member_account_list_json' => json_encode($accountListJson),
  138. 'member_account_list_addtimes' => time()
  139. ]);
  140. } catch (\Exception $e) {
  141. Log::error("提成入账失败", ['msg' => $e->getMessage()]);
  142. }
  143. }
  144. }