Order.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\common\model\plus\agent;
  3. use app\common\model\BaseModel;
  4. use app\common\enum\order\OrderTypeEnum;
  5. /**
  6. * 分销商订单模型
  7. */
  8. class Order extends BaseModel
  9. {
  10. protected $name = 'agent_order';
  11. protected $pk = 'id';
  12. /**
  13. * 订单所属用户
  14. * @return \think\model\relation\BelongsTo
  15. */
  16. public function user()
  17. {
  18. return $this->belongsTo('app\common\model\user\User');
  19. }
  20. /**
  21. * 一级分销商用户
  22. * @return \think\model\relation\BelongsTo
  23. */
  24. public function agentFirst()
  25. {
  26. return $this->belongsTo('app\common\model\user\User', 'first_user_id');
  27. }
  28. /**
  29. * 二级分销商用户
  30. * @return \think\model\relation\BelongsTo
  31. */
  32. public function agentSecond()
  33. {
  34. return $this->belongsTo('app\common\model\user\User', 'second_user_id');
  35. }
  36. /**
  37. * 三级分销商用户
  38. * @return \think\model\relation\BelongsTo
  39. */
  40. public function agentThird()
  41. {
  42. return $this->belongsTo('app\common\model\user\User', 'third_user_id');
  43. }
  44. /**
  45. * 订单类型
  46. * @param $value
  47. * @return array
  48. */
  49. public function getOrderTypeAttr($value)
  50. {
  51. $types = OrderTypeEnum::getTypeName();
  52. return ['text' => $types[$value], 'value' => $value];
  53. }
  54. /**
  55. * 订单详情
  56. */
  57. public static function getDetailByOrderId($orderId, $orderType)
  58. {
  59. return (new static())->where('order_id', '=', $orderId)
  60. ->where('order_type', '=', $orderType)
  61. ->find();
  62. }
  63. /**
  64. * 发放分销订单佣金
  65. * @param $order
  66. * @param int $orderType
  67. * @return bool
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public static function grantMoney($order, $orderType = OrderTypeEnum::MASTER)
  73. {
  74. // 订单是否已完成
  75. if ($order['order_status']['value'] != 30) {
  76. return false;
  77. }
  78. // 佣金结算天数
  79. $settleDays = Setting::getItem('settlement', $order['app_id'])['settle_days'];
  80. // 判断该订单是否满足结算时间 (订单完成时间 + 佣金结算时间) ≤ 当前时间
  81. $deadlineTime = $order['receipt_time'] + ((int)$settleDays * 86400);
  82. if ($settleDays > 0 && $deadlineTime > time()) {
  83. return false;
  84. }
  85. // 分销订单详情
  86. $model = self::getDetailByOrderId($order['order_id'], $orderType);
  87. if (!$model || $model['is_settled'] == 1) {
  88. return false;
  89. }
  90. // 重新计算分销佣金
  91. $capital = $model->getCapitalByOrder($order);
  92. // 发放一级分销商佣金
  93. $model['first_user_id'] > 0 && User::grantMoney($model['first_user_id'], $capital['first_money']);
  94. // 发放二级分销商佣金
  95. $model['second_user_id'] > 0 && User::grantMoney($model['second_user_id'], $capital['second_money']);
  96. // 发放三级分销商佣金
  97. $model['third_user_id'] > 0 && User::grantMoney($model['third_user_id'], $capital['third_money']);
  98. // 更新分销订单记录
  99. return $model->save([
  100. 'order_price' => $capital['orderPrice'],
  101. 'first_money' => $model['first_user_id'] > 0? $capital['first_money']:0,
  102. 'second_money' => $model['second_user_id'] > 0? $capital['second_money']:0,
  103. 'third_money' => $model['third_user_id'] > 0? $capital['third_money']:0,
  104. 'is_settled' => 1,
  105. 'settle_time' => time()
  106. ]);
  107. }
  108. /**
  109. * 计算订单分销佣金
  110. * @param $order
  111. * @return array
  112. */
  113. protected function getCapitalByOrder($order)
  114. {
  115. // 分销佣金设置
  116. $setting = Setting::getItem('commission', $order['app_id']);
  117. // 分销层级
  118. $level = Setting::getItem('basic', $order['app_id'])['level'];
  119. // 分销订单佣金数据
  120. $capital = [
  121. // 订单总金额(不含运费)
  122. 'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2),
  123. // 一级分销佣金
  124. 'first_money' => 0.00,
  125. // 二级分销佣金
  126. 'second_money' => 0.00,
  127. // 三级分销佣金
  128. 'third_money' => 0.00
  129. ];
  130. // 计算分销佣金
  131. foreach ($order['product'] as $product) {
  132. // 如果商品未开启分销
  133. if($product['is_agent'] == 0){
  134. continue;
  135. }
  136. // 判断商品存在售后退款则不计算佣金
  137. if ($this->checkProductRefund($product)) {
  138. continue;
  139. }
  140. // 商品实付款金额
  141. $productPrice = min($capital['orderPrice'], $product['total_pay_price']);
  142. // 计算商品实际佣金
  143. $productCapital = $this->calculateProductCapital($setting, $product, $productPrice);
  144. // 累积分销佣金
  145. $level >= 1 && $capital['first_money'] += $productCapital['first_money'];
  146. $level >= 2 && $capital['second_money'] += $productCapital['second_money'];
  147. $level == 3 && $capital['third_money'] += $productCapital['third_money'];
  148. }
  149. return $capital;
  150. }
  151. /**
  152. * 计算商品实际佣金
  153. * @param $setting
  154. * @param $product
  155. * @param $productPrice
  156. * @return float[]|int[]
  157. */
  158. private function calculateProductCapital($setting, $product, $productPrice)
  159. {
  160. // 全局分销
  161. if ($product['is_ind_agent'] == false) {
  162. // 全局分销比例
  163. return [
  164. 'first_money' => $productPrice * ($setting['first_money'] * 0.01),
  165. 'second_money' => $productPrice * ($setting['second_money'] * 0.01),
  166. 'third_money' => $productPrice * ($setting['third_money'] * 0.01)
  167. ];
  168. }
  169. // 商品单独分销
  170. if ($product['agent_money_type'] == 10) {
  171. // 分销佣金类型:百分比
  172. return [
  173. 'first_money' => $productPrice * ($product['first_money'] * 0.01),
  174. 'second_money' => $productPrice * ($product['second_money'] * 0.01),
  175. 'third_money' => $productPrice * ($product['third_money'] * 0.01)
  176. ];
  177. } else {
  178. return [
  179. 'first_money' => $product['total_num'] * $product['first_money'],
  180. 'second_money' => $product['total_num'] * $product['second_money'],
  181. 'third_money' => $product['total_num'] * $product['third_money']
  182. ];
  183. }
  184. }
  185. /**
  186. * 验证商品是否存在售后
  187. * @param $product
  188. * @return bool
  189. */
  190. private function checkProductRefund($product)
  191. {
  192. return !empty($product['refund'])
  193. && $product['refund']['type']['value'] != 20
  194. && $product['refund']['is_agree']['value'] != 20;
  195. }
  196. }