all(); $page_size=$input['page_size']; $page_index=$input['page_index']; $num = ($page_index - 1) * $page_size; $search_name=$input['search_name']; $where=[]; $where['type']=$input['type']; $count=Rewards::with('recom:id,realname','agent:id,realname') ->where($where) ->count(); if($count==0){ $this->error('400001','没有数据'); } $list=Rewards::with('recom:id,realname','agent:id,realname') ->where($where) ->orderBy('id','desc') ->skip($num)->take($page_size) ->get(); if(empty($list)){ return $this->error('400002','没有获取到数据'); } return $this->success_list($list,'success',$count); } //代理业绩奖励结算 public function uploadAgentSalesResult(Request $request){ $input=$request->all(); $userId=$input['userids']; $starttime=date("Y-m-01 00:00:00",strtotime($input['time'])); $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months")); $time = [$starttime, $endtime]; $sales_area_reward_str=System::where('keys','sales_area_reward')->value('values'); $sales_area_reward_arr=explode('/',$sales_area_reward_str); foreach($sales_area_reward_arr as $key =>$val){ $sales_area_reward[$key]=explode('*',$val); } $result = array(); foreach($sales_area_reward as $k=>$v){ $result[$k] = $v[0]; } $minValue=min($result); DB::beginTransaction(); try { foreach($userId as $key=>$val){ $rewards=Rewards::where('recom_id',$val)->where('type',2)->where('month',$input['time'])->first(); if($rewards){ DB::rollBack(); return $this->error('400111','用户id=>'.$val.'当月已结算'); } $list=DB::table('users') ->select('users.id','users.nickname','users.level','warea.name as warea','uu.nickname as agent',DB::raw('SUM(order_goods.totalprice) as account')) ->leftJoin('order','order.user_id','=','users.id') ->leftJoin('order_goods','order.id','=','order_goods.order_id') ->leftJoin('warea','warea.id','=','users.warea_id') ->leftJoin('users as uu','uu.id','=','users.agent_id') ->where('order.status',2) ->where('users.id',$val) ->whereBetween('order.created_at',$time) ->get()->toArray(); if($list){ $list[0]->rewards=0; foreach($sales_area_reward as $k=>$v){ if($list[0]->account>=$v[0] && $list[0]->account<$v[1]){ $list[0]->rewards=round($list[0]->account*$v[2]/100,2); } } if($list[0]->rewards>0){ $data['agent_id']=1; $data['recom_id']=$list[0]->id; $data['type']=2; $data['bonus']=$list[0]->rewards; $data['spec']='1'; $data['totalprice']=$list[0]->account; $data['month']=$input['time']; Rewards::create($data); }else{ DB::rollBack(); return $this->error('400111','用户'.$list[0]->nickname.'结算失败'); } }else{ DB::rollBack(); return $this->error('400111','用户结算失败'); } } DB::commit(); Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')结算代理业绩奖励,代理id:'.json_encode($input['userids'])); return $this->success([]); }catch(Exception $e){ DB::rollBack(); return $this->error(); } } //门店业绩奖励结算钱 public function uploadAgentSalesResultMoney(Request $request){ $input=$request->all(); $storeId=$input['storeids']; $starttime=date("Y-m-01 00:00:00",strtotime($input['time'])); $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months")); $time = [$starttime, $endtime]; $type=$input['type']; if($type==2){ $rate=System::where('keys','store_reward_money_enjoy')->value('values'); }elseif($type==3){ $rate=System::where('keys','store_reward_money_experience')->value('values'); } $rates=explode('/',$rate); DB::beginTransaction(); try { foreach($storeId as $key=>$val){ $list=DB::table('store')->where('id',$val)->get(); $list[0]->account=0; $rewards=Rewards::where('recom_id',$list[0]->man_id)->where('type',3)->where('month',$input['time'])->first(); if($rewards){ DB::rollBack(); return $this->error('400111','门店'.$list[0]->name.'当月已结算'); } $info=DB::table('store') ->select('goods.type','goods.name', DB::raw('SUM(order_goods.num) as total')) ->leftJoin('users', function ($join) { $join->on('users.store_id', '=', 'store.id'); }) ->leftJoin('order', 'order.user_id', '=', 'users.id') ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id') ->Join('goods', 'goods.id', '=', 'order_goods.goods_id') ->where('store.id',$val) ->where('order.status', 2) ->whereBetween('order.created_at',$time) ->groupBy('goods.type') ->get(); if(!$info){ DB::rollBack(); return $this->error('400111','门店奖励结算失败'); } foreach($info as $k =>$v){ if($v->type==1){ $info[$k]->price=$rates[0]; $info[$k]->totalprice=$v->total*$rates[0]; $list[0]->account+=$v->total*$rates[0]; } if($v->type==2){ $info[$k]->price=$rates[1]; $info[$k]->totalprice=$v->total*$rates[1]; $list[0]->account+=$v->total*$rates[1]; } if($v->type==3){ $info[$k]->price=$rates[2]; $info[$k]->totalprice=$v->total*$rates[2]; $list[0]->account+=$v->total*$rates[2]; } } $list[0]->info=$info; if(!$list[0]->account){ DB::rollBack(); return $this->error('400111','门店'.$list[0]->name.'奖励结算失败'); } $data['agent_id']=1; $data['recom_id']=$list[0]->man_id; $data['store_id']=$list[0]->id; $data['type']=3; $data['bonus']=$list[0]->account; $data['spec']='1'; $data['totalprice']=''; $data['month']=$input['time']; Rewards::create($data); } DB::commit(); Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')结算门店业绩奖励,代理id:'.json_encode($input['storeids'])); return $this->success([]); }catch(Exception $e){ DB::rollBack(); return $this->error(); } } //门店业绩奖励 物 public function uploadAgentSalesResultGoods(Request $request){ $input=$request->all(); $storeId=$input['storeids']; $starttime=date("Y-m-01 00:00:00",strtotime($input['time'])); $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months")); $time = [$starttime, $endtime]; // $goodsInfo=$input['goodsinfo']; $type=$input['type']; if($type==1){ $rate=System::where('keys','store_reward_goods_job')->value('values'); }elseif($type==2){ $rate=System::where('keys','store_reward_goods_enjoy')->value('values'); } DB::beginTransaction(); try { foreach($storeId as $key=>$val){ $list=DB::table('store') ->select('store.name','store.man_id','store.id', DB::raw('SUM(order_goods.totalprice) as account')) ->leftJoin('users', function ($join) { $join->on('users.store_id', '=', 'store.id'); }) ->leftJoin('order', 'order.user_id', '=', 'users.id') ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id') ->where('store.id',$val) ->where('order.status', 2) ->whereBetween('order.created_at',$time) ->groupBy('store.id') ->get(); if(!$list){ DB::rollBack(); return $this->error('400111','门店奖励结算失败'); } $rewards=Rewards::where('recom_id',$list[0]->man_id)->where('type',3)->where('month',$input['time'])->first(); if($rewards){ DB::rollBack(); return $this->error('400111','门店'.$list[0]->name.'当月已结算'); } foreach($list as $key =>$val){ $list[$key]->rewards=round($val->account*$rate/100,2); } if(!$list[0]->rewards){ DB::rollBack(); return $this->error('400111','门店'.$list[0]->name.'奖励结算失败'); } $data['agent_id']=1; $data['recom_id']=$list[0]->man_id; $data['store_id']=$list[0]->id; $data['type']=3; $data['bonus']=''; $data['spec']='2'; $data['totalprice']=$list[0]->account; $data['goods_id']=$input['goods_id']; $data['goods_price']=$input['goods_price']; $data['goods_num']=$input['goods_num']; $data['month']=$input['time']; $row=Rewards::create($data); // foreach($goodsInfo as $k=>$v){ // $dd['reward_id']=$row; // $dd['goods_id']=$v['id']; // $dd['sku_id']=$v['sku_id']; // $dd['price']=$v['price']; // $dd['num']=$v['num']; // $dd['totalprice']=$v['price']*$v['num']; // OrderGoods::create($dd); // } } DB::commit(); Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')结算门店业绩奖励,代理id:'.json_encode($input['storeids'])); return $this->success([]); }catch(Exception $e){ DB::rollBack(); return $this->error(); } } public function getRewardGoodsList(){ $list = Goods::where('status',1)->select('id','name','type','tjprice','total')->get(); if(!$list){ return $this->error('400133','没有可用商品信息'); } foreach($list as $key=>$val){ $sku=GoodsSku::where('goods_id',$val->id)->where('is_show',1)->select('id','sku_path')->get(); foreach($sku as $k=>$v){ $sku[$k]->info=$this->getSku($v->id); } $list[$key]->sku=$sku; } return $this->success($list); } //获取商品规格数据 public function getSku($id){ $path=GoodsSku::withTrashed()->find($id)->sku_path; $arr=explode('/',$path); $goodsSku=[]; foreach($arr as $k=>$v){ $upSpec=GoodsSpec::withTrashed()->where('id',$v)->first(); $upSpec['uptitle']=GoodsSpec::where('id',$upSpec->pid)->value('title'); $goodsSku[$upSpec['uptitle']]=$upSpec->title; } return $goodsSku; } }