Order.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace addons\shopro\service\commission;
  3. use addons\shopro\service\commission\Config;
  4. use app\admin\model\shopro\commission\Order as OrderModel;
  5. use app\admin\model\shopro\commission\Reward as RewardModel;
  6. use app\admin\model\shopro\commission\Log as LogModel;
  7. /**
  8. * 分佣业务
  9. */
  10. class Order
  11. {
  12. public $config; // 分销设置
  13. public $item; // 订单商品
  14. public $buyer; // 购买人
  15. public $goods; // 分销商品
  16. public $amount = 0; // 订单商品核算价格
  17. public $commissionLevel = 0; // 分销层级
  18. public $selfBuy; // 是否自购
  19. // 分销状态
  20. const COMMISSION_CLOSE = 0; // 分销功能已关闭
  21. /**
  22. * 构造函数
  23. *
  24. * @param array $item 分销商品
  25. */
  26. public function __construct($item)
  27. {
  28. $this->buyer = new Agent($item['user_id']);
  29. $this->item = $item;
  30. $this->config = new Config();
  31. }
  32. /**
  33. * 检测并设置分销商品规则
  34. *
  35. * @return void
  36. */
  37. public function checkAndSetCommission()
  38. {
  39. // 未找到订单商品或购买人
  40. if (!$this->item || !$this->buyer->user) {
  41. return false;
  42. }
  43. // 获取商品分销佣金规则
  44. $this->goods = new Goods($this->item['goods_id'], $this->item['goods_sku_price_id']);
  45. // 商品有独立分销设置,覆盖默认系统配置
  46. if ($commissionConfig = $this->goods->getCommissionConfig()) {
  47. $this->config->setConfig($commissionConfig);
  48. }
  49. // 获取系统设置分销层级
  50. $this->commissionLevel = $this->config->getCommissionLevel();
  51. // 分销中心已关闭
  52. if (self::COMMISSION_CLOSE === $this->commissionLevel) {
  53. return false;
  54. }
  55. // 商品不参与分销
  56. if (!$this->goods->getCommissionRules()) {
  57. return false;
  58. }
  59. // 是否自购分销订单
  60. $this->selfBuy = $this->buyer->isAgentAvaliable() && $this->config->isSelfBuy();
  61. // 未找到上级分销商且不是自购
  62. if (!$this->buyer->getParentUserId() && !$this->selfBuy) {
  63. return false;
  64. }
  65. // 获取商品结算价格(四舍五入精确到分)
  66. $this->amount = $this->getGoodsCommissionAmount();
  67. // 没有分佣的必要了
  68. if ($this->amount <= 0) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. // 获取商品核算总金额
  74. public function getGoodsCommissionAmount()
  75. {
  76. $commissionType = $this->config->getRewardType();
  77. $amount = round(0, 2);
  78. switch ($commissionType) {
  79. case 'pay_price':
  80. $amount = $this->item['pay_fee'];
  81. break;
  82. case 'goods_price':
  83. $amount = round($this->item['goods_price'] * $this->item['goods_num'], 2);
  84. break;
  85. }
  86. return $amount;
  87. }
  88. /**
  89. * 创建分销订单
  90. */
  91. public function createCommissionOrder()
  92. {
  93. $agentId = $this->selfBuy ? $this->buyer->user->id : $this->buyer->getParentUserId();
  94. if (!$agentId) {
  95. return false;
  96. }
  97. $commissionOrder = OrderModel::where('order_item_id', $this->item['id'])->find();
  98. // 已添加过分销订单
  99. if ($commissionOrder) {
  100. return $commissionOrder;
  101. }
  102. $commissionOrderParams = [
  103. 'self_buy' => intval($this->selfBuy),
  104. 'order_id' => $this->item['order_id'],
  105. 'order_item_id' => $this->item['id'],
  106. 'buyer_id' => $this->buyer->user->id,
  107. 'goods_id' => $this->item['goods_id'],
  108. 'agent_id' => $agentId,
  109. 'amount' => $this->amount,
  110. 'reward_type' => $this->config->getRewardType(),
  111. 'commission_rules' => $this->goods->getCommissionRules(), // 记录当前设置的分佣规则,防止以后系统或者分销商设置有变导致更改历史规则
  112. 'reward_event' => $this->config->getRewardEvent(),
  113. 'commission_order_status' => $this->goods->commissionGoods->commission_order_status, // 是否计入分销业绩
  114. 'commission_reward_status' => RewardModel::COMMISSION_REWARD_STATUS_PENDING, // 佣金状态
  115. ];
  116. $commissionOrder = OrderModel::create($commissionOrderParams);
  117. // 添加分销商推广订单业绩
  118. $orderAgent = new Agent($commissionOrder->agent_id);
  119. if ($orderAgent->isAgentAvaliable() && $commissionOrder->commission_order_status) {
  120. if($this->selfBuy) {
  121. // 添加自购业绩
  122. $orderAgent->agent->setInc('child_order_money_0', $commissionOrder->amount);
  123. $orderAgent->agent->setInc('child_order_count_0', 1);
  124. }else {
  125. // 添加一级业绩
  126. $orderAgent->agent->setInc('child_order_money_1', $commissionOrder->amount);
  127. $orderAgent->agent->setInc('child_order_count_1', 1);
  128. }
  129. LogModel::add($commissionOrder['agent_id'], 'order', [
  130. 'type' => 'paid',
  131. 'order' => $commissionOrder,
  132. 'item' => $this->item,
  133. 'buyer' => $this->buyer->user
  134. ], $this->buyer->user);
  135. }
  136. return $commissionOrder;
  137. }
  138. /**
  139. * 执行分销计划,递归往上分佣
  140. *
  141. * @param object $commissionOrder 分销订单
  142. * @param object $currentAgent 当前待分佣的分销商 默认自购开始算
  143. * @param int $currentCommissionLevel 当前分佣层级 一级(自购)开始算
  144. */
  145. public function runCommissionPlan($commissionOrder, $currentAgent = null, $currentCommissionLevel = 1)
  146. {
  147. // 超出分佣层级
  148. if ($this->commissionLevel < $currentCommissionLevel) {
  149. return true;
  150. }
  151. // 当前层级为1且分销订单是自购订单 则当前分销商为购买人自己
  152. if ($currentCommissionLevel === 1) {
  153. $currentAgent = new Agent($commissionOrder->agent_id);
  154. }
  155. if ($currentAgent && !empty($currentAgent->user)) {
  156. // 防止重复添加佣金
  157. $commissionReward = RewardModel::where([
  158. 'commission_order_id' => $commissionOrder->id,
  159. 'agent_id' => $currentAgent->user->id,
  160. 'commission_level' => $currentCommissionLevel, // 分佣层级
  161. ])->find();
  162. if (!$commissionReward) {
  163. $currentAgentLevel = $currentAgent->getAgentLevel();
  164. $currentCommissionLevelRule = $this->goods->getCommissionLevelRule($currentAgentLevel, $currentCommissionLevel);
  165. if ($currentCommissionLevelRule) {
  166. $commission = $this->goods->caculateGoodsCommission($currentCommissionLevelRule, $this->amount, $this->item['goods_num']);
  167. if ($commission > 0) {
  168. $commissionRewardParams = [
  169. 'order_id' => $commissionOrder->order_id,
  170. 'order_item_id' => $commissionOrder->order_item_id,
  171. 'buyer_id' => $commissionOrder->buyer_id, // 购买人
  172. 'commission_order_id' => $commissionOrder->id, // 分销订单ID
  173. 'agent_id' => $currentAgent->user->id, // 待分佣分销商ID
  174. 'type' => 'commission', // 奖金类型
  175. 'commission' => $commission, // 佣金
  176. 'status' => 0, // 佣金状态
  177. 'original_commission' => $commission, // 原始佣金
  178. 'commission_level' => $currentCommissionLevel, // 分佣层级
  179. 'commission_rules' => $currentCommissionLevelRule, // 分佣层级
  180. 'agent_level' => $currentAgentLevel // 分佣时分销商等级
  181. ];
  182. $commissionReward = RewardModel::create($commissionRewardParams);
  183. LogModel::add($commissionReward['agent_id'], 'reward', [
  184. 'type' => 'paid',
  185. 'reward' => $commissionReward,
  186. ], $this->buyer->user);
  187. }
  188. }
  189. }
  190. // 递归执行下一次
  191. $currentCommissionLevel++;
  192. // 超出分销层级
  193. if ($this->commissionLevel < $currentCommissionLevel) {
  194. return true;
  195. }
  196. $parentUserId = $currentAgent->getParentUserId();
  197. // 执行下一级分销任务
  198. if ($parentUserId) {
  199. $parentAgent = new Agent($parentUserId);
  200. $this->runCommissionPlan($commissionOrder, $parentAgent, $currentCommissionLevel);
  201. } else {
  202. return true;
  203. }
  204. }
  205. }
  206. }