CommissionsDetail.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\console\model\user;
  13. use app\common\library\helper;
  14. use app\common\model\store\Setting;
  15. use app\common\model\user\CommissionsDetail as CommissionsDetailModel;
  16. use app\console\model\Order;
  17. use app\console\model\OrderGoods;
  18. use app\console\model\OrderRefund;
  19. use app\console\model\User as UserModel;
  20. /**
  21. * 待结算分佣明细模型
  22. * Class Store
  23. * @package app\store\model
  24. */
  25. class CommissionsDetail extends CommissionsDetailModel
  26. {
  27. /**
  28. * 获取用户分佣的order_goods_id
  29. * @param $userId
  30. * @param $from
  31. * @param $to
  32. * @param int $shopId
  33. * @param int $role
  34. * @param false $monthly
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public static function getCommOrderGoodsId($userId,$from,$to,$shopId=0,$role=99,$monthly=false){
  41. $orderIds = CommissionsDetail::getCommOrderIds39($userId,$shopId,$role,$from,$to,$monthly);
  42. if (!count($orderIds))return [];
  43. return OrderGoods::whereIn('order_id',$orderIds)->field('order_goods_id')
  44. ->select()->column('order_goods_id');
  45. }
  46. /**
  47. * 获取分佣的订单ID
  48. * @param $userId
  49. * @param $shopId
  50. * @param $role
  51. * @param $from
  52. * @param $to
  53. * @param false $monthly
  54. * @return array
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public static function getCommOrderIds39($userId,$shopId,$role,$from,$to,$monthly=false): array
  60. {
  61. $model = self::field( 'distinct order_id')
  62. ->where('user_id',$userId)
  63. ->where('shop_id',$shopId)
  64. ->where('role',$role)
  65. ->where('commission_level',1);//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。
  66. if($monthly == true){
  67. $model->where('clearing_status',1);
  68. }else{
  69. $model->where('clearing_status','<',2);
  70. }
  71. return $model->whereBetweenTime('order_create_time', $from, $to)
  72. ->select()->column('order_id');
  73. }
  74. /**
  75. * @param $userId
  76. * @param $from
  77. * @param $to
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public static function getRefundCommOrderId($userId,$from,$to){
  84. $model = self::field( 'distinct order_id')
  85. ->where('user_id',$userId)
  86. ->where('commission_level',1);//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。
  87. return $model->whereBetweenTime('order_create_time', $from, $to)//防止上月的待结算记录跑到下月结算,所有加上一天时间补偿
  88. ->select()->column('order_id');
  89. }
  90. /**
  91. * 超时未结算销售额36
  92. * @param $userId
  93. * @param $shopId
  94. * @param $role
  95. * @param $from
  96. * @param $to
  97. * @return int
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. */
  102. public static function sumOvertimeSign36($userId,$shopId,$role,$from,$to){
  103. return self::where('user_id',$userId)
  104. ->where('shop_id',$shopId)
  105. ->where('role',$role)
  106. ->where('clearing_status',0)
  107. ->where('commission_level',1)
  108. ->whereBetweenTime('order_create_time', $from, $to)
  109. ->sum('order_sale_volume');
  110. /* $orderIds = self::where('user_id',$userId)
  111. ->where('shop_id',$shopId)
  112. ->where('role',$role)
  113. ->where('clearing_status',0)
  114. ->where('commission_level',1)
  115. ->whereBetweenTime('order_create_time', $from, $to)
  116. ->sum('order_id');
  117. return OrderGoods::sumOrderGoodsVolume($orderIds);*/
  118. }
  119. /**
  120. * 获取一个用户的分佣角色和身份,并计算销售额
  121. * @param $user
  122. * @param $from
  123. * @param $to
  124. * @param false $monthly
  125. * @return array
  126. */
  127. public static function getFullBonusInfo36($user,$from,$to,$monthly= false){
  128. //多门店,多角色,N*N组合
  129. $es = CommissionsDetail::where('user_id',$user['user_id'])
  130. ->whereBetweenTime('order_create_time', $from, $to)
  131. ->field('shop_id,role')
  132. ->group('shop_id,role')
  133. ->select();
  134. $arrs = [];
  135. if (count($es)){
  136. foreach ($es as $e){
  137. $temp = self::userShopRoleArr($user['user_id'],$e['shop_id'],$e['role'],$from,$to,$monthly);
  138. if ($temp){
  139. $arrs[] = $temp;
  140. }
  141. }
  142. }
  143. return $arrs;
  144. }
  145. public static function userShopRoleArr($userId,$shopId,$role,$from,$to,$monthly){
  146. $orderIds = CommissionsDetail::getCommOrderIds39($userId,$shopId,$role,$from,$to,$monthly);
  147. $overtimeSign = 0;
  148. if (count($orderIds)){
  149. /* $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds)->field('order_goods_id')
  150. ->select()->column('order_goods_id');
  151. $saleVolume = OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to);*/
  152. $wheres[] = ['user_id','=',$userId];
  153. $wheres[] = ['shop_id','=',$shopId];
  154. $wheres[] = ['role','=',$role];
  155. $wheres[] = ['commission_level','=',1];
  156. $wheres[] = ['order_create_time','>=',$from];
  157. $wheres[] = ['order_create_time','<=',$to];
  158. if ($monthly == true){
  159. $wheres[] = ['clearing_status','=',1];
  160. }else{
  161. $wheres[] = ['clearing_status','<',2];
  162. }
  163. $saleVolume = CommissionsDetail::sumOrderSaleVolume($wheres);
  164. //加入分销员奖励金
  165. if ($role == UserModel::COMMISSION_USER){
  166. $bonus = Setting::getBonusRatioBySaleAmount($saleVolume);
  167. }else{
  168. $bonus = CommissionSteps::getShopRoleSteps($shopId,$role,$saleVolume);
  169. }
  170. $refundCount = OrderRefund::getUserRefundCount36($orderIds);
  171. $orderCount = count(array_unique($orderIds));//1.3.92添加成交订单数
  172. $refundVolume = OrderRefund::sumRefundPriceByOrder($orderIds);
  173. $sumOvertimeSign = CommissionsDetail::sumOvertimeSign36($userId,$shopId,$role,$from,$to);
  174. $bonusMoney = helper::bcadd($saleVolume*$bonus['bonus_ratio']/100,0,4);
  175. }else{
  176. return null;
  177. }
  178. return [
  179. 'shop_id' =>$shopId,
  180. 'role' =>$role,
  181. 'saleVolume' =>$saleVolume,
  182. 'refundCount' =>$refundCount,
  183. 'refundVolume' =>$refundVolume,
  184. 'countOvertimeSign' =>$overtimeSign,
  185. 'sumOvertimeSign' =>$sumOvertimeSign,
  186. 'bonusRate' =>$bonus['bonus_ratio'],
  187. 'bonusMoney' =>$bonusMoney,
  188. 'orderCount' =>$orderCount,
  189. 'bonusLadder' =>$bonus['bonus_ladder'],//1.3.92添加奖励金阶梯等级
  190. ];
  191. }
  192. }