// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\console\model\user; use app\common\library\helper; use app\common\model\store\Setting; use app\common\model\user\CommissionsDetail as CommissionsDetailModel; use app\console\model\Order; use app\console\model\OrderGoods; use app\console\model\OrderRefund; use app\console\model\User as UserModel; /** * 待结算分佣明细模型 * Class Store * @package app\store\model */ class CommissionsDetail extends CommissionsDetailModel { /** * 获取用户分佣的order_goods_id * @param $userId * @param $from * @param $to * @param int $shopId * @param int $role * @param false $monthly * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getCommOrderGoodsId($userId,$from,$to,$shopId=0,$role=99,$monthly=false){ $orderIds = CommissionsDetail::getCommOrderIds39($userId,$shopId,$role,$from,$to,$monthly); if (!count($orderIds))return []; return OrderGoods::whereIn('order_id',$orderIds)->field('order_goods_id') ->select()->column('order_goods_id'); } /** * 获取分佣的订单ID * @param $userId * @param $shopId * @param $role * @param $from * @param $to * @param false $monthly * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getCommOrderIds39($userId,$shopId,$role,$from,$to,$monthly=false): array { $model = self::field( 'distinct order_id') ->where('user_id',$userId) ->where('shop_id',$shopId) ->where('role',$role) ->where('commission_level',1);//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。 if($monthly == true){ $model->where('clearing_status',1); }else{ $model->where('clearing_status','<',2); } return $model->whereBetweenTime('order_create_time', $from, $to) ->select()->column('order_id'); } /** * @param $userId * @param $from * @param $to * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getRefundCommOrderId($userId,$from,$to){ $model = self::field( 'distinct order_id') ->where('user_id',$userId) ->where('commission_level',1);//6.推荐官直接推广的订单计入奖励金,间接推广的订单不计入。 return $model->whereBetweenTime('order_create_time', $from, $to)//防止上月的待结算记录跑到下月结算,所有加上一天时间补偿 ->select()->column('order_id'); } /** * 超时未结算销售额36 * @param $userId * @param $shopId * @param $role * @param $from * @param $to * @return int * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function sumOvertimeSign36($userId,$shopId,$role,$from,$to){ return self::where('user_id',$userId) ->where('shop_id',$shopId) ->where('role',$role) ->where('clearing_status',0) ->where('commission_level',1) ->whereBetweenTime('order_create_time', $from, $to) ->sum('order_sale_volume'); /* $orderIds = self::where('user_id',$userId) ->where('shop_id',$shopId) ->where('role',$role) ->where('clearing_status',0) ->where('commission_level',1) ->whereBetweenTime('order_create_time', $from, $to) ->sum('order_id'); return OrderGoods::sumOrderGoodsVolume($orderIds);*/ } /** * 获取一个用户的分佣角色和身份,并计算销售额 * @param $user * @param $from * @param $to * @param false $monthly * @return array */ public static function getFullBonusInfo36($user,$from,$to,$monthly= false){ //多门店,多角色,N*N组合 $es = CommissionsDetail::where('user_id',$user['user_id']) ->whereBetweenTime('order_create_time', $from, $to) ->field('shop_id,role') ->group('shop_id,role') ->select(); $arrs = []; if (count($es)){ foreach ($es as $e){ $temp = self::userShopRoleArr($user['user_id'],$e['shop_id'],$e['role'],$from,$to,$monthly); if ($temp){ $arrs[] = $temp; } } } return $arrs; } public static function userShopRoleArr($userId,$shopId,$role,$from,$to,$monthly){ $orderIds = CommissionsDetail::getCommOrderIds39($userId,$shopId,$role,$from,$to,$monthly); $overtimeSign = 0; if (count($orderIds)){ /* $orderGoodsIds = OrderGoods::whereIn('order_id',$orderIds)->field('order_goods_id') ->select()->column('order_goods_id'); $saleVolume = OrderGoods::getUserSellerVolume($orderGoodsIds,$from,$to);*/ $wheres[] = ['user_id','=',$userId]; $wheres[] = ['shop_id','=',$shopId]; $wheres[] = ['role','=',$role]; $wheres[] = ['commission_level','=',1]; $wheres[] = ['order_create_time','>=',$from]; $wheres[] = ['order_create_time','<=',$to]; if ($monthly == true){ $wheres[] = ['clearing_status','=',1]; }else{ $wheres[] = ['clearing_status','<',2]; } $saleVolume = CommissionsDetail::sumOrderSaleVolume($wheres); //加入分销员奖励金 if ($role == UserModel::COMMISSION_USER){ $bonus = Setting::getBonusRatioBySaleAmount($saleVolume); }else{ $bonus = CommissionSteps::getShopRoleSteps($shopId,$role,$saleVolume); } $refundCount = OrderRefund::getUserRefundCount36($orderIds); $orderCount = count(array_unique($orderIds));//1.3.92添加成交订单数 $refundVolume = OrderRefund::sumRefundPriceByOrder($orderIds); $sumOvertimeSign = CommissionsDetail::sumOvertimeSign36($userId,$shopId,$role,$from,$to); $bonusMoney = helper::bcadd($saleVolume*$bonus['bonus_ratio']/100,0,4); }else{ return null; } return [ 'shop_id' =>$shopId, 'role' =>$role, 'saleVolume' =>$saleVolume, 'refundCount' =>$refundCount, 'refundVolume' =>$refundVolume, 'countOvertimeSign' =>$overtimeSign, 'sumOvertimeSign' =>$sumOvertimeSign, 'bonusRate' =>$bonus['bonus_ratio'], 'bonusMoney' =>$bonusMoney, 'orderCount' =>$orderCount, 'bonusLadder' =>$bonus['bonus_ladder'],//1.3.92添加奖励金阶梯等级 ]; } }