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; } //获取代理与体验店 public function getAgentExperience(){ $gg=Ordertest::query()->whereNotNUll('store_id')->groupBy('user_id')->get(); foreach($gg as $key=>$val){ ExpStoreAgent::create([ 'user_id'=>$val->user_id, 'store_id'=>$val->store_id, ]); } } //计算店铺区间销量 public function getStoreSaleNum(Request $request){ $input=$request->all(); $store_id=$input['store_id']; $month=$input['month']; if($month){ $start_time=Carbon::parse($month)->startOfMonth(); $end_time= Carbon::parse($month)->endOfMonth(); }else{ $start_time=$input['start_time']; $end_time=$input['end_time']; $month= Carbon::parse($start_time)->format('Y-m'); $month2= Carbon::parse($end_time)->format('Y-m'); if($month != $month2){ return $this->error('450001','所选时间段必须在同一个月内'); } } $sub=SubsidiesInfo::where('month',$month)->where('store_id',$store_id)->first(); if($sub){ return $this->error('450001','当月已完成结算'); } $store=Store::where('id',$store_id)->select('id','cost','amount','name','type')->first(); if(empty($store)){ return $this->error('450001','店铺不存在'); } if(empty($store->cost)){ return $this->error('450001','未设置店铺装修花费'); } $type=$input['type']; if($type==3){//优享店 $rate=System::where('keys','store_reward_money_enjoy')->value('values'); $user_id=StoreUser::where('store_id',$store_id)->value('user_id'); $order_ids=Ordertest::query()->where('user_id',$user_id)->where('is_deleted','=',0) ->whereBetween('created_at',[$start_time,$end_time]) ->pluck('id'); }elseif($type==2){//体验店 $rate=System::where('keys','store_reward_money_experience')->value('values'); $order_ids=Ordertest::query()->where('store_id',$store_id)->where('is_deleted','=',0) ->whereBetween('created_at',[$start_time,$end_time]) ->pluck('id'); } $orders=Orderdetail::whereIn('order_id',$order_ids)->groupBy('goods_name') ->select('goods_name',DB::raw('sum(num) as count'),DB::raw('sum(money) as amount'))->get(); $rate=json_decode($rate,true); foreach($orders as $key=>$val){ $n=0; foreach($rate as $k=>$v){ if($val->goods_name == $v['name'] || substr_count($val->goods_name,$v['name'])>=1){ $orders[$key]->rate = $v['num']; $orders[$key]->unit = $v['unit']; $n++; } } if($n==0){ return $this->error('450001',$val->goods_name.'没有设置补助标准'); } } $user=StoreUser::with('user:id,nickname') ->select('id','store_id','user_id','user_mobile','equity_rate') ->where('store_id',$store_id)->get(); $data['user']=$user; $data['rate']=$rate; $data['order']=$orders; $data['store']=$store; return $this->success($data); } //结算店铺补助 public function settleStoreSubsidies(Request $request){ $month=$request->input('month'); if($month){ $start_time= Carbon::parse($month)->startOfMonth(); $end_time= Carbon::parse($month)->endOfMonth(); $complete_month=1; }else{ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $month= Carbon::parse($start_time)->format('Y-m'); $month2= Carbon::parse($end_time)->format('Y-m'); if($month != $month2){ return $this->error('450001','所选时间段必须在同一个月内'); } if(Carbon::parse($month)->startOfMonth()==$start_time && Carbon::parse($month)->endOfMonth()==$end_time){ $complete_month=1; }else{ $complete_month=0; } } $store_id=$request->input('store_id'); $sub=SubsidiesInfo::where('month',$month)->where('store_id',$store_id)->first(); if($sub){ return $this->error('450001','当月已完成结算'); } $is_break=$request->input('is_break'); $user_info=$request->input('user_info'); $sale_info=$request->input('sale_info'); $rate_info=$request->input('rate_info'); $sale_amount=$request->input('sale_amount'); $subsidies_amount=$request->input('subsidies_amount'); $send_type=$request->input('send_type'); $data=[ 'month'=>$month, 'start_time'=>$start_time, 'end_time'=>$end_time, 'complete_month'=>$complete_month, 'store_id'=>$store_id, 'is_break'=>$is_break, 'user_info'=>json_encode($user_info,JSON_UNESCAPED_UNICODE), 'sale_info'=>json_encode($sale_info,JSON_UNESCAPED_UNICODE), 'rate_info'=>$rate_info, 'sale_amount'=>$sale_amount, 'subsidies_amount'=>$subsidies_amount, 'send_time'=>Carbon::now(), 'send_type'=>$send_type ]; try{ DB::transaction(function()use($data,$store_id){ SubsidiesInfo::query()->create($data); $store=Store::query()->where('id',$store_id)->lockForUpdate()->first(); $store->amount+=$data['subsidies_amount']; $store->save(); },5); return $this->success([]); }catch(Exception $e){ return $this->error('450001',$e->getMessage()); } } public function getSubsidiesInfo(Request $request){ $store_id=$request->input('store_id'); $page_index=$request->input('page_index'); $page_size=$request->input('page_size'); $num=$page_size*($page_index-1); $count=SubsidiesInfo::where('store_id',$store_id)->count(); $list=SubsidiesInfo::with('store:id,name,type,cost,amount') ->where('store_id',$store_id) ->orderBy('month','desc')->skip($num)->take($page_size)->get(); return $this->success_list($list,'',$count); } //取消结算 public function cancelSubsidies(Request $request){ $id=$request->input('id'); $sub=SubsidiesInfo::query()->where('id',$id)->first(); $store=Store::query()->where('id',$sub->store_id)->lockForUpdate()->first(); $store->amount-=$sub->subsidies_amount; try{ DB::transaction(function()use($sub,$store){ $sub->delete(); $store->save(); }); return $this->success([]); }catch(Exception $e){ return $this->error('450001',$e->getMessage()); } } }