_/___.' >'"". * | | : `- \`.;`\ _ /`;.`/ - ` : | | * \ \ `-. \_ __\ /__ _/ .-` / / * ======`-.____`-.___\_____/___.-`____.-'====== * `=---=' * ............................................. * 佛曰:bug泛滥,我已瘫痪! */ public function GetCrown(Request $request){ $user=User::where('id',$request->input('id'))->with(['agent:id,nickname,agent_id,level,warea_id'])->with(['warea'])->select('id','agent_id','level','nickname','warea_id')->first(); $data=[]; if ($user->level==3){ $data['nickname']=$user->nickname; $data['id']=$user->id; $data['warea_name']=$user->warea->name; }else{ if ($user->agent->level==3){ $data['nickname']=$user->agent->nickname; $data['id']=$user->agent->id; $data['warea_name']=Warea::where('id',$user->agent->warea_id)->value('name'); }else{ $user=User::where('id',$user->agent->agent_id)->with(['warea'])->select('id','agent_id','level','nickname','warea_id')->first(); $data['nickname']=$user->nickname; $data['id']=$user->id; $data['warea_name']=Warea::where('id',$user->warea_id)->value('name'); } } return $this->success($data); } /**客户申请表**/ public function GetApplyList(Request $request){ $page_index=$request->input('page_index',1); $page_size=$request->input('size',15); $num=($page_index-1)*$page_size; $search_name=$request->input('search_name'); $customer_name=$request->input('customer_name'); $warea_name=$request->input('warea_name'); $apply_status=$request->input('apply_status'); $audit_status=$request->input('audit_status'); $is_finish=$request->input('is_finish'); $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $apply_number=$request->input('apply_number')??null; $re=ApplyCustomer::query(); if (Auth::user()->role_id==23){ $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name'); $re->where('warea_name',$warea_names); } if (Auth::user()->role_id==29){ $re->whereIn('apply_status',[2,1]); $re->where('is_owe',$request->input('is_owe')??0); }else{ if ($request->has('is_owe')){ if (is_numeric($request->input('is_owe'))){ $re->where('is_owe',$request->input('is_owe')); } } if (is_numeric($apply_status)){ $re->where('apply_status',$apply_status); } if ($start_time){ $re->whereBetween('created_at',[$start_time,$end_time]); } } if (is_numeric($is_finish)){ $re->where('is_finish',$is_finish); } if ($apply_number){ $re->where('apply_number',$apply_number); } if (is_numeric($audit_status)){ $re->where('audit_status',$audit_status); } if ($warea_name){ $re->where('warea_name',$warea_name); } if ($search_name){ $re->where('apply_user_nickname','like','%'.$search_name.'%'); } if ($customer_name){ $re->where('customer_name','like','%'.$customer_name.'%')->orWhere('customer_phone','like','%'.$customer_name.'%'); } $count=$re->count(); $data=$re->with(['applydetail'])->orderByDesc('id')->take($page_size)->skip($num)->get(); return $this->success_list($data,'',$count); } /*客户申请**/ public function Apply(Request $request,ApplyCustomer $applyCustomer,ApplyCustomerDetail $applyCustomerDetail){ $applyUserID=$request->input('apply_user_id'); $applyUserName=$request->input('apply_user_nickname'); $applyCrownID=$request->input('apply_crown_id'); $applyCrownName=$request->input('apply_crown_nickname'); $warea_name=$request->input('warea_name'); $customer_name=$request->input('customer_name'); $customer_phone=$request->input('customer_phone'); $province=$request->input('province'); $city=$request->input('city'); $area=$request->input('area'); $address=$request->input('address'); $total=$request->input('total'); $remark=$request->input('remark'); $wear_time=$request->input('wear_time');; $is_agent=$request->input('is_agent');; $problemList=$request->input('problemList'); $sendback_info=''; $apply_number=$this->CreateApplyNumber(); $op_name=Auth::user()->name; $applyCustomer->apply_user_id=$applyUserID; $applyCustomer->apply_user_nickname=$applyUserName; $applyCustomer->apply_crown_id=$applyCrownID; $applyCustomer->apply_crown_nickname=$applyCrownName; $applyCustomer->warea_name=$warea_name; $applyCustomer->customer_name=$customer_name; $applyCustomer->customer_phone=$customer_phone; $applyCustomer->province=$province; $applyCustomer->city=$city; $applyCustomer->area=$area; $applyCustomer->address=$address; $applyCustomer->total=$total; $applyCustomer->is_agent=$is_agent; $applyCustomer->wear_time=$wear_time; $applyCustomer->remark=$remark; $applyCustomer->apply_number=$apply_number; $applyCustomer->op_name=$op_name; DB::beginTransaction(); try { if (!$applyCustomer->save()){ throw new \Exception('1'); } foreach ($problemList as $k=>$v){ $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first(); if (!$re){ return $this->error(); } $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first(); if ($res->storage<$v['total']){ throw new \Exception('商品:'.$res->good_name.'的'.$res->size.'库存不足'); } $res->storage-=$v['total']; if (!$res->save()){ throw new \Exception('商品保存出错'); } $logs=new StoreHouseLog(); $log=Auth::user()->name.'调换了商品:'.$res->good_name.',尺码:'.$res->size.'。数量为:'.$v['total']; $logs->log=$log; $logs->customer_id=$applyCustomer->id; if (!$logs->save()){ throw new \Exception('1'); } $applyCustomerDetail=new ApplyCustomerDetail(); $applyCustomerDetail->customer_id=$applyCustomer->id; $applyCustomerDetail->problem_info=$v['problem']; $applyCustomerDetail->good_name=$v['good_name']; $applyCustomerDetail->good_size=$v['good_size']; $applyCustomerDetail->total=$v['total']; $applyCustomerDetail->real_change_size=$v['real_good_size']; $applyCustomerDetail->real_change_good=$v['real_good_name']; $applyCustomerDetail->problem_attr=$v['problem_attr']; $applyCustomerDetail->label=$v['label']; if ($v['ImgList']){ $applyCustomerDetail->ImgList=json_encode($v['ImgList']); } if (!$applyCustomerDetail->save()){ throw new \Exception('1'); } } DB::commit(); return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); return $this->error(520014,$exception->getMessage()); } } /**修改申请单**/ public function UpdateApply(Request $request){ $id=$request->input('id'); $applyUserID=$request->input('apply_user_id'); $applyUserName=$request->input('apply_user_nickname'); $applyCrownID=$request->input('apply_crown_id'); $applyCrownName=$request->input('apply_crown_nickname'); $warea_name=$request->input('warea_name'); $customer_name=$request->input('customer_name'); $customer_phone=$request->input('customer_phone'); $province=$request->input('province'); $city=$request->input('city'); $area=$request->input('area'); $address=$request->input('address'); $total=$request->input('total'); $remark=$request->input('remark'); // $wear_time=$request->input('wear_time'); $is_agent=$request->input('is_agent');; $wear_time=$request->input('wear_time')??null; $NewproblemList=$request->input('NewproblemList'); $UpdateproblemList=$request->input('UpdateproblemList'); $DelproblemList=$request->input('DelproblemList')??[]; $sendback_info=''; $apply_number=$this->CreateApplyNumber(); $op_name=Auth::user()->name; $applyCustomer=ApplyCustomer::where('id',$id)->first(); if ($applyCustomer->audit_status==1){ return $this->error(500213,'请联系审核人员退回后修改'); } $applyCustomer->apply_user_id=$applyUserID; $applyCustomer->apply_user_nickname=$applyUserName; $applyCustomer->apply_crown_id=$applyCrownID; $applyCustomer->apply_crown_nickname=$applyCrownName; $applyCustomer->warea_name=$warea_name; $applyCustomer->customer_name=$customer_name; $applyCustomer->customer_phone=$customer_phone; $applyCustomer->province=$province; $applyCustomer->city=$city; $applyCustomer->area=$area; $applyCustomer->address=$address; $applyCustomer->total=$total; $applyCustomer->is_agent=$is_agent; $applyCustomer->remark=$remark; $applyCustomer->apply_number=$apply_number; $applyCustomer->op_name=$op_name; $applyCustomer->wear_time=$wear_time; DB::beginTransaction(); try { if (!$applyCustomer->save()){ throw new \Exception('1'); } /**新增的**/ foreach ($NewproblemList as $k=>$v){ $applyCustomerDetail=new ApplyCustomerDetail(); $applyCustomerDetail->customer_id=$applyCustomer->id; $applyCustomerDetail->problem_info=$v['problem']; $applyCustomerDetail->good_name=$v['good_name']; $applyCustomerDetail->good_size=$v['good_size']; $applyCustomerDetail->total=$v['total']; $applyCustomerDetail->real_change_size=$v['real_good_size']; $applyCustomerDetail->real_change_good=$v['real_good_name']; $applyCustomerDetail->problem_attr=$v['problem_attr']; $applyCustomerDetail->label=$v['label']; if ($v['ImgList']){ $applyCustomerDetail->ImgList=json_encode($v['ImgList']); } if (!$applyCustomerDetail->save()){ throw new \Exception('1'); } $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first(); if (!$re){ return $this->error(); } $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first(); if ($res->storage<$v['total']){ throw new \Exception('商品:'.$res->good_name.'的'.$res->size.'库存不足'); } $res->storage-=$v['total']; if (!$res->save()){ throw new \Exception('商品保存出错'); } $logs=new StoreHouseLog(); $log=Auth::user()->name.'调换了商品:'.$res->good_name.',尺码:'.$res->size.'。数量为:'.$v['total']; $logs->log=$log; $logs->customer_id=$applyCustomer->id; if (!$logs->save()){ throw new \Exception('1'); } } /**修改的*/ foreach ($UpdateproblemList as $k=>$v){ $applyCustomerDetail=ApplyCustomerDetail::where('id',$v['id'])->first(); if ($applyCustomerDetail->total<$v['total']){ $num=$v['total']-$applyCustomerDetail->total; $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first(); if (!$re){ return $this->error(); } $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first(); if ($res->storage<$num){ throw new \Exception('商品:'.$res->good_name.'的'.$res->size.'库存不足'); } $res->storage-=$num; if (!$res->save()){ throw new \Exception('商品保存出错'); } } else{ $num=$applyCustomerDetail->total-$v['total']; $re=StoreHouseStorage::where('good_name',$v['real_good_name'])->where('size',$v['real_good_size'])->first(); if (!$re){ return $this->error(); } $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first(); // if ($res->storage<$num){ // throw new \Exception('商品:'.$res->good_name.'的库存不足'); // } $res->storage+=$num; if (!$res->save()){ throw new \Exception('商品保存出错'); } } $applyCustomerDetail->problem_info=$v['problem']; $applyCustomerDetail->good_name=$v['good_name']; $applyCustomerDetail->good_size=$v['good_size']; $applyCustomerDetail->total=$v['total']; $applyCustomerDetail->real_change_size=$v['real_good_size']; $applyCustomerDetail->real_change_good=$v['real_good_name']; $applyCustomerDetail->problem_attr=$v['problem_attr']; $applyCustomerDetail->label=$v['label']; if ($v['ImgList']){ $applyCustomerDetail->ImgList=json_encode($v['ImgList']); } if (!$applyCustomerDetail->save()){ throw new \Exception('1'); } } /**删除的*/ foreach ($DelproblemList as $k=>$v){ $applyCustomerDetail=ApplyCustomerDetail::find($v); $re=StoreHouseStorage::where('good_name',$applyCustomerDetail->real_change_good)->where('size',$applyCustomerDetail->real_change_size)->first(); if (!$re){ return $this->error(); } $res=StoreHouseStorage::where('id',$re->id)->lockForUpdate()->first(); // if ($res->storage<$num){ // throw new \Exception('商品:'.$res->good_name.'的库存不足'); // } $res->storage+=$applyCustomerDetail->total; if (!$res->save()){ throw new \Exception('商品保存出错'); } $applyCustomerDetail->delete(); } DB::commit(); return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); return $this->error(520014,$exception->getMessage()); } } /**判断数量**/ public function CheckNum(Request $request){ $re=StoreHouseStorage::where('good_name',$request->input('real_change_good'))->where('size',$request->input('real_change_size')) ->lockForUpdate() ->first(); if (!$re){ return $this->error(500214,'没有此商品'); } if ($re->storage<$request->input('total')){ return $this->error(500214,'更换的商品库存不足'); } return $this->success([]); } /*客户退换申请单号**/ public function CreateApplyNumber(){ $id=Auth::user()->id; $str='DWBS-CE-'; $string=$str.$id.substr(time(),6,9).rand(1111,9999); return $string; } /**特殊尺码的库存信息**/ public function StoreHouseStorage(Request $request){ // $page_index=$request->input('page_index',1); // $page_size=$request->input('size',15); // $num=($page_index-1)*$page_size; // $search_name=$request->input('search_name'); $re=StoreHouseStorage::query(); $count=$re->count(); $re=$re->get(); return $this->success_list($re,'',$count); } /**更新库存**/ public function UpdateStoreHouseStorage(Request $request){ $id=$request->input('id'); $storage=$request->input('storage'); $re=StoreHouseStorage::where('id',$id)->lockForUpdate()->first(); $num=$re->storage; $re->storage=$storage; $logs=new StoreHouseLog(); $log=Auth::user()->name.'更新了商品:'.$re->good_name.',尺码:'.$re->size.'。更改前数量为:'.$num.',更新后数量为:'.$storage; $logs->log=$log; $logs->type=1; DB::beginTransaction(); try { if ( !$re->save()){ throw new \Exception('1'); } if (!$logs->save()){ throw new \Exception('1'); } DB::commit(); return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); return $this->error(500214,$exception->getMessage()); } } /**更新库存**/ public function AddStoreHouseStorage(Request $request){ $storage=$request->input('storage'); $good_name=$request->input('good_name'); $size=$request->input('size'); $re=new StoreHouseStorage(); $re->storage=$storage; $re->good_name=$good_name; $re->size=$size; $logs=new StoreHouseLog(); $log=Auth::user()->name.'增加了商品:'.$good_name.',尺码:'.$size.'。数量为:'.$storage; $logs->log=$log; $logs->type=1; DB::beginTransaction(); try { if ( !$re->save()){ throw new \Exception('1'); } if (!$logs->save()){ throw new \Exception('1'); } DB::commit(); return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); return $this->error(500214,$exception->getMessage()); } } /**上传订单号**/ public function UploadCustomerInfo(Request $request){ $file = $request->file('file'); if ( !$file->isValid()) { //判断文件是否有效 return redirect() ->back() ->withErrors('文件上传失败,请重新上传'); } $array = Excel::toArray(new ExpressImport, request()->file('file')); if(count($array)>5001){ return $this->error('450001',"单次最多导入5000条数据"); } $one = array_shift($array[0]); $express_num_keys=array_search('快递单号',$one); if(!$express_num_keys && !is_numeric($express_num_keys)){ return $this->error('450001',"表头无'快递单号'字段"); } $order_num_keys=array_search('申请单编号',$one); if(!$order_num_keys && !is_numeric($order_num_keys)){ return $this->error('450001',"表头无'申请单编号'字段"); } $express_com_keys=array_search('快递公司',$one); if(!$express_com_keys){ return $this->error('450001',"表头无'快递公司'字段"); } $express_code_keys=array_search('快递公司代码',$one); if(!$express_code_keys){ return $this->error('450001',"表头无'快递公司代码'字段"); } $arr=$array[0]; $express_nums=array_column($arr,$express_num_keys); $unique_express_nums=array_unique($express_nums); $que_express_nums=array_diff_assoc($express_nums,$unique_express_nums); if(count($que_express_nums)>0){ $que_express_str=implode(',',$que_express_nums); return $this->error('450001','快递单号'.$que_express_str.'重复出现'); } $new_arr=[]; $new_arrs=[]; $z=0; foreach ($array[0] as $k=>$v){ $st=''; if ($v[$express_com_keys]=='中通快递'){ $patterns = "/\d+/"; preg_match_all($patterns,$v[$express_num_keys],$aa); foreach ($aa[0] as $kee=>$vaa){ $st.=$vaa; } $new_arr[$z]=$st; }else{ $new_arr[$z]=$v[$express_num_keys]; } $new_arrs[$z]=$v[$order_num_keys]; $z++; } $res=ApplyCustomer::whereIn('express_num',$new_arr)->pluck('express_num')->toArray(); if (count($res)>0){ $ar_r=array_intersect($new_arr,$res); $que_express_str=implode(',',$ar_r); return $this->error('450001','快递单号:'.$que_express_str.'已经上传',$ar_r); } $apply_arr=ApplyCustomer::whereIn('apply_number',$new_arrs)->whereNotNull('express_num')->pluck('apply_number')->toArray(); if ($apply_arr){ return $this->error(500214,implode(',',$apply_arr).'已经上传过快递单号,请核实数据'); } DB::beginTransaction(); try { foreach ($arr as $k=>$v){ $apply_number=$v[$order_num_keys]; $apply_info=ApplyCustomer::where('apply_number',$apply_number)->first(); if (!$apply_info){ return $this->error(20014,'申请单号:'.$apply_number.'不存在'); } $st=''; if ($v[$express_com_keys]=='中通快递'){ $patterns = "/\d+/"; preg_match_all($patterns,$v[$express_num_keys],$aa); foreach ($aa[0] as $kee=>$vaa){ $st.=$vaa; } $apply_info->express_num=$st; }else{ $apply_info->express_num=$v[$express_num_keys]; } $apply_info->express_com=$v[$express_com_keys]; $apply_info->express_code=$v[$express_code_keys]; $apply_info->express_time=Carbon::now(); $apply_info->apply_status=2; $apply_info->is_owe=0; if (!$apply_info->save()){ throw new \Exception(1); } } DB::commit(); /**发送 快递信息*/ foreach ($arr as $k=>$v){ $apply_info=ApplyCustomer::where('apply_number',$v[$order_num_keys])->first(); $data=[ 'mobile'=>$apply_info->customer_phone, 'express_com'=>$apply_info->express_com, 'express_num'=>$apply_info->express_num, 'status'=>0 ]; event(new CustomerSendSmsEvent($data)); } return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); return $this->error(500214,$exception->getMessage()); } } /**上传单个物流信息**/ public function Express(Request $request){ $re=ApplyCustomer::find($request->input('id')); $re->express_num=$request->input('express_number'); $re->express_time=Carbon::now(); $re->apply_status=2; $re->is_owe=0; $re->express_com=$request->input('express_name'); $re->express_code=$request->input('express_code'); $data=[ 'mobile'=>$re->customer_phone, 'express_com'=>$re->express_com, 'express_num'=>$re->express_num, 'status'=>0 ]; event(new CustomerSendSmsEvent($data)); $re->save(); return $this->success([]); } /**上传订单号**/ public function UpdateCustomerInfo(Request $request){ $file = $request->file('file'); if ( !$file->isValid()) { //判断文件是否有效 return redirect() ->back() ->withErrors('文件上传失败,请重新上传'); } $array = Excel::toArray(new ExpressImport, request()->file('file')); if(count($array)>5001){ return $this->error('450001',"单次最多导入5000条数据"); } $one = array_shift($array[0]); $express_num_keys=array_search('旧快递单号',$one); if(!$express_num_keys && !is_numeric($express_num_keys)){ return $this->error('450001',"表头无'旧快递单号'字段"); } $new_express_num_keys=array_search('新快递单号',$one); if(!$express_num_keys && !is_numeric($express_num_keys)){ return $this->error('450001',"表头无'新快递单号'字段"); } $express_com_keys=array_search('快递公司',$one); if(!$express_com_keys){ return $this->error('450001',"表头无'快递公司'字段"); } $express_code_keys=array_search('快递公司代码',$one); if(!$express_code_keys){ return $this->error('450001',"表头无'快递公司代码'字段"); } $arr=$array[0]; $express_nums=array_column($arr,$express_num_keys); $unique_express_nums=array_unique($express_nums); $que_express_nums=array_diff_assoc($express_nums,$unique_express_nums); if(count($que_express_nums)>0){ $que_express_str=implode(',',$que_express_nums); return $this->error('450001','快递单号'.$que_express_str.'重复出现'); } DB::beginTransaction(); try { foreach ($arr as $k=>$v){ $apply_info=ApplyCustomer::where('express_num',$v[$express_num_keys])->first(); if (!$apply_info){ return $this->error(20014,'申请单号:'.$v[$express_num_keys].'不存在'); } $st=''; if ($v[$express_com_keys]=='中通快递'){ $patterns = "/\d+/"; preg_match_all($patterns,$v[$new_express_num_keys],$aa); foreach ($aa[0] as $kee=>$vaa){ $st.=$vaa; } $apply_info->express_num=$st; }else{ $apply_info->express_num=$v[$new_express_num_keys]; } $apply_info->express_com=$v[$express_com_keys]; $apply_info->express_code=$v[$express_code_keys]; $apply_info->express_time=Carbon::now(); $apply_info->apply_status=2; $apply_info->is_owe=0; if (!$apply_info->save()){ throw new \Exception(1); } } DB::commit(); /**发送 快递信息*/ foreach ($arr as $k=>$v){ $apply_info=ApplyCustomer::where('express_num',$v[$new_express_num_keys])->first(); $data=[ 'mobile'=>$apply_info->customer_phone, 'express_com'=>$apply_info->express_com, 'express_num'=>$apply_info->express_num, 'status'=>1, 'old_express_num'=>$v[$express_num_keys] ]; try { event(new CustomerSendSmsEvent($data)); }catch (\Exception $exception){ } } return $this->success([]); }catch (\Exception $exception){ DB::rollBack(); return $this->error(500214,$exception->getMessage()); } } /**添加退货原因**/ public function AddProblemCollection(Request $request,ProblemCollection $problemCollection){ $problem=$request->input('problem'); $problem_id=$request->input('problem_id',0); $problemCollection->problem=$problem; $problemCollection->type=1; $problemCollection->problem_id=$problem_id; $problemCollection->save(); return $this->success([]); } public function GetProblemCollection(Request $request){ $type=$request->input('type')??1; $re=ProblemCollection::where('type',$type)->with(['children'=>function($q){ $q->select('problem as label','id as value','problem_id'); }])->where('problem_id',0)->select('problem as label','id as value','problem_id')->orderByDesc('created_at')->get(); return $this->success($re); } public function GetGoodNum(){ $re=StoreHouseStorage::groupBy('good_name')->with(['GetGood'])->select('*')->get(); return $this->success($re); } /**添加退货原因**/ public function AddLabelCollection(Request $request,ProblemCollection $problemCollection){ $problem=$request->input('label'); $problemCollection->problem=$problem; $problemCollection->type=2; $problemCollection->save(); return $this->success([]); } public function GetLabelCollection(Request $request){ $type=$request->input('type')??2; $re=ProblemCollection::where('type',$type)->where('type',$type)->select('problem as label','id')->orderByDesc('created_at')->get(); return $this->success($re); } /**问题统计分析**/ public function ProblemAnalyse(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $re=ApplyCustomerDetail::query(); if ($start_time && $end_time){ $re->whereBetween('created_at',[$start_time ,$end_time]); } $re=$re->groupBy('problem_info')->select('problem_info',DB::raw('sum(total) as total'))->get(); return $this->success($re); } /**战区调换数量统计**/ public function WareaProblemAnalyse(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $re=ApplyCustomer::query(); if ($start_time && $end_time){ $re->whereBetween('created_at',[$start_time ,$end_time]); } $re=$re->groupBy('warea_name')->select('warea_name',DB::raw('sum(total) as total'))->get(); return $this->success($re); } /**代理公司调换数量统计**/ public function CrownProblemAnalyse(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $re=ApplyCustomer::query(); if ($start_time && $end_time){ $re->whereBetween('created_at',[$start_time ,$end_time]); } $re=$re->groupBy('apply_crown_id')->select('apply_crown_nickname','apply_crown_id',DB::raw('sum(total) as total'))->get(); return $this->success($re); } /**战区调换数量统计**/ public function WareaProblemAnalyseOnlyOne(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $warea_name=$request->input('warea_name'); $re=ApplyCustomerDetail::query(); $ids=ApplyCustomer::where('warea_name',$warea_name); if ($start_time && $end_time){ $ids->whereBetween('created_at',[$start_time ,$end_time]); } $ids=$ids->pluck('id'); $re=$re->whereIn('customer_id',$ids)->groupBy('problem_info')->select('problem_info',DB::raw('sum(total) as total'))->get(); return $this->success($re); } /**代理公司调换数量统计**/ public function CrownProblemAnalyseOnlyOne(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $search_id=$request->input('search_id'); $re=ApplyCustomerDetail::query(); $ids=ApplyCustomer::where('apply_crown_id',$search_id); if ($start_time && $end_time){ $ids->whereBetween('created_at',[$start_time ,$end_time]); } $ids=$ids->pluck('id'); $re=$re->whereIn('customer_id',$ids)->groupBy('problem_info')->select('problem_info',DB::raw('sum(total) as total'))->get(); return $this->success($re); } /**获取物流轨迹**/ public function ExpressSearch(Request $request,ExpressHandler $expressHandler){ $express=ApplyCustomer::find($request->id); if (!$express){ return $this->error(40011,'','该物流订单不存在'); } if ($express->express_status==1){ return $this->success(json_decode($express->express_data)); }else{ $re=$expressHandler->search($express->express_num,$express->express_code); $re=(array)$re; if ($re['State']==3){ DB::beginTransaction(); try{ $express->express_data=json_encode($re); $express->express_status=1; $express->save(); DB::commit(); }catch (\Exception $exception){ Log::error('-------物流信息保存出错--------'.$exception); DB::rollBack(); } } return $this->success($re); } } /*导出申请单**/ // public function ExportApplyList(Request $request){ // $search_name=$request->input('search_name'); // $customer_name=$request->input('customer_name'); // $warea_name=$request->input('warea_name'); // $apply_status=$request->input('apply_status'); // $is_finish=$request->input('is_finish'); // $re=ApplyCustomer::query(); // // if (is_numeric($apply_status)){ // $re->where('apply_status',$apply_status); // } // if (is_numeric($is_finish)){ // $re->where('is_finish',$is_finish); // } // // if ($warea_name){ // $re->where('warea_name',$warea_name); // } // if ($search_name){ // $re->where('apply_user_nickname','like',"$search_name"); // } // if ($customer_name){ // $re->where('customer_name','like',"$customer_name")->orWhere('customer_phone','like',"$customer_name"); // } // $data=$re->where('audit_status',1)->with(['applydetail'])->orderByDesc('id')->get(); // return $this->success($data); // } /**毛毛审核**/ public function AuditApplyList(Request $request){ $ids=$request->input('id'); ApplyCustomer::whereIn('id',$ids)->update(['audit_status'=>1]); return $this->success([]); } /*退回审核**/ public function AuditBackApply(Request $request){ $id=$request->input('id'); ApplyCustomer::where('id',$id)->update(['audit_status'=>0]); return $this->success([]); } /*导出申请单**/ public function ExportApplyLists(Request $request){ $search_name=$request->input('search_name'); $customer_name=$request->input('customer_name'); $warea_name=$request->input('warea_name'); $apply_status=$request->input('apply_status'); $is_finish=$request->input('is_finish'); $start_time=$request->input('start_time')??null; $end_time=$request->input('end_time'); $re=ApplyCustomer::query(); if (is_numeric($apply_status)){ $re->where('apply_status',$apply_status); } if (is_numeric($is_finish)){ $re->where('is_finish',$is_finish); } if ($warea_name){ $re->where('warea_name',$warea_name); } if ($search_name){ $re->where('apply_user_nickname','like',"$search_name"); } if ($customer_name){ $re->where('customer_name','like',"$customer_name")->orWhere('customer_phone','like',"$customer_name"); } if (Auth::user()->role_id==23){ $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name'); $re->where('warea_name',$warea_names); } if (Auth::user()->role_id==29){ $re->where('audit_status',1); $re->where('is_owe',0); }else{ if ($request->has('is_owe')){ if (is_numeric($request->input('is_owe'))){ $re->where('is_owe',$request->input('is_owe')); } } $audit_status=$request->input('audit_status'); if (is_numeric($audit_status)){ $re->where('audit_status',$audit_status); } if ($start_time){ $re->whereBetween('created_at',[$start_time,$end_time]); } } /**只能导出已经审核的申请表**/ $data=$re->with(['applydetail'=>function($q){ $q->select('id','real_change_good','real_change_size','total as totals','customer_id','problem_info'); }])->orderByDesc('id')->get(); $arr=[ '男款-L'=>'男L', '男款-XL'=>'男XL', '男款-XXL'=>'男XXL', '男款-3XL'=>'男3XL', '男款-4XL'=>'男4XL', '女款-M'=>'女M', '女款-L'=>'女L', '女款-XL'=>'女XL', '女款-XXL'=>'女XXL', '女款-3XL'=>'女3XL', '女款-4XL'=>'女4XL', '男款-5XL'=>'男5XL' ]; // $a=["L" => null,"XL" => null,"XXL" => null, "XXXL" => null, "女M" => null, "女L" => null, "女XL" => null, "女XXL" => null, "女XXXL" => null, "女4XL" => null, "5XL" => null]; // $arrs=array_keys($arr); $res=[]; foreach ($data as $k=>$v){ $res[$k]['调换单号']=$v->apply_number; $res[$k]['政委']=$v->op_name; $res[$k]['代理公司']=$v->apply_crown_nickname; $res[$k]['申请代理']=$v->apply_user_nickname; $res[$k]['收货人']=$v->customer_name; $res[$k]['收货手机号']=$v->customer_phone; $res[$k]['省份']=$v->province; $res[$k]['城市']=$v->city; $res[$k]['地区']=$v->area; $res[$k]['详细地址']=$v->address; $res[$k]['总数量']=$v->total; $res[$k]['穿着时间']=$v->wear_time; $res[$k]['登记时间']=$v->created_at->toDateTimeString(); $res[$k]['调换原因']=''; $res[$k]['男款']=0; $res[$k]['女款']=0; $res[$k]['男L']=''; $res[$k]['男XL']=''; $res[$k]['男XXL']=''; $res[$k]['男3XL']=''; $res[$k]['男4XL']=''; $res[$k]['女M']=''; $res[$k]['女L']=''; $res[$k]['女XL']=''; $res[$k]['女XXL']=''; $res[$k]['女3XL']=''; $res[$k]['女4XL']=''; $res[$k]['男5XL']=''; $res[$k]['备注']=$v->remark??'调'; $res[$k]['款式']=''; $arrs=[]; foreach ($v->applydetail as $ke=>$va){ if ($ke==0){ $res[$k]['款式']=$va->real_change_good; }else{ $good=$va->real_change_good; if ($good!=$res[$k]['款式']){ $res[$k]['款式']='杂'; } } if (isset($arrs[$va->real_change_good])){ $size_arr=array_column($arrs[$va->real_change_good],'size'); if (in_array($va->real_change_size,$size_arr)){ foreach ($arrs[$va->real_change_good] as $kes=>$vals){ if ($vals['size']==$va->real_change_size){ $arrs[$va->real_change_good][$kes]['total']+=$va->totals; } } }else{ $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id]; } }else{ $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id]; } $sex=strpos($va->real_change_size,'男'); if (is_numeric($sex)){ $res[$k]['男款']+=$va->totals; }else{ $res[$k]['女款']+=$va->totals; } $res[$k]['调换原因'].=$va->problem_info.','; } $res[$k]['arrs']=$arrs; foreach ($arrs as $ke=>$vas){ $nu=strpos($ke,'棉'); foreach ($vas as $kes=>$val){ if (is_numeric($nu)){ $res[$k][$arr[$val['size']]].=$val['total'].'条棉'; }else{ $res[$k][$arr[$val['size']]].=$val['total'].'条'.mb_substr($ke,0,1); } } } } return $this->success($res); } /**异常统计*/ public function GetExceptionList(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $page_index=$request->input('page_index'); $page_size=$request->input('page_size'); $type=$request->input('type')??1; $num=($page_index-1)*$page_size; $re=ApplyCustomer::query(); if ($start_time){ $re-> whereBetween('created_at',[$start_time,$end_time]); } if (Auth::user()->role_id==23){ $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name'); $re->where('warea_name',$warea_names); } if ($type==0){ $re=$re->groupBy('customer_phone') ->select('*',DB::raw('sum(total) as totals')) ->having('totals','>=',2); $res=clone $re; $res=$res->get(); $count=count($res); }else{ $re->whereNotNull('express_time')->where('express_status',0) ->where('express_time','<',Carbon::now()->addDays(-4)) ->select('*'); $count=$re->count(); } $list=$re->take($page_size)->skip($num)->get(); return $this->success_list($list,'',$count); } public function GetExceptionExpressList(Request $request){ $start_time=$request->input('start_time'); $end_time=$request->input('end_time'); $page_index=$request->input('page_index'); $page_size=$request->input('page_size'); $num=($page_index-1)*$page_size; $re=ApplyCustomer::query(); if ($start_time){ $re-> whereBetween('created_at',[$start_time,$end_time]); } if (Auth::user()->role_id==23){ $warea_names=Warea::where('admin_id',Auth::user()->id)->value('name'); $re->where('warea_name',$warea_names); } $re=$re->whereNotNull('express_time')->where('express_status',0) ->where('express_time','<',Carbon::now()->addDays(-4))->groupBy('customer_phone') ->select('*'); $count=$re->count(); $list=$re->take($page_size)->skip($num)->get(); return $this->success_list($list,'',$count); } /**更新状态**/ public function UpdateApplyInfos(Request $request){ $file = $request->file('file'); if ( !$file->isValid()) { //判断文件是否有效 return redirect() ->back() ->withErrors('文件上传失败,请重新上传'); } $array = Excel::toArray(new ExpressImport, request()->file('file')); if(count($array)>10001){ return $this->error('450001',"单次最多导入10000条数据"); } $one = array_shift($array[0]); $express_num_keys=array_search('快递单号',$one); if(!$express_num_keys && !is_numeric($express_num_keys)){ return $this->error('450001',"表头无'快递单号'字段"); } $arr=array_column($array[0],$express_num_keys); } /**标记为欠货**/ public function TagOwe(Request $request){ $ids=$request->input('id'); ApplyCustomer::whereIn('id',$ids)->update(['is_owe'=>1]); return $this->success([]); } /**取消标记为欠货**/ public function CancelTagOwe(Request $request){ $ids=$request->input('id'); ApplyCustomer::whereIn('id',$ids)->update(['is_owe'=>0]); return $this->success([]); } /**校验是否有重复的*/ public function CheckRepeatInfo(Request $request){ $customer_phone=$request->input('customer_phone'); $customer_name=$request->input('customer_name'); $time=Carbon::now()->addDays(-6); $count=ApplyCustomer::where('customer_phone',$customer_phone) ->where('customer_name',$customer_name) ->where('created_at','>',$time) ->count(); if ($count>0){ return $this->success(['status'=>1]); } return $this->success(['status'=>0]); } /* * 删除申请 * **/ public function DelApplyCustomer(Request $request){ $id=$request->input('id'); // $CustomerApply=ApplyCustomer::find($id); $ApplyDetail=ApplyCustomerDetail::where('customer_id',$id)->get(); foreach ($ApplyDetail as $k=>$v){ $re=StoreHouseStorage::where('good_name',$v->real_change_good) ->where('size',$v->real_change_size) ->lockForUpdate() ->first(); $re->storage+=$v->total; $re->save(); ApplyCustomerDetail::where('id',$v->id)->delete(); } ApplyCustomer::where('id',$id)->delete(); return $this->success([]); } /* * 获取该代理下所有的退货人信息 * **/ public function GetCustomerInfo(Request $request){ $user_id=$request->input('user_id'); $re=ApplyCustomer::where('apply_user_id',$user_id)->with(['applydetail'])->get(); return $this->success($re); } /* * 手动签收 * ***/ public function Receipt(Request $request){ $re=ApplyCustomer::where('id',$request->input('id'))->first(); $re->express_status=1; $re->remark=$re->remark.'**'.Auth::user()->name.'主动签收了改退货单'; $re->save(); return $this->success([]); } public function SearchUser(Request $request){ $search_name=$request->input('search_name'); $warea_id=$request->input('warea_id')??null; $user=User::whereNull('deleted_at'); if ($warea_id){ $user->where('warea_id',$warea_id); } $user=$user->where(function ($q)use($search_name){ $q->where('nickname','like',"%$search_name%")->orwhere('mobile','like',"%$search_name%")->orwhere('remark_name','like',"%$search_name%"); })->with(['warea'])->select('id','nickname','remark_name','mobile','level','headimgurl','warea_id')->get(); return $this->success($user); } public function ExportApplyListss(Request $request){ $search_name=$request->input('search_name'); $customer_name=$request->input('customer_name'); $warea_name=$request->input('warea_name'); $apply_status=$request->input('apply_status'); $is_finish=$request->input('is_finish'); $start_time=$request->input('start_time')??null; $end_time=$request->input('end_time'); $re=ApplyCustomer::query(); if (is_numeric($apply_status)){ $re->where('apply_status',$apply_status); } if (is_numeric($is_finish)){ $re->where('is_finish',$is_finish); } if ($warea_name){ $re->where('warea_name',$warea_name); } if ($search_name){ $re->where('apply_user_nickname','like',"$search_name"); } if ($customer_name){ $re->where('customer_name','like',"$customer_name")->orWhere('customer_phone','like',"$customer_name"); } if ($start_time){ $re->whereBetween('created_at',[$start_time,$end_time]); } /**只能导出已经审核的申请表**/ $data=$re->with(['applydetail'=>function($q){ $q->select('id','real_change_good','real_change_size','total as totals','customer_id','problem_info'); },'applydetails'=>function($q){ $q->select('id','real_change_good','real_change_size','total as totals','customer_id','problem_info')->groupBy('real_change_good'); }])->orderByDesc('id')->get(); $arr=[ '男款-L'=>'男L', '男款-XL'=>'男XL', '男款-XXL'=>'男XXL', '男款-3XL'=>'男3XL', '男款-4XL'=>'男4XL', '女款-M'=>'女M', '女款-L'=>'女L', '女款-XL'=>'女XL', '女款-XXL'=>'女XXL', '女款-3XL'=>'女3XL', '女款-4XL'=>'女4XL', '男款-5XL'=>'男5XL' ]; // $a=["L" => null,"XL" => null,"XXL" => null, "XXXL" => null, "女M" => null, "女L" => null, "女XL" => null, "女XXL" => null, "女XXXL" => null, "女4XL" => null, "5XL" => null]; // $arrs=array_keys($arr); $res=[]; foreach ($data as $k=>$v){ $res[$k]['调换单号']=$v->apply_number; $res[$k]['政委']=$v->op_name; $res[$k]['代理公司']=$v->apply_crown_nickname; $res[$k]['申请代理']=$v->apply_user_nickname; $res[$k]['收货人']=$v->customer_name; $res[$k]['收货手机号']=$v->customer_phone; $res[$k]['省份']=$v->province; $res[$k]['城市']=$v->city; $res[$k]['地区']=$v->area; $res[$k]['详细地址']=$v->address; $res[$k]['总数量']=$v->total; $res[$k]['穿着时间']=$v->wear_time; $res[$k]['登记时间']=$v->created_at->toDateTimeString(); $res[$k]['调换原因']=''; $res[$k]['男款']=0; $res[$k]['女款']=0; $res[$k]['男L']=''; $res[$k]['男XL']=''; $res[$k]['男XXL']=''; $res[$k]['男3XL']=''; $res[$k]['男4XL']=''; $res[$k]['女M']=''; $res[$k]['女L']=''; $res[$k]['女XL']=''; $res[$k]['女XXL']=''; $res[$k]['女3XL']=''; $res[$k]['女4XL']=''; $res[$k]['男5XL']=''; $res[$k]['备注']=$v->remark??'调'; $res[$k]['款式']=''; $arrs=[]; foreach ($v->applydetail as $ke=>$va){ if ($ke==0){ $res[$k]['款式']=$va->real_change_good; }else{ $good=$va->real_change_good; if ($good!=$res[$k]['款式']){ $res[$k]['款式']='杂'; } } if (isset($arrs[$va->real_change_good])){ $size_arr=array_column($arrs[$va->real_change_good],'size'); if (in_array($va->real_change_size,$size_arr)){ foreach ($arrs[$va->real_change_good] as $kes=>$vals){ if ($vals['size']==$va->real_change_size){ $arrs[$va->real_change_good][$kes]['total']+=$va->totals; } } }else{ $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id]; } }else{ $arrs[$va->real_change_good][]=['total'=>$va->totals,'size'=>$va->real_change_size,'customer_id'=>$va->customer_id]; } dump($arrs); // $arrs[$va->real_change_good]=['total'=>$va->total,'']; $sex=strpos($va->real_change_size,'男'); if (is_numeric($sex)){ $res[$k]['男款']+=$va->totals; }else{ $res[$k]['女款']+=$va->totals; } $res[$k]['调换原因'].=$va->problem_info.','; } foreach ($arrs as $ke=>$vas){ $nu=strpos($ke,'棉'); foreach ($vas as $kes=>$val){ if (is_numeric($nu)){ $res[$k][$arr[$val['size']]].=$val['total'].'条棉'; }else{ $res[$k][$arr[$val['size']]].=$val['total'].'条'.mb_substr($ke,0,1); } } } $res[$k]['arr']=$arrs; } return $this->success($res); } }