CommissionService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\admin\service\order;
  3. use app\admin\service\member\MemberService;
  4. use app\model\Member;
  5. use app\model\MemberAccount;
  6. use app\model\MemberAccountList;
  7. use app\model\SysConfig;
  8. use support\Log;
  9. class CommissionService
  10. {
  11. /*
  12. {
  13. "config": {
  14. "name": "名称",
  15. "valid": 365, //有效天
  16. "protocol": 229, //服务协议分类ID
  17. "notice":["", ""] //注意事项数组
  18. },
  19. "goods": {
  20. "goods_id": "", //产品ID
  21. "goods_sku_id": 0, //产品SKUID
  22. "amount": "goods_sales_price" //核算金额字段
  23. },
  24. "commission": {
  25. "direct": 0.2, //直属提成
  26. "indirect": 0.1, //间属提成
  27. "relate-consum": 0.1, //消费关联提成 带客
  28. "invite-non-identity": 0.05, //邀请人非身份会员达成提成
  29. "agent-senior-join": 0.05, //中介加盟合伙人提成
  30. "agent-senior-strategy": 0.05, //中介战略合伙人提成
  31. "account":"CASH" //提成写入账户
  32. },
  33. "account": {
  34. "account":"CASH", //达成金额进入账户 不则为空
  35. "added":0.12 //达成金额增值 不则为0
  36. },
  37. "control": {
  38. "notify": "procMemberPartnerExec", //回调函数
  39. "if_installment": "N", //是否支持分期
  40. "if_commission_out": "Y" //提成是否可提现
  41. }
  42. }
  43. */
  44. /**
  45. * @Desc 处理提成
  46. * @Author Gorden
  47. * @Date 2024/9/28 16:09
  48. *
  49. * @param $config
  50. * @param $memberId
  51. * @param $orderId
  52. * @param $amount
  53. * @param $accountListCategory
  54. * @return void
  55. */
  56. public static function commission($config, $memberId, $orderId, $amount, $accountListCategory)
  57. {
  58. Log::info("开始处理提成", ['member_id' => $memberId, 'amount' => $amount]);
  59. try {
  60. $member = Member::where('member_id', $memberId)->first();
  61. $configParams1 = [];
  62. $configParams2 = [];
  63. if (!empty($member->join_invite_member_id)) {
  64. $member1 = Member::where('member_id', $member->join_invite_member_id)->first();
  65. if ($member1->member_is_partner == 'Y') {
  66. $config1 = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  67. } else if ($member1->member_is_referrer == 'Y') {
  68. $config1 = SysConfig::where('config_key', 'app-identity-referrer-config')->first();
  69. } else if ($member1->member_is_franchisee == 'Y'){
  70. $config1 = SysConfig::where('config_key','app-identity-franchisee-config')->first();
  71. }
  72. if (!empty($config1)) {
  73. $configParams1 = json_decode($config1->config_value_json, true);
  74. Log::info("一级提成参数", ['config' => $configParams1]);
  75. }
  76. if (!empty($member1->join_invite_member_id)) {
  77. $member2 = Member::where('member_id', $member1->join_invite_member_id)->first();
  78. if ($member2->member_is_partner == 'Y') {
  79. $config2 = SysConfig::where('config_key', 'app-identity-partner-config')->first();
  80. } else if ($member1->member_is_referrer == 'Y') {
  81. $config2 = SysConfig::where('config_key', 'app-identity-referrer-config')->first();
  82. } else if ($member1->member_is_franchisee == 'Y') {
  83. $config2 = SysConfig::where('config_key', 'app-identity-franchisee-config')->first();
  84. }
  85. if (!empty($config2)) {
  86. $configParams2 = json_decode($config2->config_value_json, true);
  87. Log::info("二级提成参数", ['config' => $configParams2]);
  88. }
  89. }
  90. }
  91. if (empty($configParams1) && empty($configParams2)) {
  92. Log::info("提成失败,提成参数异常", ['member_id' => $memberId, 'amount' => $amount]);
  93. }
  94. // 提成记录JSON
  95. $accountListJson = [
  96. 'line' => 'relate-consum',
  97. 'amount' => $amount,
  98. 'order_id' => $orderId,
  99. 'master_member_id' => $memberId
  100. ];
  101. // 直属提成
  102. if (!empty($member1) && ($member1->member_is_partner == 'Y' || $member1->member_is_referrer == 'Y' || $member1->member_is_franchisee == 'Y') && !empty($configParams1)) {
  103. $accountListJson['identity'] = MemberService::getIdentity($member1->member_is_partner, $member1->member_is_referrer,$member1->member_is_franchisee);
  104. $accountListJson['line'] = 'direct';
  105. $member1CommissionAmount = $configParams1['commission']['direct'] * $amount;
  106. self::commissionToMember($member1->member_id, $accountListJson, $member1CommissionAmount, $accountListCategory);
  107. Log::info("直属提成参数", ['member_id' => $member1->member_id, 'account_list_json' => $accountListJson, 'amount' => $member1CommissionAmount, 'category' => $accountListCategory]);
  108. }
  109. // 间属提成
  110. if (!empty($member2) && ($member2->member_is_partner == 'Y' || $member2->member_is_referrer == 'Y' || $member1->member_is_franchisee == 'Y') && !empty($configParams2)) {
  111. $accountListJson['identity'] = MemberService::getIdentity($member2->member_is_partner, $member2->member_is_referrer,$member2->member_is_franchisee);
  112. $accountListJson['line'] = 'indirect';
  113. $member2CommissionAmount = $configParams2['commission']['indirect'] * $amount;
  114. self::commissionToMember($member2->member_id, $accountListJson, $member2CommissionAmount, $accountListCategory);
  115. Log::info("间属提成参数", ['member_id' => $member2->member_id, 'account_list_json' => $accountListJson, 'amount' => $member2CommissionAmount, 'category' => $accountListCategory]);
  116. }
  117. } catch (\Exception $e) {
  118. Log::error("提成处理失败", ['msg' => $e->getMessage()]);
  119. _syslog("提成处理失败", '提成处理失败');
  120. }
  121. }
  122. /**
  123. * @Desc 提成到账户
  124. * @Author Gorden
  125. * @Date 2024/9/25 15:14
  126. *
  127. * @param $memberId
  128. * @param $accountListJson
  129. * @param $money
  130. * @return void
  131. */
  132. private static function commissionToMember($memberId, $accountListJson, $money, $accountListCategory)
  133. {
  134. $account = MemberAccount::where('join_account_member_id', $memberId)->where('member_account_classify', 'CASH')->first();
  135. $account->member_account_income = $account->member_account_income + $money;
  136. $account->member_account_surplus = $account->member_account_surplus + $money;
  137. $account->save();
  138. MemberAccountList::insert([
  139. 'join_list_member_account_nbr' => $memberId . '-CASH',
  140. 'join_member_account_list_member_id' => $memberId,
  141. 'member_account_list_status' => 'ACTIVED',
  142. 'member_account_list_attr' => 'IN',
  143. 'member_account_list_classify' => $accountListJson['identity'],
  144. 'member_account_list_category' => $accountListCategory,
  145. 'member_account_list_datetime' => date('Y-m-d H:i:s'),
  146. 'member_account_list_amount' => $money,
  147. 'member_account_list_json' => json_encode($accountListJson),
  148. 'member_account_list_addtimes' => time()
  149. ]);
  150. }
  151. }